2323 * mediator.exec('format:clean', elem); // Clean the HTML inside elem
2424 */
2525import Module from '../core/Module' ;
26- import commands from '../utils/commands' ;
2726import DOM from '../utils/DOM' ;
2827import zeroWidthSpace from '../utils/zeroWidthSpace' ;
2928
30- import toolbarConfig from '../config/toolbar' ;
31-
32- let validTags = toolbarConfig . getValidTags ( ) ;
33- let blockTags = toolbarConfig . getBlockTags ( ) ;
34- let listTags = toolbarConfig . getListTags ( ) ;
29+ let validTags , blockTags , listTags ;
3530
3631const BaseFormatter = Module ( {
3732 name : 'BaseFormatter' ,
@@ -49,7 +44,13 @@ const BaseFormatter = Module({
4944 }
5045 } ,
5146 methods : {
52- init ( ) { } ,
47+ init ( ) {
48+ const { mediator } = this ;
49+
50+ validTags = mediator . get ( 'config:toolbar:validTags' ) ;
51+ blockTags = mediator . get ( 'config:toolbar:blockTags' ) ;
52+ listTags = mediator . get ( 'config:toolbar:listTags' ) ;
53+ } ,
5354
5455 /**
5556 * @func exportToCanvas
@@ -64,10 +65,7 @@ const BaseFormatter = Module({
6465 this . injectHooks ( rootElement ) ;
6566
6667 const rangeCoordinates = mediator . get ( 'selection:range:coordinates' ) ;
67- const clonedNodes = this . cloneNodes ( rootElement ) ;
68- clonedNodes . forEach ( ( node ) => {
69- DOM . trimNodeText ( node ) ;
70- } ) ;
68+ const clonedNodes = DOM . cloneNodes ( rootElement , { trim : true } ) ;
7169
7270 mediator . exec ( 'canvas:content' , clonedNodes ) ;
7371 mediator . exec ( 'canvas:select:by:coordinates' , rangeCoordinates ) ;
@@ -109,7 +107,8 @@ const BaseFormatter = Module({
109107 formatDefault ( ) {
110108 const { mediator } = this ;
111109 const rootElem = mediator . get ( 'selection:rootelement' ) ;
112- commands . defaultBlockFormat ( ) ;
110+
111+ mediator . exec ( 'commands:format:default' ) ;
113112 this . removeStyledSpans ( rootElem ) ;
114113 } ,
115114
@@ -125,17 +124,6 @@ const BaseFormatter = Module({
125124 this . ensureRootElems ( rootElem ) ;
126125 this . removeStyleAttributes ( rootElem ) ;
127126 this . removeEmptyNodes ( rootElem , { recursive : true } ) ;
128-
129- // -----
130-
131- // this.removeBrNodes(rootElem);
132- // // this.removeEmptyNodes(rootElem);
133- // this.removeFontTags(rootElem);
134- // this.removeStyledSpans(rootElem);
135- // this.clearEntities(rootElem);
136- // this.removeZeroWidthSpaces(rootElem);
137- // this.defaultOrphanedTextNodes(rootElem);
138- // this.removeEmptyNodes(rootElem, { recursive: true });
139127 } ,
140128
141129 /**
@@ -165,18 +153,21 @@ const BaseFormatter = Module({
165153 formatEmptyNewLine ( ) {
166154 const { mediator } = this ;
167155 const anchorNode = mediator . get ( 'selection:anchornode' ) ;
168- const canDefaultNewline = ! ( anchorNode . innerText && anchorNode . innerText . trim ( ) . length ) && ! DOM . isIn ( anchorNode , toolbarConfig . preventNewlineDefault ) ;
156+ const preventNewlineDefault = mediator . get ( 'config:toolbar:preventNewlineDefault' ) ;
157+ const canDefaultNewline = ! ( anchorNode . innerText && anchorNode . innerText . trim ( ) . length ) && ! DOM . isIn ( anchorNode , preventNewlineDefault ) ;
169158 const anchorIsContentEditable = anchorNode . hasAttribute && anchorNode . hasAttribute ( 'contenteditable' ) ;
170159
171160 if ( canDefaultNewline || anchorIsContentEditable ) {
172161 this . formatDefault ( ) ;
173162 }
174163 } ,
175164
176- formateBlockquoteNewLine ( ) {
165+ formatBlockquoteNewLine ( ) {
177166 const { mediator } = this ;
178167
179- commands . exec ( 'outdent' ) ;
168+ mediator . exec ( 'commands:exec' , {
169+ command : 'outdent'
170+ } ) ;
180171 this . formatDefault ( ) ;
181172
182173 const currentRangeClone = mediator . get ( 'selection:range' ) . cloneRange ( ) ;
@@ -207,7 +198,7 @@ const BaseFormatter = Module({
207198 const isContentEditable = startContainer . nodeType === Node . ELEMENT_NODE && startContainer . hasAttribute ( 'contenteditable' ) ;
208199
209200 if ( containerIsBlockquote ) {
210- this . formateBlockquoteNewLine ( ) ;
201+ this . formatBlockquoteNewLine ( ) ;
211202 } else if ( containerIsEmpty || isContentEditable ) {
212203 this . formatEmptyNewLine ( ) ;
213204 }
@@ -235,9 +226,10 @@ const BaseFormatter = Module({
235226 const isLastChild = brNode === brNode . parentNode . lastChild ;
236227 const isDoubleBreak = brNode . nextSibling && brNode . nextSibling . nodeName === 'BR' ;
237228 const isInBlock = DOM . isIn ( brNode , blockTags , rootElem ) ;
229+ const isOrphan = brNode . parentNode === rootElem ;
238230
239- if ( isLastChild ) {
240- brNodesToRemove . push ( isLastChild ) ;
231+ if ( isLastChild || isOrphan ) {
232+ brNodesToRemove . push ( brNode ) ;
241233 return ;
242234 }
243235
@@ -291,8 +283,9 @@ const BaseFormatter = Module({
291283 let isInvalid = validTags . indexOf ( currentNode . nodeName ) < 0 ;
292284 let isBrNode = currentNode . nodeName === 'BR' ; // BR nodes are handled elsewhere
293285 let isTypesterElem = currentNode . className && / t y p e s t e r / . test ( currentNode . className ) ;
286+ let isElement = currentNode . nodeType !== Node . TEXT_NODE ;
294287
295- if ( isInvalid && ! isBrNode && ! isTypesterElem ) {
288+ if ( isInvalid && ! isBrNode && ! isTypesterElem && isElement ) {
296289 invalidElements . unshift ( currentNode ) ;
297290 }
298291 }
0 commit comments