Skip to content

Commit 710c28a

Browse files
committed
this seems to work to fix origin#125
1 parent ac7c788 commit 710c28a

4 files changed

+77
-5
lines changed

src/BlocklyWorkspace.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { BlocklyWorkspaceProps } from "./BlocklyWorkspaceProps";
66
const propTypes = {
77
initialXml: PropTypes.string,
88
initialJson: PropTypes.object,
9+
updateXml: PropTypes.string,
10+
updateJson: PropTypes.object,
911
toolboxConfiguration: PropTypes.object, // eslint-disable-line react/forbid-prop-types
1012
workspaceConfiguration: PropTypes.object, // eslint-disable-line react/forbid-prop-types
1113
className: PropTypes.string,
@@ -21,6 +23,8 @@ const propTypes = {
2123
function BlocklyWorkspace({
2224
initialXml,
2325
initialJson,
26+
updateXml,
27+
updateJson,
2428
toolboxConfiguration,
2529
workspaceConfiguration,
2630
className,
@@ -37,13 +41,16 @@ function BlocklyWorkspace({
3741
ref: editorDiv,
3842
initialXml,
3943
initialJson,
44+
updateXml,
45+
updateJson,
4046
toolboxConfiguration,
4147
workspaceConfiguration,
4248
onWorkspaceChange,
4349
onImportXmlError,
4450
onImportError,
4551
onInject,
4652
onDispose,
53+
4754
});
4855
const onXmlChangeRef = React.useRef(onXmlChange);
4956
React.useEffect(() => {

src/BlocklyWorkspaceProps.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { RefObject } from "react";
55
export interface CommonBlocklyProps {
66
initialXml?: string;
77
initialJson?: object;
8+
updateXml?: string;
9+
updateJson?: object;
810
toolboxConfiguration?: Blockly.utils.toolbox.ToolboxDefinition;
911
workspaceConfiguration: Blockly.BlocklyOptions;
1012
onWorkspaceChange?: (workspace: WorkspaceSvg) => void;

src/dev-index.tsx

+23-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const TestEditor = () => {
1515
const [generatedXml, setGeneratedXml] = useState("");
1616
const [generatedJson, setGeneratedJson] = useState("");
1717
const [generatedCode, setGeneratedCode] = useState("");
18+
const [updateXml, setUpdateXml] = useState("");
19+
const [updateJson, setUpdateJson] = useState({});
1820

1921
React.useEffect(() => {
2022
window.setTimeout(() => {
@@ -85,7 +87,7 @@ const TestEditor = () => {
8587
const [serialState, setSerialState] = useState<"XML" | "JSON">("XML");
8688
return (
8789
<>
88-
<div style={{ height: "600px", width: "800px" }}>
90+
<div style={{ height: "600px", width: "800px", marginBlockEnd: "2rem" }}>
8991
<button
9092
onClick={(e) =>
9193
setSerialState(
@@ -116,10 +118,28 @@ const TestEditor = () => {
116118
onWorkspaceChange={onWorkspaceChange}
117119
onXmlChange={onXmlChange}
118120
onJsonChange={onJsonChange}
121+
updateXml={updateXml}
122+
updateJson={updateJson}
119123
/>
120124
</div>
121-
<pre>{generatedXml}</pre>
122-
<p>{generatedJson}</p>
125+
<textarea
126+
style={{ height: "200px", width: "400px" }}
127+
value={generatedXml}
128+
onChange={(ev)=>{
129+
console.log('onChange generated xml', ev)
130+
console.log(ev.target.value)
131+
setUpdateXml(ev.target.value)
132+
}}
133+
/>
134+
<textarea
135+
style={{ height: "200px", width: "400px" }}
136+
value={generatedJson}
137+
onChange={(ev)=>{
138+
console.log('onChange generated json', ev)
139+
console.log(ev.target.value)
140+
setUpdateJson(JSON.parse(ev.target.value))
141+
}}
142+
/>
123143
<textarea
124144
style={{ height: "200px", width: "400px" }}
125145
value={generatedCode}

src/useBlocklyWorkspace.ts

+45-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,32 @@ function importFromXml(
2323
}
2424
}
2525

26+
function repeatableImportFromXml(
27+
xml: string,
28+
workspace: Workspace,
29+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30+
onImportError?: (error: any) => void
31+
) {
32+
try {
33+
workspace.clear();
34+
Blockly.Xml.domToWorkspace(Blockly.utils.xml.textToDom(xml), workspace);
35+
return true;
36+
} catch (e) {
37+
if (onImportError) {
38+
onImportError(e);
39+
}
40+
return false;
41+
}
42+
}
43+
2644
function importFromJson(
2745
json: object,
2846
workspace: Workspace,
2947
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3048
onImportError?: (error: any) => void
3149
) {
3250
try {
51+
workspace.clear();
3352
Blockly.serialization.workspaces.load(json, workspace);
3453
return true;
3554
} catch (e) {
@@ -44,6 +63,8 @@ const useBlocklyWorkspace = ({
4463
ref,
4564
initialXml,
4665
initialJson,
66+
updateXml,
67+
updateJson,
4768
toolboxConfiguration,
4869
workspaceConfiguration,
4970
onWorkspaceChange,
@@ -57,7 +78,7 @@ const useBlocklyWorkspace = ({
5778
json: object | null;
5879
} => {
5980
// onImportError replaces onImportXmlError
60-
// This is done for not breaking the signature until depreaction
81+
// This is done for not breaking the signature until deprecation
6182
onImportError = onImportError ?? onImportXmlError;
6283

6384
const [workspace, setWorkspace] = React.useState<WorkspaceSvg | null>(null);
@@ -181,7 +202,7 @@ const useBlocklyWorkspace = ({
181202
};
182203
}, [workspace, xml]);
183204

184-
// Initial Xml Changes
205+
// Initial Xml/Json Changes
185206
React.useEffect(() => {
186207
if (xml && workspace && !didInitialImport) {
187208
const success = importFromXml(xml, workspace, onImportError);
@@ -202,6 +223,28 @@ const useBlocklyWorkspace = ({
202223
}
203224
}, [json, xml, workspace, didInitialImport, onImportError]);
204225

226+
// update xml/json
227+
React.useEffect(() => {
228+
console.log('update xml/json', updateXml, updateJson)
229+
230+
if (updateXml && workspace) {
231+
const success = repeatableImportFromXml(updateXml, workspace, onImportError);
232+
if (!success) {
233+
setXml(null);
234+
}
235+
} else if (updateJson && workspace) {
236+
const success = importFromJson(updateJson, workspace, onImportError);
237+
if (!success) {
238+
setJson(null);
239+
}
240+
}
241+
242+
}, [updateXml, updateJson, workspace, onImportError]);
243+
244+
React.useEffect(() => {
245+
246+
}, [updateJson, workspace, onImportError]);
247+
205248
return { workspace, xml, json };
206249
};
207250

0 commit comments

Comments
 (0)