Skip to content

Commit e633ab4

Browse files
authored
feat: emit flag for setEditorMode (#334)
1 parent 13a9fe3 commit e633ab4

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/bundle/Editor.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export interface Editor extends Receiver<EventMap>, CommonEditor {
5555
readonly currentMode: EditorMode;
5656
readonly toolbarVisible: boolean;
5757

58-
setEditorMode(mode: EditorMode): void;
58+
setEditorMode(mode: EditorMode, opts?: SetEditorModeOptions): void;
5959

6060
moveCursor(position: 'start' | 'end' | {line: number}): void;
6161

@@ -91,7 +91,7 @@ export interface EditorInt
9191

9292
changeEditorMode(opts: ChangeEditorModeOptions): void;
9393

94-
setEditorMode(mode: EditorMode): void;
94+
setEditorMode(mode: EditorMode, opts?: SetEditorModeOptions): void;
9595

9696
moveCursor(position: 'start' | 'end' | {line: number}): void;
9797

@@ -102,10 +102,13 @@ export interface EditorInt
102102
destroy(): void;
103103
}
104104

105+
type SetEditorModeOptions = Pick<ChangeEditorModeOptions, 'emit'>;
106+
105107
/** @internal */
106108
type ChangeEditorModeOptions = {
107109
mode: EditorMode;
108110
reason: 'error-boundary' | 'settings' | 'manually';
111+
emit?: boolean;
109112
};
110113

111114
export type MarkupConfig = {
@@ -367,15 +370,15 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
367370
this.#wysiwygEditor = undefined;
368371
}
369372

370-
setEditorMode(mode: EditorMode): void {
371-
this.changeEditorMode({mode, reason: 'manually'});
373+
setEditorMode(mode: EditorMode, opts?: SetEditorModeOptions): void {
374+
this.changeEditorMode({mode, reason: 'manually', emit: opts?.emit});
372375
}
373376

374-
changeEditorMode(opts: ChangeEditorModeOptions): void {
377+
changeEditorMode({emit = true, ...opts}: ChangeEditorModeOptions): void {
375378
if (this.#editorMode === opts.mode) return;
376379
this.currentMode = opts.mode;
377380
this.emit('rerender', null);
378-
if (opts.reason !== 'error-boundary') {
381+
if (emit) {
379382
this.emit('change-editor-mode', opts);
380383
}
381384
}

src/bundle/MarkdownEditorView.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ export const MarkdownEditorView = React.forwardRef<HTMLDivElement, MarkdownEdito
181181
});
182182
setTimeout(() => {
183183
resetErrorBoundary();
184-
editor.changeEditorMode({mode: 'markup', reason: 'error-boundary'});
184+
editor.changeEditorMode({
185+
mode: 'markup',
186+
reason: 'error-boundary',
187+
emit: false,
188+
});
185189
});
186190
return null;
187191
}}

0 commit comments

Comments
 (0)