mirror of
https://github.com/google/blockly.git
synced 2026-01-10 02:17:09 +01:00
Ws cursor move (#3509)
* Add actions to move the cursor around the workspace
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
goog.provide('Blockly.BasicCursor');
|
||||
|
||||
goog.require('Blockly.ASTNode');
|
||||
goog.require('Blockly.Cursor');
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -161,6 +161,14 @@ Blockly.user.keyMap.createDefaultKeyMap = function() {
|
||||
var map = {};
|
||||
var controlK = Blockly.user.keyMap.createSerializedKey(
|
||||
Blockly.utils.KeyCodes.K, [Blockly.user.keyMap.modifierKeys.CONTROL]);
|
||||
var shiftW = Blockly.user.keyMap.createSerializedKey(
|
||||
Blockly.utils.KeyCodes.W, [Blockly.user.keyMap.modifierKeys.SHIFT]);
|
||||
var shiftA = Blockly.user.keyMap.createSerializedKey(
|
||||
Blockly.utils.KeyCodes.A, [Blockly.user.keyMap.modifierKeys.SHIFT]);
|
||||
var shiftS = Blockly.user.keyMap.createSerializedKey(
|
||||
Blockly.utils.KeyCodes.S, [Blockly.user.keyMap.modifierKeys.SHIFT]);
|
||||
var shiftD = Blockly.user.keyMap.createSerializedKey(
|
||||
Blockly.utils.KeyCodes.D, [Blockly.user.keyMap.modifierKeys.SHIFT]);
|
||||
|
||||
map[Blockly.utils.KeyCodes.W] = Blockly.navigation.ACTION_PREVIOUS;
|
||||
map[Blockly.utils.KeyCodes.A] = Blockly.navigation.ACTION_OUT;
|
||||
@@ -173,5 +181,9 @@ Blockly.user.keyMap.createDefaultKeyMap = function() {
|
||||
map[Blockly.utils.KeyCodes.E] = Blockly.navigation.ACTION_EXIT;
|
||||
map[Blockly.utils.KeyCodes.ESC] = Blockly.navigation.ACTION_EXIT;
|
||||
map[controlK] = Blockly.navigation.ACTION_TOGGLE_KEYBOARD_NAV;
|
||||
map[shiftW] = Blockly.navigation.ACTION_MOVE_WS_CURSOR_UP;
|
||||
map[shiftA] = Blockly.navigation.ACTION_MOVE_WS_CURSOR_LEFT;
|
||||
map[shiftS] = Blockly.navigation.ACTION_MOVE_WS_CURSOR_DOWN;
|
||||
map[shiftD] = Blockly.navigation.ACTION_MOVE_WS_CURSOR_RIGHT;
|
||||
return map;
|
||||
};
|
||||
|
||||
@@ -45,21 +45,31 @@ Blockly.navigation.loggingCallback = null;
|
||||
/**
|
||||
* State indicating focus is currently on the flyout.
|
||||
* @type {number}
|
||||
* @const
|
||||
*/
|
||||
Blockly.navigation.STATE_FLYOUT = 1;
|
||||
|
||||
/**
|
||||
* State indicating focus is currently on the workspace.
|
||||
* @type {number}
|
||||
* @const
|
||||
*/
|
||||
Blockly.navigation.STATE_WS = 2;
|
||||
|
||||
/**
|
||||
* State indicating focus is currently on the toolbox.
|
||||
* @type {number}
|
||||
* @const
|
||||
*/
|
||||
Blockly.navigation.STATE_TOOLBOX = 3;
|
||||
|
||||
/**
|
||||
* The distance to move the cursor on the workspace.
|
||||
* @type {number}
|
||||
* @const
|
||||
*/
|
||||
Blockly.navigation.WS_MOVE_DISTANCE = 40;
|
||||
|
||||
/**
|
||||
* The current state the user is in.
|
||||
* Initialized to workspace state since a user enters navigation mode by shift
|
||||
@@ -83,7 +93,11 @@ Blockly.navigation.actionNames = {
|
||||
DISCONNECT: 'disconnect',
|
||||
TOOLBOX: 'toolbox',
|
||||
EXIT: 'exit',
|
||||
TOGGLE_KEYBOARD_NAV: 'toggle_keyboard_nav'
|
||||
TOGGLE_KEYBOARD_NAV: 'toggle_keyboard_nav',
|
||||
MOVE_WS_CURSOR_UP: 'move_ws_cursor_up',
|
||||
MOVE_WS_CURSOR_DOWN: 'move_ws_cursor_down',
|
||||
MOVE_WS_CURSOR_LEFT: 'move_ws_cursor_left',
|
||||
MOVE_WS_CURSOR_RIGHT: 'move_ws_cursor_right'
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -108,7 +122,6 @@ Blockly.navigation.getMarker = function() {
|
||||
/**
|
||||
* If a toolbox exists, set the navigation state to toolbox and select the first
|
||||
* category in the toolbox.
|
||||
* category.
|
||||
* @private
|
||||
*/
|
||||
Blockly.navigation.focusToolbox_ = function() {
|
||||
@@ -871,6 +884,30 @@ Blockly.navigation.toolboxOnAction_ = function(action) {
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Move the workspace cursor in the given direction.
|
||||
* @param {number} xDirection -1 to move cursor left. 1 to move cursor right.
|
||||
* @param {number} yDirection -1 to move cursor up. 1 to move cursor down.
|
||||
* @return {boolean} True if the current node is a workspace, false otherwise.
|
||||
* @private
|
||||
*/
|
||||
Blockly.navigation.moveWSCursor_ = function(xDirection, yDirection) {
|
||||
var cursor = Blockly.getMainWorkspace().getCursor();
|
||||
var curNode = Blockly.getMainWorkspace().getCursor().getCurNode();
|
||||
|
||||
if (curNode.getType() !== Blockly.ASTNode.types.WORKSPACE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var wsCoord = curNode.getWsCoordinate();
|
||||
var newX = xDirection * Blockly.navigation.WS_MOVE_DISTANCE + wsCoord.x;
|
||||
var newY = yDirection * Blockly.navigation.WS_MOVE_DISTANCE + wsCoord.y;
|
||||
|
||||
cursor.setCurNode(Blockly.ASTNode.createWorkspaceNode(
|
||||
Blockly.getMainWorkspace(), new Blockly.utils.Coordinate(newX, newY)));
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles the given action for the workspace.
|
||||
* @param {!Blockly.Action} action The action to handle.
|
||||
@@ -891,6 +928,14 @@ Blockly.navigation.workspaceOnAction_ = function(action) {
|
||||
case Blockly.navigation.actionNames.DISCONNECT:
|
||||
Blockly.navigation.disconnectBlocks_();
|
||||
return true;
|
||||
case Blockly.navigation.actionNames.MOVE_WS_CURSOR_UP:
|
||||
return Blockly.navigation.moveWSCursor_(0, -1);
|
||||
case Blockly.navigation.actionNames.MOVE_WS_CURSOR_DOWN:
|
||||
return Blockly.navigation.moveWSCursor_(0, 1);
|
||||
case Blockly.navigation.actionNames.MOVE_WS_CURSOR_LEFT:
|
||||
return Blockly.navigation.moveWSCursor_(-1, 0);
|
||||
case Blockly.navigation.actionNames.MOVE_WS_CURSOR_RIGHT:
|
||||
return Blockly.navigation.moveWSCursor_(1, 0);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -996,6 +1041,39 @@ Blockly.navigation.ACTION_TOGGLE_KEYBOARD_NAV = new Blockly.Action(
|
||||
Blockly.navigation.actionNames.TOGGLE_KEYBOARD_NAV,
|
||||
'Turns on and off keyboard navigation.');
|
||||
|
||||
/**
|
||||
* The action to move the cursor to the keft on a worksapce.
|
||||
* @type {!Blockly.Action}
|
||||
*/
|
||||
Blockly.navigation.ACTION_MOVE_WS_CURSOR_LEFT = new Blockly.Action(
|
||||
Blockly.navigation.actionNames.MOVE_WS_CURSOR_LEFT,
|
||||
'Move the workspace cursor to the lefts.');
|
||||
|
||||
/**
|
||||
* The action to move the cursor to the right on a worksapce.
|
||||
* @type {!Blockly.Action}
|
||||
*/
|
||||
Blockly.navigation.ACTION_MOVE_WS_CURSOR_RIGHT = new Blockly.Action(
|
||||
Blockly.navigation.actionNames.MOVE_WS_CURSOR_RIGHT,
|
||||
'Move the workspace cursor to the right.');
|
||||
|
||||
/**
|
||||
* The action to move the cursor up on a worksapce.
|
||||
* @type {!Blockly.Action}
|
||||
*/
|
||||
Blockly.navigation.ACTION_MOVE_WS_CURSOR_UP = new Blockly.Action(
|
||||
Blockly.navigation.actionNames.MOVE_WS_CURSOR_UP,
|
||||
'Move the workspace cursor up.');
|
||||
|
||||
/**
|
||||
* The action to move the cursor down on a worksapce.
|
||||
* @type {!Blockly.Action}
|
||||
*/
|
||||
Blockly.navigation.ACTION_MOVE_WS_CURSOR_DOWN = new Blockly.Action(
|
||||
Blockly.navigation.actionNames.MOVE_WS_CURSOR_DOWN,
|
||||
'Move the workspace cursor down.');
|
||||
|
||||
|
||||
/**
|
||||
* List of actions that can be performed in read only mode.
|
||||
* @type {!Array.<!Blockly.Action>}
|
||||
|
||||
Reference in New Issue
Block a user