@@ -4,7 +4,6 @@ import {Actions} from './actions';
4
4
import { Settings } from './settings' ;
5
5
import { listenToCommonEvents } from './common-events' ;
6
6
import { init as initCodemirror } from './codemirror' ;
7
- import { CodeModule } from "../global" ;
8
7
import { MarkdownEditorInput } from "./inputs/interface" ;
9
8
import { CodemirrorInput } from "./inputs/codemirror" ;
10
9
import { TextareaInput } from "./inputs/textarea" ;
@@ -34,8 +33,6 @@ export interface MarkdownEditor {
34
33
* Initiate a new Markdown editor instance.
35
34
*/
36
35
export async function init ( config : MarkdownEditorConfig ) : Promise < MarkdownEditor > {
37
- // const Code = await window.importVersioned('code') as CodeModule;
38
-
39
36
const editor : MarkdownEditor = {
40
37
config,
41
38
markdown : new Markdown ( ) ,
@@ -46,15 +43,25 @@ export async function init(config: MarkdownEditorConfig): Promise<MarkdownEditor
46
43
editor . display = new Display ( editor ) ;
47
44
48
45
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
+ } ;
57
56
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
+ } ) ;
58
65
// window.devinput = editor.input;
59
66
60
67
listenToCommonEvents ( editor ) ;
0 commit comments