@@ -104,7 +104,6 @@ export interface EditorInt
104104
105105type SetEditorModeOptions = Pick < ChangeEditorModeOptions , 'emit' > ;
106106
107- /** @internal */
108107type ChangeEditorModeOptions = {
109108 mode : EditorMode ;
110109 reason : 'error-boundary' | 'settings' | 'manually' ;
@@ -157,6 +156,9 @@ export type EditorOptions = Pick<
157156 * You can use it to pre-process the value from the markup editor before it gets into the wysiwyg editor.
158157 */
159158 prepareRawMarkup ?: ( value : MarkupString ) => MarkupString ;
159+ experimental_beforeEditorModeChange ?: (
160+ options : Pick < ChangeEditorModeOptions , 'mode' | 'reason' > ,
161+ ) => boolean | undefined ;
160162 splitMode ?: SplitMode ;
161163 renderPreview ?: RenderPreview ;
162164 preset : EditorPreset ;
@@ -188,6 +190,9 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
188190 #fileUploadHandler?: FileUploadHandler ;
189191 #needToSetDimensionsForUploadedImages: boolean ;
190192 #prepareRawMarkup?: ( value : MarkupString ) => MarkupString ;
193+ #beforeEditorModeChange?: (
194+ options : Pick < ChangeEditorModeOptions , 'mode' | 'reason' > ,
195+ ) => boolean | undefined ;
191196
192197 get _wysiwygView ( ) : PMEditorView {
193198 // @ts -expect-error internal typing
@@ -349,6 +354,7 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
349354 ) ;
350355 this . #prepareRawMarkup = opts . prepareRawMarkup ;
351356 this . #escapeConfig = opts . escapeConfig ;
357+ this . #beforeEditorModeChange = opts . experimental_beforeEditorModeChange ;
352358 }
353359
354360 // ---> implements CodeEditor
@@ -385,8 +391,14 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
385391
386392 changeEditorMode ( { emit = true , ...opts } : ChangeEditorModeOptions ) : void {
387393 if ( this . #editorMode === opts . mode ) return ;
394+
395+ if ( this . #beforeEditorModeChange?.( { mode : opts . mode , reason : opts . reason } ) === false ) {
396+ return ;
397+ }
398+
388399 this . currentMode = opts . mode ;
389400 this . emit ( 'rerender' , null ) ;
401+
390402 if ( emit ) {
391403 this . emit ( 'change-editor-mode' , opts ) ;
392404 }
0 commit comments