Skip to content

Commit b07093c

Browse files
committed
feat: ✨ add support contentType html, text & delta
1 parent c94672e commit b07093c

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

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

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export const QuillEditor = defineComponent({
2727
type: [String, Object] as PropType<string | Delta>,
2828
default: {},
2929
},
30+
contentType: {
31+
type: String as PropType<'delta' | 'html' | 'text'>,
32+
default: 'delta',
33+
validator: (value: string) => {
34+
return ['delta', 'html', 'text'].includes(value)
35+
},
36+
},
3037
enable: {
3138
type: Boolean,
3239
default: true,
@@ -40,7 +47,7 @@ export const QuillEditor = defineComponent({
4047
required: false,
4148
},
4249
theme: {
43-
type: String,
50+
type: String as PropType<'snow' | 'bubble' | ''>,
4451
default: 'snow',
4552
validator: (value: string) => {
4653
return ['snow', 'bubble', ''].includes(value)
@@ -96,12 +103,7 @@ export const QuillEditor = defineComponent({
96103
// Create new instance
97104
quill = new Quill(editor.value, options)
98105
// Set editor content
99-
if (typeof props.content === 'string') {
100-
quill.setText(props.content)
101-
ctx.emit('update:content', quill.getContents())
102-
} else {
103-
quill.setContents(props.content as Delta)
104-
}
106+
setContents(props.content)
105107
// Set event handlers
106108
quill.on('text-change', handleTextChange)
107109
quill.on('selection-change', handleSelectionChange)
@@ -150,7 +152,7 @@ export const QuillEditor = defineComponent({
150152
source: Sources
151153
) => {
152154
// Update v-model:content when text changes
153-
ctx.emit('update:content', quill?.getContents())
155+
ctx.emit('update:content', getContents())
154156
ctx.emit('textChange', { delta, oldContents, source })
155157
}
156158

@@ -212,6 +214,33 @@ export const QuillEditor = defineComponent({
212214
or use v-on:ready="onReady(quill)" event instead.`
213215
}
214216

217+
const getContents = () => {
218+
if (props.contentType === 'html') {
219+
return getHTML()
220+
} else if (props.contentType === 'text') {
221+
return getText()
222+
}
223+
return quill?.getContents()
224+
}
225+
226+
const setContents = (content: string | Delta) => {
227+
if (props.contentType === 'html') {
228+
setHTML(content as string)
229+
} else if (props.contentType === 'text') {
230+
setText(content as string)
231+
} else {
232+
quill?.setContents(content as Delta)
233+
}
234+
}
235+
236+
const getText = (): string => {
237+
return quill?.getText() ?? ''
238+
}
239+
240+
const setText = (text: string) => {
241+
quill?.setText(text)
242+
}
243+
215244
const getHTML = (): string => {
216245
return quill?.root.innerHTML ?? ''
217246
}
@@ -232,8 +261,7 @@ export const QuillEditor = defineComponent({
232261
() => props.content,
233262
(newContent) => {
234263
if (!quill || !newContent || newContent === props.content) return
235-
if (typeof newContent === 'string') quill.setText(props.content as string)
236-
else quill.setContents(newContent as Delta)
264+
setContents(newContent)
237265
}
238266
)
239267

@@ -249,8 +277,12 @@ export const QuillEditor = defineComponent({
249277
getEditor,
250278
getToolbar,
251279
getQuill,
280+
getContents,
281+
setContents,
252282
getHTML,
253283
setHTML,
284+
getText,
285+
setText,
254286
reinit,
255287
}
256288
},

packages/vue-quill/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
*/
66

77
import Quill from 'quill'
8+
import Delta from 'quill-delta'
89
import { QuillEditor } from './components/QuillEditor'
9-
export { QuillEditor, Quill }
10+
export { QuillEditor, Quill, Delta }

0 commit comments

Comments
 (0)