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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import type { EditorPlugin, IEditor, PluginEvent } from 'roosterjs-content-model-types'; import { getNodePositionFromEvent } from '../utils/getNodePositionFromEvent'; const MAX_TOUCH_MOVE_DISTANCE = 6; // the max number of offsets for the touch selection to move const POINTER_DETECTION_DELAY = 150; // Delay time to wait for selection to be updated and also detect if pointerup is a tap or part of double tap const PUNCTUATION_MATCHING_REGEX = /[.,;:!]/; const SPACE_MATCHING_REGEX = /\s/; /** * Touch plugin to manage touch behaviors */ export class TouchPlugin implements EditorPlugin { private editor: IEditor | null = null; private timer = 0; private isDblClicked: boolean = false; private isTouchPenPointerEvent: boolean = false; /** * Create an instance of Touch plugin */ constructor() {} /** * Get a friendly name of this plugin */ getName() { return 'Touch'; } /** * Initialize this plugin. This should only be called from Editor * @param editor Editor instance */ initialize(editor: IEditor) { this.editor = editor; this.isDblClicked = false; } /** * Dispose this plugin */ dispose() { if (this.timer) { this.editor?.getDocument()?.defaultView?.clearTimeout(this.timer); this.timer = 0; } this.editor = null; } /** * Handle events triggered from editor * @param event PluginEvent object */ onPluginEvent(event: PluginEvent) { if (!this.editor) { return; } switch (event.eventType) { case 'pointerDown': this.isDblClicked = false; this.isTouchPenPointerEvent = true; event.originalEvent.preventDefault(); const targetWindow = this.editor.getDocument()?.defaultView || window; if (this.timer) { targetWindow.clearTimeout(this.timer); } this.timer = targetWindow.setTimeout(() => { this.timer = 0; if (!this.isDblClicked && this.editor) { const caretPosition = getNodePositionFromEvent( this.editor, event.rawEvent.x, event.rawEvent.y ); if (caretPosition) { const { node, offset } = caretPosition; const nodeTextContent = node.textContent || ''; const charAtSelection = nodeTextContent[offset]; if ( node.nodeType === Node.TEXT_NODE && charAtSelection && !SPACE_MATCHING_REGEX.test(charAtSelection) && !PUNCTUATION_MATCHING_REGEX.test(charAtSelection) ) { const { wordStart, wordEnd } = findWordBoundaries( nodeTextContent, offset ); // Move cursor to the calculated offset const leftCursorWordLength = offset - wordStart; const rightCursorWordLength = wordEnd - offset; let movingOffset: number = leftCursorWordLength >= rightCursorWordLength ? rightCursorWordLength : -leftCursorWordLength; movingOffset = Math.abs(movingOffset) > MAX_TOUCH_MOVE_DISTANCE ? 0 : movingOffset; const newOffsetPosition = offset + movingOffset; if ( movingOffset !== 0 && nodeTextContent.length >= newOffsetPosition ) { const newRange = this.editor.getDocument().createRange(); newRange.setStart(node, newOffsetPosition); newRange.setEnd(node, newOffsetPosition); this.editor.setDOMSelection({ type: 'range', range: newRange, isReverted: false, }); return; } } // Place cursor at same position of browser handler const newRange = this.editor.getDocument().createRange(); newRange.setStart(node, offset); newRange.setEnd(node, offset); this.editor.setDOMSelection({ type: 'range', range: newRange, isReverted: false, }); } // reset values this.isTouchPenPointerEvent = false; } }, POINTER_DETECTION_DELAY); break; case 'doubleClick': if (this.isTouchPenPointerEvent) { event.rawEvent.preventDefault(); this.isDblClicked = true; const caretPosition = getNodePositionFromEvent( this.editor, event.rawEvent.x, event.rawEvent.y ); if (caretPosition) { const { node, offset } = caretPosition; if (node.nodeType !== Node.TEXT_NODE) { return; } const nodeTextContent = node.nodeValue || ''; const char = nodeTextContent.charAt(offset); // Check if the clicked character is a punctuation mark, then highlight that character only if (PUNCTUATION_MATCHING_REGEX.test(char)) { const newRange = this.editor.getDocument()?.createRange(); if (newRange) { newRange.setStart(node, offset); newRange.setEnd(node, offset + 1); this.editor.setDOMSelection({ type: 'range', range: newRange, isReverted: false, }); } } else if (SPACE_MATCHING_REGEX.test(char)) { // If the clicked character is an open space with no word of right side const rightSideOfChar = nodeTextContent.substring( offset, nodeTextContent.length ); const isRightSideAllSpaces = rightSideOfChar.length > 0 && !/\S/.test(rightSideOfChar); if (isRightSideAllSpaces) { // select the first space only let start = offset; while ( start > 0 && SPACE_MATCHING_REGEX.test(nodeTextContent.charAt(start - 1)) ) { start--; } const newRange = this.editor.getDocument()?.createRange(); if (newRange) { newRange.setStart(node, start); newRange.setEnd(node, start + 1); this.editor.setDOMSelection({ type: 'range', range: newRange, isReverted: false, }); } } } else { const { wordStart, wordEnd } = findWordBoundaries( nodeTextContent, offset ); const newRange = this.editor.getDocument()?.createRange(); if (newRange) { newRange.setStart(node, wordStart); newRange.setEnd(node, wordEnd); this.editor.setDOMSelection({ type: 'range', range: newRange, isReverted: false, }); } } } } break; } } } /** * @internal * Finds the start and end indices of the word at the given offset in the text. * @param text The string to search within. * @param offset The index within the string to find the word boundaries around. * @returns An object containing wordStart and wordEnd indices. */ function findWordBoundaries(text: string, offset: number) { let start = offset; let end = offset; // Move start backwards to find word start while ( start > 0 && !SPACE_MATCHING_REGEX.test(text[start - 1]) && !PUNCTUATION_MATCHING_REGEX.test(text[start - 1]) ) { start--; } // Move end forward to find word end while ( end < text.length && !SPACE_MATCHING_REGEX.test(text[end]) && !PUNCTUATION_MATCHING_REGEX.test(text[end]) ) { end++; } return { wordStart: start, wordEnd: end, }; } |