1
1
import { markdown , markdownLanguage } from "@codemirror/lang-markdown"
2
2
import { EditorState , EditorStateConfig } from '@codemirror/state'
3
- import { keymap , ViewUpdate , EditorView } from '@codemirror/view'
3
+ import { keymap , ViewUpdate , EditorView , KeyBinding } from '@codemirror/view'
4
4
import { basicSetup } from 'codemirror'
5
5
import { indentWithTab } from "@codemirror/commands"
6
6
import { italicKeyBinding } from './commands/Italic'
@@ -10,6 +10,8 @@ import { linkKeyBinding } from "./commands/Link"
10
10
import { quoteKeyBinding } from "./commands/Quote"
11
11
import { ulKeyBinding } from "./commands/BulletedList"
12
12
import { Toolbar } from "./components/Toolbar"
13
+ import IEditorPlugin from "./plugins/IEditorPlugin"
14
+ import { createImageUploadPlugin } from "./plugins/ImageUpload"
13
15
14
16
interface ChunInterface {
15
17
dom : Element ,
@@ -20,59 +22,39 @@ interface ChunInterface {
20
22
21
23
interface ChunConfig extends EditorStateConfig {
22
24
onUpdateListener ?: ( update : ViewUpdate ) => void ,
23
- indentWithTab ?: boolean ,
24
- lineWrapping ?: boolean ,
25
+ toolbar : boolean ,
26
+ indentWithTab : boolean ,
27
+ lineWrapping : boolean ,
28
+ toolbarItems : ( ( editor : EditorView ) => HTMLElement ) [ ] ,
25
29
}
26
30
27
- function ChunMDE ( this : ChunInterface , containerId : string , customConfig ?: ChunConfig ) {
31
+ const ChunMDE = function ChunMDE ( this : ChunInterface , containerId : string , customConfig : ChunConfig = {
32
+ doc : "" ,
33
+ toolbar : true ,
34
+ indentWithTab : true ,
35
+ lineWrapping : false ,
36
+ toolbarItems : [ ]
37
+ } ) {
28
38
const parentElement = document . getElementById ( containerId ) as Element
29
39
30
- const defaultKeybinds = [
31
- italicKeyBinding ,
32
- boldKeyBinding ,
33
- codeKeyBinding ,
34
- linkKeyBinding ,
35
- quoteKeyBinding ,
36
- ulKeyBinding ,
37
- ]
38
-
39
- const defaultExtensions = [
40
- keymap . of ( defaultKeybinds ) ,
41
- markdown ( { base : markdownLanguage } ) ,
42
- basicSetup ,
43
- ]
44
-
45
- let config : EditorStateConfig = {
46
- doc : "Start writing!" ,
47
- extensions : defaultExtensions ,
48
- }
49
-
50
- if ( customConfig ) {
51
- if ( customConfig . onUpdateListener ) {
52
- defaultExtensions . push ( EditorView . updateListener . of ( customConfig . onUpdateListener ! ) )
53
- }
54
- if ( customConfig . lineWrapping ) {
55
- defaultExtensions . push ( EditorView . lineWrapping )
56
- }
57
- if ( customConfig . indentWithTab ) {
58
- defaultExtensions . push ( keymap . of ( [ indentWithTab ] ) )
59
- }
60
- config . doc = customConfig . doc ? customConfig . doc : config . doc
61
- config . extensions = customConfig . extensions ? customConfig . extensions : defaultExtensions
40
+ const config : EditorStateConfig = {
41
+ doc : customConfig . doc ,
42
+ extensions : customConfig . extensions ,
62
43
}
63
44
64
45
/** CodeMirror6's EditorView */
65
46
const editorView = new EditorView ( {
66
- // parent: parentElement,
67
47
state : EditorState . create ( config )
68
48
} )
69
49
70
50
parentElement . className += " chunmde-container"
71
51
72
52
// toolbar
73
- const toolbar = new Toolbar ( editorView )
74
-
75
- parentElement . appendChild ( toolbar . dom )
53
+ if ( customConfig . toolbar ) {
54
+ const toolbar = new Toolbar ( editorView , customConfig . toolbarItems )
55
+ parentElement . appendChild ( toolbar . dom )
56
+ this . toolbar = toolbar
57
+ }
76
58
parentElement . appendChild ( editorView . dom )
77
59
78
60
/** Shortcut to get the editor value */
@@ -81,14 +63,89 @@ function ChunMDE(this: ChunInterface, containerId: string, customConfig?: ChunCo
81
63
}
82
64
83
65
this . dom = parentElement
84
- this . toolbar = toolbar
85
66
this . editor = editorView
67
+ } as any as { new ( containerId : string , customConfig ?: ChunConfig ) : ChunInterface ; }
68
+
69
+ interface IEditorBuilder {
70
+ use : ( plugin : IEditorPlugin ) => IEditorBuilder ;
71
+ mount : ( selector : string ) => void ;
72
+ }
73
+
74
+ export function createChunEditor ( customConfig : ChunConfig = {
75
+ doc : "" ,
76
+ toolbar : true ,
77
+ indentWithTab : true ,
78
+ lineWrapping : false ,
79
+ toolbarItems : [ ] ,
80
+ } ) : IEditorBuilder {
81
+
82
+ const defaultKeybinds = [
83
+ italicKeyBinding ,
84
+ boldKeyBinding ,
85
+ codeKeyBinding ,
86
+ linkKeyBinding ,
87
+ quoteKeyBinding ,
88
+ ulKeyBinding ,
89
+ ]
90
+
91
+ const defaultExtensions = [
92
+ keymap . of ( defaultKeybinds ) ,
93
+ markdown ( { base : markdownLanguage } ) ,
94
+ basicSetup ,
95
+ ]
96
+
97
+ const keybinds : KeyBinding [ ] = [ ]
98
+ const toolbarItemDelegates : ( ( editor : EditorView ) => HTMLButtonElement ) [ ] = [ ]
99
+
100
+ return {
101
+ use ( plugin ) {
102
+ if ( plugin . keybind ) {
103
+ keybinds . push ( plugin . keybind )
104
+ }
105
+
106
+ if ( plugin . createToolbarItem ) {
107
+ toolbarItemDelegates . push ( plugin . createToolbarItem )
108
+ }
109
+
110
+ return this
111
+ } ,
112
+ mount ( selector ) {
113
+
114
+ if ( customConfig . onUpdateListener ) {
115
+ defaultExtensions . push ( EditorView . updateListener . of ( customConfig . onUpdateListener ! ) )
116
+ }
117
+ if ( customConfig . lineWrapping ) {
118
+ defaultExtensions . push ( EditorView . lineWrapping )
119
+ }
120
+ if ( customConfig . indentWithTab ) {
121
+ keybinds . push ( indentWithTab )
122
+ }
123
+
124
+ defaultExtensions . push ( keymap . of ( keybinds ) )
125
+ customConfig . extensions = defaultExtensions
126
+ customConfig . toolbarItems = toolbarItemDelegates
127
+
128
+ return new ChunMDE ( selector , customConfig )
129
+ } ,
130
+ }
131
+ }
132
+
133
+ interface IGlobalChunEditor {
134
+ createChunEditor : ( ) => IEditorBuilder ,
135
+ createImageUploadPlugin : ( imageUploadUrl : string , imageFormats : string [ ] ) => IEditorPlugin ,
86
136
}
87
137
88
138
declare global {
89
- interface Window { ChunMDE : typeof ChunMDE ; }
139
+ interface Window { ChunMDE : typeof ChunMDE ; Chun : IGlobalChunEditor }
90
140
}
91
141
92
- window . ChunMDE = ChunMDE ;
142
+ const Chun = {
143
+ createChunEditor,
144
+ createImageUploadPlugin,
145
+ }
146
+
147
+ window . ChunMDE = ChunMDE
148
+ window . Chun = Chun ;
149
+
150
+ export default Chun ;
93
151
94
- export default ChunMDE ;
0 commit comments