-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathcommand_bar.js
666 lines (586 loc) · 21.2 KB
/
command_bar.js
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/**
* @typedef {import("../editor").Editor} Editor
* @typedef {import("../../ace-internal").Ace.TooltipCommand} TooltipCommand
*/
var Tooltip = require("../tooltip").Tooltip;
var EventEmitter = require("../lib/event_emitter").EventEmitter;
var lang = require("../lib/lang");
var dom = require("../lib/dom");
var oop = require("../lib/oop");
var useragent = require("../lib/useragent");
var BUTTON_CLASS_NAME = 'command_bar_tooltip_button';
var VALUE_CLASS_NAME = 'command_bar_button_value';
var CAPTION_CLASS_NAME = 'command_bar_button_caption';
var KEYBINDING_CLASS_NAME = 'command_bar_keybinding';
var TOOLTIP_CLASS_NAME = 'command_bar_tooltip';
var MORE_OPTIONS_BUTTON_ID = 'MoreOptionsButton';
var defaultDelay = 100;
var defaultMaxElements = 4;
var minPosition = function (posA, posB) {
if (posB.row > posA.row) {
return posA;
} else if (posB.row === posA.row && posB.column > posA.column) {
return posA;
}
return posB;
};
var keyDisplayMap = {
"Ctrl": { mac: "^"},
"Option": { mac: "⌥"},
"Command": { mac: "⌘"},
"Cmd": { mac: "⌘"},
"Shift": "⇧",
"Left": "←",
"Right": "→",
"Up": "↑",
"Down": "↓"
};
/**
* Displays a command tooltip above the currently active line selection, with clickable elements.
*
* Internally it is a composite of two tooltips, one for the main tooltip and one for the
* overflowing commands.
* The commands are added sequentially in registration order.
* When attached to an editor, it is either always shown or only when the active line is hovered
* with mouse, depending on the alwaysShow property.
*/
class CommandBarTooltip {
/**
* @param {HTMLElement} parentNode
* @param {Partial<import("../../ace-internal").Ace.CommandBarOptions>} [options]
*/
constructor(parentNode, options) {
options = options || {};
this.parentNode = parentNode;
this.tooltip = new Tooltip(this.parentNode);
this.moreOptions = new Tooltip(this.parentNode);
this.maxElementsOnTooltip = options.maxElementsOnTooltip || defaultMaxElements;
this.$alwaysShow = options.alwaysShow || false;
this.eventListeners = {};
this.elements = {};
this.commands = {};
this.tooltipEl = dom.buildDom(['div', { class: TOOLTIP_CLASS_NAME }], this.tooltip.getElement());
this.moreOptionsEl = dom.buildDom(['div', { class: TOOLTIP_CLASS_NAME + " tooltip_more_options" }], this.moreOptions.getElement());
this.$showTooltipTimer = lang.delayedCall(this.$showTooltip.bind(this), options.showDelay || defaultDelay);
this.$hideTooltipTimer = lang.delayedCall(this.$hideTooltip.bind(this), options.hideDelay || defaultDelay);
this.$tooltipEnter = this.$tooltipEnter.bind(this);
this.$onMouseMove = this.$onMouseMove.bind(this);
this.$onChangeScroll = this.$onChangeScroll.bind(this);
this.$onEditorChangeSession = this.$onEditorChangeSession.bind(this);
this.$scheduleTooltipForHide = this.$scheduleTooltipForHide.bind(this);
this.$preventMouseEvent = this.$preventMouseEvent.bind(this);
for (var event of ["mousedown", "mouseup", "click"]) {
this.tooltip.getElement().addEventListener(event, this.$preventMouseEvent);
this.moreOptions.getElement().addEventListener(event, this.$preventMouseEvent);
}
}
/**
* Registers a command on the command bar tooltip.
*
* The commands are added in sequential order. If there is not enough space on the main
* toolbar, the remaining elements are added to the overflow menu.
*
* @param {string} id
* @param {TooltipCommand} command
*/
registerCommand(id, command) {
var registerForMainTooltip = Object.keys(this.commands).length < this.maxElementsOnTooltip;
if (!registerForMainTooltip && !this.elements[MORE_OPTIONS_BUTTON_ID]) {
this.$createCommand(MORE_OPTIONS_BUTTON_ID, {
name: "···",
exec:
/**
* @this {CommandBarTooltip}
*/
function() {
this.$shouldHideMoreOptions = false;
this.$setMoreOptionsVisibility(!this.isMoreOptionsShown());
}.bind(this),
type: "checkbox",
getValue: function() {
return this.isMoreOptionsShown();
}.bind(this),
enabled: true
}, true);
}
this.$createCommand(id, command, registerForMainTooltip);
if (this.isShown()) {
this.updatePosition();
}
}
isShown() {
return !!this.tooltip && this.tooltip.isOpen;
}
isMoreOptionsShown() {
return !!this.moreOptions && this.moreOptions.isOpen;
}
getAlwaysShow() {
return this.$alwaysShow;
}
/**
* Sets the display mode of the tooltip
*
* When true, the tooltip is always displayed while it is attached to an editor.
* When false, the tooltip is displayed only when the mouse hovers over the active editor line.
*
* @param {boolean} alwaysShow
*/
setAlwaysShow(alwaysShow) {
this.$alwaysShow = alwaysShow;
this.$updateOnHoverHandlers(!this.$alwaysShow);
this._signal("alwaysShow", this.$alwaysShow);
}
/**
* Attaches the clickable command bar tooltip to an editor
*
* Depending on the alwaysShow parameter it either displays the tooltip immediately,
* or subscribes to the necessary events to display the tooltip on hover.
*
* @param {Editor} editor
*/
attach(editor) {
if (!editor || (this.isShown() && this.editor === editor)) {
return;
}
this.detach();
this.editor = editor;
this.editor.on("changeSession", this.$onEditorChangeSession);
if (this.editor.session) {
this.editor.session.on("changeScrollLeft", this.$onChangeScroll);
this.editor.session.on("changeScrollTop", this.$onChangeScroll);
}
if (this.getAlwaysShow()) {
this.$showTooltip();
} else {
this.$updateOnHoverHandlers(true);
}
}
/**
* Updates the position of the command bar tooltip. It aligns itself above the active line in the editor.
*/
updatePosition() {
if (!this.editor) {
return;
}
var renderer = this.editor.renderer;
var ranges;
if (this.editor.selection.getAllRanges) {
ranges = this.editor.selection.getAllRanges();
} else {
ranges = [this.editor.getSelectionRange()];
}
if (!ranges.length) {
return;
}
var minPos = minPosition(ranges[0].start, ranges[0].end);
for (var i = 0, range; range = ranges[i]; i++) {
minPos = minPosition(minPos, minPosition(range.start, range.end));
}
var pos = renderer.$cursorLayer.getPixelPosition(minPos, true);
var tooltipEl = this.tooltip.getElement();
var screenWidth = window.innerWidth;
var screenHeight = window.innerHeight;
var rect = this.editor.container.getBoundingClientRect();
pos.top += rect.top - renderer.layerConfig.offset;
pos.left += rect.left + renderer.gutterWidth - renderer.scrollLeft;
var cursorVisible = pos.top >= rect.top && pos.top <= rect.bottom &&
pos.left >= rect.left + renderer.gutterWidth && pos.left <= rect.right;
if (!cursorVisible && this.isShown()) {
this.$hideTooltip();
return;
} else if (cursorVisible && !this.isShown() && this.getAlwaysShow()) {
this.$showTooltip();
return;
}
var top = pos.top - tooltipEl.offsetHeight;
var left = Math.min(screenWidth - tooltipEl.offsetWidth, pos.left);
var tooltipFits = top >= 0 && top + tooltipEl.offsetHeight <= screenHeight &&
left >= 0 && left + tooltipEl.offsetWidth <= screenWidth;
if (!tooltipFits) {
this.$hideTooltip();
return;
}
this.tooltip.setPosition(left, top);
if (this.isMoreOptionsShown()) {
top = top + tooltipEl.offsetHeight;
left = this.elements[MORE_OPTIONS_BUTTON_ID].getBoundingClientRect().left;
var moreOptionsEl = this.moreOptions.getElement();
var screenHeight = window.innerHeight;
if (top + moreOptionsEl.offsetHeight > screenHeight) {
top -= tooltipEl.offsetHeight + moreOptionsEl.offsetHeight;
}
if (left + moreOptionsEl.offsetWidth > screenWidth) {
left = screenWidth - moreOptionsEl.offsetWidth;
}
this.moreOptions.setPosition(left, top);
}
}
/**
* Updates each command element in the tooltip.
*
* This is automatically called on certain events, but can be called manually as well.
*/
update() {
Object.keys(this.elements).forEach(this.$updateElement.bind(this));
}
/**
* Detaches the tooltip from the editor.
*/
detach() {
this.tooltip.hide();
this.moreOptions.hide();
this.$updateOnHoverHandlers(false);
if (this.editor) {
this.editor.off("changeSession", this.$onEditorChangeSession);
if (this.editor.session) {
this.editor.session.off("changeScrollLeft", this.$onChangeScroll);
this.editor.session.off("changeScrollTop", this.$onChangeScroll);
}
}
this.$mouseInTooltip = false;
this.editor = null;
}
destroy() {
if (this.tooltip && this.moreOptions) {
this.detach();
this.tooltip.destroy();
this.moreOptions.destroy();
}
this.eventListeners = {};
this.commands = {};
this.elements = {};
this.tooltip = this.moreOptions = this.parentNode = null;
}
/**
* @param {string} id
* @param {TooltipCommand} command
* @param {boolean} forMainTooltip
*/
$createCommand(id, command, forMainTooltip) {
var parentEl = forMainTooltip ? this.tooltipEl : this.moreOptionsEl;
var keyParts = [];
var bindKey = command.bindKey;
if (bindKey) {
if (typeof bindKey === 'object') {
bindKey = useragent.isMac ? bindKey.mac : bindKey.win;
}
bindKey = bindKey.split("|")[0];
keyParts = bindKey.split("-");
keyParts = keyParts.map(function(key) {
if (keyDisplayMap[key]) {
if (typeof keyDisplayMap[key] === 'string') {
return keyDisplayMap[key];
} else if (useragent.isMac && keyDisplayMap[key].mac) {
return keyDisplayMap[key].mac;
}
}
return key;
});
}
/**@type {any[]} */
var buttonNode;
if (forMainTooltip && command.iconCssClass) {
//Only support icon button for main tooltip, otherwise fall back to text button
buttonNode = [
'div',
{
class: ["ace_icon_svg", command.iconCssClass].join(" "),
"aria-label": command.name + " (" + command.bindKey + ")"
}
];
} else {
buttonNode = [
['div', { class: VALUE_CLASS_NAME }],
['div', { class: CAPTION_CLASS_NAME }, command.name]
];
if (keyParts.length) {
buttonNode.push(
[
'div',
{ class: KEYBINDING_CLASS_NAME },
keyParts.map(function(keyPart) {
return ['div', keyPart];
})
]
);
}
}
// @ts-ignore
dom.buildDom(['div', { class: [BUTTON_CLASS_NAME, command.cssClass || ""].join(" "), ref: id }, buttonNode], parentEl, this.elements);
this.commands[id] = command;
var eventListener =
/**
* @this {CommandBarTooltip}
*/
function(e) {
if (this.editor) {
this.editor.focus();
}
// Internal variable to properly handle when the more options button is clicked
this.$shouldHideMoreOptions = this.isMoreOptionsShown();
if (!this.elements[id].disabled && command.exec) {
command.exec(this.editor);
}
if (this.$shouldHideMoreOptions) {
this.$setMoreOptionsVisibility(false);
}
this.update();
e.preventDefault();
}.bind(this);
this.eventListeners[id] = eventListener;
this.elements[id].addEventListener('click', eventListener.bind(this));
this.$updateElement(id);
}
/**
* @param {boolean} visible
*/
$setMoreOptionsVisibility(visible) {
if (visible) {
this.moreOptions.setTheme(this.editor.renderer.theme);
this.moreOptions.setClassName(TOOLTIP_CLASS_NAME + "_wrapper");
this.moreOptions.show();
this.update();
this.updatePosition();
} else {
this.moreOptions.hide();
}
}
$onEditorChangeSession(e) {
if (e.oldSession) {
e.oldSession.off("changeScrollTop", this.$onChangeScroll);
e.oldSession.off("changeScrollLeft", this.$onChangeScroll);
}
this.detach();
}
$onChangeScroll() {
if (this.editor.renderer && (this.isShown() || this.getAlwaysShow())) {
this.editor.renderer.once("afterRender", this.updatePosition.bind(this));
}
}
$onMouseMove(e) {
if (this.$mouseInTooltip) {
return;
}
var cursorPosition = this.editor.getCursorPosition();
var cursorScreenPosition = this.editor.renderer.textToScreenCoordinates(cursorPosition.row, cursorPosition.column);
var lineHeight = this.editor.renderer.lineHeight;
var isInCurrentLine = e.clientY >= cursorScreenPosition.pageY && e.clientY < cursorScreenPosition.pageY + lineHeight;
if (isInCurrentLine) {
if (!this.isShown() && !this.$showTooltipTimer.isPending()) {
this.$showTooltipTimer.delay();
}
if (this.$hideTooltipTimer.isPending()) {
this.$hideTooltipTimer.cancel();
}
} else {
if (this.isShown() && !this.$hideTooltipTimer.isPending()) {
this.$hideTooltipTimer.delay();
}
if (this.$showTooltipTimer.isPending()) {
this.$showTooltipTimer.cancel();
}
}
}
$preventMouseEvent(e) {
if (this.editor) {
this.editor.focus();
}
e.preventDefault();
}
$scheduleTooltipForHide() {
this.$mouseInTooltip = false;
this.$showTooltipTimer.cancel();
this.$hideTooltipTimer.delay();
}
$tooltipEnter() {
this.$mouseInTooltip = true;
if (this.$showTooltipTimer.isPending()) {
this.$showTooltipTimer.cancel();
}
if (this.$hideTooltipTimer.isPending()) {
this.$hideTooltipTimer.cancel();
}
}
/**
* @param {boolean} [enableHover]
*/
$updateOnHoverHandlers(enableHover) {
var tooltipEl = this.tooltip.getElement();
var moreOptionsEl = this.moreOptions.getElement();
if (enableHover) {
if (this.editor) {
this.editor.on("mousemove", this.$onMouseMove);
this.editor.renderer.getMouseEventTarget().addEventListener("mouseout", this.$scheduleTooltipForHide, true);
}
tooltipEl.addEventListener('mouseenter', this.$tooltipEnter);
tooltipEl.addEventListener('mouseleave', this.$scheduleTooltipForHide);
moreOptionsEl.addEventListener('mouseenter', this.$tooltipEnter);
moreOptionsEl.addEventListener('mouseleave', this.$scheduleTooltipForHide);
} else {
if (this.editor) {
this.editor.off("mousemove", this.$onMouseMove);
this.editor.renderer.getMouseEventTarget().removeEventListener("mouseout", this.$scheduleTooltipForHide, true);
}
tooltipEl.removeEventListener('mouseenter', this.$tooltipEnter);
tooltipEl.removeEventListener('mouseleave', this.$scheduleTooltipForHide);
moreOptionsEl.removeEventListener('mouseenter', this.$tooltipEnter);
moreOptionsEl.removeEventListener('mouseleave', this.$scheduleTooltipForHide);
}
}
$showTooltip() {
if (this.isShown()) {
return;
}
this.tooltip.setTheme(this.editor.renderer.theme);
this.tooltip.setClassName(TOOLTIP_CLASS_NAME + "_wrapper");
this.tooltip.show();
this.update();
this.updatePosition();
this._signal("show");
}
$hideTooltip() {
this.$mouseInTooltip = false;
if (!this.isShown()) {
return;
}
this.moreOptions.hide();
this.tooltip.hide();
this._signal("hide");
}
/**
* @param {string} id
*/
$updateElement(id) {
var command = this.commands[id];
if (!command) {
return;
}
var el = this.elements[id];
var commandEnabled = command.enabled;
if (typeof commandEnabled === 'function') {
commandEnabled = commandEnabled(this.editor);
}
if (typeof command.getValue === 'function') {
var value = command.getValue(this.editor);
if (command.type === 'text') {
el.textContent = value;
} else if (command.type === 'checkbox') {
var domCssFn = value ? dom.addCssClass : dom.removeCssClass;
var isOnTooltip = el.parentElement === this.tooltipEl;
el.ariaChecked = value;
if (isOnTooltip) {
domCssFn(el, "ace_selected");
} else {
el = el.querySelector("." + VALUE_CLASS_NAME);
domCssFn(el, "ace_checkmark");
}
}
}
if (commandEnabled && el.disabled) {
dom.removeCssClass(el, "ace_disabled");
el.ariaDisabled = el.disabled = false;
el.removeAttribute("disabled");
} else if (!commandEnabled && !el.disabled) {
dom.addCssClass(el, "ace_disabled");
el.ariaDisabled = el.disabled = true;
el.setAttribute("disabled", "");
}
}
}
oop.implement(CommandBarTooltip.prototype, EventEmitter);
dom.importCssString(`
.ace_tooltip.${TOOLTIP_CLASS_NAME}_wrapper {
padding: 0;
}
.ace_tooltip .${TOOLTIP_CLASS_NAME} {
padding: 1px 5px;
display: flex;
pointer-events: auto;
}
.ace_tooltip .${TOOLTIP_CLASS_NAME}.tooltip_more_options {
padding: 1px;
flex-direction: column;
}
div.${BUTTON_CLASS_NAME} {
display: inline-flex;
cursor: pointer;
margin: 1px;
border-radius: 2px;
padding: 2px 5px;
align-items: center;
}
div.${BUTTON_CLASS_NAME}.ace_selected,
div.${BUTTON_CLASS_NAME}:hover:not(.ace_disabled) {
background-color: rgba(0, 0, 0, 0.1);
}
div.${BUTTON_CLASS_NAME}.ace_disabled {
color: #777;
pointer-events: none;
}
div.${BUTTON_CLASS_NAME} .ace_icon_svg {
height: 12px;
background-color: #000;
}
div.${BUTTON_CLASS_NAME}.ace_disabled .ace_icon_svg {
background-color: #777;
}
.${TOOLTIP_CLASS_NAME}.tooltip_more_options .${BUTTON_CLASS_NAME} {
display: flex;
}
.${TOOLTIP_CLASS_NAME}.${VALUE_CLASS_NAME} {
display: none;
}
.${TOOLTIP_CLASS_NAME}.tooltip_more_options .${VALUE_CLASS_NAME} {
display: inline-block;
width: 12px;
}
.${CAPTION_CLASS_NAME} {
display: inline-block;
}
.${KEYBINDING_CLASS_NAME} {
margin: 0 2px;
display: inline-block;
font-size: 8px;
}
.${TOOLTIP_CLASS_NAME}.tooltip_more_options .${KEYBINDING_CLASS_NAME} {
margin-left: auto;
}
.${KEYBINDING_CLASS_NAME} div {
display: inline-block;
min-width: 8px;
padding: 2px;
margin: 0 1px;
border-radius: 2px;
background-color: #ccc;
text-align: center;
}
.ace_dark.ace_tooltip .${TOOLTIP_CLASS_NAME} {
background-color: #373737;
color: #eee;
}
.ace_dark div.${BUTTON_CLASS_NAME}.ace_disabled {
color: #979797;
}
.ace_dark div.${BUTTON_CLASS_NAME}.ace_selected,
.ace_dark div.${BUTTON_CLASS_NAME}:hover:not(.ace_disabled) {
background-color: rgba(255, 255, 255, 0.1);
}
.ace_dark div.${BUTTON_CLASS_NAME} .ace_icon_svg {
background-color: #eee;
}
.ace_dark div.${BUTTON_CLASS_NAME}.ace_disabled .ace_icon_svg {
background-color: #979797;
}
.ace_dark .${BUTTON_CLASS_NAME}.ace_disabled {
color: #979797;
}
.ace_dark .${KEYBINDING_CLASS_NAME} div {
background-color: #575757;
}
.ace_checkmark::before {
content: '✓';
}
`, "commandbar.css", false);
exports.CommandBarTooltip = CommandBarTooltip;
exports.TOOLTIP_CLASS_NAME = TOOLTIP_CLASS_NAME;
exports.BUTTON_CLASS_NAME = BUTTON_CLASS_NAME;