Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(editor): random crashes due to lacking null safety in syntax highlight
Elian Dorand03ee2640813
diff --git a/src/public/app/types.d.ts b/src/public/app/types.d.tsindex e8ea4ad75b..1ce3645270 100644--- a/src/public/app/types.d.ts+++ b/src/public/app/types.d.ts@@ -357,8 +357,8 @@ declare global {getChanges(): {type: string;name: string;- position: {- nodeAfter: CKNode;+ position?: {+ nodeAfter?: CKNode;parent: CKNode;toJSON(): Object;}diff --git a/src/public/app/widgets/type_widgets/ckeditor/syntax_highlight.ts b/src/public/app/widgets/type_widgets/ckeditor/syntax_highlight.tsindex 42a36b2b2a..efd35969ee 100644--- a/src/public/app/widgets/type_widgets/ckeditor/syntax_highlight.ts+++ b/src/public/app/widgets/type_widgets/ckeditor/syntax_highlight.ts@@ -118,7 +118,7 @@ function initTextEditor(textEditor: TextEditor) {for (const change of changes) {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* 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);} else if (change.type == "insert" && change.name == "codeBlock") {// 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// it already has children, like when pasting one or more// full codeblocks, undoing a delete, changing the language,// etc (the postfixer won't get later changes for those).- log("dirtying inserted codeBlock " + JSON.stringify(codeBlock.toJSON()));- dirtyCodeBlocks.add(codeBlock);- } else if (change.type == "remove" && change.name == "codeBlock") {+ if (codeBlock) {+ log("dirtying inserted codeBlock " + JSON.stringify(codeBlock.toJSON()));+ dirtyCodeBlocks.add(codeBlock);+ }+ } else if (change.type == "remove" && change.name == "codeBlock" && change.position) {// An existing codeblock was removed, do nothing. Note the// node is no longer in the editor so the codeblock cannot// be inspected here. No need to dirty the codeblock since// it has been removedlog("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// highlightconst codeBlock = change.position.parent;