Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
YousefED committed May 15, 2024
1 parent a2f2e4e commit f791465
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 117 deletions.
138 changes: 28 additions & 110 deletions packages/frame/src/EditorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
});

Expand All @@ -46,11 +47,12 @@ export class EditorStore {
}

customBlocks = observable.map<string, any>();
blockSettings = observable.map<string, any>();

/**
* 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;
Expand All @@ -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
Expand Down Expand Up @@ -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<string, any> = {};

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);
}
}
113 changes: 113 additions & 0 deletions packages/frame/src/TypeCellBlock.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> = {};

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 },
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}) => {
Expand Down Expand Up @@ -115,7 +115,7 @@ export const FormField = observer(
{showCode ? (
<MonacoEdit
value={props.value || "export default"}
documentid={props.modelPath}
documentid={"TODO"}
onChange={(newValue) => {
if (!newValue || newValue.trim() === "export default") {
props.setValue(undefined);
Expand Down
2 changes: 1 addition & 1 deletion packages/frame/src/runtime/executor/lib/autoForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions packages/frame/src/runtime/executor/lib/exports.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
},
/**
Expand Down
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f791465

Please sign in to comment.