Skip to content

Commit

Permalink
start with block api cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
YousefED committed Oct 22, 2023
1 parent e27227e commit 1a81755
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 105 deletions.
106 changes: 1 addition & 105 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 "./TypeCellCodeBlock";
import LocalExecutionHost from "./runtime/executor/executionHosts/local/LocalExecutionHost";

export class EditorStore {
Expand Down Expand Up @@ -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<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 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 },
);
}
}
113 changes: 113 additions & 0 deletions packages/frame/src/TypeCellCodeBlock.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
_blockNoteBlock: Block;
storage: Record<string, any> = {};

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 },
);
}
}

0 comments on commit 1a81755

Please sign in to comment.