@@ -4,7 +4,6 @@ import {Actions} from './actions';
44import { Settings } from './settings' ;
55import { listenToCommonEvents } from './common-events' ;
66import { init as initCodemirror } from './codemirror' ;
7- import { CodeModule } from "../global" ;
87import { MarkdownEditorInput } from "./inputs/interface" ;
98import { CodemirrorInput } from "./inputs/codemirror" ;
109import { TextareaInput } from "./inputs/textarea" ;
@@ -34,8 +33,6 @@ export interface MarkdownEditor {
3433 * Initiate a new Markdown editor instance.
3534 */
3635export async function init ( config : MarkdownEditorConfig ) : Promise < MarkdownEditor > {
37- // const Code = await window.importVersioned('code') as CodeModule;
38-
3936 const editor : MarkdownEditor = {
4037 config,
4138 markdown : new Markdown ( ) ,
@@ -46,15 +43,25 @@ export async function init(config: MarkdownEditorConfig): Promise<MarkdownEditor
4643 editor . display = new Display ( editor ) ;
4744
4845 const eventHandlers = getMarkdownDomEventHandlers ( editor ) ;
49- // TODO - Switching
50- // const codeMirror = initCodemirror(editor, Code);
51- // editor.input = new CodemirrorInput(codeMirror);
52- editor . input = new TextareaInput (
53- config . inputEl ,
54- provideShortcutMap ( editor ) ,
55- eventHandlers
56- ) ;
46+ const shortcuts = provideShortcutMap ( editor ) ;
47+ const onInputChange = ( ) => editor . actions . updateAndRender ( ) ;
48+
49+ const initCodemirrorInput : ( ) => Promise < MarkdownEditorInput > = async ( ) => {
50+ const codeMirror = await initCodemirror ( config . inputEl , shortcuts , eventHandlers , onInputChange ) ;
51+ return new CodemirrorInput ( codeMirror ) ;
52+ } ;
53+ const initTextAreaInput : ( ) => Promise < MarkdownEditorInput > = async ( ) => {
54+ return new TextareaInput ( config . inputEl , shortcuts , eventHandlers , onInputChange ) ;
55+ } ;
5756
57+ const isPlainEditor = Boolean ( editor . settings . get ( 'plainEditor' ) ) ;
58+ editor . input = await ( isPlainEditor ? initTextAreaInput ( ) : initCodemirrorInput ( ) ) ;
59+ editor . settings . onChange ( 'plainEditor' , async ( value ) => {
60+ const isPlain = Boolean ( value ) ;
61+ const newInput = await ( isPlain ? initTextAreaInput ( ) : initCodemirrorInput ( ) ) ;
62+ editor . input . teardown ( ) ;
63+ editor . input = newInput ;
64+ } ) ;
5865 // window.devinput = editor.input;
5966
6067 listenToCommonEvents ( editor ) ;
0 commit comments