Skip to content

Commit 97c4e1e

Browse files
authored
fix 'run me ' button click callback (#169)
1 parent 129c340 commit 97c4e1e

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/components/CodeEditor/index.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ export const CodeEditor = ({ code: codeProp, children, canvasId, autoRun = true,
1818
const [code, setCode] = useState('');
1919

2020
const runCallback = useCallback(
21-
debounce((newcode = [codeProp]) => {
21+
debounce((newcode = [codeProp], isClickByRunMeButton = false) => {
2222
if (window.canvasesId[canvasId]) {
2323
window.canvasesId[canvasId].dispose();
2424
}
2525
const preamble = [
2626
`const fabric = window.fabric;`,
2727
`const canvasEl = document.getElementById('${canvasId}');`,
2828
];
29+
// when click 'run me' button, makes code string change to trigger 'setCode' function.
30+
if (isClickByRunMeButton) preamble.push(`const randomValue = ${Math.random()};`);
2931
const exec = `try {
3032
${newcode.join('\n')}
3133
window.canvasesId['${canvasId}'] = canvas;
@@ -90,7 +92,7 @@ export const CodeEditor = ({ code: codeProp, children, canvasId, autoRun = true,
9092
</Helmet>
9193
{canvasDown || children}
9294
<div ref={divRef} style={{ marginTop: '1rem' }} />
93-
<button onClick={() => runCallback([editorRef.current.state.doc.toString()])}>runMe</button>
95+
<button onClick={() => runCallback([editorRef.current.state.doc.toString()], true)}>Run me</button>
9496
{canvasDown && children}
9597
</div>
9698
);

src/content/docs/docs/configuring-controls.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ If you prefer to have shared controls among instances, you have to act on the de
164164
This will let you configure the controls once for all class types, and let you add controls globally at runtime.
165165

166166
Each setup comes with pros and cons that depends on your personal preference and project's functionalities.
167-
For this snippet you have to press the runMe button. Once you run it, the above snippets are affected too.
167+
For this snippet you have to press the Run me button. Once you run it, the above snippets are affected too.
168168

169169
<CodeEditor code={code5} canvasId="g" client:idle autoRun={false} canvasDown >
170170
<canvas width="400" height="200" id="g"></canvas>

0 commit comments

Comments
 (0)