diff --git a/packages/frame/src/EditorStore.ts b/packages/frame/src/EditorStore.ts index 8487ab71..63442f43 100644 --- a/packages/frame/src/EditorStore.ts +++ b/packages/frame/src/EditorStore.ts @@ -3,14 +3,12 @@ import { Block, BlockNoteEditor } from "@blocknote/core"; import { ObservableMap, action, - computed, makeObservable, observable, onBecomeObserved, - reaction, runInAction, } from "mobx"; -import { ExecutionHost } from "./runtime/executor/executionHosts/ExecutionHost"; +import { TypeCellBlock } from "./TypeCellCodeBlock"; import LocalExecutionHost from "./runtime/executor/executionHosts/local/LocalExecutionHost"; export class EditorStore { @@ -149,105 +147,3 @@ export class EditorStore { return this.getBlock(this.editor!.topLevelBlocks[0].id); } } - -/** - * EXPERIMENTAL - */ -class TypeCellBlock { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore it's set using updatePropertiesFromEditorBlock - block: Block; - storage: Record = {}; - - get context() { - // TODO: hacky - const keys = [...this.executionHost.outputs.keys()].filter((key) => - key.includes(this.block.id), - ); - - if (!keys.length) { - return undefined; - } - // return undefined; - - // debugger; - const val = this.executionHost.outputs.get(keys[0])?.value; - if (val instanceof Error) { - return undefined; - } - return val; - // return Object.fromEntries( - // Object.getOwnPropertyNames(val).map((key) => [key, val[key]]), - // ); - } - - constructor( - id: string, - editor: BlockNoteEditor, - private readonly executionHost: ExecutionHost, - onRemoved: () => void, - ) { - makeObservable(this, { - block: observable.ref, - context: computed, - storage: true, - }); - - const editorBlock = editor.getBlock(id); - if (!editorBlock) { - throw new Error("Editor block not found"); - } - - const updatePropertiesFromEditorBlock = (newBlock: Block) => { - runInAction(() => { - this.block = newBlock; - - if ((newBlock.props as any).storage !== JSON.stringify(this.storage)) { - if (newBlock.props as any) { - try { - console.log("update cell storage"); - this.storage = JSON.parse((newBlock.props as any).storage) || {}; - } catch (e) { - console.error(e); - } - } else { - this.storage = {}; - } - } - }); - }; - updatePropertiesFromEditorBlock(editorBlock); - - const updateBlock = () => { - const newBlock = editor.getBlock(id); - if (!newBlock) { - onRemoved(); - return; - } - if (newBlock !== this.block) { - updatePropertiesFromEditorBlock(newBlock); - } - }; - - onBecomeObserved(this, "block", () => { - editor.onEditorContentChange(() => { - updateBlock(); - }); - updateBlock(); - }); - - reaction( - () => (this.storage ? JSON.stringify(this.storage) : undefined), - (val) => { - if (val) { - editor.updateBlock(this.block, { - props: { - storage: val, - } as any, - }); - } - }, - { fireImmediately: false }, - ); - } -} diff --git a/packages/frame/src/TypeCellCodeBlock.ts b/packages/frame/src/TypeCellCodeBlock.ts new file mode 100644 index 00000000..e347c253 --- /dev/null +++ b/packages/frame/src/TypeCellCodeBlock.ts @@ -0,0 +1,113 @@ +import { Block, BlockNoteEditor } from "@blocknote/core"; + +import { + computed, + makeObservable, + observable, + onBecomeObserved, + reaction, + runInAction, +} from "mobx"; +import { ExecutionHost } from "./runtime/executor/executionHosts/ExecutionHost"; + +/** + * EXPERIMENTAL + */ +export class TypeCellBlock { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore it's set using updatePropertiesFromEditorBlock + _blockNoteBlock: Block; + storage: Record = {}; + + get id() { + return this._blockNoteBlock.id; + } + + get context() { + // TODO: hacky? + const keys = [...this.executionHost.outputs.keys()].filter((key) => + key.includes(this.id), + ); + + if (!keys.length) { + return undefined; + } + + const val = this.executionHost.outputs.get(keys[0])?.value; + if (val instanceof Error) { + return undefined; + } + return val; + } + + constructor( + id: string, + editor: BlockNoteEditor, + private readonly executionHost: ExecutionHost, + onRemoved: () => void, + ) { + makeObservable(this, { + _blockNoteBlock: observable.ref, + context: computed, + id: computed, + storage: true, + }); + + const editorBlock = editor.getBlock(id); + if (!editorBlock) { + throw new Error("Editor block not found"); + } + + const updatePropertiesFromEditorBlock = (newBlock: Block) => { + runInAction(() => { + this._blockNoteBlock = newBlock; + + if ((newBlock.props as any).storage !== JSON.stringify(this.storage)) { + if (newBlock.props as any) { + try { + console.log("update cell storage"); + this.storage = JSON.parse((newBlock.props as any).storage) || {}; + } catch (e) { + console.error(e); + } + } else { + this.storage = {}; + } + } + }); + }; + updatePropertiesFromEditorBlock(editorBlock); + + const updateBlock = () => { + const newBlock = editor.getBlock(id); + if (!newBlock) { + onRemoved(); + return; + } + if (newBlock !== this.block) { + updatePropertiesFromEditorBlock(newBlock); + } + }; + + onBecomeObserved(this, "block", () => { + editor.onEditorContentChange(() => { + updateBlock(); + }); + updateBlock(); + }); + + reaction( + () => (this.storage ? JSON.stringify(this.storage) : undefined), + (val) => { + if (val) { + editor.updateBlock(this.block, { + props: { + storage: val, + } as any, + }); + } + }, + { fireImmediately: false }, + ); + } +}