-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathKeyboardOverlayMode.js
More file actions
216 lines (199 loc) · 8.01 KB
/
KeyboardOverlayMode.js
File metadata and controls
216 lines (199 loc) · 8.01 KB
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
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2013 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// @INCLUDE_IN_API_DOCS
/*global Phoenix*/
/**
* This handles the overlay mode
*/
define(function (require, exports, module) {
const EditorManager = require("editor/EditorManager"),
AppInit = require("utils/AppInit"),
MainViewManager = require("view/MainViewManager"),
Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
Menus = require("command/Menus"),
Strings = require("strings"),
Metrics = require("utils/Metrics"),
Keys = require("command/Keys");
const CONTROL_NAV_OVERLAY_ID = "ctrl-nav-overlay";
let overlay;
let paneToFocusOnExit, overlayMode = false,
overlayOrderCentralElement, currentOverlayElement;
function showOverlay(targetId) {
// Find the target div and the overlay div
Metrics.countEvent(Metrics.EVENT_TYPE.UI, "palette", "showOverlay");
if(!targetId){
console.error("No target ID for selecting overlay. Ignoring");
return;
}
const targetElement = document.getElementById(targetId);
paneToFocusOnExit = MainViewManager.getActivePaneId();
if (targetElement && overlay) {
// Get the position and dimensions of the target div
const rect = targetElement.getBoundingClientRect();
// Set the overlay div's styles to match the target's dimensions and position
overlay.style.left = rect.left + 'px';
overlay.style.top = rect.top + 'px';
overlay.style.width = rect.width + 'px';
overlay.style.height = rect.height + 'px';
overlay.classList.remove('forced-hidden'); // Remove the class that hides the overlay
overlay.classList.add('hide-cursor'); // Remove the class that hides the overlay
overlay.focus();
overlayMode = true;
Menus.closeAll();
document.addEventListener('click', exitOverlayMode, true);
}
}
/**
* Responsible to start the overlay mode
*/
function startOverlayMode() {
overlayOrderCentralElement = calculateUINavOrder();
currentOverlayElement = overlayOrderCentralElement;
showOverlay(overlayOrderCentralElement.htmlID);
}
const ELEM_TYPE_PANE = "pane",
ELEM_TYPE_TOP_MENU = "topMenu";
function addElementUp(element, upElement) {
element.up = upElement;
upElement.down = element;
}
function addElementRight(element, rightElement) {
element.right = rightElement;
rightElement.left = element;
}
function calculateUINavOrder() {
const firstPane = {
type: ELEM_TYPE_PANE,
htmlID: MainViewManager.FIRST_PANE
};
const secondPane = {
type: ELEM_TYPE_PANE,
htmlID: MainViewManager.SECOND_PANE
};
addElementUp(firstPane, {type: ELEM_TYPE_TOP_MENU});
const paneLayout = MainViewManager.getLayoutScheme();
if(paneLayout.rows === 2){
addElementUp(secondPane, firstPane);
} else if(paneLayout.columns === 2){
addElementRight(firstPane, secondPane);
addElementUp(secondPane, {type: ELEM_TYPE_TOP_MENU});
}
const startingPane = MainViewManager.getActivePaneId() || "first-pane";
if(startingPane === MainViewManager.FIRST_PANE){
return firstPane;
}
return secondPane;
}
/**
* Responsible to exit the overlay mode.
* restores focus to previously active pane
*/
function exitOverlayMode() {
const overlay = document.getElementById(CONTROL_NAV_OVERLAY_ID);
overlay.classList.add('forced-hidden'); // Remove the class that hides the overlay
overlayMode = false;
if(paneToFocusOnExit){
MainViewManager.setActivePaneId(paneToFocusOnExit);
}
document.removeEventListener('click', exitOverlayMode, true);
}
/**
* Handles the keyboard navigation in overlay mode
* Process the arrow keys to move between panes, Enter to select a pane, and Escape to exit overlay mode
*
* @param {KeyboardEvent} event
*/
function processOverlayKeyboardEvent(event) {
const upElement = currentOverlayElement.up;
const downElement = currentOverlayElement.down;
const leftElement = currentOverlayElement.left;
const rightElement = currentOverlayElement.right;
switch (event.key) {
case Keys.KEY.ARROW_UP:
if(upElement && upElement.type === ELEM_TYPE_TOP_MENU){
exitOverlayMode();
Menus.openMenu();
} else if(upElement && upElement.type === ELEM_TYPE_PANE){
currentOverlayElement = upElement;
showOverlay(upElement.htmlID);
}
break;
case Keys.KEY.ARROW_DOWN:
if(downElement && downElement.type === ELEM_TYPE_PANE){
currentOverlayElement = downElement;
showOverlay(downElement.htmlID);
}
break;
case Keys.KEY.ARROW_LEFT:
if(leftElement && leftElement.type === ELEM_TYPE_PANE){
currentOverlayElement = leftElement;
showOverlay(leftElement.htmlID);
}
break;
case Keys.KEY.ARROW_RIGHT:
if(rightElement && rightElement.type === ELEM_TYPE_PANE){
currentOverlayElement = rightElement;
showOverlay(rightElement.htmlID);
}
break;
case Keys.KEY.RETURN:
case Keys.KEY.ENTER:
if(currentOverlayElement && currentOverlayElement.type === ELEM_TYPE_PANE){
MainViewManager.setActivePaneId(currentOverlayElement.htmlID);
paneToFocusOnExit = MainViewManager.getActivePaneId();
exitOverlayMode();
}
break;
case Keys.KEY.ESCAPE:
default:
exitOverlayMode();
break;
}
event.stopPropagation();
event.preventDefault();
return true;
}
/**
* to check whether in overlay mode or not
*
* @returns {boolean} returns true if in overlay mode otherwise false
*/
function isInOverlayMode() {
return overlayMode;
}
AppInit.htmlReady(function () {
overlay = document.getElementById(CONTROL_NAV_OVERLAY_ID);
const overlayTextElement = document.getElementById("overlay-instruction-text");
overlayTextElement.textContent = Strings.KEYBOARD_OVERLAY_TEXT;
});
AppInit.appReady(function () {
CommandManager.register(Strings.CMD_KEYBOARD_NAV_OVERLAY,
Commands.CMD_KEYBOARD_NAV_UI_OVERLAY, startOverlayMode);
const viewMenu = Menus.getMenu(Menus.AppMenuBar.VIEW_MENU);
viewMenu.addMenuItem(Commands.CMD_KEYBOARD_NAV_UI_OVERLAY, 'Ctrl-P',
Menus.AFTER, Commands.VIEW_TOGGLE_INSPECTION);
});
exports.processOverlayKeyboardEvent = processOverlayKeyboardEvent;
exports.startOverlayMode = startOverlayMode;
exports.exitOverlayMode = exitOverlayMode;
exports.isInOverlayMode = isInOverlayMode;
});