From 50205597a481d33c58166e9c2d4cc831334f1e80 Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 1 Nov 2015 15:53:26 -0500 Subject: [PATCH] Adds #128 copy to clipboard functionality in docs --- server/files/javascript/docs.js | 50 +++++++++++++++++++++++++------ server/files/stylesheets/docs.css | 9 ++++++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/server/files/javascript/docs.js b/server/files/javascript/docs.js index 8a7c36967..b01e81b0e 100644 --- a/server/files/javascript/docs.js +++ b/server/files/javascript/docs.js @@ -71,7 +71,7 @@ semantic.ready = function() { $example = $('.example'), $popupExample = $example.not('.no'), $shownExample = $example.filter('.shown'), - $prerenderedExample = $example.has('.ui.checkbox, .ui.dropdown, .ui.search, .ui.rating, .ui.dimmer, .ui.embed'), + $prerenderedExample = $example.has('.ui.checkbox, .ui.dropdown, .ui.search, .ui.progress, .ui.rating, .ui.dimmer, .ui.embed'), $visibilityExample = $example.filter('.visiblity').find('.overlay, .demo.segment, .items img'), @@ -722,6 +722,13 @@ semantic.ready = function() { $example.data('code', code); return code; }, + + copyCode: function() { + $(this) + .popup('change content', 'Copied to clipboard') + ; + }, + createCode: function() { var $example = $(this).closest('.example'), @@ -732,6 +739,7 @@ semantic.ready = function() { $ignoredContent = $('.ui.popup, i.code:last-child, .anchor, .code, .existing.segment, .instructive, .language.label, .annotation, .ignore, style, script, .ignored'), $demo = $example.children().not($intro).not($ignoredContent), code = $example.data('code') || $.proxy(handler.generateCode, this)(), + $copyCode, $label ; @@ -751,9 +759,28 @@ semantic.ready = function() { } if($html.size() === 0) { - $html = $('
').insertBefore($annotation); - $label = $('
').html('Example'); - $label.prependTo($html); + $html = $('
').insertBefore($annotation); + $label = $('
').html('Example '); + $copyCode = $label.find('i.copy'); + $copyCode + .on('click', handler.copyCode) + .popup({ + variation : 'inverted', + offset : -12, + distanceAway : 6 + }) + ; + $label + .prependTo($html) + ; + new Clipboard($copyCode.get(0), { + text: function() { + var + code = $copyCode.closest('.example').data('code') || '' + ; + return handler.formatCode(code); + } + }); if($demo.length === 0) { $html.addClass('empty'); } @@ -978,6 +1005,14 @@ semantic.ready = function() { }, + formatCode: function(code) { + var + indent = handler.getIndent(code) || 2, + whiteSpace = new RegExp('\\n\\s{' + indent + '}', 'g') + ; + return $.trim(code).replace(whiteSpace, '\n'); + }, + initializeCode: function(codeSample) { var $code = $(this).show(), @@ -1007,8 +1042,6 @@ semantic.ready = function() { ? 'instructive bottom attached' : 'existing', formattedCode = code, - whiteSpace, - indent, styledCode, $example, $label, @@ -1028,6 +1061,7 @@ semantic.ready = function() { return $('
').html(string).text(); } + // escape html entities if(contentType != 'html' || escape) { code = escapeHTML(code); @@ -1043,9 +1077,7 @@ semantic.ready = function() { formattedCode = code; } else { - indent = handler.getIndent(code) || 2; - whiteSpace = new RegExp('\\n\\s{' + indent + '}', 'g'); - formattedCode = $.trim(code).replace(whiteSpace, '\n'); + formattedCode = handler.formatCode(code); } // color code diff --git a/server/files/stylesheets/docs.css b/server/files/stylesheets/docs.css index 84401c97d..6941145fb 100755 --- a/server/files/stylesheets/docs.css +++ b/server/files/stylesheets/docs.css @@ -934,6 +934,15 @@ blockquote .author { background-color: #F8F8F8; box-shadow: 0px 0px 0px 1px #DDDDDD; } +#example .example > .html > .top.attached.label .copy { + position: absolute; + top: 9px; + right: 8px; + color: #AAAAAA; +} +#example .example > .html > .top.attached.label .copy:hover { + color: #666666; +} #example .example > .empty.html.segment { padding: 1.15em 1em 1em; }