From f832a3849a483fb9cad21e12a0b020b2bbb202e1 Mon Sep 17 00:00:00 2001 From: HellowVirgil <1135895008@qq.com> Date: Tue, 16 Dec 2025 19:24:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(code-editor):=20=E4=BF=AE=E5=A4=8D=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E5=99=A8=E5=8F=AF=E8=83=BD=E4=B8=BA=20undefined=20?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E5=AE=89=E5=85=A8=E8=AE=BF=E9=97=AE=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../libro-code-editor/src/code-editor-view.tsx | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/libro-code-editor/src/code-editor-view.tsx b/packages/libro-code-editor/src/code-editor-view.tsx index 578a51cd..ee7c0563 100644 --- a/packages/libro-code-editor/src/code-editor-view.tsx +++ b/packages/libro-code-editor/src/code-editor-view.tsx @@ -185,7 +185,7 @@ export class CodeEditorView extends BaseView { }; override onViewUnmount = () => { - if (this.editor.hasFocus()) { + if (this.editor?.hasFocus()) { // 保存编辑器状态 const editorState = this.editor.getState(); this.codeEditorStateManager.updateEditorState(this.options.uuid, editorState); @@ -193,7 +193,7 @@ export class CodeEditorView extends BaseView { this.editorHostRef = this.getEditorHost(); this.editorHostRef?.current?.focus(); } - this.editor.dispose(); + this.editor?.dispose(); const prevState = this.editorStatus; this.editorStatus = 'disposed'; @@ -219,7 +219,7 @@ export class CodeEditorView extends BaseView { * Get the model used by the widget. */ get model(): IModel { - return this.editor.model; + return this.editor?.model || this.options.model; } /** @@ -230,11 +230,11 @@ export class CodeEditorView extends BaseView { return; } super.dispose(); - this.editor.dispose(); + this.editor?.dispose(); } protected onViewActive = (): void => { - this.editor.focus(); + this.editor?.focus(); }; /** @@ -242,7 +242,7 @@ export class CodeEditorView extends BaseView { */ protected onResize(): void { if (this.isVisible) { - this.editor.resizeToFit(); + this.editor?.resizeToFit(); } } @@ -261,6 +261,9 @@ export class CodeEditorView extends BaseView { * Handle a change in model selections. */ protected _onSelectionsChanged(): void { + if (!this.editor) { + return; + } const { start, end } = this.editor.getSelection(); if (start.column !== end.column || start.line !== end.line) { @@ -285,7 +288,7 @@ export class CodeEditorView extends BaseView { * Handle the `'lm-dragenter'` event for the widget. */ protected _evtDragEnter = (event: DragEvent): void => { - if (this.editor.getOption('readOnly') === true) { + if (this.editor?.getOption('readOnly') === true) { return; } const data = findTextData(event);