@@ -2,8 +2,8 @@ import { PositiveInt, useEvolu } from '@evolu/react'
2
2
import { Button } from '@mui/material'
3
3
import * as Blockly from 'blockly/core'
4
4
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'
7
7
import type { GroupKey , LevelKey } from '../data/levels'
8
8
import type { Database } from '../database/Database'
9
9
import { getLevelIdentifier } from '../utilities/getLevelIdentifier'
@@ -80,7 +80,7 @@ export type BlockType = (typeof blocks)[number]['type']
80
80
81
81
Blockly . defineBlocksWithJsonArray ( [ ...blocks ] )
82
82
83
- const configuration = {
83
+ const workspaceConfiguration = {
84
84
grid : {
85
85
spacing : 22 ,
86
86
length : 3 ,
@@ -94,7 +94,7 @@ export const Playground: FunctionComponent<{
94
94
levelKey : LevelKey
95
95
groupKey : GroupKey
96
96
} > = ( { allowedBlocks, levelKey, groupKey } ) => {
97
- const toolbox = useMemo (
97
+ const toolboxConfiguration = useMemo (
98
98
( ) => ( {
99
99
kind : 'flyoutToolbox' ,
100
100
contents : blocks
@@ -103,34 +103,35 @@ export const Playground: FunctionComponent<{
103
103
} ) ,
104
104
[ allowedBlocks ] ,
105
105
)
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 ) )
106
127
const { createOrUpdate } = useEvolu < Database > ( )
107
128
const [ code , setCode ] = useState ( '' )
108
129
109
130
// @TODO : update theme with media query changes (workspace.setTheme(theme))
110
131
111
132
return (
112
133
< >
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 } />
134
135
< div className = { styles . run } >
135
136
< Button
136
137
variant = "contained"
0 commit comments