Skip to content

Commit fce9541

Browse files
committed
feat: Enhance QEditor
1 parent 9d0b9a7 commit fce9541

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/components/editor/QEditor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getEventKey } from '../../utils/event'
2-
import { getToolbar, getFonts } from './editor-utils'
2+
import { getToolbar, getFonts, getContentObject } from './editor-utils'
33
import { buttons } from './editor-definitions'
44
import { Caret } from './editor-caret'
55
import extend from '../../utils/extend'
@@ -179,6 +179,9 @@ export default {
179179
},
180180
focus () {
181181
this.$refs.content.focus()
182+
},
183+
getContentObject () {
184+
return getContentObject(this.$refs.content).children
182185
}
183186
},
184187
created () {

src/components/editor/editor-utils.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,58 @@ export function getFonts (defaultFont, fonts = {}) {
194194

195195
return def
196196
}
197+
198+
const camelizeRE = /(?:^|[-])(\w)/g
199+
function camelize (str) {
200+
return str.replace(
201+
camelizeRE,
202+
(_, c) => c ? c.toUpperCase() : ''
203+
)
204+
}
205+
206+
function getStyleObject (el) {
207+
const output = {}
208+
209+
el.style.cssText.split(';').forEach(rule => {
210+
if (rule) {
211+
const parts = rule.split(':')
212+
output[ camelize(parts[0].trim()) ] = parts[1].trim()
213+
}
214+
})
215+
216+
return output
217+
}
218+
219+
export function getContentObject (el) {
220+
if (el.nodeType === Node.TEXT_NODE) {
221+
return {
222+
nodeType: Node.TEXT_NODE,
223+
text: el.textContent
224+
}
225+
}
226+
227+
const node = {
228+
nodeType: el.nodeType,
229+
tagName: el.tagName,
230+
attributes: {}
231+
}
232+
233+
Array.from(el.attributes, ({name, value}) => {
234+
if (name === 'style') {
235+
node.style = getStyleObject(el)
236+
}
237+
else {
238+
node.attributes[name] = value
239+
}
240+
})
241+
242+
const children = Array.from(el.childNodes, getContentObject)
243+
if (children.length === 1 && children[0].nodeType === Node.TEXT_NODE) {
244+
node.text = children[0].text
245+
}
246+
else {
247+
node.children = children
248+
}
249+
250+
return node
251+
}

0 commit comments

Comments
 (0)