Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions addons-l10n/en/editor-copy-scratchblocks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor-copy-scratchblocks/copy-scratchblocks": "Copy scratchblocks text"
}
1 change: 1 addition & 0 deletions addons/addons.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
"asset-conflict-dialog",
"remaining-replies",
"forum-user-agent",
"editor-copy-scratchblocks",

"// NEW ADDONS ABOVE THIS ↑↑",
"// Note: these themes need this exact order to work properly,",
Expand Down
56 changes: 56 additions & 0 deletions addons/editor-copy-scratchblocks/addon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "Copy scratchblocks text from editor",
"description": "Adds a right-click option to blocks in the project editor to convert them into scratchblocks text form and copy to the clipboard, for sharing code on places like the Discussion Forums or Scratch Wiki.",
"credits": [
{
"name": "pumpkinhasapatch",
"link": "https://github.com/pumpkinhasapatch"
}
],
"userscripts": [
{
"url": "userscript.js",
"matches": ["projects"]
}
],
"libraries": ["parse-sb3-blocks"],
"info": [
{
"type": "notice",
"text": "Blocks in languages other than English may not display correctly in the Discussion Forums and other places. If you use Scratch in another language and have problems with the output text, try enabling the \"Always copy block text in English\" option.",
"id": "forceEnglishNotice"
}
],
"settings": [
{
"name": "Include code tags",
"id": "codeTags",
"type": "select",
"potentialValues": [
{
"id": "none",
"name": "None"
},
{
"id": "square",
"name": "[scratchblocks]"
},
{
"id": "angled",
"name": "<scratchblocks>"
}
],
"default": "none"
},
{
"name": "Always copy block text in English",
"id": "forceEnglish",
"type": "boolean",
"default": false
}
],
"dynamicEnable": true,
"dynamicDisable": true,
"versionAdded": "1.40.0",
"tags": ["editor", "codeEditor"]
}
58 changes: 58 additions & 0 deletions addons/editor-copy-scratchblocks/userscript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// From https://github.com/apple502j/parse-sb3-blocks/releases/tag/v0.5.2
import { toScratchblocks } from "../../libraries/thirdparty/cs/parse-sb3-blocks.module.js";

// Some code referenced from blocks2image, block-switching and editor-devtools
export default async function ({ addon, console, msg }) {
// Add right-click "Copy scratchblocks code" button to blocks
addon.tab.createBlockContextMenu(
(items, block) => {
if (addon.self.disabled) return items;
const makeSpaceItemIndex = items.findIndex((obj) => obj._isDevtoolsFirstItem);
const insertBeforeIndex =
makeSpaceItemIndex !== -1
? // If "make space" button exists, add own items before it
makeSpaceItemIndex
: // If there's no such button, insert at end
items.length;
// "Copy scratchblocks code" message in /addons-l10n/en/editor-copy-scratchblocks.json (translate this)
items.splice(insertBeforeIndex, 0, {
enabled: true,
text: msg("copy-scratchblocks"),
callback: () => {
convertToScratchblocks(block);
},
separator: true,
});
return items;
},
{ blocks: true }
);

async function convertToScratchblocks(block) {
// const blockId = block.getRootBlock().id; // Start from the topmost block, parsing the entire stack
let blockId = block.id;

// Get blocks in current sprite as project.json format
const vm = addon.tab.traps.vm;
let blocksJSON = JSON.parse(vm.toJSON(vm.editingTarget.id)).blocks;
let language = addon.settings.get("forceEnglish") ? "en" : addon.auth.scratchLang;

// Send ID and current blocks to parse-sb3-blocks, set indent spacing and fix variables that have the same name as reporters.
// Only outputs English blocks, TODO: Detect editor language and pass it to third input for multilingual scratchblocks.
let scratchblocks = toScratchblocks(blockId, blocksJSON, language, {
tabs: " ".repeat(4),
variableStyle: "as-needed",
});

let codeTags = addon.settings.get("codeTags");
// Add square or angled code tags around scratchblocks for Forums and Wiki formatting
if (codeTags == "square") {
scratchblocks = "[scratchblocks]\n" + scratchblocks + "\n[/scratchblocks]";
} else if (codeTags == "angled") {
scratchblocks = "<scratchblocks>\n" + scratchblocks + "\n</scratchblocks>";
}

console.log(scratchblocks);
navigator.clipboard.writeText(scratchblocks);
}
}
1 change: 1 addition & 0 deletions libraries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ Third-party libraries included are:
- [Fuse.js 6.6.2](https://unpkg.com/fuse.js@6.6.2/dist/fuse.esm.min.js) (Apache-2.0)
- [idb 7.1.1](https://unpkg.com/idb@7.1.1/build/umd.js) (ISC)
- [Sortable.js 1.15.0](https://unpkg.com/sortablejs@1.15.0/Sortable.min.js) (MIT)
- [parse-sb3-blocks 0.5.2](https://github.com/apple502j/parse-sb3-blocks/releases/tag/v0.5.2) (BSD-3-Clause)

Note that these libraries are either from official release websites or from unpkg (which distributes the content uploaded to NPM as-is).
5 changes: 5 additions & 0 deletions libraries/license-info.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,10 @@
"license": "ISC",
"year": "2016,",
"fullname": "Jake Archibald <jaffathecake@gmail.com>"
},
"parse-sb3-blocks": {
"license": "BSD-3-Clause",
"year": "2020",
"fullname": "apple502j"
}
}
2 changes: 2 additions & 0 deletions libraries/thirdparty/cs/parse-sb3-blocks.module.js

Large diffs are not rendered by default.