Skip to content

Commit

Permalink
Add helper button to copy the main workspace XML to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlory committed Dec 18, 2018
1 parent 4b1658f commit f9bf7c8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apps/src/sites/studio/pages/levelbuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import _ from 'lodash';
import codemirror from 'codemirror';
import marked from 'marked';
import renderer from '@cdo/apps/util/StylelessRenderer';
import copyToClipboard from '@cdo/apps/util/copyToClipboard';

window.levelbuilder = window.levelbuilder || {};
_.extend(window.levelbuilder, {
Expand All @@ -23,6 +24,11 @@ window.levelbuilder.installBlocks = function (app, blockly, options) {
appBlocks.install(blockly, options);
};

window.levelbuilder.copyWorkspaceToClipboard = function () {
const str = Blockly.Xml.domToPrettyText(Blockly.Xml.blockSpaceToDom(Blockly.mainBlockSpace));
copyToClipboard(str);
};

// TODO: Remove when global `CodeMirror` is no longer required.
window.CodeMirror = codemirror;
// TODO: Remove when global `marked` is no longer required.
Expand Down
15 changes: 15 additions & 0 deletions apps/src/util/copyToClipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default function copyToClipboard(str) {
window.getSelection().removeAllRanges();

const tempDiv = document.createElement('pre');
tempDiv.innerText = str;
document.body.appendChild(tempDiv);

try {
window.getSelection().selectAllChildren(tempDiv);
document.execCommand('copy');
window.getSelection().removeAllRanges();
} finally {
document.body.removeChild(tempDiv);
}
}
2 changes: 2 additions & 0 deletions dashboard/app/views/levels/_admin.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
%li= link_to "initialization (#{Blockly.count_xml_blocks(@level.initialization_blocks)})", level_edit_blocks_path(@level, :initialization_blocks)
- if @level.is_a? Artist
%li= link_to 'pre-draw', level_edit_blocks_path(@level, :predraw_blocks)
%button{type: 'button', onclick: 'window.levelbuilder.copyWorkspaceToClipboard()', class: 'btn btn-default btn-sm'}
Copy workspace to clipboard
%li= link_to 'delete', level_path(@level), method: :delete, data: { confirm: t('crud.confirm') }, style: 'color: red'
%li
= link_to 'clone', '', onclick: "$('#clone_#{@level.id}').toggle(); return false;"
Expand Down

0 comments on commit f9bf7c8

Please sign in to comment.