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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 37x 22x 1x 20x 1x 21x 1x 3x 17x 1x 21x 21x 21x 21x 19x 2x 1x 20x 20x 20x 1x 26x 26x 20x 20x 1x 2x 2x 1x 20x 20x 20x 19x 2x 1x 2x 6x 5x 6x 3x 2x 3x 2x 2x 2x 5x 4x 5x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 1x 3x 3x 1x 2x 1x | import { keyboardDelete } from './keyboardDelete'; import { keyboardEnter } from './keyboardEnter'; import { keyboardInput } from './keyboardInput'; import { keyboardTab } from './keyboardTab'; import { parseTableCells } from 'roosterjs-content-model-dom'; import type { DOMSelection, EditorPlugin, IEditor, KeyDownEvent, PluginEvent, } from 'roosterjs-content-model-types'; /** * Options to customize the keyboard handling behavior of Edit plugin */ export type EditOptions = { /** * Whether to handle Tab key in keyboard. @default true */ handleTabKey?: boolean; /** * Whether expanded selection within a text node should be handled by CM when pressing Backspace/Delete key. * @default true */ handleExpandedSelectionOnDelete?: boolean; /** * Callback function to determine whether the Rooster should handle the Enter key press. * If the function returns true, the Rooster will handle the Enter key press instead of the browser. * @param editor - The editor instance. * @returns A boolean */ shouldHandleEnterKey?: ((editor: IEditor) => boolean) | boolean; /** * Callback or boolean to determine whether the browser (not Content Model) should handle the Backspace key press. * If the value/callback returns true, Rooster will NOT handle Backspace and will defer to the browser's native behavior. * @param editor - The editor instance (when using callback). * @returns A boolean */ shouldHandleBackspaceKey?: ((editor: IEditor) => boolean) | boolean; }; const BACKSPACE_KEY = 8; const DELETE_KEY = 46; /** * According to https://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html * 229 can be sent in variants generated when Long press (iOS) or using IM. * * Other cases: https://stackoverflow.com/questions/25043934/is-it-ok-to-ignore-keydown-events-with-keycode-229 */ const DEAD_KEY = 229; const DefaultOptions: Partial<EditOptions> = { handleTabKey: true, handleExpandedSelectionOnDelete: true, }; /** * Edit plugins helps editor to do editing operation on top of content model. * This includes: * 1. Delete Key * 2. Backspace Key * 3. Tab Key */ export class EditPlugin implements EditorPlugin { private editor: IEditor | null = null; private disposer: (() => void) | null = null; private shouldHandleNextInputEvent = false; private selectionAfterDelete: DOMSelection | null = null; private handleNormalEnter: (editor: IEditor) => boolean = (editor: IEditor) => false; /** * @param options An optional parameter that takes in an object of type EditOptions, which includes the following properties: * handleTabKey: A boolean that enables or disables Tab key handling. Defaults to true. */ constructor(private options: EditOptions = DefaultOptions) { this.options = { ...DefaultOptions, ...options }; } private createNormalEnterChecker(result: boolean) { return result ? () => true : () => false; } private getHandleNormalEnter(editor: IEditor) { switch (typeof this.options.shouldHandleEnterKey) { case 'function': return this.options.shouldHandleEnterKey; break; case 'boolean': return this.createNormalEnterChecker(this.options.shouldHandleEnterKey); break; default: return this.createNormalEnterChecker( editor.isExperimentalFeatureEnabled('HandleEnterKey') ); break; } } /** * Get name of this plugin */ getName() { return 'Edit'; } /** * The first method that editor will call to a plugin when editor is initializing. * It will pass in the editor instance, plugin should take this chance to save the * editor reference so that it can call to any editor method or format API later. * @param editor The editor object */ initialize(editor: IEditor) { this.editor = editor; this.handleNormalEnter = this.getHandleNormalEnter(editor); if (editor.getEnvironment().isAndroid) { this.disposer = this.editor.attachDomEvent({ beforeinput: { beforeDispatch: e => this.handleBeforeInputEvent(editor, e), }, }); } } /** * The last method that editor will call to a plugin before it is disposed. * Plugin can take this chance to clear the reference to editor. After this method is * called, plugin should not call to any editor method since it will result in error. */ dispose() { this.editor = null; this.disposer?.(); this.disposer = null; } /** * Core method for a plugin. Once an event happens in editor, editor will call this * method of each plugin to handle the event as long as the event is not handled * exclusively by another plugin. * @param event The event to handle: */ onPluginEvent(event: PluginEvent) { Eif (this.editor) { switch (event.eventType) { case 'keyDown': this.handleKeyDownEvent(this.editor, event); break; case 'keyUp': if (this.selectionAfterDelete) { this.editor.setDOMSelection(this.selectionAfterDelete); this.selectionAfterDelete = null; } break; } } } /** * Check if the plugin should handle the given event exclusively. * Handle an event exclusively means other plugin will not receive this event in * onPluginEvent method. * If two plugins will return true in willHandleEventExclusively() for the same event, * the final result depends on the order of the plugins are added into editor * @param event The event to check: */ willHandleEventExclusively(event: PluginEvent) { Iif ( this.editor && this.options.handleTabKey && event.eventType == 'keyDown' && event.rawEvent.key == 'Tab' && !event.rawEvent.shiftKey ) { const selection = this.editor.getDOMSelection(); const startContainer = selection?.type == 'range' && selection.range.collapsed ? selection.range.startContainer : null; const table = startContainer ? this.editor.getDOMHelper().findClosestElementAncestor(startContainer, 'table') : null; const parsedTable = table && parseTableCells(table); if (parsedTable) { const lastRow = parsedTable[parsedTable.length - 1]; const lastCell = lastRow && lastRow[lastRow.length - 1]; if (typeof lastCell == 'object' && lastCell.contains(startContainer)) { // When TAB in the last cell of a table, we will generate new table row, so prevent other plugins handling this event // e.g. SelectionPlugin will move the focus out of table, which is conflict with this behavior return true; } } } return false; } private handleKeyDownEvent(editor: IEditor, event: KeyDownEvent) { const rawEvent = event.rawEvent; const hasCtrlOrMetaKey = rawEvent.ctrlKey || rawEvent.metaKey; if (!rawEvent.defaultPrevented && !event.handledByEditFeature) { switch (rawEvent.key) { case 'Backspace': // Use our API to handle BACKSPACE/DELETE key. // No need to clear cache here since if we rely on browser's behavior, there will be Input event and its handler will reconcile cache if (!this.shouldBrowserHandleBackspace(editor)) { keyboardDelete( editor, rawEvent, this.options.handleExpandedSelectionOnDelete ); } break; case 'Delete': // Use our API to handle BACKSPACE/DELETE key. // No need to clear cache here since if we rely on browser's behavior, there will be Input event and its handler will reconcile cache // And leave it to browser when shift key is pressed so that browser will trigger cut event if (!event.rawEvent.shiftKey) { keyboardDelete( editor, rawEvent, this.options.handleExpandedSelectionOnDelete ); } break; case 'Tab': if (this.options.handleTabKey && !hasCtrlOrMetaKey) { keyboardTab(editor, rawEvent); } break; case 'Unidentified': Eif (editor.getEnvironment().isAndroid) { this.shouldHandleNextInputEvent = true; } break; case 'Enter': if ( !hasCtrlOrMetaKey && !event.rawEvent.isComposing && event.rawEvent.keyCode !== DEAD_KEY ) { keyboardEnter(editor, rawEvent, this.handleNormalEnter(editor)); } break; default: keyboardInput(editor, rawEvent); break; } } } private handleBeforeInputEvent(editor: IEditor, rawEvent: Event) { // Some Android IMEs doesn't fire correct keydown event for BACKSPACE/DELETE key // Here we translate input event to BACKSPACE/DELETE keydown event to be compatible with existing logic Iif ( !this.shouldHandleNextInputEvent || !(rawEvent instanceof InputEvent) || rawEvent.defaultPrevented ) { return; } this.shouldHandleNextInputEvent = false; let handled = false; switch (rawEvent.inputType) { case 'deleteContentBackward': Eif (!this.shouldBrowserHandleBackspace(editor)) { // This logic is Android specific. It's because some Android keyboard doesn't support key and keycode, the value of them is always Unidentified, so we have to manually create a new one. handled = keyboardDelete( editor, new KeyboardEvent('keydown', { key: 'Backspace', keyCode: BACKSPACE_KEY, which: BACKSPACE_KEY, }), this.options.handleExpandedSelectionOnDelete ); } break; case 'deleteContentForward': handled = keyboardDelete( editor, new KeyboardEvent('keydown', { key: 'Delete', keyCode: DELETE_KEY, which: DELETE_KEY, }), this.options.handleExpandedSelectionOnDelete ); break; } Iif (handled) { rawEvent.preventDefault(); // Restore the selection on keyup event to avoid the cursor jump issue // See: https://issues.chromium.org/issues/330596261 this.selectionAfterDelete = editor.getDOMSelection(); } } private shouldBrowserHandleBackspace(editor: IEditor): boolean { const opt = this.options.shouldHandleBackspaceKey; switch (typeof opt) { case 'function': return opt(editor); case 'boolean': return opt; default: return false; } } } |