-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
149 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters