@@ -20,6 +20,8 @@ import {
20
20
} from 'vue'
21
21
import { toolbarOptions , ToolbarOptions } from './options'
22
22
23
+ type Module = [ string , any , object ?]
24
+
23
25
export const QuillEditor = defineComponent ( {
24
26
name : 'QuillEditor' ,
25
27
inheritAttrs : false ,
@@ -66,6 +68,10 @@ export const QuillEditor = defineComponent({
66
68
return true
67
69
} ,
68
70
} ,
71
+ modules : {
72
+ type : Array as PropType < Module | Module [ ] > ,
73
+ required : false ,
74
+ } ,
69
75
options : {
70
76
type : Object as PropType < QuillOptionsStatic > ,
71
77
required : false ,
@@ -99,28 +105,37 @@ export const QuillEditor = defineComponent({
99
105
100
106
// Initialize Quill
101
107
const initialize = ( ) => {
102
- if ( editor . value ) {
103
- options = composeOptions ( )
104
- // Create new instance
105
- quill = new Quill ( editor . value , options )
106
- // Set editor content
107
- setContents ( props . content )
108
- // Set event handlers
109
- quill . on ( 'text-change' , handleTextChange )
110
- quill . on ( 'selection-change' , handleSelectionChange )
111
- quill . on ( 'editor-change' , handleEditorChange )
112
- // Remove editor class when theme changes
113
- if ( props . theme !== 'bubble' ) editor . value . classList . remove ( 'ql-bubble' )
114
- if ( props . theme !== 'snow' ) editor . value . classList . remove ( 'ql-snow' )
115
- // Fix clicking the quill toolbar is detected as blur event
116
- quill
117
- . getModule ( 'toolbar' )
118
- ?. container . addEventListener ( 'mousedown' , ( e : MouseEvent ) => {
119
- e . preventDefault ( )
120
- } )
121
- // Emit ready event
122
- ctx . emit ( 'ready' , quill )
108
+ if ( ! editor . value ) return
109
+ options = composeOptions ( )
110
+ // Register modules
111
+ if ( props . modules ) {
112
+ if ( Array . isArray ( props . modules [ 0 ] ) ) {
113
+ for ( const module of props . modules ) {
114
+ Quill . register ( `modules/${ module [ 0 ] } ` , module [ 1 ] )
115
+ }
116
+ } else if ( typeof props . modules [ 0 ] === 'string' ) {
117
+ Quill . register ( `modules/${ props . modules [ 0 ] } ` , props . modules [ 1 ] )
118
+ }
123
119
}
120
+ // Create new Quill instance
121
+ quill = new Quill ( editor . value , options )
122
+ // Set editor content
123
+ setContents ( props . content )
124
+ // Set event handlers
125
+ quill . on ( 'text-change' , handleTextChange )
126
+ quill . on ( 'selection-change' , handleSelectionChange )
127
+ quill . on ( 'editor-change' , handleEditorChange )
128
+ // Remove editor class when theme changes
129
+ if ( props . theme !== 'bubble' ) editor . value . classList . remove ( 'ql-bubble' )
130
+ if ( props . theme !== 'snow' ) editor . value . classList . remove ( 'ql-snow' )
131
+ // Fix clicking the quill toolbar is detected as blur event
132
+ quill
133
+ . getModule ( 'toolbar' )
134
+ ?. container . addEventListener ( 'mousedown' , ( e : MouseEvent ) => {
135
+ e . preventDefault ( )
136
+ } )
137
+ // Emit ready event
138
+ ctx . emit ( 'ready' , quill )
124
139
}
125
140
126
141
// Compose Options
@@ -144,7 +159,26 @@ export const QuillEditor = defineComponent({
144
159
} ) ( ) ,
145
160
}
146
161
}
147
- return Object . assign ( { } , props . globalOptions , props . options , clientOptions )
162
+ if ( props . modules ) {
163
+ const modules = ( ( ) => {
164
+ const modulesOption : { [ key : string ] : any } = { }
165
+ if ( Array . isArray ( props . modules [ 0 ] ) ) {
166
+ for ( const module of props . modules ) {
167
+ modulesOption [ module [ 0 ] ] = module [ 2 ] ?? { }
168
+ }
169
+ } else if ( typeof props . modules [ 0 ] === 'string' ) {
170
+ modulesOption [ props . modules [ 0 ] ] = props . modules [ 2 ] ?? { }
171
+ }
172
+ return modulesOption
173
+ } ) ( )
174
+ Object . assign ( clientOptions . modules , modules )
175
+ }
176
+ return Object . assign (
177
+ { } ,
178
+ props . globalOptions ,
179
+ props . options ,
180
+ clientOptions
181
+ )
148
182
}
149
183
150
184
const handleTextChange : TextChangeHandler = (
@@ -175,7 +209,12 @@ export const QuillEditor = defineComponent({
175
209
176
210
const handleEditorChange : EditorChangeHandler = (
177
211
...args :
178
- | [ name : 'text-change' , delta : Delta , oldContents : Delta , source : Sources ]
212
+ | [
213
+ name : 'text-change' ,
214
+ delta : Delta ,
215
+ oldContents : Delta ,
216
+ source : Sources
217
+ ]
179
218
| [
180
219
name : 'selection-change' ,
181
220
range : RangeStatic ,
@@ -252,7 +291,8 @@ export const QuillEditor = defineComponent({
252
291
253
292
const reinit = ( ) => {
254
293
nextTick ( ( ) => {
255
- if ( ! ctx . slots . toolbar && quill ) quill . getModule ( 'toolbar' ) ?. container . remove ( )
294
+ if ( ! ctx . slots . toolbar && quill )
295
+ quill . getModule ( 'toolbar' ) ?. container . remove ( )
256
296
initialize ( )
257
297
console . log ( 'reinit call' )
258
298
} )
@@ -288,6 +328,9 @@ export const QuillEditor = defineComponent({
288
328
}
289
329
} ,
290
330
render ( ) {
291
- return [ this . $slots . toolbar ?.( ) , h ( 'div' , { ref : 'editor' , ...this . $attrs } ) ]
331
+ return [
332
+ this . $slots . toolbar ?.( ) ,
333
+ h ( 'div' , { ref : 'editor' , ...this . $attrs } ) ,
334
+ ]
292
335
} ,
293
336
} )
0 commit comments