mirror of
https://github.com/google/blockly.git
synced 2026-01-07 17:10:11 +01:00
feat: add getFirstNode and getLastNode to cursor with tests (#8878)
* feat: add getFirstNode and getlastNode to line_cursor.ts * chore: add simple tests for getFirstNode and getLastNode * chore: broken tests for debugging * chore: additional cursor tests * chore: lint, format, reenable tasks
This commit is contained in:
@@ -756,6 +756,43 @@ export class LineCursor extends Marker {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first navigable node on the workspace, or null if none exist.
|
||||
*
|
||||
* @returns The first navigable node on the workspace, or null.
|
||||
*/
|
||||
getFirstNode(): ASTNode | null {
|
||||
const topBlocks = this.workspace.getTopBlocks(true);
|
||||
if (!topBlocks.length) return null;
|
||||
return ASTNode.createTopNode(topBlocks[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last navigable node on the workspace, or null if none exist.
|
||||
*
|
||||
* @returns The last navigable node on the workspace, or null.
|
||||
*/
|
||||
getLastNode(): ASTNode | null {
|
||||
// Loop back to last block if it exists.
|
||||
const topBlocks = this.workspace.getTopBlocks(true);
|
||||
if (!topBlocks.length) return null;
|
||||
|
||||
// Find the last stack.
|
||||
const lastTopBlockNode = ASTNode.createStackNode(
|
||||
topBlocks[topBlocks.length - 1],
|
||||
);
|
||||
let prevNode = lastTopBlockNode;
|
||||
let nextNode: ASTNode | null = lastTopBlockNode;
|
||||
// Iterate until you fall off the end of the stack.
|
||||
while (nextNode) {
|
||||
prevNode = nextNode;
|
||||
nextNode = this.getNextNode(prevNode, (node) => {
|
||||
return !!node;
|
||||
});
|
||||
}
|
||||
return prevNode;
|
||||
}
|
||||
}
|
||||
|
||||
registry.register(registry.Type.CURSOR, registry.DEFAULT, LineCursor);
|
||||
|
||||
Reference in New Issue
Block a user