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 | 1x 1x 1x 1x 1x 31x 31x 62x 1x 1x 30x 30x 1x 1x 1x 30x 1x 33x 28x 28x 24x 24x 56x 56x 28x 28x 28x 28x 420x 1x 420x 28x 112x 280x 1x 1765x 353x 353x 353x 1x | import { cacheGetEventData } from 'roosterjs-content-model-dom'; import type { ShortcutCommand, ShortcutKeyDefinition } from './ShortcutCommand'; import { ShortcutBold, ShortcutBullet, ShortcutClearFormat, ShortcutDecreaseFont, ShortcutIncreaseFont, ShortcutIndentList, ShortcutItalic, ShortcutNumbering, ShortcutOutdentList, ShortcutRedo, ShortcutRedoAlt, ShortcutRedoMacOS, ShortcutUnderline, ShortcutUndo, ShortcutUndo2, } from './shortcuts'; import type { EditorPlugin, IEditor, KeyDownEvent, PluginEvent, } from 'roosterjs-content-model-types'; const defaultShortcuts: ShortcutCommand[] = [ ShortcutBold, ShortcutItalic, ShortcutUnderline, ShortcutClearFormat, ShortcutUndo, ShortcutUndo2, ShortcutRedo, ShortcutRedoAlt, ShortcutRedoMacOS, ShortcutBullet, ShortcutNumbering, ShortcutIncreaseFont, ShortcutDecreaseFont, ShortcutIndentList, ShortcutOutdentList, ]; const CommandCacheKey = '__ShortcutCommandCache'; /** * Shortcut plugin hook on the specified shortcut keys and trigger related format API */ export class ShortcutPlugin implements EditorPlugin { private editor: IEditor | null = null; private isMac = false; /** * Create a new instance of ShortcutPlugin * @param [shortcuts=defaultShortcuts] Allowed commands */ constructor(private Eshortcuts: ShortcutCommand[] = defaultShortcuts) {} /** * Get name of this plugin */ getName() { return 'Shortcut'; } /** * 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.isMac = !!this.editor.getEnvironment().isMac; } /** * 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; } /** * 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) { return ( event.eventType == 'keyDown' && (event.rawEvent.ctrlKey || event.rawEvent.altKey || event.rawEvent.metaKey) && !!this.cacheGetCommand(event) ); } /** * 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) { if (this.editor && event.eventType == 'keyDown') { const command = this.cacheGetCommand(event); if (command) { command.onClick(this.editor); event.rawEvent.preventDefault(); } } } private cacheGetCommand(event: KeyDownEvent) { return cacheGetEventData(event, CommandCacheKey, event => { const editor = this.editor; const { ctrlKey, metaKey } = event.rawEvent; Iif (ctrlKey && metaKey) { // We don't support both Ctrl and Meta key pressed at the same time. return null; } return ( editor && this.shortcuts.filter( command => this.matchOS(command.environment) && this.matchShortcut(command.shortcutKey, event.rawEvent) )[0] ); }); } private matchOS(environment?: 'all' | 'mac' | 'nonMac') { switch (environment) { case 'mac': return this.isMac; case 'nonMac': return !this.isMac; default: return true; } } private matchShortcut(shortcutKey: ShortcutKeyDefinition, event: KeyboardEvent) { const { ctrlKey, altKey, shiftKey, which, metaKey } = event; const ctrlOrMeta = this.isMac ? metaKey : ctrlKey; const matchModifier = (shortcutKey.modifierKey == 'ctrl' && ctrlOrMeta && !altKey) || (shortcutKey.modifierKey == 'alt' && altKey && !ctrlOrMeta); return matchModifier && shiftKey == shortcutKey.shiftKey && shortcutKey.which == which; } } |