Skip to content

Commit 7d522a1

Browse files
committed
ao process callback
1 parent 93952d8 commit 7d522a1

File tree

3 files changed

+60
-31
lines changed

3 files changed

+60
-31
lines changed

README.md

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Now you can integrate our code cells into your webapps and run AO LUA anywhere
44

55
[![npm](https://img.shields.io/badge/@betteridea/codecell-npm-red)](https://www.npmjs.com/package/@betteridea/codecell)
66
[![downloads](https://img.shields.io/npm/dt/@betteridea/codecell?color=red)](https://www.npmjs.com/package/@betteridea/codecell)
7-
[![X Follow](https://img.shields.io/twitter/follow/betteridea-dev)](https://twitter.com/betteridea-dev)
7+
[![X Follow](https://img.shields.io/twitter/follow/betteridea_dev)](https://twitter.com/betteridea_dev)
88

99

1010
## Installation
@@ -15,34 +15,39 @@ npm install @betteridea/codecell
1515

1616
## API
1717

18-
### `CodeCell`
18+
### `<CodeCell .../>`
1919

2020
A react component to render a code cell in your app.
2121

2222
#### Props
2323

24-
- `cellId` - Unique id for the cell
25-
- `appName` - Unique app name
26-
- `code` - Initial code for the cell
27-
- `width` - Width of the cell
28-
- `height` - Height of the cell
29-
- `className` - Class names for styling
30-
- `style` - Inline styles
31-
- `devMode` - Boolean to enable dev mode
24+
| Prop | Type | Description |
25+
| ------------- | ---------------------- | ------------------------------------------------------------------ |
26+
| `cellId` | `string` | Unique id for the cell |
27+
| `appName` | `string` | Unique app name |
28+
| `code` | `string` | Initial code for the cell |
29+
| `onAOProcess` | `(pid:string) => void` | Callback function that gets called whenever a process is is loaded |
30+
| `width` | `string` | Width of the cell |
31+
| `height` | `string` | Height of the cell |
32+
| `className` | `string` | Class names for styling |
33+
| `style` | `React.CSSProperties` | Inline styles |
34+
| `devMode` | `boolean` | Boolean to enable dev mode |
3235

33-
### `setCellCode`
36+
### `setCellCode(...)`
3437

3538
To update the code in a cell, after it has been rendered.
36-
It is discouraged to update code by changing the `code` prop directly, since it re-renders the iframe, again this is personal preference.
39+
It is discouraged to update code by changing the `code` prop directly, since it re-renders the webview, again this is personal preference.
3740

3841
#### Arguments
3942

40-
- `cellId` - Unique id of the cell
41-
- `code` - Code to set in the cell
42-
- `devMode` - Boolean to enable dev mode
43+
| Argument | Type | Description |
44+
| --------- | --------- | -------------------------- |
45+
| `cellId` | `string` | Unique id of the cell |
46+
| `code` | `string` | Code to set in the cell |
47+
| `devMode` | `boolean` | Boolean to enable dev mode |
4348

4449

45-
### ~~`runCell`~~ (deprecated due to security reasons)
50+
### ~~`runCell(...)`~~ (deprecated due to security reasons)
4651

4752
~~To run the code in a cell, after it has been rendered (optional, since the cell already has a run button)~~
4853

@@ -54,38 +59,47 @@ It is discouraged to update code by changing the `code` prop directly, since it
5459
## Usage
5560

5661
```javascript
57-
import { CodeCell, runCell } from '@betteridea/codecell';
62+
import { CodeCell } from '@betteridea/codecell';
5863

5964
// in your react app
6065
<CodeCell
6166
cellId="1" // any unique cell id
6267
appName="BetterIDEa-Code-Cell" // Your unique app name
6368
code="print('Portable code cell ftw!')" // initial code (optional)
69+
onAOProcess={(pid) => console.log("using process: ",pid)} // print the process id whenever it loads
6470
/>
6571
```
6672

67-
~~To run code from external sources, you can use the `runCell` function.~~ (deprecated due to security reasons)
73+
To update the cell with a different code snippet, you can use the `setCellCode` function.
6874

6975
```javascript
70-
import { runCell } from '@betteridea/codecell';
76+
import { setCellCode } from '@betteridea/codecell';
7177

7278
...
7379

74-
// This will run whatever code is typed in the cell with the id provided
75-
runCell("1");
80+
// This will update the code in the cell with the id provided
81+
setCellCode("1", "print('Updated code!')");
7682
```
7783

78-
To update the cell with a different code snippet, you can use the `setCellCode` function.
84+
<details>
85+
<summary>Deprecation Warning</summary>
86+
87+
**runCell() function has been deprecated due to security reasons, since it might be possible anyone can run some mischevious code in your process without you knowing.**
88+
89+
To run code from external sources, you can use the `runCell` function.
7990

8091
```javascript
81-
import { setCellCode } from '@betteridea/codecell';
92+
import { runCell } from '@betteridea/codecell';
8293

8394
...
8495

85-
// This will update the code in the cell with the id provided
86-
setCellCode("1", "print('Updated code!')");
96+
// This will run whatever code is typed in the cell with the id provided
97+
runCell("1");
8798
```
8899

100+
</details>
101+
102+
89103
## Developing
90104

91105
To start the vite development server, run:
@@ -98,10 +112,10 @@ npm run dev
98112

99113
then make changes to the component and run function and test them in the vite app at [http://localhost:5173](http://localhost:5173)
100114

101-
1. `CodeCell` component -> [./src/components/CodeCell.tsx](https://github.com/betteridea-dev/ide/blob/main/packages/codecell/src/components/codecell.tsx)
115+
1. `CodeCell` component -> [./src/components/CodeCell.tsx](https://github.com/betteridea-dev/codecell/blob/main/src/components/codecell.tsx)
102116

103-
2. `runCell` function -> [./src/lib/runCell.ts](https://github.com/betteridea-dev/ide/blob/main/packages/codecell/src/lib/runCell.ts)
117+
2. Library functions -> [./src/lib](https://github.com/betteridea-dev/codecell/tree/main/src/lib)
104118

105-
Both are essentially a wrapper around https://ide.betteridea.dev/codecell page from the main [IDE](https://ide.betteridea.dev) to run the code in any webapp through an iframe.
119+
3. `/codecell` webview -> [next_app/src/pages/codecell.tsx](https://github.com/betteridea-dev/ide/blob/main/next_app/src/pages/codecell.tsx)
106120

107-
`/codecell` -> [next_app/src/pages/codecell.tsx](https://github.com/betteridea-dev/ide/blob/main/next_app/src/pages/codecell.tsx)
121+
The package is essentially a wrapper around https://ide.betteridea.dev/codecell route from the main [IDE](https://ide.betteridea.dev) to run code in any webapp through a webview.

src/components/codecell.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect } from "react";
12

23

34
export default function CodeCell({ cellId,
@@ -7,7 +8,8 @@ export default function CodeCell({ cellId,
78
width = "100%",
89
height = "300px",
910
className = "",
10-
style = {}
11+
style = {},
12+
onAOProcess = (pid: string) => { }
1113
}: {
1214
cellId: string;
1315
appName: string;
@@ -17,12 +19,25 @@ export default function CodeCell({ cellId,
1719
height?: string;
1820
className?: string;
1921
style?: React.CSSProperties;
22+
onAOProcess?: (pid: string) => void;
2023
}) {
2124
const url = new URL(devMode ? "http://localhost:3000/codecell" : "https://ide.betteridea.dev/codecell");
2225

2326
url.searchParams.append("app-name", appName);
2427
url.searchParams.append("code", code);
2528

29+
useEffect(() => {
30+
const callback = async (e: any) => {
31+
if (e.data.action == "set_process") {
32+
onAOProcess(e.data.process);
33+
}
34+
};
35+
36+
window.removeEventListener("message", callback);
37+
window.addEventListener("message", callback);
38+
return () => window.removeEventListener("message", callback);
39+
}, [])
40+
2641

2742
return <iframe
2843
id={cellId}

src/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ReactDOM.createRoot(document.getElementById('root')!).render(
1818
runCell("1", true)
1919
}}>run</button>
2020
<input type="text" id="code" onChange={(e) => { onInput(e.target.value) }} />
21-
<CodeCell appName="test-cell" cellId="1" devMode />
21+
<CodeCell appName="test-cell" cellId="1" devMode onAOProcess={(p) => console.log("got pid from webview", p)} />
2222
</div>
2323
</React.StrictMode>,
2424
)

0 commit comments

Comments
 (0)