blockly-fluid 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +106 -0
  3. package/index.js +122 -0
  4. package/index.ts +46 -0
  5. package/package.json +13 -0
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2025 DinoscapeProgramming
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the β€œSoftware”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED β€œAS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # Blockly Dynamic Blocks Proxy πŸŽ›οΈβœ¨
2
+
3
+ A flexible **Proxy wrapper** for Blockly blocks that lets you define **dynamic inputs, layouts, fields, and shadows** with ease! Perfect for creating modular, reusable blocks that can adapt at runtime.
4
+
5
+ ---
6
+
7
+ ## Features πŸš€
8
+
9
+ * **Dynamic Inputs** πŸ”Œ
10
+ Define `inputs` with optional `check` types and `shadow DOM` β€” all can be functions for runtime evaluation.
11
+
12
+ * **Flexible Layouts** 🧩
13
+ Use `layout` arrays to arrange your block fields. Tokens can be functions or strings.
14
+
15
+ * **Custom Fields** ✏️
16
+ Supports:
17
+
18
+ * `text` β†’ `FieldTextInput`
19
+ * `multiline` β†’ `FieldMultilineInput`
20
+ * `dropdown` β†’ `FieldDropdown`
21
+ * Default β†’ translated text
22
+
23
+ * **Dynamic Statements** πŸ“‘
24
+ `statements` labels can be functions for runtime flexibility.
25
+
26
+ * **Previous / Next / Output / Tooltip / Color** 🎨
27
+ All block properties can be static values or functions.
28
+
29
+ * **Automatic Shadow DOM** 🌟
30
+ Automatically adds shadow DOM to inputs with `"String"` or `"Number"` checks.
31
+
32
+ * **Translation Ready** 🌍
33
+ Built-in support for multi-language translations with placeholders.
34
+
35
+ ---
36
+
37
+ ## Usage πŸ’‘
38
+
39
+ ```js
40
+ const Blockly = require("blockly");
41
+ const Blocks = require("blockly-fluid")(Blockly);
42
+
43
+ Blocks["controls_repeat_forever"] = {
44
+ layout: ["repeat forever", "DO"],
45
+ statements: {
46
+ DO: {
47
+ label: () => Blockly.Msg["CONTROLS_REPEAT_INPUT_DO"] || "do"
48
+ }
49
+ },
50
+ previous: true,
51
+ next: true,
52
+ tooltip: "Repeat the enclosed blocks forever",
53
+ };
54
+ ```
55
+
56
+ ### Features in action:
57
+
58
+ * Dynamic field labels βœ…
59
+ * Custom multiline input βœ…
60
+ * Shadow DOM automatically set βœ…
61
+ * Runtime evaluated inputs, statements, colors, tooltips βœ…
62
+
63
+ ---
64
+
65
+ ## Example Block 🧱
66
+
67
+ ```js
68
+ Blocks["metadata_set_description"] = {
69
+ color: "#565353",
70
+ layout: ["set description", "DESCRIPTION"],
71
+ fields: {
72
+ DESCRIPTION: {
73
+ type: "multiline",
74
+ default: "Enter description..."
75
+ }
76
+ },
77
+ tooltip: "Metadata block to specify command's description."
78
+ };
79
+ ```
80
+
81
+ ---
82
+
83
+ ## Supported Field Types ✨
84
+
85
+ | Type | Blockly Field |
86
+ | ----------- | ------------------- |
87
+ | `text` | FieldTextInput |
88
+ | `multiline` | FieldMultilineInput |
89
+ | `dropdown` | FieldDropdown |
90
+ | Default | Translated text |
91
+
92
+ ---
93
+
94
+ ## Why use this module? πŸ’–
95
+
96
+ * Save time creating complex Blockly blocks
97
+ * All block properties can be **dynamic functions**
98
+ * Built-in **shadow DOM support**
99
+ * Built-in **translation** handling
100
+ * Works for **both static and runtime-generated blocks**
101
+
102
+ ---
103
+
104
+ ## License πŸ“„
105
+
106
+ MIT License β€” free to use, modify, and distribute!
package/index.js ADDED
@@ -0,0 +1,122 @@
1
+ module.exports = (Blockly, { translate = (text) => text } = {}) => new Proxy({}, {
2
+ set(_, name, {
3
+ inputs = {},
4
+ layout = [],
5
+ fields = {},
6
+ statements = {},
7
+ previous = true,
8
+ next = true,
9
+ output,
10
+ color,
11
+ inline = true,
12
+ deletable = true,
13
+ tooltip = "",
14
+ help = ""
15
+ }) {
16
+ Blockly.Blocks[name] = {
17
+ init: function () {
18
+ if (typeof inputs === "function") (inputs = inputs());
19
+ if (typeof layout === "function") (layout = layout());
20
+ if (typeof statements === "function") (statements = statements());
21
+
22
+ if ((Array.isArray(layout)) ? layout.length : ((typeof layout === "string") ? layout : false)) {
23
+ ((Array.isArray(layout)) ? layout : [layout]).forEach((token) => {
24
+ if (typeof token === "function") (token = token());
25
+
26
+ const input = (typeof inputs[token] === "function") ? inputs[token]() : inputs[token];
27
+ const field = (typeof fields[token] === "function") ? fields[token]() : fields[token];
28
+ const statement = (typeof statements[token] === "function") ? statements[token]() : statements[token];
29
+
30
+ if (input) {
31
+ if ((typeof input.check === "function") && ![String, Number, Boolean, Array, Object].includes(input.check)) (input.check = input.check());
32
+
33
+ const valueInput = this.appendValueInput(token)
34
+
35
+ if (input.check) {
36
+ if (!Array.isArray(input.check)) (input.check = [input.check]);
37
+
38
+ input.check = input.check.map((check) => [null, "null"].includes(check) ? "Null" : ((check === "undefined") ? "Undefined" : ([String, Number, Boolean, Array, Object].includes(check) ? check.name : check)));
39
+
40
+ valueInput.setCheck(input.check || null);
41
+ } else {
42
+ valueInput.connection.setShadowDom(new DOMParser().parseFromString(`<shadow type="string"><field name="TEXT"></field></shadow>`, "text/xml").firstChild);
43
+ };
44
+
45
+ if (input.shadow) {
46
+ if (typeof input.shadow === "function") (input.shadow = input.shadow());
47
+
48
+ valueInput.connection.setShadowDom(new DOMParser().parseFromString(`<shadow type="${((typeof input.shadow.type === "function") ? input.shadow.type() : (input.shadow.type || "text")).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;")}">${(typeof input.shadow.content === "function") ? input.shadow.content() : (input.shadow.content || "")}</shadow>`, "text/xml").firstChild);
49
+ };
50
+ } else if (field) {
51
+ const dummy = this.appendDummyInput();
52
+
53
+ switch (field.type) {
54
+ case "text":
55
+ dummy.appendField(new Blockly.FieldTextInput(...[
56
+ translate((typeof field.default === "function") ? field.default() : (field.default || "")),
57
+ ...(field.validator) ? [field.validator] : []
58
+ ]), token);
59
+ break;
60
+ case "multiline":
61
+ dummy.appendField(new Blockly.FieldMultilineInput(...[
62
+ translate((typeof field.default === "function") ? field.default() : (field.default || "")),
63
+ ...(field.validator) ? [field.validator] : []
64
+ ]), token);
65
+ break;
66
+ case "dropdown":
67
+ dummy.appendField(new Blockly.FieldDropdown(...[
68
+ Object.entries(field.options).map(([name, text]) => [translate((typeof text === "function") ? text() : text), (typeof name === "function") ? name() : name]),
69
+ ...(field.validator) ? [field.validator] : []
70
+ ]), token);
71
+ break;
72
+ case "variable":
73
+ dummy.appendField(new Blockly.FieldVariable(...[
74
+ translate((typeof field.default === "function") ? field.default() : (field.default || "")),
75
+ ...(field.validator) ? [field.validator] : []
76
+ ]), token);
77
+ break;
78
+ default:
79
+ dummy.appendField(translate(token));
80
+ };
81
+ } else if (statement) {
82
+ const statementInput = this.appendStatementInput(token).setCheck(null);
83
+
84
+ if (statement.label) statementInput.appendField(translate((typeof statement.label === "function") ? statement.label() : statement.label));
85
+ } else {
86
+ this.appendDummyInput().appendField(translate(token));
87
+ };
88
+ });
89
+ };
90
+
91
+ if (!output) {
92
+ if ((typeof previous === "function") ? previous() : previous) this.setPreviousStatement(true, null);
93
+ if ((typeof next === "function") ? next() : next) this.setNextStatement(true, null);
94
+ } else {
95
+ if ((typeof output === "function") && ![String, Number, Boolean, Array, Object].includes(output)) (output = output());
96
+
97
+ this.setPreviousStatement(false);
98
+ this.setNextStatement(false);
99
+ this.setOutput(true, (output === true) ? null : (([String, Number, Boolean, Array, Object].includes(output)) ? output.name : output));
100
+ };
101
+
102
+ this.setColour((color != null) ? ((typeof color === "function") ? color() : color) : (Blockly.getMainWorkspace().getToolbox().contents_.find((category) => category.toolboxItemDef_.contents?.some((block) => block.type === name))?.toolboxItemDef_?.colour || "#000000"));
103
+ this.setInputsInline((typeof inline === "function") ? inline() : inline);
104
+ this.setDeletable((typeof deletable === "function") ? deletable() : deletable);
105
+ this.setTooltip(translate((typeof tooltip === "function") ? tooltip() : tooltip));
106
+ this.setHelpUrl((typeof help === "function") ? help() : help);
107
+
108
+ this.inputList.forEach((input) => {
109
+ if (input.connection?.shadowDom || !Array.isArray(input.connection?.check) || input.connection.check.every((check) => !["String", "Number"].includes(check))) return;
110
+
111
+ input.connection.setShadowDom(new DOMParser().parseFromString(`<shadow type="${((input.connection.check?.length === 1) && (input.connection.check[0] === "Number")) ? "number" : "string"}"><field name="${((input.connection.check?.length === 1) && (input.connection.check[0] === "Number")) ? "NUMBER" : "TEXT"}"></field></shadow>`, "text/xml").firstChild);
112
+ });
113
+ }
114
+ };
115
+
116
+ return true;
117
+ },
118
+
119
+ get(_, name) {
120
+ return Blockly.Blocks[name];
121
+ }
122
+ });
package/index.ts ADDED
@@ -0,0 +1,46 @@
1
+ type InputDefinition = {
2
+ check?: string | Function | (string | Function)[];
3
+ shadow?: {
4
+ type?: string | Function;
5
+ content?: string | Function;
6
+ } | Function;
7
+ };
8
+
9
+ type FieldDefinition = {
10
+ type?: "text" | "multiline" | "dropdown" | "variable" | string;
11
+ default?: string | Function;
12
+ options?: Record<string, string | Function>;
13
+ validator?: Function;
14
+ };
15
+
16
+ type StatementDefinition = {
17
+ label?: string | Function;
18
+ };
19
+
20
+ type BlockDefinition = {
21
+ inputs?: Record<string, InputDefinition | Function>;
22
+ layout?: string | string[] | Function | Function[];
23
+ fields?: Record<string, FieldDefinition | Function>;
24
+ statements?: Record<string, StatementDefinition | Function>;
25
+ previous?: boolean | Function;
26
+ next?: boolean | Function;
27
+ output?: boolean | string | Function;
28
+ color?: string | Function;
29
+ inline?: boolean | Function;
30
+ deletable?: boolean | Function;
31
+ tooltip?: string | Function;
32
+ help?: string | Function;
33
+ };
34
+
35
+ interface BlocklyProxy {
36
+ [name: string]: BlockDefinition | undefined;
37
+ }
38
+
39
+ /**
40
+ * Main module function.
41
+ * @param Blockly - The Blockly instance.
42
+ * @returns A Proxy object for defining or accessing Blockly blocks.
43
+ */
44
+ declare function initBlocklyModule(Blockly: any): BlocklyProxy;
45
+
46
+ export default initBlocklyModule;
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "blockly-fluid",
3
+ "version": "1.0.0",
4
+ "description": "A flexible proxy wrapper for Blockly blocks.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "DinoscapeProgramming",
11
+ "license": "MIT",
12
+ "type": "commonjs"
13
+ }