Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/libro-code-editor/src/code-editor-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ 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);
// focus 到 host 避免进入命令模式
this.editorHostRef = this.getEditorHost();
this.editorHostRef?.current?.focus();
}
this.editor.dispose();
this.editor?.dispose();

const prevState = this.editorStatus;
this.editorStatus = 'disposed';
Expand All @@ -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;
}

/**
Expand All @@ -230,19 +230,19 @@ export class CodeEditorView extends BaseView {
return;
}
super.dispose();
this.editor.dispose();
this.editor?.dispose();
}

protected onViewActive = (): void => {
this.editor.focus();
this.editor?.focus();
};

/**
* A message handler invoked on a `'resize'` message.
*/
protected onResize(): void {
if (this.isVisible) {
this.editor.resizeToFit();
this.editor?.resizeToFit();
}
}

Expand All @@ -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) {
Expand All @@ -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);
Expand Down
Loading