|
| 1 | + |
| 2 | +/** |
| 3 | + * @license |
| 4 | + * Copyright 2024 Google LLC |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +import { LineCursor } from '@blockly/keyboard-navigation'; |
| 9 | +import { ASTNode } from 'blockly'; |
| 10 | + |
| 11 | +export class ExtendedLineCursor extends LineCursor { |
| 12 | + |
| 13 | + nextSibling() { |
| 14 | + const curNode = this.getCurNode(); |
| 15 | + if (!curNode) { |
| 16 | + return null; |
| 17 | + } |
| 18 | + let newNode = null; |
| 19 | + switch (curNode.type) { |
| 20 | + case ASTNode.types.STACK: { |
| 21 | + newNode = curNode.navigateBetweenStacks(true); |
| 22 | + break; |
| 23 | + } |
| 24 | + case ASTNode.types.WORKSPACE: { |
| 25 | + break; |
| 26 | + } |
| 27 | + default: { |
| 28 | + const block = curNode.getSourceBlock(); |
| 29 | + const nextBlock = block.getNextBlock(); |
| 30 | + newNode = ASTNode.createBlockNode(nextBlock); |
| 31 | + break; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + if (newNode) { |
| 36 | + this.setCurNode(newNode); |
| 37 | + } |
| 38 | + return newNode; |
| 39 | + } |
| 40 | + |
| 41 | + previousSibling() { |
| 42 | + |
| 43 | + const curNode = this.getCurNode(); |
| 44 | + if (!curNode) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + let newNode = null; |
| 48 | + switch (curNode.type) { |
| 49 | + case ASTNode.types.STACK: { |
| 50 | + newNode = curNode.navigateBetweenStacks(false); |
| 51 | + break; |
| 52 | + } |
| 53 | + case ASTNode.types.WORKSPACE: { |
| 54 | + break; |
| 55 | + } |
| 56 | + default: { |
| 57 | + const block = curNode.getSourceBlock(); |
| 58 | + // TODO: Decide what this should do if the source block is |
| 59 | + // the first block inside a statement input. |
| 60 | + // TODO: Decide what this should do if the source block |
| 61 | + // has an output instead of a previous. |
| 62 | + const prevBlock = block.getPreviousBlock(); |
| 63 | + newNode = ASTNode.createBlockNode(prevBlock); |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (newNode) { |
| 69 | + this.setCurNode(newNode); |
| 70 | + } |
| 71 | + return newNode; |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + contextOut() { |
| 76 | + const curNode = this.getCurNode(); |
| 77 | + if (!curNode) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + // Returns null at the workspace level. |
| 82 | + // TODO: Decide where the cursor goes from the workspace level. |
| 83 | + let newNode = curNode.out(); |
| 84 | + if (newNode) { |
| 85 | + this.setCurNode(newNode); |
| 86 | + } |
| 87 | + return newNode; |
| 88 | + } |
| 89 | + |
| 90 | + contextIn() { |
| 91 | + let curNode = this.getCurNode(); |
| 92 | + if (!curNode) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + // If we are on a previous or output connection, go to the block level |
| 96 | + // before performing next operation. |
| 97 | + if ( |
| 98 | + curNode.getType() === ASTNode.types.PREVIOUS || |
| 99 | + curNode.getType() === ASTNode.types.OUTPUT |
| 100 | + ) { |
| 101 | + curNode = curNode.next(); |
| 102 | + } |
| 103 | + const newNode = curNode?.in() ?? null; |
| 104 | + |
| 105 | + if (newNode) { |
| 106 | + this.setCurNode(newNode); |
| 107 | + } |
| 108 | + return newNode; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export function installCursor(markerManager) { |
| 113 | + const oldCurNode = markerManager.getCursor().getCurNode(); |
| 114 | + const lineCursor = new ExtendedLineCursor(); |
| 115 | + markerManager.setCursor(lineCursor); |
| 116 | + if (oldCurNode) { |
| 117 | + markerManager.getCursor().setCurNode(oldCurNode); |
| 118 | + } |
| 119 | +} |
0 commit comments