Skip to content

Commit ef3d41c

Browse files
committed
feat: ✨ add props to register modules
1 parent a19df11 commit ef3d41c

File tree

2 files changed

+70
-26
lines changed

2 files changed

+70
-26
lines changed

packages/vue-quill/src/components/QuillEditor.ts

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
} from 'vue'
2121
import { toolbarOptions, ToolbarOptions } from './options'
2222

23+
type Module = [string, any, object?]
24+
2325
export const QuillEditor = defineComponent({
2426
name: 'QuillEditor',
2527
inheritAttrs: false,
@@ -66,6 +68,10 @@ export const QuillEditor = defineComponent({
6668
return true
6769
},
6870
},
71+
modules: {
72+
type: Array as PropType<Module | Module[]>,
73+
required: false,
74+
},
6975
options: {
7076
type: Object as PropType<QuillOptionsStatic>,
7177
required: false,
@@ -99,28 +105,37 @@ export const QuillEditor = defineComponent({
99105

100106
// Initialize Quill
101107
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+
}
123119
}
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)
124139
}
125140

126141
// Compose Options
@@ -144,7 +159,26 @@ export const QuillEditor = defineComponent({
144159
})(),
145160
}
146161
}
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+
)
148182
}
149183

150184
const handleTextChange: TextChangeHandler = (
@@ -175,7 +209,12 @@ export const QuillEditor = defineComponent({
175209

176210
const handleEditorChange: EditorChangeHandler = (
177211
...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+
]
179218
| [
180219
name: 'selection-change',
181220
range: RangeStatic,
@@ -252,7 +291,8 @@ export const QuillEditor = defineComponent({
252291

253292
const reinit = () => {
254293
nextTick(() => {
255-
if (!ctx.slots.toolbar && quill) quill.getModule('toolbar')?.container.remove()
294+
if (!ctx.slots.toolbar && quill)
295+
quill.getModule('toolbar')?.container.remove()
256296
initialize()
257297
console.log('reinit call')
258298
})
@@ -288,6 +328,9 @@ export const QuillEditor = defineComponent({
288328
}
289329
},
290330
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+
]
292335
},
293336
})

packages/vue-quill/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
* Author: luthfimasruri@gmail.com
44
* Github: https://github.com/vueup/vue-quill.git
55
*/
6+
type Module = [string, any, object?]
67

78
import Quill from 'quill'
89
import Delta from 'quill-delta'
910
import { QuillEditor } from './components/QuillEditor'
10-
export { QuillEditor, Quill, Delta }
11+
export { QuillEditor, Quill, Delta, Module }

0 commit comments

Comments
 (0)