From ff3577fdf6bb8b307c5910566d42759fb88a5465 Mon Sep 17 00:00:00 2001 From: mrredo Date: Thu, 30 May 2024 11:59:57 +0300 Subject: [PATCH 1/2] variables done --- src/lib/blocks/Javascript/Loops.ts | 6 +- src/lib/blocks/Javascript/logic.ts | 10 +- src/lib/blocks/Test Blocks/variables.ts | 153 ++++++++++++++++++ src/lib/components/Workspace.svelte | 12 +- src/lib/enums/BlockTypes.ts | 3 +- src/lib/types/BlockDefinition.ts | 9 +- src/lib/types/CategoryDefinition.ts | 3 + src/lib/utils/BlockGen/Blocks/Block.ts | 15 +- src/lib/utils/BlockGen/Inputs/Dropdown.ts | 3 +- .../utils/BlockGen/Inputs/VariableInput.ts | 37 +++++ .../BlockGen/Mutators/AssemblerMutatorV2.ts | 1 - .../BlockGen/Mutators/CheckboxMutator.ts | 1 - src/lib/utils/ToolboxGen/CallBacks.ts | 19 +++ src/lib/utils/ToolboxGen/Toolbox.ts | 58 ++++++- src/lib/utils/helpers/getInputValue.ts | 4 + src/routes/editor/+page.svelte | 7 +- 16 files changed, 309 insertions(+), 32 deletions(-) create mode 100644 src/lib/blocks/Test Blocks/variables.ts create mode 100644 src/lib/utils/BlockGen/Inputs/VariableInput.ts create mode 100644 src/lib/utils/ToolboxGen/CallBacks.ts diff --git a/src/lib/blocks/Javascript/Loops.ts b/src/lib/blocks/Javascript/Loops.ts index 30a77de..c5f1970 100644 --- a/src/lib/blocks/Javascript/Loops.ts +++ b/src/lib/blocks/Javascript/Loops.ts @@ -1,7 +1,7 @@ import { BlockShape, BlockType, DropdownType } from "$lib/enums/BlockTypes"; import type { BlockDefinition } from "$lib/types/BlockDefinition"; import type { CategoryDefinition } from "$lib/types/CategoryDefinition"; -import Dropdown from "$lib/utils/BlockGen/Inputs/Dropdown"; +import DropdownInput from "$lib/utils/BlockGen/Inputs/Dropdown"; import StatementInput from "$lib/utils/BlockGen/Inputs/StatementInput"; import ValueInput from "$lib/utils/BlockGen/Inputs/ValueInput"; import rgbToHex from "$lib/utils/helpers/rgbToHex"; @@ -25,7 +25,7 @@ const blocks: BlockDefinition[] = [ id: "repeat_while", text: "Repeat {WHILE} {CONDITION}\n {INPUT}", args: [ - new Dropdown("WHILE", DropdownType.Auto, { + new DropdownInput("WHILE", DropdownType.Auto, { while: "while", until: "until" }), @@ -92,7 +92,7 @@ const blocks: BlockDefinition[] = [ id: "break", text: "{ACTION} of loop", args: [ - new Dropdown("ACTION", DropdownType.Auto, { + new DropdownInput("ACTION", DropdownType.Auto, { break: "break", continue: "continue" }) diff --git a/src/lib/blocks/Javascript/logic.ts b/src/lib/blocks/Javascript/logic.ts index c787b38..21090d5 100644 --- a/src/lib/blocks/Javascript/logic.ts +++ b/src/lib/blocks/Javascript/logic.ts @@ -1,7 +1,7 @@ import { BlockShape, BlockType, DropdownType, WarningType } from "$lib/enums/BlockTypes"; import type { BlockDefinition } from "$lib/types/BlockDefinition"; import type { CategoryDefinition } from "$lib/types/CategoryDefinition"; -import Dropdown from "$lib/utils/BlockGen/Inputs/Dropdown"; +import DropdownInput from "$lib/utils/BlockGen/Inputs/Dropdown"; import ValueInput from "$lib/utils/BlockGen/Inputs/ValueInput"; import Warning from "$lib/utils/BlockGen/Warnings/Warning"; import rgbToHex from "$lib/utils/helpers/rgbToHex"; @@ -60,7 +60,7 @@ const blocks: BlockDefinition[] = [ text: "{A} {CONDITION} {B}", args: [ new ValueInput("A", BlockType.Any), - new Dropdown("CONDITION", DropdownType.Auto, { + new DropdownInput("CONDITION", DropdownType.Auto, { "=": "===", "≠": "!=", "<": "<", @@ -93,7 +93,7 @@ const blocks: BlockDefinition[] = [ text: "{A} {CONDITION} {B}", args: [ new ValueInput("A", BlockType.Boolean), - new Dropdown("CONDITION", DropdownType.Auto, { and: "&&", or: "||" }), + new DropdownInput("CONDITION", DropdownType.Auto, { and: "&&", or: "||" }), new ValueInput("B", BlockType.Boolean) ], warnings: [ @@ -134,7 +134,7 @@ const blocks: BlockDefinition[] = [ id: "values", text: "{INPUT}", args: [ - new Dropdown("INPUT", DropdownType.Auto, { + new DropdownInput("INPUT", DropdownType.Auto, { true: "true", false: "false", null: "null", @@ -195,7 +195,7 @@ const blocks: BlockDefinition[] = [ text: "typeof types {TYPE}", args: [ // new ValueInput("OPERAND", BlockType.Any), - new Dropdown("TYPE", DropdownType.Auto, { + new DropdownInput("TYPE", DropdownType.Auto, { string: "string", number: "number", boolean: "boolean", diff --git a/src/lib/blocks/Test Blocks/variables.ts b/src/lib/blocks/Test Blocks/variables.ts new file mode 100644 index 0000000..f1284bd --- /dev/null +++ b/src/lib/blocks/Test Blocks/variables.ts @@ -0,0 +1,153 @@ + +import type { BlockDefinition } from "$lib/types/BlockDefinition"; +import type { CategoryDefinition } from "$lib/types/CategoryDefinition"; +import Blockly from "blockly" +import { BlockShape, BlockType, DropdownType } from "$lib/enums/BlockTypes"; +import DropdownInput from "$lib/utils/BlockGen/Inputs/Dropdown"; +import ValueInput from "$lib/utils/BlockGen/Inputs/ValueInput"; +import VariableInput from "$lib/utils/BlockGen/Inputs/VariableInput"; +// import { xml } from "blockly/core/utils"; +// import BaseInput from "$lib/utils/BlockGen/Inputs/BaseInput"; +// let variableBlockDef: BaseInput = new BaseInput() +function fixVariableName(variable: string): string { + // Remove non-alphabetic characters from the start and end + let fixedVariable = variable.replace(/^[^a-zA-Z]+|[^a-zA-Z0-9]+$/g, ''); + + // Replace non-alphabetic characters in the middle with underscores + fixedVariable = fixedVariable.replace(/[^a-zA-Z0-9]+/g, '_'); + + return fixedVariable; +} +const blocks: BlockDefinition[] = [ + { + kind: "button", + text: "Create a variable...", + callbackKey: "CREATE_VARIABLE_OUR", + callback(p1) { + Blockly.Variables.createVariableButtonHandler(p1.getTargetWorkspace(), undefined, ''); + // let pro = prompt("New variable:") + // p1.getTargetWorkspace().createVariable(pro as string, "any", pro) + }, + }, + { + id: "variable_get_discodes", + text: "{VAR}", + args: [ + new VariableInput("VAR", "") + ], + code(args, block) { + + const varName = fixVariableName(args.VAR as string) + console.log(varName, args.VAR) + return varName + + }, + output: BlockType.Any, + shape: BlockShape.Value, + inline: false, // This is "inputsInline" + colour: "#A55B99", + tooltip: "variable block", + helpUrl: "" + }, + { + id: "variable_set_discodes", + text: "set {VAR} to {INPUT}", + args: [ + new VariableInput("VAR", ""), + new ValueInput("INPUT", BlockType.Any) + ], + code(args, block) { + const varName = fixVariableName(args.VAR as string) + + return `${varName}${args.INPUT !== ""? " =" : ";"} ${args.INPUT !== ""? args.INPUT+";": ""}\n` + }, + shape: BlockShape.Action, + inline: true, // This is "inputsInline" + colour: "#A55B99", + tooltip: "variable block", + helpUrl: "" + }, + { + id: "variable_change_discodes", + text: "change {VAR} by {DROPDOWN} {INPUT}", + args: [ + new VariableInput("VAR", ""), + new DropdownInput("DROPDOWN", DropdownType.Auto, + { + "adding": "+", + "subtracting": "-", + "multiplying": "*", + "dividing": "/", + } + ), + new ValueInput("INPUT", BlockType.Any) + + ], + code(args, block) { + const varName = fixVariableName(args.VAR as string) + console.log(varName) + return `${varName}${args.INPUT !== ""? " "+ args.DROPDOWN + "=" : ";"} ${args.INPUT !== ""? args.INPUT+";": ""}\n` + + }, + shape: BlockShape.Action, + inline: true, // This is "inputsInline" + colour: "#A55B99", + tooltip: "variable block", + helpUrl: "" + } +]; + +const category: CategoryDefinition = { + name: "Variables", + colour: "#db5c53", + custom: "VARIABLE_DYNAMIC_OUR", + customFunction(workspace) { + let variableModelList = workspace.getAllVariables() + let blocklist = []; + blocklist.push({ + "kind": "button", + "text": "Create a variable...", + "callbackKey": "CREATE_VARIABLE_OUR" + }) + if(variableModelList.length !== 0) { + const lastVariable = variableModelList[variableModelList.length-1] + blocklist.push({ + "kind": "block", + "type": "variable_set_discodes", + "fields": { + "VAR": { + "name": lastVariable.name, + "type": lastVariable.type, + } + }, + }) + blocklist.push({ + "kind": "block", + "type": "variable_change_discodes", + "fields": { + "VAR": { + "name": lastVariable.name, + "type": lastVariable.type, + } + }, + }) + } + variableModelList.sort(Blockly.VariableModel.compareByName); + + for(const value of variableModelList) { + blocklist.push( { + "kind": "block", + "fields": { + "VAR": { + "name": value.name, + "type": value.type, + } + }, + "type": "variable_get_discodes", + }) + } + return blocklist; + }, +}; + +export default { blocks, category }; diff --git a/src/lib/components/Workspace.svelte b/src/lib/components/Workspace.svelte index 9dd1a44..e501de1 100644 --- a/src/lib/components/Workspace.svelte +++ b/src/lib/components/Workspace.svelte @@ -11,19 +11,22 @@ import loadBlocks from "$lib/utils/helpers/loadBlocks"; import { warnings } from "$lib/utils/BlockGen/Warnings/WarningsList"; import { imports, wipeImports } from "$lib/utils/BlockGen/Blocks/importsList"; + import type Toolbox from "$lib/utils/ToolboxGen/Toolbox"; export let workspace: Blockly.WorkspaceSvg; export let options: typeof OPTIONS; - export let toolbox: Blockly.utils.toolbox.ToolboxDefinition; + export let toolboxJson: Blockly.utils.toolbox.ToolboxDefinition; + export let toolbox: Toolbox; Blockly.setLocale({ ...En }); onMount(async() => { - await loadBlocks(); - workspace = Blockly.inject("blocklyDiv", { ...options, toolbox: toolbox }); - + await loadBlocks(); + workspace = Blockly.inject("blocklyDiv", { ...options, toolbox: toolboxJson }); + toolbox.registerCallbacks(workspace) + // workspace.refreshToolboxSelection(); // Only console log the code and warnings when debug mode is enabled. const supportedEvents = new Set([ Blockly.Events.BLOCK_CHANGE, @@ -48,6 +51,7 @@ } } workspace.addChangeListener(updateCode); + }); diff --git a/src/lib/enums/BlockTypes.ts b/src/lib/enums/BlockTypes.ts index 9f3f2de..5acf03e 100644 --- a/src/lib/enums/BlockTypes.ts +++ b/src/lib/enums/BlockTypes.ts @@ -24,7 +24,8 @@ export enum BlockShape { Event, //Block shape for a floating block with an input inside. Can be replaced with FLOATING, but keeps code consistent. Floating, //Block shape for a block that cannot have any parent blocks. Bottom, //Block shape for a block with no blocks allowed to attach after. - Top //Block shape for a block that cannot have any blocks attached before it. + Top, //Block shape for a block that cannot have any blocks attached before it. + Value // Block shape for a block that is a value and is placed inside inputs } // This is used for the toolbox generation, it translates to kind:"block" in the blockly shadows. diff --git a/src/lib/types/BlockDefinition.ts b/src/lib/types/BlockDefinition.ts index 53f9eeb..a88c32b 100644 --- a/src/lib/types/BlockDefinition.ts +++ b/src/lib/types/BlockDefinition.ts @@ -4,6 +4,7 @@ import type BaseInput from "$lib/utils/BlockGen/Inputs/BaseInput"; import type {Mutator} from "$lib/utils/BlockGen/Mutators/Mutator"; import type Warning from "$lib/utils/BlockGen/Warnings/Warning"; import type Placeholder from "$lib/utils/ToolboxGen/Placeholder"; +import {FlyoutButton} from "blockly"; export type Argument = BaseInput; @@ -30,7 +31,13 @@ export type BlockDefinition = | { label: true; text: string; - }; + } | + { + kind: "button"; + text: string; + callbackKey: string; + callback: (p1: FlyoutButton) => void; + }; export interface MutatorBlock { // What inputs it adds to the block diff --git a/src/lib/types/CategoryDefinition.ts b/src/lib/types/CategoryDefinition.ts index b19a7ed..e77d22b 100644 --- a/src/lib/types/CategoryDefinition.ts +++ b/src/lib/types/CategoryDefinition.ts @@ -1,6 +1,9 @@ +import Blockly from "blockly" export type CategoryDefinition = { name: string; colour: string; hidden?: boolean; weight?: number; + custom?: string; // add if you want to make dynamic categories + customFunction?: (workspace: Blockly.WorkspaceSvg) => any[]; // used in pair with custom }; diff --git a/src/lib/utils/BlockGen/Blocks/Block.ts b/src/lib/utils/BlockGen/Blocks/Block.ts index 1e63ba1..99a9139 100644 --- a/src/lib/utils/BlockGen/Blocks/Block.ts +++ b/src/lib/utils/BlockGen/Blocks/Block.ts @@ -47,23 +47,28 @@ export default class Block { } addWarning(warning: Warning): void { + if (this._blockDefinition.kind) throw new Error("Cannot add a warning to a input/button"); + if (this._blockDefinition.label) throw new Error("Cannot add a warning to a label"); if (warningsObj[this._block.id] && warningsObj[this._block.id][warning.data.fieldName]) return; this._blockDefinition.warnings = this._blockDefinition.warnings ? [...this._blockDefinition.warnings, warning] : [warning]; } removeWarning(fieldName: string): void { - if (this._blockDefinition.label) throw new Error("Cannot remove a warning form a label"); + if (this._blockDefinition.kind) throw new Error("Cannot remove a warning from a input/button"); + + if (this._blockDefinition.label) throw new Error("Cannot remove a warning from a label"); if ((!warningsObj[this._block.id] || !warningsObj[this._block.id][fieldName]) && this._blockDefinition.warnings !== undefined) return; this._blockDefinition.warnings = this._blockDefinition.warnings?.filter(warning => warning.data.fieldName !== fieldName); } generate(): void { - if (this._blockDefinition.label) return; + if (this._blockDefinition.label || this._blockDefinition.kind) return; // eslint-disable-next-line @typescript-eslint/no-this-alias const blockClass = this; // Used because `this` is overwritten in the blockly functions. + const code = this._blockDefinition.code; const shape = this._blockDefinition.shape; const output = this._blockDefinition.output; @@ -121,6 +126,10 @@ export default class Block { // opposite of terminal this.setNextStatement(true); break; + case BlockShape.Value: + this.setPreviousStatement(false); + this.setNextStatement(false); + break; } // Here we add an output if needed @@ -220,11 +229,9 @@ export default class Block { const argName: string = blockDef.args0[arg].name as string; args[argName] = getInputValue(block, argName, argValue.type); } - //parse mutator values for(const propertyKey of Object.keys(propertyMap)) { const property = propertyMap[propertyKey]; - console.log(propertyMap) for (const add of property.adds) { const valueList: string[] = []; let i = 1; diff --git a/src/lib/utils/BlockGen/Inputs/Dropdown.ts b/src/lib/utils/BlockGen/Inputs/Dropdown.ts index c287739..51957b0 100644 --- a/src/lib/utils/BlockGen/Inputs/Dropdown.ts +++ b/src/lib/utils/BlockGen/Inputs/Dropdown.ts @@ -15,7 +15,7 @@ interface DropdownJSON { * @class Dropdown * @extends {BaseInput} */ -export default class Dropdown extends BaseInput { +export default class DropdownInput extends BaseInput { private readonly _options: Array>; private _dropdownType: DropdownType; @@ -52,7 +52,6 @@ export default class Dropdown extends BaseInput { // Automatically swaps between grid and list type depending on the length of the arguments. this._dropdownType = this._options.length > 10 ? DropdownType.Grid : DropdownType.List; } - console.log(this._options) return { type: this._dropdownType, name: super.name, diff --git a/src/lib/utils/BlockGen/Inputs/VariableInput.ts b/src/lib/utils/BlockGen/Inputs/VariableInput.ts new file mode 100644 index 0000000..c44f0c8 --- /dev/null +++ b/src/lib/utils/BlockGen/Inputs/VariableInput.ts @@ -0,0 +1,37 @@ +import BaseInput from "./BaseInput"; + +interface VariableInputObject { + type: "field_variable"; + name: string; + variable: string; + variableTypes?: string[]; + defaultType?: string; +} + +export default class VariableInput extends BaseInput { + private readonly _variable: string; + private readonly _opt_defaultType?: string; + private readonly _opt_variableTypes?: string[]; + + constructor(name: string, variable: string, opt_defaultType?: string, opt_variableTypes?: string[]) { + super(name); + + this.setMethod(this.getDefinition); + super.setName(name); + this._variable = variable; + this._opt_defaultType = opt_defaultType; + this._opt_variableTypes = opt_variableTypes + } + + private getDefinition(): VariableInputObject { + return { + type: "field_variable", + name: super.name, + variable: this._variable === ""? "%{BKY_VARIABLES_DEFAULT_NAME}" : this._variable, + variableTypes: this._opt_variableTypes, + defaultType: this._opt_defaultType, + + }; + } + +} diff --git a/src/lib/utils/BlockGen/Mutators/AssemblerMutatorV2.ts b/src/lib/utils/BlockGen/Mutators/AssemblerMutatorV2.ts index 2493019..b762109 100644 --- a/src/lib/utils/BlockGen/Mutators/AssemblerMutatorV2.ts +++ b/src/lib/utils/BlockGen/Mutators/AssemblerMutatorV2.ts @@ -59,7 +59,6 @@ export default class AssemblerMutatorV2 extends Mutator { for(const prop of properties) { propertieMap[prop.block] = prop; } - console.log(settings) Blockly.Blocks[containerBlockName] = { init: function(this: Blockly.Block) { this.jsonInit({ diff --git a/src/lib/utils/BlockGen/Mutators/CheckboxMutator.ts b/src/lib/utils/BlockGen/Mutators/CheckboxMutator.ts index aae344f..583cbbc 100644 --- a/src/lib/utils/BlockGen/Mutators/CheckboxMutator.ts +++ b/src/lib/utils/BlockGen/Mutators/CheckboxMutator.ts @@ -140,7 +140,6 @@ export default class CheckboxMutator extends Mutator { if(!conn) continue; conn.reconnect(this, connectionKey); } - console.log(connections) }, // eslint-disable-next-line saveConnections: function(this: any, containerBlock: any) { diff --git a/src/lib/utils/ToolboxGen/CallBacks.ts b/src/lib/utils/ToolboxGen/CallBacks.ts new file mode 100644 index 0000000..86bdd44 --- /dev/null +++ b/src/lib/utils/ToolboxGen/CallBacks.ts @@ -0,0 +1,19 @@ +import Blockly from "blockly/core"; +import toolbox from "blockly/core/utils/toolbox"; + + + +class CallBacks { + private categoryCallBacks: Record toolbox.FlyoutDefinition>; + private buttonCallbacks: Record toolbox.FlyoutDefinition>; + + constructor() { + this.categoryCallBacks = {}; + this.buttonCallbacks = {}; + }; + addCategoryCallback(name: string, func:(p1: Blockly.WorkspaceSvg) => toolbox.FlyoutDefinition) {} + register(workspace: Blockly.WorkspaceSvg) { + workspace.reg + } + +} diff --git a/src/lib/utils/ToolboxGen/Toolbox.ts b/src/lib/utils/ToolboxGen/Toolbox.ts index e19a36f..028d496 100644 --- a/src/lib/utils/ToolboxGen/Toolbox.ts +++ b/src/lib/utils/ToolboxGen/Toolbox.ts @@ -1,7 +1,17 @@ import type { BlockDefinition } from "$lib/types/BlockDefinition"; import type { ToolboxDefinition, ToolboxItemInfo } from "blockly/core/utils/toolbox"; +import type {FlyoutButton} from "blockly"; +import Blockly from "blockly/core"; export default class Toolbox { + private callbackCategory: Record; + private callbackOther: Record; + + constructor() { + this.callbackCategory = {}; + this.callbackOther = {}; + + } public async generate(): Promise { const structure = await this.fetchFiles(); const contents = []; @@ -15,7 +25,15 @@ export default class Toolbox { contents: contents as ToolboxItemInfo[] }; } - + public registerCallbacks(workspace: Blockly.WorkspaceSvg) { + for (const catKey of Object.keys(this.callbackCategory)) { + workspace.registerToolboxCategoryCallback(catKey, this.callbackCategory[catKey]) + } + for (const otherKey of Object.keys(this.callbackOther)) { + console.log(otherKey) + workspace.registerButtonCallback(otherKey, this.callbackOther[otherKey]) + } + } private async fetchFiles() { const files = import.meta.glob("../../blocks/**/**/*.ts"); const directories: string[] = Object.keys(files).map((key) => key); @@ -61,12 +79,36 @@ export default class Toolbox { //! FIX THE TYPE OR ELSE ITS GONNA BREAK LMFAO (Memory leak also?) const definitions = await import(/* @vite-ignore */ `${topPath}/${directory}`); + if(definitions.default.category.custom) { + // if(!definitions.default.category.customFunction && definitions.default.category.custom !== "VARIABLE_DYNAMIC") throw new Error("'customFunction' is required for a custom category!") + contents.push({ + kind: "category", + name: definitions.default.category.name, + colour: definitions.default.category.colour, + contents: [], + custom: definitions.default.category.custom + }); + if(!definitions.default.category.customFunction) return + this.callbackCategory[definitions.default.category.custom] = definitions.default.category.customFunction + for (const blockDef of definitions.default.blocks as BlockDefinition[]) { + if(blockDef.kind === "button") { + this.callbackOther[blockDef.callbackKey] = blockDef.callback; + continue; + } + } + } const blockContents = []; for (const blockDef of definitions.default.blocks as BlockDefinition[]) { if (!blockDef.label && blockDef.hidden === true) continue; const inputs: Record = {}; + if(blockDef.kind === "button") { + this.callbackOther[blockDef.callbackKey] = blockDef.callback; + // this.workspace.registerButtonCallback(blockDef.callbackKey, blockDef.callback); + blockContents.push(blockDef); + continue; + } if (!blockDef.label && blockDef.placeholders) { for (const placeholder of blockDef.placeholders) { const data = placeholder.values; @@ -93,13 +135,15 @@ export default class Toolbox { } ); } + if(!definitions.default.category.custom) { + contents.push({ + kind: "category", + name: definitions.default.category.name, + contents: blockContents, + colour: definitions.default.category.colour + }); + } - contents.push({ - kind: "category", - name: definitions.default.category.name, - contents: blockContents, - colour: definitions.default.category.colour - }); } return; } diff --git a/src/lib/utils/helpers/getInputValue.ts b/src/lib/utils/helpers/getInputValue.ts index 0ae49e0..7ab9b9a 100644 --- a/src/lib/utils/helpers/getInputValue.ts +++ b/src/lib/utils/helpers/getInputValue.ts @@ -1,4 +1,5 @@ import Blockly from "blockly/core"; +import Blockl from "blockly/core"; import pkg from "blockly/javascript"; const {javascriptGenerator} = pkg; export function getInputValue(block: Blockly.Block, inputName: string, inputType: string): string { @@ -10,6 +11,9 @@ export function getInputValue(block: Blockly.Block, inputName: string, inputType inputName, javascriptGenerator.ORDER_ATOMIC ); + case "field_variable": + return Blockl.Variables.getVariable(block.workspace, block.getFieldValue(inputName))?.name ?? "null" + case "input_statement": return javascriptGenerator.statementToCode(block, inputName); default: diff --git a/src/routes/editor/+page.svelte b/src/routes/editor/+page.svelte index 0fe79d0..6371171 100644 --- a/src/routes/editor/+page.svelte +++ b/src/routes/editor/+page.svelte @@ -14,7 +14,7 @@ let workspace: Blockly.WorkspaceSvg; let toolboxJson: Blockly.utils.toolbox.ToolboxDefinition; - + let toolbox: Toolbox; let currentFile: string; let discodesWorkspaceID: string; @@ -30,7 +30,8 @@ }; onMount(async() => { - toolboxJson = await new Toolbox().generate(); + toolbox = await new Toolbox(); + toolboxJson = await toolbox.generate(); discodesWorkspaceID = $page.url.searchParams.get("id") || "1"; const discodesWorksapce = localDB.getWorkspaceByID(discodesWorkspaceID); @@ -54,4 +55,4 @@ }}>LOAD - + From 43721f493121fa4e143df87a0ae289139d3eb994 Mon Sep 17 00:00:00 2001 From: mrredo Date: Thu, 30 May 2024 12:15:22 +0300 Subject: [PATCH 2/2] less eslint errors --- src/lib/blocks/Test Blocks/variables.ts | 10 ++++------ src/lib/types/CategoryDefinition.ts | 1 + src/lib/utils/ToolboxGen/CallBacks.ts | 19 ------------------- src/lib/utils/ToolboxGen/Toolbox.ts | 6 +++--- 4 files changed, 8 insertions(+), 28 deletions(-) delete mode 100644 src/lib/utils/ToolboxGen/CallBacks.ts diff --git a/src/lib/blocks/Test Blocks/variables.ts b/src/lib/blocks/Test Blocks/variables.ts index f1284bd..daf9ccd 100644 --- a/src/lib/blocks/Test Blocks/variables.ts +++ b/src/lib/blocks/Test Blocks/variables.ts @@ -6,9 +6,7 @@ import { BlockShape, BlockType, DropdownType } from "$lib/enums/BlockTypes"; import DropdownInput from "$lib/utils/BlockGen/Inputs/Dropdown"; import ValueInput from "$lib/utils/BlockGen/Inputs/ValueInput"; import VariableInput from "$lib/utils/BlockGen/Inputs/VariableInput"; -// import { xml } from "blockly/core/utils"; -// import BaseInput from "$lib/utils/BlockGen/Inputs/BaseInput"; -// let variableBlockDef: BaseInput = new BaseInput() + function fixVariableName(variable: string): string { // Remove non-alphabetic characters from the start and end let fixedVariable = variable.replace(/^[^a-zA-Z]+|[^a-zA-Z0-9]+$/g, ''); @@ -35,7 +33,7 @@ const blocks: BlockDefinition[] = [ args: [ new VariableInput("VAR", "") ], - code(args, block) { + code(args) { const varName = fixVariableName(args.VAR as string) console.log(varName, args.VAR) @@ -56,7 +54,7 @@ const blocks: BlockDefinition[] = [ new VariableInput("VAR", ""), new ValueInput("INPUT", BlockType.Any) ], - code(args, block) { + code(args) { const varName = fixVariableName(args.VAR as string) return `${varName}${args.INPUT !== ""? " =" : ";"} ${args.INPUT !== ""? args.INPUT+";": ""}\n` @@ -83,7 +81,7 @@ const blocks: BlockDefinition[] = [ new ValueInput("INPUT", BlockType.Any) ], - code(args, block) { + code(args) { const varName = fixVariableName(args.VAR as string) console.log(varName) return `${varName}${args.INPUT !== ""? " "+ args.DROPDOWN + "=" : ";"} ${args.INPUT !== ""? args.INPUT+";": ""}\n` diff --git a/src/lib/types/CategoryDefinition.ts b/src/lib/types/CategoryDefinition.ts index e77d22b..e0c2e93 100644 --- a/src/lib/types/CategoryDefinition.ts +++ b/src/lib/types/CategoryDefinition.ts @@ -5,5 +5,6 @@ export type CategoryDefinition = { hidden?: boolean; weight?: number; custom?: string; // add if you want to make dynamic categories + // eslint-disable-next-line no-use-before-define customFunction?: (workspace: Blockly.WorkspaceSvg) => any[]; // used in pair with custom }; diff --git a/src/lib/utils/ToolboxGen/CallBacks.ts b/src/lib/utils/ToolboxGen/CallBacks.ts deleted file mode 100644 index 86bdd44..0000000 --- a/src/lib/utils/ToolboxGen/CallBacks.ts +++ /dev/null @@ -1,19 +0,0 @@ -import Blockly from "blockly/core"; -import toolbox from "blockly/core/utils/toolbox"; - - - -class CallBacks { - private categoryCallBacks: Record toolbox.FlyoutDefinition>; - private buttonCallbacks: Record toolbox.FlyoutDefinition>; - - constructor() { - this.categoryCallBacks = {}; - this.buttonCallbacks = {}; - }; - addCategoryCallback(name: string, func:(p1: Blockly.WorkspaceSvg) => toolbox.FlyoutDefinition) {} - register(workspace: Blockly.WorkspaceSvg) { - workspace.reg - } - -} diff --git a/src/lib/utils/ToolboxGen/Toolbox.ts b/src/lib/utils/ToolboxGen/Toolbox.ts index 028d496..13df6cb 100644 --- a/src/lib/utils/ToolboxGen/Toolbox.ts +++ b/src/lib/utils/ToolboxGen/Toolbox.ts @@ -1,11 +1,11 @@ import type { BlockDefinition } from "$lib/types/BlockDefinition"; import type { ToolboxDefinition, ToolboxItemInfo } from "blockly/core/utils/toolbox"; -import type {FlyoutButton} from "blockly"; import Blockly from "blockly/core"; export default class Toolbox { - private callbackCategory: Record; - private callbackOther: Record; + // eslint-disable-next-line no-use-before-define + private callbackCategory: Record any[]>; + private callbackOther: Record void>; constructor() { this.callbackCategory = {};