Skip to content

Commit 1ff44fd

Browse files
committed
setCellCode function
1 parent 39162af commit 1ff44fd

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import CodeCell from "./components/codecell"
22
import runCell from "./lib/runCell"
3+
import setCellCode from "./lib/setCellCode"
34

4-
export { CodeCell, runCell }
5+
export { CodeCell, runCell, setCellCode }

src/lib/setCellCode.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default function setCellCode(cellId: string, code: string, devMode = false) {
2+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3+
const iframe: any = document.getElementById(`${cellId}`);
4+
if (!iframe) return console.error(`Cell with id ${cellId} not found`);
5+
if (iframe.contentWindow != null) {
6+
const host = devMode ? "*" : "https://ide.betteridea.dev";
7+
iframe.contentWindow.postMessage({ action: "set_code", code }, host);
8+
}
9+
}

src/main.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ import React from 'react'
22
import ReactDOM from 'react-dom/client'
33
import CodeCell from "./components/codecell"
44
import runCell from "./lib/runCell"
5+
import setCellCode from './lib/setCellCode'
56
import "./styles/index.css"
67

8+
function onInput(e: string) {
9+
console.log(e)
10+
setCellCode("1", e, true)
11+
}
12+
13+
714
ReactDOM.createRoot(document.getElementById('root')!).render(
815
<React.StrictMode>
916
<div style={{ padding: "10px", backgroundColor: "black", height: "100vh" }}>
1017
<button onClick={() => {
1118
runCell("1", true)
1219
}}>run</button>
20+
<input type="text" id="code" onChange={(e) => { onInput(e.target.value) }} />
1321
<CodeCell appName="test-cell" cellId="1" devMode />
1422
</div>
1523
</React.StrictMode>,

0 commit comments

Comments
 (0)