Fix BesQuillService when pasting a simple block of text
This commit is contained in:
		
							
								
								
									
										51
									
								
								service.js
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								service.js
									
									
									
									
									
								
							| @@ -1857,6 +1857,7 @@ class BesQuillService extends BesTreeService { | |||||||
|   onChangeData(delta) { |   onChangeData(delta) { | ||||||
|     let index = 0 |     let index = 0 | ||||||
|     let reproofNeeded = false |     let reproofNeeded = false | ||||||
|  |     const affectedBlocks = new Set() | ||||||
|  |  | ||||||
|     delta.ops.forEach(op => { |     delta.ops.forEach(op => { | ||||||
|       if (op.retain) { |       if (op.retain) { | ||||||
| @@ -1866,11 +1867,12 @@ class BesQuillService extends BesTreeService { | |||||||
|         } |         } | ||||||
|       } else if (op.insert) { |       } else if (op.insert) { | ||||||
|         reproofNeeded = true |         reproofNeeded = true | ||||||
|         index += op.insert.length |         index += typeof op.insert === 'string' ? op.insert.length : 1 // Handle string or embed | ||||||
|       } else if (op.delete) { |       } else if (op.delete) { | ||||||
|         reproofNeeded = true |         reproofNeeded = true | ||||||
|       } |       } | ||||||
|     }) |     }) | ||||||
|  |  | ||||||
|     if (reproofNeeded) { |     if (reproofNeeded) { | ||||||
|       const editorLength = this.quillInstance.getLength() |       const editorLength = this.quillInstance.getLength() | ||||||
|       const clampedIndex = Math.min(index, editorLength - 1) |       const clampedIndex = Math.min(index, editorLength - 1) | ||||||
| @@ -1879,22 +1881,55 @@ class BesQuillService extends BesTreeService { | |||||||
|       if (leaf) { |       if (leaf) { | ||||||
|         let domElement = leaf.domNode |         let domElement = leaf.domNode | ||||||
|  |  | ||||||
|  |         // Traverse up to find the block element | ||||||
|         while (domElement && !this.isBlockElement(domElement)) { |         while (domElement && !this.isBlockElement(domElement)) { | ||||||
|           domElement = domElement.parentNode |           domElement = domElement.parentNode | ||||||
|         } |         } | ||||||
|         if (domElement) { |  | ||||||
|           this.clearProofing(domElement) |  | ||||||
|  |  | ||||||
|           setTimeout(() => { |         if (domElement) affectedBlocks.add(domElement) | ||||||
|             this.redrawAllMistakeMarkup() |  | ||||||
|             this.scheduleProofing(1000) |  | ||||||
|           }, 0) |  | ||||||
|         } |  | ||||||
|       } else { |       } else { | ||||||
|         console.warn( |         console.warn( | ||||||
|           'Leaf is null. The index might be out of bounds or the editor content is empty.' |           'Leaf is null. The index might be out of bounds or the editor content is empty.' | ||||||
|         ) |         ) | ||||||
|       } |       } | ||||||
|  |  | ||||||
|  |       // Handle pasted content spanning multiple blocks | ||||||
|  |       const selection = this.quillInstance.getSelection() | ||||||
|  |       if (selection) { | ||||||
|  |         const [startLeaf] = this.quillInstance.getLeaf(selection.index) | ||||||
|  |         const [endLeaf] = this.quillInstance.getLeaf( | ||||||
|  |           selection.index + selection.length | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         if (startLeaf && endLeaf) { | ||||||
|  |           let startElement = startLeaf.domNode | ||||||
|  |           let endElement = endLeaf.domNode | ||||||
|  |  | ||||||
|  |           while (startElement && !this.isBlockElement(startElement)) { | ||||||
|  |             startElement = startElement.parentNode | ||||||
|  |           } | ||||||
|  |           while (endElement && !this.isBlockElement(endElement)) { | ||||||
|  |             endElement = endElement.parentNode | ||||||
|  |           } | ||||||
|  |  | ||||||
|  |           if (startElement && endElement) { | ||||||
|  |             let currentElement = startElement | ||||||
|  |             while (currentElement) { | ||||||
|  |               affectedBlocks.add(currentElement) | ||||||
|  |               if (currentElement === endElement) break | ||||||
|  |               currentElement = currentElement.nextElementSibling | ||||||
|  |             } | ||||||
|  |           } | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       // Clear proofing for all affected blocks | ||||||
|  |       affectedBlocks.forEach(block => this.clearProofing(block)) | ||||||
|  |  | ||||||
|  |       // Schedule proofing for all affected blocks | ||||||
|  |       setTimeout(() => { | ||||||
|  |         this.scheduleProofing(1000) | ||||||
|  |       }, 0) | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user