fix(editor): random crashes due to lacking null safety in syntax highlight

This commit is contained in:
Elian Doran 2025-04-08 09:06:27 +03:00
parent 8b7f16d49b
commit d03ee26408
No known key found for this signature in database
2 changed files with 10 additions and 8 deletions

View File

@ -357,8 +357,8 @@ declare global {
getChanges(): { getChanges(): {
type: string; type: string;
name: string; name: string;
position: { position?: {
nodeAfter: CKNode; nodeAfter?: CKNode;
parent: CKNode; parent: CKNode;
toJSON(): Object; toJSON(): Object;
} }

View File

@ -118,7 +118,7 @@ function initTextEditor(textEditor: TextEditor) {
for (const change of changes) { for (const change of changes) {
dbg("change " + JSON.stringify(change)); dbg("change " + JSON.stringify(change));
if (change.name !== "paragraph" && change.name !== "codeBlock" && change.position.nodeAfter?.childCount > 0) { if (change.name !== "paragraph" && change.name !== "codeBlock" && change?.position?.nodeAfter && change.position.nodeAfter.childCount > 0) {
/* /*
* We need to look for code blocks recursively, as they can be placed within a <div> due to * We need to look for code blocks recursively, as they can be placed within a <div> due to
* general HTML support or normally underneath other elements such as tables, blockquotes, etc. * general HTML support or normally underneath other elements such as tables, blockquotes, etc.
@ -126,20 +126,22 @@ function initTextEditor(textEditor: TextEditor) {
lookForCodeBlocks(change.position.nodeAfter); lookForCodeBlocks(change.position.nodeAfter);
} else if (change.type == "insert" && change.name == "codeBlock") { } else if (change.type == "insert" && change.name == "codeBlock") {
// A new code block was inserted // A new code block was inserted
const codeBlock = change.position.nodeAfter; const codeBlock = change.position?.nodeAfter;
// Even if it's a new codeblock, it needs dirtying in case // Even if it's a new codeblock, it needs dirtying in case
// it already has children, like when pasting one or more // it already has children, like when pasting one or more
// full codeblocks, undoing a delete, changing the language, // full codeblocks, undoing a delete, changing the language,
// etc (the postfixer won't get later changes for those). // etc (the postfixer won't get later changes for those).
if (codeBlock) {
log("dirtying inserted codeBlock " + JSON.stringify(codeBlock.toJSON())); log("dirtying inserted codeBlock " + JSON.stringify(codeBlock.toJSON()));
dirtyCodeBlocks.add(codeBlock); dirtyCodeBlocks.add(codeBlock);
} else if (change.type == "remove" && change.name == "codeBlock") { }
} else if (change.type == "remove" && change.name == "codeBlock" && change.position) {
// An existing codeblock was removed, do nothing. Note the // An existing codeblock was removed, do nothing. Note the
// node is no longer in the editor so the codeblock cannot // node is no longer in the editor so the codeblock cannot
// be inspected here. No need to dirty the codeblock since // be inspected here. No need to dirty the codeblock since
// it has been removed // it has been removed
log("removing codeBlock at path " + JSON.stringify(change.position.toJSON())); log("removing codeBlock at path " + JSON.stringify(change.position.toJSON()));
} else if ((change.type == "remove" || change.type == "insert") && change.position.parent.is("element", "codeBlock")) { } else if ((change.type == "remove" || change.type == "insert") && change?.position?.parent.is("element", "codeBlock")) {
// Text was added or removed from the codeblock, force a // Text was added or removed from the codeblock, force a
// highlight // highlight
const codeBlock = change.position.parent; const codeBlock = change.position.parent;