-
Notifications
You must be signed in to change notification settings - Fork 302
[New Ext] HTML Builder #1202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[New Ext] HTML Builder #1202
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
41309b4
Create HTML Builder Ext
showierdata9978 7ff6875
Change the unsandboxed error
showierdata9978 8880f57
Add extension metadata
showierdata9978 7892e52
Update html.js
showierdata9978 316adcc
Fix ID
showierdata9978 c77b68f
Make sprite specific, sanitise element names
showierdata9978 1c76024
Add icon, and add it to ext list
showierdata9978 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,260 @@ | ||
| // Name: HTWL | ||
| // ID: ShowierTWHtml | ||
| // Description: Allows for building HTML within scratch. | ||
| // By: ShowierData9978 <github.com/showierdata9978> | ||
| /* eslint-disable require-await */ | ||
| /// <reference types="@turbowarp/types" /> | ||
|
|
||
| /** | ||
| * @typedef {import("@turbowarp/scratch-vm/")} | ||
|
|
||
| */ | ||
|
|
||
| ((Scratch) => { | ||
| "use strict"; | ||
|
|
||
| if (!Scratch.extensions.unsandboxed) { | ||
| throw new Error("HTML Extension must be run unsandboxed"); | ||
| } | ||
|
|
||
| /** | ||
| * @type {VM} | ||
| */ | ||
| const vm = Scratch.vm; | ||
|
|
||
| class Html { | ||
| constructor() { | ||
| /** | ||
| * @typedef {Object.<string, Array.<string>>} stack | ||
| * @typedef {Object.<string, string>} html | ||
| */ | ||
|
|
||
| /** @type {stack} */ | ||
| this.stack = {}; | ||
| /** @type {html} */ | ||
| this.html = {}; | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @returns {Scratch.Info} | ||
| */ | ||
| getInfo() { | ||
| return { | ||
| id: "HTWL", | ||
| name: "HTML", | ||
| color1: "#FF0000", | ||
| blocks: [ | ||
| { | ||
| opcode: "htmlWrap", | ||
| blockType: Scratch.BlockType.CONDITIONAL, | ||
| text: "<[element] [attributes]>", | ||
| arguments: { | ||
| element: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "div", | ||
| }, | ||
| attributes: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "htmlCommand", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: "<[element] [attributes]>[text] </>", | ||
| arguments: { | ||
| element: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "div", | ||
| }, | ||
| attributes: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "", | ||
| }, | ||
| text: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "rawInsert", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: "Insert raw [html]", | ||
| arguments: { | ||
| html: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "noInner", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: "<[element] [attributes] />", | ||
| arguments: { | ||
| element: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "div", | ||
| }, | ||
| attributes: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "", | ||
| }, | ||
| }, | ||
| }, | ||
|
|
||
| "---", | ||
| { | ||
| opcode: "exit", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: "</>", | ||
| }, | ||
| { | ||
| opcode: "html_ret", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: "html", | ||
| }, | ||
| { | ||
| opcode: "clear", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: "reset html", | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| sanitise(text) { | ||
| return text.replace(/</g, "<").replace(/>/g, ">"); | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("scratch-vm").BlockUtility} util | ||
| * | ||
| */ | ||
| pushStack(element, util) { | ||
| if (!this.stack[util.target.id]) this.stack[util.target.id] = []; | ||
|
|
||
| this.stack[util.target.id].push(element); | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("scratch-vm").BlockUtility} util | ||
| * @returns {string} | ||
| */ | ||
|
|
||
| popStack(util) { | ||
| if (!this.stack[util.target.id]) { | ||
| this.stack[util.target.id] = []; | ||
| return; | ||
| } | ||
|
|
||
| return this.stack[util.target.id].pop(); | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("scratch-vm").BlockUtility} util | ||
| */ | ||
| getStack(util) { | ||
| if (!this.stack[util.target.id]) { | ||
| this.stack[util.target.id] = []; | ||
| } | ||
|
|
||
| return this.stack[util.target.id]; | ||
| } | ||
|
|
||
| /** | ||
| * @typedef arg_wrap | ||
| * @prop {string} element | ||
| * @prop {string} attributes | ||
| * @param {arg_wrap} args | ||
| * @param {import("scratch-vm").BlockUtility} util | ||
| */ | ||
| async htmlWrap({ element, attributes }, util) { | ||
| this._appendHtml( | ||
| `<${this.sanitise(element)} ${this.sanitise(attributes)}>`, | ||
| util | ||
| ); | ||
| this.pushStack(element, util); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @param {arg_wrap} args | ||
| * @param {import("scratch-vm").BlockUtility} util | ||
| */ | ||
| async noInner({ element, attributes }, util) { | ||
| this._appendHtml( | ||
| `<${this.sanitise(element)} ${this.sanitise(attributes)} />`, | ||
| util | ||
| ); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @typedef arg_command | ||
| * @prop {string} element | ||
| * @prop {string} attributes | ||
| * @prop {string} text | ||
| * @param {arg_command} args | ||
| * @param {import("scratch-vm").BlockUtility} util | ||
| */ | ||
| async htmlCommand({ element, attributes, text }, util) { | ||
| element = this.sanitise(element); | ||
| attributes = this.sanitise(attributes); | ||
|
|
||
| this._appendHtml(`<${element} ${attributes}>${text}</${element}>`, util); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} text | ||
| * @param {import("scratch-vm").BlockUtility} util | ||
| */ | ||
| _appendHtml(text, util) { | ||
| let whitespace = " ".repeat( | ||
| this.stack.length ? this.getStack(util).length - 1 : 0 | ||
| ); | ||
| if (this.html[util.target.id] === undefined) { | ||
| this.html[util.target.id] = ""; | ||
| } | ||
| this.html[util.target.id] += whitespace + text + "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("scratch-vm").BlockUtility} util | ||
| */ | ||
| exit(args, util) { | ||
| /* @type {string} */ | ||
| const element = this.popStack(util); | ||
| if (!element) { | ||
| throw "Error: No element to close"; | ||
| } | ||
| this._appendHtml(`</${this.sanitise(element)}>`, util); | ||
| } | ||
|
|
||
| rawInsert({ html }, util) { | ||
| this._appendHtml(html, util); | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("scratch-vm").BlockUtility} util | ||
| * @returns {string} | ||
| */ | ||
| html_ret(args, util) { | ||
| return this.html[util.target.id]; | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("scratch-vm").BlockUtility} util | ||
| */ | ||
| clear(args, util) { | ||
| this.html[util.target.id] = ""; | ||
| } | ||
| } | ||
|
|
||
| Scratch.extensions.register(new Html()); | ||
| // @ts-ignore | ||
| })(Scratch); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,5 +83,6 @@ | |
| "itchio", | ||
| "gamejolt", | ||
| "obviousAlexC/newgroundsIO", | ||
| "Lily/McUtils" | ||
| "Lily/McUtils", | ||
| "ShowierData9978/html" | ||
| ] | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.