Skip to content

Commit b2860ff

Browse files
committed
Blockly: use hook
1 parent 2734a34 commit b2860ff

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

src/Components/Playground.tsx

+26-25
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { PositiveInt, useEvolu } from '@evolu/react'
22
import { Button } from '@mui/material'
33
import * as Blockly from 'blockly/core'
44
import { javascriptGenerator } from 'blockly/javascript'
5-
import { FunctionComponent, useMemo, useState } from 'react'
6-
import { BlocklyWorkspace } from 'react-blockly'
5+
import { FunctionComponent, useMemo, useRef, useState } from 'react'
6+
import { useBlocklyWorkspace } from 'react-blockly'
77
import type { GroupKey, LevelKey } from '../data/levels'
88
import type { Database } from '../database/Database'
99
import { getLevelIdentifier } from '../utilities/getLevelIdentifier'
@@ -80,7 +80,7 @@ export type BlockType = (typeof blocks)[number]['type']
8080

8181
Blockly.defineBlocksWithJsonArray([...blocks])
8282

83-
const configuration = {
83+
const workspaceConfiguration = {
8484
grid: {
8585
spacing: 22,
8686
length: 3,
@@ -94,7 +94,7 @@ export const Playground: FunctionComponent<{
9494
levelKey: LevelKey
9595
groupKey: GroupKey
9696
}> = ({ allowedBlocks, levelKey, groupKey }) => {
97-
const toolbox = useMemo(
97+
const toolboxConfiguration = useMemo(
9898
() => ({
9999
kind: 'flyoutToolbox',
100100
contents: blocks
@@ -103,34 +103,35 @@ export const Playground: FunctionComponent<{
103103
}),
104104
[allowedBlocks],
105105
)
106+
const blocklyRef = useRef<HTMLDivElement>(null)
107+
const { workspace, xml, json } = useBlocklyWorkspace({
108+
ref: blocklyRef,
109+
workspaceConfiguration,
110+
toolboxConfiguration,
111+
initialXml,
112+
onWorkspaceChange(workspace) {
113+
setCode(javascriptGenerator.workspaceToCode(workspace))
114+
// @TODO: do this with first change only
115+
const blocks = workspace.getAllBlocks()
116+
blocks.forEach((block) => {
117+
if (block.type === 'start') {
118+
block.setMovable(false)
119+
block.setEditable(false)
120+
block.setDeletable(false)
121+
}
122+
})
123+
},
124+
})
125+
console.log(workspace, xml)
126+
console.log(JSON.stringify(json))
106127
const { createOrUpdate } = useEvolu<Database>()
107128
const [code, setCode] = useState('')
108129

109130
// @TODO: update theme with media query changes (workspace.setTheme(theme))
110131

111132
return (
112133
<>
113-
<BlocklyWorkspace
114-
initialXml={initialXml}
115-
className={styles.workspace}
116-
workspaceConfiguration={configuration}
117-
toolboxConfiguration={toolbox}
118-
onWorkspaceChange={(workspace) => {
119-
setCode(javascriptGenerator.workspaceToCode(workspace))
120-
// @TODO: do this with first change only
121-
const blocks = workspace.getAllBlocks()
122-
blocks.forEach((block) => {
123-
if (block.type === 'start') {
124-
block.setMovable(false)
125-
block.setEditable(false)
126-
block.setDeletable(false)
127-
}
128-
})
129-
}}
130-
onXmlChange={() => {
131-
// @TODO: save this xml to database to remember last state so user can continue later
132-
}}
133-
/>
134+
<div ref={blocklyRef} className={styles.workspace} />
134135
<div className={styles.run}>
135136
<Button
136137
variant="contained"

0 commit comments

Comments
 (0)