Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 1x 1x | import { ContentPosition, NodeType, PositionType, RegionType } from 'roosterjs-editor-types'; import type { BlockElement, InsertOption, NodePosition } from 'roosterjs-editor-types'; import { createRange, getBlockElementAtNode, getFirstLastBlockElement, isBlockElement, isVoidHtmlElement, Position, safeInstanceOf, toArray, wrap, adjustInsertPosition, getRegionsFromRange, splitTextNode, splitParentNode, } from 'roosterjs-editor-dom'; import type { DOMSelection } from 'roosterjs-content-model-types'; function getInitialRange( selection: DOMSelection | null, option: InsertOption ): { range: Range | null; rangeToRestore: Range | null } { // Selection start replaces based on the current selection. // Range inserts based on a provided range. // Both have the potential to use the current selection to restore cursor position // So in both cases we need to store the selection state. let range = selection?.type == 'range' ? selection.range : null; let rangeToRestore = null; if (option.position == ContentPosition.Range) { rangeToRestore = range; range = option.range; } else if (range) { rangeToRestore = range.cloneRange(); } return { range, rangeToRestore }; } /** * @internal * Insert a DOM node into editor content */ export function insertNode( contentDiv: HTMLDivElement, selection: DOMSelection | null, node: Node, option: InsertOption ): DOMSelection | undefined { switch (option.position) { case ContentPosition.Begin: case ContentPosition.End: { const isBegin = option.position == ContentPosition.Begin; const block = getFirstLastBlockElement(contentDiv, isBegin); let insertedNode: Node | Node[] | undefined; if (block) { const refNode = isBegin ? block.getStartNode() : block.getEndNode(); if ( option.insertOnNewLine || refNode.nodeType == NodeType.Text || isVoidHtmlElement(refNode) ) { // For insert on new line, or refNode is text or void html element (HR, BR etc.) // which cannot have children, i.e. <div>hello<br>world</div>. 'hello', 'world' are the // first and last node. Insert before 'hello' or after 'world', but still inside DIV if (safeInstanceOf(node, 'DocumentFragment')) { // if the node to be inserted is DocumentFragment, use its childNodes as insertedNode // because insertBefore() returns an empty DocumentFragment insertedNode = toArray(node.childNodes); refNode.parentNode?.insertBefore( node, isBegin ? refNode : refNode.nextSibling ); } else { insertedNode = refNode.parentNode?.insertBefore( node, isBegin ? refNode : refNode.nextSibling ); } } else { // if the refNode can have child, use appendChild (which is like to insert as first/last child) // i.e. <div>hello</div>, the content will be inserted before/after hello insertedNode = refNode.insertBefore(node, isBegin ? refNode.firstChild : null); } } else { // No first block, this can happen when editor is empty. Use appendChild to insert the content in contentDiv insertedNode = contentDiv.appendChild(node); } // Final check to see if the inserted node is a block. If not block and the ask is to insert on new line, // add a DIV wrapping if (insertedNode && option.insertOnNewLine) { const nodes = Array.isArray(insertedNode) ? insertedNode : [insertedNode]; if (!isBlockElement(nodes[0]) || !isBlockElement(nodes[nodes.length - 1])) { wrap(nodes); } } break; } case ContentPosition.DomEnd: // Use appendChild to insert the node at the end of the content div. const insertedNode = contentDiv.appendChild(node); // Final check to see if the inserted node is a block. If not block and the ask is to insert on new line, // add a DIV wrapping if (insertedNode && option.insertOnNewLine && !isBlockElement(insertedNode)) { wrap(insertedNode); } break; case ContentPosition.Range: case ContentPosition.SelectionStart: let { range, rangeToRestore } = getInitialRange(selection, option); if (!range) { break; } // if to replace the selection and the selection is not collapsed, remove the the content at selection first if (option.replaceSelection && !range.collapsed) { range.deleteContents(); } let pos: NodePosition = Position.getStart(range); let blockElement: BlockElement | null; if (option.insertOnNewLine && option.insertToRegionRoot) { pos = adjustInsertPositionRegionRoot(contentDiv, range, pos); } else if ( option.insertOnNewLine && (blockElement = getBlockElementAtNode(contentDiv, pos.normalize().node)) ) { pos = adjustInsertPositionNewLine(blockElement, contentDiv, pos); } else { pos = adjustInsertPosition(contentDiv, node, pos, range); } const nodeForCursor = node.nodeType == NodeType.DocumentFragment ? node.lastChild : node; range = createRange(pos); range.insertNode(node); if (option.updateCursor && nodeForCursor) { rangeToRestore = createRange( new Position(nodeForCursor, PositionType.After).normalize() ); } return rangeToRestore ? { type: 'range', range: rangeToRestore, isReverted: false, } : undefined; } } function adjustInsertPositionRegionRoot( contentDiv: HTMLDivElement, range: Range, position: NodePosition ) { const region = getRegionsFromRange(contentDiv, range, RegionType.Table)[0]; let node: Node | null = position.node; if (region) { if (node.nodeType == NodeType.Text && !position.isAtEnd) { node = splitTextNode(node as Text, position.offset, true /*returnFirstPart*/); } if (node != region.rootNode) { while (node && node.parentNode != region.rootNode) { splitParentNode(node, false /*splitBefore*/); node = node.parentNode; } } if (node) { position = new Position(node, PositionType.After); } } return position; } function adjustInsertPositionNewLine( blockElement: BlockElement, contentDiv: HTMLDivElement, pos: Position ) { let tempPos = new Position(blockElement.getEndNode(), PositionType.After); if (safeInstanceOf(tempPos.node, 'HTMLTableRowElement')) { const div = contentDiv.ownerDocument.createElement('div'); const range = createRange(pos); range.insertNode(div); tempPos = new Position(div, PositionType.Begin); } return tempPos; } |