@@ -180,6 +180,41 @@ export default class Paste extends Module {
180180 }
181181 }
182182
183+ /**
184+ * Process pasted text and divide them into Blocks
185+ *
186+ * @param {string } data - text to process. Can be HTML or plain.
187+ * @param {boolean } isHTML - if passed string is HTML, this parameter should be true
188+ */
189+ public async processText ( data : string , isHTML : boolean = false ) {
190+ const { Caret, BlockManager, Tools} = this . Editor ;
191+ const dataToInsert = isHTML ? this . processHTML ( data ) : this . processPlain ( data ) ;
192+
193+ if ( ! dataToInsert . length ) {
194+ return ;
195+ }
196+
197+ if ( dataToInsert . length === 1 ) {
198+ if ( ! dataToInsert [ 0 ] . isBlock ) {
199+ this . processInlinePaste ( dataToInsert . pop ( ) ) ;
200+ } else {
201+ this . processSingleBlock ( dataToInsert . pop ( ) ) ;
202+ }
203+ return ;
204+ }
205+
206+ const isCurrentBlockInitial = BlockManager . currentBlock && Tools . isInitial ( BlockManager . currentBlock . tool ) ;
207+ const needToReplaceCurrentBlock = isCurrentBlockInitial && BlockManager . currentBlock . isEmpty ;
208+
209+ await Promise . all ( dataToInsert . map (
210+ async ( content , i ) => await this . insertBlock ( content , i === 0 && needToReplaceCurrentBlock ) ,
211+ ) ) ;
212+
213+ if ( BlockManager . currentBlock ) {
214+ Caret . setToBlock ( BlockManager . currentBlock , Caret . positions . END ) ;
215+ }
216+ }
217+
183218 /**
184219 * Set onPaste callback handler
185220 */
@@ -424,39 +459,6 @@ export default class Paste extends Module {
424459 } ;
425460 }
426461
427- /**
428- * Process pasted text and divide them into Blocks
429- *
430- * @param {string } data - text to process. Can be HTML or plain.
431- * @param {boolean } isHTML - if passed string is HTML, this parameter should be true
432- */
433- private async processText ( data : string , isHTML : boolean = false ) {
434- const { Caret, BlockManager, Tools} = this . Editor ;
435- const dataToInsert = isHTML ? this . processHTML ( data ) : this . processPlain ( data ) ;
436-
437- if ( ! dataToInsert . length ) {
438- return ;
439- }
440-
441- if ( dataToInsert . length === 1 ) {
442- if ( ! dataToInsert [ 0 ] . isBlock ) {
443- this . processInlinePaste ( dataToInsert . pop ( ) ) ;
444- } else {
445- this . processSingleBlock ( dataToInsert . pop ( ) ) ;
446- }
447- return ;
448- }
449-
450- const isCurrentBlockInitial = Tools . isInitial ( BlockManager . currentBlock . tool ) ;
451- const needToReplaceCurrentBlock = isCurrentBlockInitial && BlockManager . currentBlock . isEmpty ;
452-
453- await Promise . all ( dataToInsert . map (
454- async ( content , i ) => await this . insertBlock ( content , i === 0 && needToReplaceCurrentBlock ) ,
455- ) ) ;
456-
457- Caret . setToBlock ( BlockManager . currentBlock , Caret . positions . END ) ;
458- }
459-
460462 /**
461463 * Split HTML string to blocks and return it as array of Block data
462464 *
@@ -558,8 +560,12 @@ export default class Paste extends Module {
558560 /**
559561 * If pasted tool isn`t equal current Block or if pasted content contains block elements, insert it as new Block
560562 */
561- if ( dataToInsert . tool !== currentBlock . name || ! $ . containsOnlyInlineElements ( dataToInsert . content . innerHTML ) ) {
562- this . insertBlock ( dataToInsert , Tools . isInitial ( currentBlock . tool ) && currentBlock . isEmpty ) ;
563+ if (
564+ ! currentBlock ||
565+ dataToInsert . tool !== currentBlock . name ||
566+ ! $ . containsOnlyInlineElements ( dataToInsert . content . innerHTML )
567+ ) {
568+ this . insertBlock ( dataToInsert , currentBlock && Tools . isInitial ( currentBlock . tool ) && currentBlock . isEmpty ) ;
563569 return ;
564570 }
565571
@@ -578,7 +584,7 @@ export default class Paste extends Module {
578584 const { BlockManager, Caret, Sanitizer, Tools} = this . Editor ;
579585 const { content, tool} = dataToInsert ;
580586
581- const currentBlockIsInitial = Tools . isInitial ( BlockManager . currentBlock . tool ) ;
587+ const currentBlockIsInitial = BlockManager . currentBlock && Tools . isInitial ( BlockManager . currentBlock . tool ) ;
582588
583589 if ( currentBlockIsInitial && content . textContent . length < Paste . PATTERN_PROCESSING_MAX_LENGTH ) {
584590 const blockData = await this . processPattern ( content . textContent ) ;
@@ -597,10 +603,10 @@ export default class Paste extends Module {
597603 }
598604 }
599605
600- const currentToolSanitizeConfig = Sanitizer . getInlineToolsConfig ( BlockManager . currentBlock . name ) ;
601-
602606 /** If there is no pattern substitute - insert string as it is */
603- if ( BlockManager . currentBlock . currentInput ) {
607+ if ( BlockManager . currentBlock && BlockManager . currentBlock . currentInput ) {
608+ const currentToolSanitizeConfig = Sanitizer . getInlineToolsConfig ( BlockManager . currentBlock . name ) ;
609+
604610 document . execCommand ( 'insertHTML' , false , Sanitizer . clean ( content . innerHTML , currentToolSanitizeConfig ) ) ;
605611 } else {
606612 this . insertBlock ( dataToInsert ) ;
0 commit comments