From f79146587d5b69ead03e595a5f7c642f9bbce8e4 Mon Sep 17 00:00:00 2001 From: yousefed Date: Wed, 15 May 2024 15:57:45 +0200 Subject: [PATCH] fix build --- packages/frame/src/EditorStore.ts | 138 ++++-------------- packages/frame/src/TypeCellBlock.ts | 113 ++++++++++++++ .../executor/lib/autoForm/FormField.tsx | 4 +- .../runtime/executor/lib/autoForm/index.tsx | 2 +- .../src/runtime/executor/lib/exports.tsx | 8 +- packages/server/package.json | 1 + 6 files changed, 149 insertions(+), 117 deletions(-) create mode 100644 packages/frame/src/TypeCellBlock.ts diff --git a/packages/frame/src/EditorStore.ts b/packages/frame/src/EditorStore.ts index a9279655..4b83e33c 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 "./TypeCellBlock"; import LocalExecutionHost from "./runtime/executor/executionHosts/local/LocalExecutionHost"; export class EditorStore { @@ -26,8 +24,11 @@ export class EditorStore { constructor() { makeObservable(this, { customBlocks: observable.shallow, - add: action, - delete: action, + addCustomBlock: action, + deleteCustomBlock: action, + blockSettings: observable.shallow, + addBlockSettings: action, + deleteBlockSettings: action, topLevelBlocks: observable.ref, }); @@ -46,11 +47,12 @@ export class EditorStore { } customBlocks = observable.map(); + blockSettings = observable.map(); /** * Add a custom block (slash menu command) to the editor */ - public add(config: any) { + public addCustomBlock(config: any) { if (this.customBlocks.has(config.id)) { // already has block with this id, maybe loop of documents? return; @@ -61,10 +63,28 @@ export class EditorStore { /** * Remove a custom block (slash menu command) from the editor */ - public delete(config: any) { + public deleteCustomBlock(config: any) { this.customBlocks.delete(config.id); } + /** + * Add a block settings (block settings menu) to the editor + */ + public addBlockSettings(config: any) { + if (this.blockSettings.has(config.id)) { + // already has block with this id, maybe loop of documents? + return; + } + this.blockSettings.set(config.id, config); + } + + /** + * Remove block settings (block settings menu) from the editor + */ + public deleteBlockSettings(config: any) { + this.blockSettings.delete(config.id); + } + /** * EXPERIMENTAL * @internal @@ -124,108 +144,6 @@ export class EditorStore { * @internal * */ public get firstBlock() { - 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.storage !== JSON.stringify(this.storage)) { - if (newBlock.props.storage) { - try { - console.log("update cell storage"); - this.storage = JSON.parse(newBlock.props.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, - }, - }); - } - }, - { fireImmediately: false }, - ); + return this.getBlock(this.editor!.document[0].id); } } diff --git a/packages/frame/src/TypeCellBlock.ts b/packages/frame/src/TypeCellBlock.ts new file mode 100644 index 00000000..87f7f3be --- /dev/null +++ b/packages/frame/src/TypeCellBlock.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 + block: Block; + storage: Record = {}; + + get id() { + return this.block.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, { + block: 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.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/runtime/executor/lib/autoForm/FormField.tsx b/packages/frame/src/runtime/executor/lib/autoForm/FormField.tsx index b9b41095..a58e9386 100644 --- a/packages/frame/src/runtime/executor/lib/autoForm/FormField.tsx +++ b/packages/frame/src/runtime/executor/lib/autoForm/FormField.tsx @@ -20,7 +20,7 @@ export const FormField = observer( [key: string]: unknown; }; fieldKey: Key; - modelPath: string; + // modelPath: string; value: string | undefined; setValue: (value: string | undefined) => void; }) => { @@ -115,7 +115,7 @@ export const FormField = observer( {showCode ? ( { if (!newValue || newValue.trim() === "export default") { props.setValue(undefined); diff --git a/packages/frame/src/runtime/executor/lib/autoForm/index.tsx b/packages/frame/src/runtime/executor/lib/autoForm/index.tsx index d0117361..d0a8bfb9 100644 --- a/packages/frame/src/runtime/executor/lib/autoForm/index.tsx +++ b/packages/frame/src/runtime/executor/lib/autoForm/index.tsx @@ -54,7 +54,7 @@ export const AutoForm = observer( key={input} inputObject={props.inputObject as any} fieldKey={input} - modelPath={props.modelPath} + // modelPath={props.modelPath} value={props.settings[input]} setValue={(value: string | undefined) => { props.setSetting(input, value); diff --git a/packages/frame/src/runtime/executor/lib/exports.tsx b/packages/frame/src/runtime/executor/lib/exports.tsx index 7ad20c90..995feac7 100644 --- a/packages/frame/src/runtime/executor/lib/exports.tsx +++ b/packages/frame/src/runtime/executor/lib/exports.tsx @@ -34,12 +34,12 @@ export default function getExposeGlobalVariables( id, documentId, }; - console.log("ADD BLOCK", completeConfig.id); - editorStore.add(completeConfig); + // console.log("ADD BLOCK", completeConfig.id); + editorStore.addBlockSettings(completeConfig); runContext.onDispose(() => { - console.log("REMOVE BLOCK", completeConfig.id); - editorStore.delete(completeConfig); + // console.log("REMOVE BLOCK", completeConfig.id); + editorStore.deleteCustomBlock(completeConfig); }); }, /** diff --git a/packages/server/package.json b/packages/server/package.json index 558553c6..9c880e9f 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -34,6 +34,7 @@ "start": "node dist/src/index.js", "start:supabase": "./supabase.sh start", "stop:supabase": "./supabase.sh stop", + "dump": "./supabase.sh db dump -f export.sql --db-url=postgresql://postgres:postgres@localhost:54322 --data-only", "clean": "rimraf dist && rimraf types", "dev": "MODE=development vite-node src/index.ts", "build": "npm run clean && tsc -p tsconfig.json",