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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 7x 3x 2x 2x 12x 12x 10x 10x 3x 3x 1x 1x 2x 2x 1x 1x 3x 3x 3x 9x 9x 7x 2x 2x 1x 1x 1x 1x 7x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 7x 1x 27x 27x 27x 27x 1x 1x 26x 26x 26x 26x 26x 25x 25x 4x 3x 3x 3x 3x 3x 5x 5x 4x 4x 5x 5x 3x 2x 3x 3x 3x 3x 3x 3x 3x 2x 2x 3x 3x 3x 3x 96x 96x 93x 93x 93x 93x 93x 93x 13x 93x 93x 93x 87x 93x 93x | import React from 'react'; import CodeMirror from 'codemirror'; import SHARED from './shared'; import {delete_, copy, paste, InsertTarget, ReplaceNodeTarget, OverwriteTarget, activateByNid} from './actions'; import {partition, getRoot, skipCollapsed, say, getLastVisibleNode, preambleUndoRedo, playSound, BEEP} from './utils'; const userAgent = navigator.userAgent; const platform = navigator.platform; const edge = /Edge\/(\d+)/.exec(userAgent); const ios = !edge && /AppleWebKit/.test(userAgent) && /Mobile\/\w+/.test(userAgent); const mac = ios || /Mac/.test(platform); export const defaultKeyMap = { 'Down' : 'nextNode', 'Up' : 'prevNode', 'Home' : 'firstNode', 'End' : 'lastVisibleNode', 'Left' : 'collapseOrSelectParent', 'Right' : 'expandOrSelectFirstChild', 'Shift-Left': 'collapseAll', 'Shift-Right':'expandAll', 'Shift-Alt-Left': 'collapseCurrentRoot', 'Shift-Alt-Right':'expandCurrentRoot', 'Enter' : 'edit', 'Space' : 'toggleSelection', 'Esc' : 'clearSelection', 'Ctrl-Esc' : 'clearSelection', 'Alt-Q' : 'clearSelection', 'Delete' : 'deleteNodes', 'Backspace' : 'deleteNodes', 'Ctrl-[' : 'insertLeft', 'Ctrl-]' : 'insertRight', 'Shift-,' : 'jumpToRoot', '\\' : 'readAncestors', 'Shift-\\' : 'readChildren', 'PageUp' : 'searchPrevious', 'PageDown' : 'searchNext', 'F3' : 'activateSearchDialog', 'Shift-Tab' : 'prevFocus', }; const macKeyMap = { 'Cmd-Enter': 'editAnything', 'Cmd-F' : 'activateSearchDialog', 'Cmd-Z' : 'undo', 'Shift-Cmd-Z': 'redo', 'Cmd-Y' : 'redo', 'Cmd-C' : 'copy', 'Cmd-V' : 'paste', 'Shift-Cmd-V': 'pasteBefore', 'Cmd-X' : 'cut', 'Shift-Ctrl-/': 'help', }; const pcKeyMap = { 'Ctrl-Enter': 'editAnything', 'Ctrl-F' : 'activateSearchDialog', 'Ctrl-Z' : 'undo', 'Ctrl-Y' : 'redo', 'Shift-Ctrl-Z':'redo', 'Ctrl-C' : 'copy', 'Ctrl-V' : 'paste', 'Shift-Ctrl-V' : 'pasteBefore', 'Ctrl-X' : 'cut', 'Shift-Ctrl-/': 'help', }; // Add platform-specific keys Object.assign(defaultKeyMap, mac? macKeyMap : pcKeyMap); // see https://codemirror.net/doc/manual.html#keymaps CodeMirror.normalizeKeyMap(defaultKeyMap); export const commandMap = { prevFocus : function (_, e) { e.preventDefault(); this.toolbarRef.current.primitiveSearch.focus(); }, prevNode : function (_, e) { e.preventDefault(); if(this.node) { let prev = this.fastSkip(node => node.prev); if (prev) { return this.activateByNid(prev.nid); } } const prevNode = this.cur && this.ast.getNodeBeforeCur(this.cur); return prevNode? this.activateByNid(prevNode.nid, {allowMove: true}) : playSound(BEEP); }, nextNode : function (_, e) { e.preventDefault(); if(this.node) { let next = this.fastSkip(node => node.next); if (next) { return this.activateByNid(next.nid); } } const nextNode = this.cur && this.ast.getNodeAfterCur(this.cur); return nextNode? this.activateByNid(nextNode.nid, {allowMove: true}) : playSound(BEEP); }, firstNode : function (_) { Iif(!this.node) { return CodeMirror.Pass; } this.activateByNid(0, {allowMove: true}); }, lastVisibleNode : function (_) { Iif(!this.node) { return CodeMirror.Pass; } else { this.activateByNid(getLastVisibleNode(this.state).nid); } }, jumpToRoot : function (_) { Iif(!this.node) { return CodeMirror.Pass; } else { this.activateByNid(getRoot(this.node).nid); } }, readAncestors : function (_) { if(!this.node) { return CodeMirror.Pass; } const parents = [this.node.options['aria-label']]; let next = this.node.parent; while (next) { parents.push(next.options['aria-label'] + ", at level " + next.level); next = next.parent; } if (parents.length > 1) { say(parents.join(", inside ")); } else { playSound(BEEP); } }, readChildren : function (_) { if(!this.node) { return CodeMirror.Pass; } else { say(this.node.describe(this.node.level)); } }, collapseAll : function (_) { Iif(!this.node) { return CodeMirror.Pass; } this.dispatch({type: 'COLLAPSE_ALL'}); this.activateByNid(getRoot(this.node).nid); }, collapseOrSelectParent : function(_) { Iif(!this.node) { return CodeMirror.Pass; } if (this.expandable && !this.isCollapsed && !this.isLocked()) { this.collapse(this.node.id); } else Iif (this.node.parent) { this.activateByNid(this.node.parent.nid); } else { playSound(BEEP); } }, collapseCurrentRoot : function (_) { Iif(!this.node) { return CodeMirror.Pass; } Iif(!this.node.parent && (this.isCollapsed || !this.expandable)) { playSound(BEEP); } else { let root = getRoot(this.node); let descendants = [...root.descendants()]; descendants.forEach(d => this.collapse(d.id)); this.activateByNid(root.nid); } }, expandAll : function (_) { Iif(!this.node) { return CodeMirror.Pass; } else { return this.dispatch({type: 'UNCOLLAPSE_ALL'}); } }, expandOrSelectFirstChild : function (_) { Iif(!this.node) { return CodeMirror.Pass; } const node = this.node; if (this.expandable && this.isCollapsed && !this.isLocked()) { this.uncollapse(node.id); } else Eif (node.next?.parent === node) { this.activateByNid(node.next.nid); } else { playSound(BEEP); } }, expandCurrentRoot : function (_) { Iif(!this.node) { return CodeMirror.Pass; } let root = getRoot(this.node); [...root.descendants()].forEach(d => this.uncollapse(d.id)); this.activateByNid(root.nid); }, toggleSelection : function (_, e) { Iif(!this.node) { return CodeMirror.Pass; } e.preventDefault(); const node = this.node; if (this.selections.includes(this.node.id)) { this.dispatch({ type: 'SET_SELECTIONS', selections: this.selections.filter(s => s !== node.id) }); // TODO(Emmanuel): announce removal } else { const isContained = id => this.ast.isAncestor(node.id, id); const doesContain = id => this.ast.isAncestor(id, node.id); const [removed, newSelections] = partition(this.selections, isContained); for (const r of removed) { // TODO(Emmanuel): announce removal } if (newSelections.some(doesContain)) { // TODO(Emmanuel): announce failure } else { // TODO(Emmanuel): announce addition newSelections.push(this.node.id); this.dispatch({ type: 'SET_SELECTIONS', selections: newSelections }); } } }, edit : function (_, e) { if(!this.node) { return CodeMirror.Pass; } Eif (this.normallyEditable) { this.handleMakeEditable(e); e.preventDefault(); } else if (this.expandable && !this.isLocked()) { (this.isCollapsed ? this.uncollapse : this.collapse)(this.node.id); } else { playSound(BEEP); } }, editAnything : function (_, e) { if(!this.node) { return CodeMirror.Pass; } else { return this.handleMakeEditable(e); } }, clearSelection : function (_) { Iif(!this.node) { return CodeMirror.Pass; } this.dispatch({type: 'SET_SELECTIONS', selections: []}); }, deleteNodes : function (_) { Iif(!this.node) { return CodeMirror.Pass; } if(!this.selections.length) { return say('Nothing selected'); } const nodesToDelete = this.selections.map(this.ast.getNodeById); delete_(nodesToDelete, "deleted"); }, // use the srcRange() to insert before/after the node *and* // any associated comments insertRight : function (_) { Iif(!this.node) { return CodeMirror.Pass; } if(!this.setRight()) { this.setCursor(this.node.srcRange().to); } }, insertLeft : function (_) { if(!this.node) { return CodeMirror.Pass; } Eif(!this.setLeft()) { this.setCursor(this.node.srcRange().from); } }, cut : function (_) { Iif(!this.node) { return CodeMirror.Pass; } Iif(!this.selections.length) { return say('Nothing selected'); } const nodesToCut = this.selections.map(this.ast.getNodeById); copy(nodesToCut, "cut"); delete_(nodesToCut); }, copy : function (_) { if(!this.node) { return CodeMirror.Pass; } // if no nodes are selected, do it on focused node's id instead const nodeIds = !this.selections.length? [this.node.id] : this.selections; const nodesToCopy = nodeIds.map(this.ast.getNodeById); copy(nodesToCopy, "copied"); }, paste : function (_, e) { if(!this.node) { return CodeMirror.Pass; } const node = this.node; if (this.selections.includes(this.node.id)) { paste(new ReplaceNodeTarget(this.node)); } else if (this.node.parent) { // We're inside the AST somewhere. Try to paste to the left/right. const pos = e.shiftKey ? node.srcRange().from : node.srcRange().to; const target = new InsertTarget(this.context.node, this.context.field, pos); if (target) { paste(target); } else { say(`Cannot paste ${(e.shiftKey ? "before" : "after")} this node.`); } } else { // We're at a root node. Insert to the left or right, at the top level. const pos = e.shiftKey ? node.srcRange().from : node.srcRange().to; paste(new OverwriteTarget(pos, pos)); } }, activateSearchDialog : function (_) { SHARED.search.onSearch( this.state, () => {}, () => this.activateNoRecord(SHARED.search.search(true, this.state)) ); }, searchPrevious : function (_, e) { e.preventDefault(); this.activateNoRecord(SHARED.search.search(false, this.state)); }, searchNext : function (_, e) { e.preventDefault(); this.activateNoRecord(SHARED.search.search(true, this.state)); }, undo : function(_, e) { e.preventDefault(); if(this.node) { preambleUndoRedo('undo'); SHARED.cm.undo(); } }, redo : function(_, e) { e.preventDefault(); Eif(this.node) { preambleUndoRedo('redo'); SHARED.cm.redo(); } }, help : function (_) { this.showDialog(renderKeyMap(this.keyMap)); } }; // Recieves the key event, an environment (BlockEditor or Node), and the // editor's keyMap. If there is a handler for that event, flatten the // environment and add some utility methods, then set the key handler's // "this" object to be that environment and call it. export function keyDown(e, env, keyMap) { var handler = commandMap[keyMap[CodeMirror.keyName(e)]]; if(handler) { e.stopPropagation(); env.props.dispatch((_, getState) => { // set up the environment const state = getState(); const {ast, selections} = state; Object.assign(env, env.props, {ast, selections, state}); // add convenience methods env.fastSkip = function (next) { return skipCollapsed(env.node, next, state); }; env.activate = (n, options={allowMove: true, record: true}) => { if (n === null) { playSound(BEEP); } env.props.activateByNid((n === undefined)? env.node.nid : n.nid, options); }; env.activateNoRecord = node => { if(!node) { return playSound(BEEP); } // nothing to activate env.dispatch(activateByNid(node.nid, {record: false, allowMove: true})); }; // If there's a node, make sure it's fresh if(env.node) { env.node = env.ast.getNodeByNId(env.ast.getNodeById(env.node.id).nid) } }); handler = handler.bind(env); return handler(SHARED.cm, e); } } export function renderKeyMap(keyMap) { const reverseMap = {}; Object.keys(keyMap).forEach(key => { if(!reverseMap[keyMap[key]]) { reverseMap[keyMap[key]] = key; } else reverseMap[keyMap[key]] = reverseMap[keyMap[key]] + " or " + key; }); return ( <> <h1>Blocks Shortcuts</h1> <span style={{display: 'block'}}>(Note: on MacOS, <kbd>Cmd</kbd> replaces <kbd>Ctrl</kbd>.)</span> <span className="screenreader"> Screenreader users: Make sure to either increase the verbosity of your screenreader, or character over the shortcut column in the tables below. Some shortcuts use punctuation keys that may not always be spoken. </span> <div className="shortcutGroup" tabIndex="-1"> <h2>Navigation</h2> <table className="shortcuts"> <thead><tr><th>Command</th><th>Shortcut</th></tr></thead> <tbody> <tr><td>Previous Block</td><td><kbd>{reverseMap['prevNode']}</kbd></td></tr> <tr><td>Next Block</td><td><kbd>{reverseMap['nextNode']}</kbd></td></tr> <tr><td>Collapse Block</td><td><kbd>{reverseMap['collapseOrSelectParent']}</kbd></td></tr> <tr><td>Expand Block</td><td><kbd>{reverseMap['expandOrSelectFirstChild']}</kbd></td></tr> <tr><td>Collapse Root</td><td><kbd>{reverseMap['collapseCurrentRoot']}</kbd></td></tr> <tr><td>Expand Root</td><td><kbd>{reverseMap['expandCurrentRoot']}</kbd></td></tr> <tr><td>Collapse All</td><td><kbd>{reverseMap['collapseAll']}</kbd></td></tr> <tr><td>Expand All</td><td><kbd>{reverseMap['expandAll']}</kbd></td></tr> <tr><td>First Visible Block</td><td><kbd>{reverseMap['firstNode']}</kbd></td></tr> <tr><td>Last Visible Block</td><td><kbd>{reverseMap['lastVisibleNode']}</kbd></td></tr> <tr><td>Read Ancestors</td><td><kbd>{reverseMap['readAncestors']}</kbd></td></tr> <tr><td>Read Block and Children</td><td><kbd>{reverseMap['readChildren']}</kbd></td></tr> </tbody> </table> </div> <div className="shortcutGroup" tabIndex="-1"> <h2>Editing</h2> <table className="shortcuts"> <thead><tr><th>Command</th><th>Shortcut</th></tr></thead> <tbody> <tr><td>Edit a Literal</td><td><kbd>{reverseMap['edit']}</kbd></td></tr> <tr><td>Edit any Block</td><td><kbd>{reverseMap['editAnything']}</kbd></td></tr> <tr><td>Cancel</td><td><kbd>{reverseMap['cancel']}</kbd></td></tr> <tr><td>Insert Before</td><td><kbd>{reverseMap['insertLeft']}</kbd></td></tr> <tr><td>Insert After</td><td><kbd>{reverseMap['insertRight']}</kbd></td></tr> </tbody> </table> </div> <div className="shortcutGroup" tabIndex="-1"> <h2>Search</h2> <table className="shortcuts"> <thead><tr><th>Command</th><th>Shortcut</th></tr></thead> <tbody> <tr><td>Enter Search Mode</td><td><kbd>{reverseMap['activateSearchDialog']}</kbd></td></tr> <tr><td>Exit Search Mode</td><td><kbd>{reverseMap['cancel']}</kbd></td></tr> <tr><td>Find Next</td><td><kbd>{reverseMap['searchNext']}</kbd></td></tr> <tr><td>Find Previous</td><td><kbd>{reverseMap['searchPrevious']}</kbd></td></tr> </tbody> </table> </div> <div className="shortcutGroup" tabIndex="-1"> <h2>Selection and Clipboard</h2> <table className="shortcuts"> <thead><tr><th>Command</th><th>Shortcut</th></tr></thead> <tbody> <tr><td>Toggle Selection</td><td><kbd>{reverseMap['toggleSelection']}</kbd></td></tr> <tr><td>Cut </td><td><kbd>{reverseMap['cut']}</kbd></td></tr> <tr><td>Copy</td><td><kbd>{reverseMap['copy']}</kbd></td></tr> <tr><td>Paste After Block</td><td><kbd>{reverseMap['paste']}</kbd></td></tr> <tr><td>Paste Before Block</td><td><kbd>{reverseMap['pasteBefore']}</kbd></td></tr> <tr><td>Delete Selection</td><td><kbd>{reverseMap['delete']}</kbd></td></tr> </tbody> </table> </div> </> ); } |