File tree Expand file tree Collapse file tree 2 files changed +59
-1
lines changed
Expand file tree Collapse file tree 2 files changed +59
-1
lines changed Original file line number Diff line number Diff line change 11import { getEventKey } from '../../utils/event'
2- import { getToolbar , getFonts } from './editor-utils'
2+ import { getToolbar , getFonts , getContentObject } from './editor-utils'
33import { buttons } from './editor-definitions'
44import { Caret } from './editor-caret'
55import 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 ( ) {
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments