Skip to content

Commit 570164d

Browse files
committed
Support Blockly 11
1 parent bb2f404 commit 570164d

6 files changed

Lines changed: 157 additions & 107 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"author": "Nat Budin <natbudin@gmail.com>",
2020
"license": "MIT",
2121
"dependencies": {
22-
"blockly": ">= 10.0.0 < 11.0.0",
22+
"blockly": ">= 11.0.0",
2323
"prop-types": "^15.8.1"
2424
},
2525
"peerDependencies": {

src/BlocklyWorkspaceProps.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import Blockly, { WorkspaceSvg } from "blockly";
1+
import * as Blockly from "blockly/core";
2+
import { WorkspaceSvg } from "blockly";
23
import { RefObject } from "react";
34

45
export interface CommonBlocklyProps {

src/dev-index.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState } from "react";
22
import ReactDOM from "react-dom";
3-
import Blockly from "blockly";
3+
import * as Blockly from "blockly/core";
44
import { javascriptGenerator } from "blockly/javascript";
55

66
import { BlocklyWorkspace } from "./index";
@@ -67,7 +67,9 @@ const TestEditor = () => {
6767
});
6868
const newXml = Blockly.Xml.domToText(Blockly.Xml.workspaceToDom(workspace));
6969
setGeneratedXml(newXml);
70-
const newJson = JSON.stringify(Blockly.serialization.workspaces.save(workspace));
70+
const newJson = JSON.stringify(
71+
Blockly.serialization.workspaces.save(workspace)
72+
);
7173
setGeneratedJson(newJson);
7274
const code = javascriptGenerator.workspaceToCode(workspace);
7375
setGeneratedCode(code);
@@ -80,11 +82,19 @@ const TestEditor = () => {
8082
const onJsonChange = React.useCallback((newJson) => {
8183
setGeneratedJson(JSON.stringify(newJson));
8284
}, []);
83-
const [ serialState, setSerialState ] = useState<"XML" | "JSON">("XML")
85+
const [serialState, setSerialState] = useState<"XML" | "JSON">("XML");
8486
return (
8587
<>
8688
<div style={{ height: "600px", width: "800px" }}>
87-
<button onClick={(e)=>setSerialState((e.target as HTMLElement).innerText == "XML" ? "XML" : "JSON")}>{serialState == "XML" ? "JSON" : "XML"} </button>
89+
<button
90+
onClick={(e) =>
91+
setSerialState(
92+
(e.target as HTMLElement).innerText == "XML" ? "XML" : "JSON"
93+
)
94+
}
95+
>
96+
{serialState == "XML" ? "JSON" : "XML"}{" "}
97+
</button>
8898
<BlocklyWorkspace
8999
key={serialState}
90100
toolboxConfiguration={toolboxConfiguration}
@@ -96,8 +106,12 @@ const TestEditor = () => {
96106
snap: true,
97107
},
98108
}}
99-
initialXml={serialState === "XML" ? ConfigFiles.INITIAL_XML : undefined}
100-
initialJson={serialState === "JSON" ? ConfigFiles.INITIAL_JSON : undefined}
109+
initialXml={
110+
serialState === "XML" ? ConfigFiles.INITIAL_XML : undefined
111+
}
112+
initialJson={
113+
serialState === "JSON" ? ConfigFiles.INITIAL_JSON : undefined
114+
}
101115
className="fill-height"
102116
onWorkspaceChange={onWorkspaceChange}
103117
onXmlChange={onXmlChange}

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import useBlocklyWorkspace from "./useBlocklyWorkspace";
22
import BlocklyWorkspace from "./BlocklyWorkspace";
3-
import Blockly, {WorkspaceSvg, Workspace} from "blockly";
3+
import { WorkspaceSvg, Workspace } from "blockly";
4+
import * as Blockly from "blockly/core";
45
import ToolboxDefinition1 = Blockly.utils.toolbox.ToolboxDefinition;
56
export type ToolboxDefinition = ToolboxDefinition1;
67
export { BlocklyWorkspace, useBlocklyWorkspace };
7-
export {WorkspaceSvg, Workspace};
8+
export { WorkspaceSvg, Workspace };

src/useBlocklyWorkspace.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react";
2-
import Blockly, { Workspace, WorkspaceSvg } from "blockly";
2+
import * as Blockly from "blockly/core";
3+
import { Workspace, WorkspaceSvg } from "blockly";
34
import { UseBlocklyProps } from "./BlocklyWorkspaceProps";
45

56
import debounce from "./debounce";
@@ -50,10 +51,14 @@ const useBlocklyWorkspace = ({
5051
onImportError,
5152
onInject,
5253
onDispose,
53-
}: UseBlocklyProps): { workspace: WorkspaceSvg | null; xml: string | null, json: object | null } => {
54-
// onImportError replaces onImportXmlError
54+
}: UseBlocklyProps): {
55+
workspace: WorkspaceSvg | null;
56+
xml: string | null;
57+
json: object | null;
58+
} => {
59+
// onImportError replaces onImportXmlError
5560
// This is done for not breaking the signature until depreaction
56-
onImportError = onImportError ?? onImportXmlError
61+
onImportError = onImportError ?? onImportXmlError;
5762

5863
const [workspace, setWorkspace] = React.useState<WorkspaceSvg | null>(null);
5964
const [xml, setXml] = React.useState<string | null>(initialXml || null);
@@ -72,7 +77,11 @@ const useBlocklyWorkspace = ({
7277
const toolboxConfigurationRef = React.useRef(toolboxConfiguration);
7378
React.useEffect(() => {
7479
toolboxConfigurationRef.current = toolboxConfiguration;
75-
if (toolboxConfiguration && workspace && !workspaceConfiguration?.readOnly) {
80+
if (
81+
toolboxConfiguration &&
82+
workspace &&
83+
!workspaceConfiguration?.readOnly
84+
) {
7685
workspace.updateToolbox(toolboxConfiguration);
7786
}
7887
}, [toolboxConfiguration, workspace, workspaceConfiguration]);
@@ -180,15 +189,14 @@ const useBlocklyWorkspace = ({
180189
setXml(null);
181190
}
182191
setDidInitialImport(true);
183-
}
184-
else if (json && workspace && !didInitialImport) {
192+
} else if (json && workspace && !didInitialImport) {
185193
const success = importFromJson(json, workspace, onImportError);
186194
if (!success) {
187195
setJson(null);
188196
}
189197
const jsonToXml = Blockly.Xml.domToText(
190198
Blockly.Xml.workspaceToDom(workspace)
191-
)
199+
);
192200
setXml(jsonToXml);
193201
setDidInitialImport(true);
194202
}

0 commit comments

Comments
 (0)