Debian Codegraph @

extensions/CodeEditor/modules/lib/ace/ext-command_bar.js

raw symbols SCIP
1 ace.define("ace/ext/command_bar",["require","exports","module","ace/tooltip","ace/lib/event_emitter","ace/lib/lang","ace/lib/dom","ace/lib/oop","ace/lib/useragent"], function(require, exports, module){var __values = (this && this.__values) || function(o) {
2 var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
3 if (m) return m.call(o);
4 if (o && typeof o.length === "number") return {
5 next: function () {
6 if (o && i >= o.length) o = void 0;
7 return { value: o && o[i++], done: !o };
8 }
9 };
10 throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
11 };
12 var Tooltip = require("../tooltip").Tooltip;
13 var EventEmitter = require("../lib/event_emitter").EventEmitter;
14 var lang = require("../lib/lang");
15 var dom = require("../lib/dom");
16 var oop = require("../lib/oop");
17 var useragent = require("../lib/useragent");
18 var BUTTON_CLASS_NAME = 'command_bar_tooltip_button';
19 var VALUE_CLASS_NAME = 'command_bar_button_value';
20 var CAPTION_CLASS_NAME = 'command_bar_button_caption';
21 var KEYBINDING_CLASS_NAME = 'command_bar_keybinding';
22 var TOOLTIP_CLASS_NAME = 'command_bar_tooltip';
23 var MORE_OPTIONS_BUTTON_ID = 'MoreOptionsButton';
24 var defaultDelay = 100;
25 var defaultMaxElements = 4;
26 var minPosition = function (posA, posB) {
27 if (posB.row > posA.row) {
28 return posA;
29 }
30 else if (posB.row === posA.row && posB.column > posA.column) {
31 return posA;
32 }
33 return posB;
34 };
35 var keyDisplayMap = {
36 "Ctrl": { mac: "^" },
37 "Option": { mac: "⌥" },
38 "Command": { mac: "⌘" },
39 "Cmd": { mac: "⌘" },
40 "Shift": "⇧",
41 "Left": "←",
42 "Right": "→",
43 "Up": "↑",
44 "Down": "↓"
45 };
46 var CommandBarTooltip = /** @class */ (function () {
47 function CommandBarTooltip(parentNode, options) {
48 var e_1, _a;
49 options = options || {};
50 this.parentNode = parentNode;
51 this.tooltip = new Tooltip(this.parentNode);
52 this.moreOptions = new Tooltip(this.parentNode);
53 this.maxElementsOnTooltip = options.maxElementsOnTooltip || defaultMaxElements;
54 this.$alwaysShow = options.alwaysShow || false;
55 this.eventListeners = {};
56 this.elements = {};
57 this.commands = {};
58 this.tooltipEl = dom.buildDom(['div', { class: TOOLTIP_CLASS_NAME }], this.tooltip.getElement());
59 this.moreOptionsEl = dom.buildDom(['div', { class: TOOLTIP_CLASS_NAME + " tooltip_more_options" }], this.moreOptions.getElement());
60 this.$showTooltipTimer = lang.delayedCall(this.$showTooltip.bind(this), options.showDelay || defaultDelay);
61 this.$hideTooltipTimer = lang.delayedCall(this.$hideTooltip.bind(this), options.hideDelay || defaultDelay);
62 this.$tooltipEnter = this.$tooltipEnter.bind(this);
63 this.$onMouseMove = this.$onMouseMove.bind(this);
64 this.$onChangeScroll = this.$onChangeScroll.bind(this);
65 this.$onEditorChangeSession = this.$onEditorChangeSession.bind(this);
66 this.$scheduleTooltipForHide = this.$scheduleTooltipForHide.bind(this);
67 this.$preventMouseEvent = this.$preventMouseEvent.bind(this);
68 try {
69 for (var _b = __values(["mousedown", "mouseup", "click"]), _c = _b.next(); !_c.done; _c = _b.next()) {
70 var event = _c.value;
71 this.tooltip.getElement().addEventListener(event, this.$preventMouseEvent);
72 this.moreOptions.getElement().addEventListener(event, this.$preventMouseEvent);
73 }
74 }
75 catch (e_1_1) { e_1 = { error: e_1_1 }; }
76 finally {
77 try {
78 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
79 }
80 finally { if (e_1) throw e_1.error; }
81 }
82 }
83 CommandBarTooltip.prototype.registerCommand = function (id, command) {
84 var registerForMainTooltip = Object.keys(this.commands).length < this.maxElementsOnTooltip;
85 if (!registerForMainTooltip && !this.elements[MORE_OPTIONS_BUTTON_ID]) {
86 this.$createCommand(MORE_OPTIONS_BUTTON_ID, {
87 name: "···",
88 exec:
89 function () {
90 this.$shouldHideMoreOptions = false;
91 this.$setMoreOptionsVisibility(!this.isMoreOptionsShown());
92 }.bind(this),
93 type: "checkbox",
94 getValue: function () {
95 return this.isMoreOptionsShown();
96 }.bind(this),
97 enabled: true
98 }, true);
99 }
100 this.$createCommand(id, command, registerForMainTooltip);
101 if (this.isShown()) {
102 this.updatePosition();
103 }
104 };
105 CommandBarTooltip.prototype.isShown = function () {
106 return !!this.tooltip && this.tooltip.isOpen;
107 };
108 CommandBarTooltip.prototype.isMoreOptionsShown = function () {
109 return !!this.moreOptions && this.moreOptions.isOpen;
110 };
111 CommandBarTooltip.prototype.getAlwaysShow = function () {
112 return this.$alwaysShow;
113 };
114 CommandBarTooltip.prototype.setAlwaysShow = function (alwaysShow) {
115 this.$alwaysShow = alwaysShow;
116 this.$updateOnHoverHandlers(!this.$alwaysShow);
117 this._signal("alwaysShow", this.$alwaysShow);
118 };
119 CommandBarTooltip.prototype.attach = function (editor) {
120 if (!editor || (this.isShown() && this.editor === editor)) {
121 return;
122 }
123 this.detach();
124 this.editor = editor;
125 this.editor.on("changeSession", this.$onEditorChangeSession);
126 if (this.editor.session) {
127 this.editor.session.on("changeScrollLeft", this.$onChangeScroll);
128 this.editor.session.on("changeScrollTop", this.$onChangeScroll);
129 }
130 if (this.getAlwaysShow()) {
131 this.$showTooltip();
132 }
133 else {
134 this.$updateOnHoverHandlers(true);
135 }
136 };
137 CommandBarTooltip.prototype.updatePosition = function () {
138 if (!this.editor) {
139 return;
140 }
141 var renderer = this.editor.renderer;
142 var ranges;
143 if (this.editor.selection.getAllRanges) {
144 ranges = this.editor.selection.getAllRanges();
145 }
146 else {
147 ranges = [this.editor.getSelectionRange()];
148 }
149 if (!ranges.length) {
150 return;
151 }
152 var minPos = minPosition(ranges[0].start, ranges[0].end);
153 for (var i = 0, range; range = ranges[i]; i++) {
154 minPos = minPosition(minPos, minPosition(range.start, range.end));
155 }
156 var pos = renderer.$cursorLayer.getPixelPosition(minPos, true);
157 var tooltipEl = this.tooltip.getElement();
158 var screenWidth = window.innerWidth;
159 var screenHeight = window.innerHeight;
160 var rect = this.editor.container.getBoundingClientRect();
161 pos.top += rect.top - renderer.layerConfig.offset;
162 pos.left += rect.left + renderer.gutterWidth - renderer.scrollLeft;
163 var cursorVisible = pos.top >= rect.top && pos.top <= rect.bottom &&
164 pos.left >= rect.left + renderer.gutterWidth && pos.left <= rect.right;
165 if (!cursorVisible && this.isShown()) {
166 this.$hideTooltip();
167 return;
168 }
169 else if (cursorVisible && !this.isShown() && this.getAlwaysShow()) {
170 this.$showTooltip();
171 return;
172 }
173 var top = pos.top - tooltipEl.offsetHeight;
174 var left = Math.min(screenWidth - tooltipEl.offsetWidth, pos.left);
175 var tooltipFits = top >= 0 && top + tooltipEl.offsetHeight <= screenHeight &&
176 left >= 0 && left + tooltipEl.offsetWidth <= screenWidth;
177 if (!tooltipFits) {
178 this.$hideTooltip();
179 return;
180 }
181 this.tooltip.setPosition(left, top);
182 if (this.isMoreOptionsShown()) {
183 top = top + tooltipEl.offsetHeight;
184 left = this.elements[MORE_OPTIONS_BUTTON_ID].getBoundingClientRect().left;
185 var moreOptionsEl = this.moreOptions.getElement();
186 var screenHeight = window.innerHeight;
187 if (top + moreOptionsEl.offsetHeight > screenHeight) {
188 top -= tooltipEl.offsetHeight + moreOptionsEl.offsetHeight;
189 }
190 if (left + moreOptionsEl.offsetWidth > screenWidth) {
191 left = screenWidth - moreOptionsEl.offsetWidth;
192 }
193 this.moreOptions.setPosition(left, top);
194 }
195 };
196 CommandBarTooltip.prototype.update = function () {
197 Object.keys(this.elements).forEach(this.$updateElement.bind(this));
198 };
199 CommandBarTooltip.prototype.detach = function () {
200 this.tooltip.hide();
201 this.moreOptions.hide();
202 this.$updateOnHoverHandlers(false);
203 if (this.editor) {
204 this.editor.off("changeSession", this.$onEditorChangeSession);
205 if (this.editor.session) {
206 this.editor.session.off("changeScrollLeft", this.$onChangeScroll);
207 this.editor.session.off("changeScrollTop", this.$onChangeScroll);
208 }
209 }
210 this.$mouseInTooltip = false;
211 this.editor = null;
212 };
213 CommandBarTooltip.prototype.destroy = function () {
214 if (this.tooltip && this.moreOptions) {
215 this.detach();
216 this.tooltip.destroy();
217 this.moreOptions.destroy();
218 }
219 this.eventListeners = {};
220 this.commands = {};
221 this.elements = {};
222 this.tooltip = this.moreOptions = this.parentNode = null;
223 };
224 CommandBarTooltip.prototype.$createCommand = function (id, command, forMainTooltip) {
225 var parentEl = forMainTooltip ? this.tooltipEl : this.moreOptionsEl;
226 var keyParts = [];
227 var bindKey = command.bindKey;
228 if (bindKey) {
229 if (typeof bindKey === 'object') {
230 bindKey = useragent.isMac ? bindKey.mac : bindKey.win;
231 }
232 bindKey = bindKey.split("|")[0];
233 keyParts = bindKey.split("-");
234 keyParts = keyParts.map(function (key) {
235 if (keyDisplayMap[key]) {
236 if (typeof keyDisplayMap[key] === 'string') {
237 return keyDisplayMap[key];
238 }
239 else if (useragent.isMac && keyDisplayMap[key].mac) {
240 return keyDisplayMap[key].mac;
241 }
242 }
243 return key;
244 });
245 }
246 var buttonNode;
247 if (forMainTooltip && command.iconCssClass) {
248 buttonNode = [
249 'div',
250 {
251 class: ["ace_icon_svg", command.iconCssClass].join(" "),
252 "aria-label": command.name + " (" + command.bindKey + ")"
253 }
254 ];
255 }
256 else {
257 buttonNode = [
258 ['div', { class: VALUE_CLASS_NAME }],
259 ['div', { class: CAPTION_CLASS_NAME }, command.name]
260 ];
261 if (keyParts.length) {
262 buttonNode.push([
263 'div',
264 { class: KEYBINDING_CLASS_NAME },
265 keyParts.map(function (keyPart) {
266 return ['div', keyPart];
267 })
268 ]);
269 }
270 }
271 dom.buildDom(['div', { class: [BUTTON_CLASS_NAME, command.cssClass || ""].join(" "), ref: id }, buttonNode], parentEl, this.elements);
272 this.commands[id] = command;
273 var eventListener =
274 function (e) {
275 if (this.editor) {
276 this.editor.focus();
277 }
278 this.$shouldHideMoreOptions = this.isMoreOptionsShown();
279 if (!this.elements[id].disabled && command.exec) {
280 command.exec(this.editor);
281 }
282 if (this.$shouldHideMoreOptions) {
283 this.$setMoreOptionsVisibility(false);
284 }
285 this.update();
286 e.preventDefault();
287 }.bind(this);
288 this.eventListeners[id] = eventListener;
289 this.elements[id].addEventListener('click', eventListener.bind(this));
290 this.$updateElement(id);
291 };
292 CommandBarTooltip.prototype.$setMoreOptionsVisibility = function (visible) {
293 if (visible) {
294 this.moreOptions.setTheme(this.editor.renderer.theme);
295 this.moreOptions.setClassName(TOOLTIP_CLASS_NAME + "_wrapper");
296 this.moreOptions.show();
297 this.update();
298 this.updatePosition();
299 }
300 else {
301 this.moreOptions.hide();
302 }
303 };
304 CommandBarTooltip.prototype.$onEditorChangeSession = function (e) {
305 if (e.oldSession) {
306 e.oldSession.off("changeScrollTop", this.$onChangeScroll);
307 e.oldSession.off("changeScrollLeft", this.$onChangeScroll);
308 }
309 this.detach();
310 };
311 CommandBarTooltip.prototype.$onChangeScroll = function () {
312 if (this.editor.renderer && (this.isShown() || this.getAlwaysShow())) {
313 this.editor.renderer.once("afterRender", this.updatePosition.bind(this));
314 }
315 };
316 CommandBarTooltip.prototype.$onMouseMove = function (e) {
317 if (this.$mouseInTooltip) {
318 return;
319 }
320 var cursorPosition = this.editor.getCursorPosition();
321 var cursorScreenPosition = this.editor.renderer.textToScreenCoordinates(cursorPosition.row, cursorPosition.column);
322 var lineHeight = this.editor.renderer.lineHeight;
323 var isInCurrentLine = e.clientY >= cursorScreenPosition.pageY && e.clientY < cursorScreenPosition.pageY + lineHeight;
324 if (isInCurrentLine) {
325 if (!this.isShown() && !this.$showTooltipTimer.isPending()) {
326 this.$showTooltipTimer.delay();
327 }
328 if (this.$hideTooltipTimer.isPending()) {
329 this.$hideTooltipTimer.cancel();
330 }
331 }
332 else {
333 if (this.isShown() && !this.$hideTooltipTimer.isPending()) {
334 this.$hideTooltipTimer.delay();
335 }
336 if (this.$showTooltipTimer.isPending()) {
337 this.$showTooltipTimer.cancel();
338 }
339 }
340 };
341 CommandBarTooltip.prototype.$preventMouseEvent = function (e) {
342 if (this.editor) {
343 this.editor.focus();
344 }
345 e.preventDefault();
346 };
347 CommandBarTooltip.prototype.$scheduleTooltipForHide = function () {
348 this.$mouseInTooltip = false;
349 this.$showTooltipTimer.cancel();
350 this.$hideTooltipTimer.delay();
351 };
352 CommandBarTooltip.prototype.$tooltipEnter = function () {
353 this.$mouseInTooltip = true;
354 if (this.$showTooltipTimer.isPending()) {
355 this.$showTooltipTimer.cancel();
356 }
357 if (this.$hideTooltipTimer.isPending()) {
358 this.$hideTooltipTimer.cancel();
359 }
360 };
361 CommandBarTooltip.prototype.$updateOnHoverHandlers = function (enableHover) {
362 var tooltipEl = this.tooltip.getElement();
363 var moreOptionsEl = this.moreOptions.getElement();
364 if (enableHover) {
365 if (this.editor) {
366 this.editor.on("mousemove", this.$onMouseMove);
367 this.editor.renderer.getMouseEventTarget().addEventListener("mouseout", this.$scheduleTooltipForHide, true);
368 }
369 tooltipEl.addEventListener('mouseenter', this.$tooltipEnter);
370 tooltipEl.addEventListener('mouseleave', this.$scheduleTooltipForHide);
371 moreOptionsEl.addEventListener('mouseenter', this.$tooltipEnter);
372 moreOptionsEl.addEventListener('mouseleave', this.$scheduleTooltipForHide);
373 }
374 else {
375 if (this.editor) {
376 this.editor.off("mousemove", this.$onMouseMove);
377 this.editor.renderer.getMouseEventTarget().removeEventListener("mouseout", this.$scheduleTooltipForHide, true);
378 }
379 tooltipEl.removeEventListener('mouseenter', this.$tooltipEnter);
380 tooltipEl.removeEventListener('mouseleave', this.$scheduleTooltipForHide);
381 moreOptionsEl.removeEventListener('mouseenter', this.$tooltipEnter);
382 moreOptionsEl.removeEventListener('mouseleave', this.$scheduleTooltipForHide);
383 }
384 };
385 CommandBarTooltip.prototype.$showTooltip = function () {
386 if (this.isShown()) {
387 return;
388 }
389 this.tooltip.setTheme(this.editor.renderer.theme);
390 this.tooltip.setClassName(TOOLTIP_CLASS_NAME + "_wrapper");
391 this.tooltip.show();
392 this.update();
393 this.updatePosition();
394 this._signal("show");
395 };
396 CommandBarTooltip.prototype.$hideTooltip = function () {
397 this.$mouseInTooltip = false;
398 if (!this.isShown()) {
399 return;
400 }
401 this.moreOptions.hide();
402 this.tooltip.hide();
403 this._signal("hide");
404 };
405 CommandBarTooltip.prototype.$updateElement = function (id) {
406 var command = this.commands[id];
407 if (!command) {
408 return;
409 }
410 var el = this.elements[id];
411 var commandEnabled = command.enabled;
412 if (typeof commandEnabled === 'function') {
413 commandEnabled = commandEnabled(this.editor);
414 }
415 if (typeof command.getValue === 'function') {
416 var value = command.getValue(this.editor);
417 if (command.type === 'text') {
418 el.textContent = value;
419 }
420 else if (command.type === 'checkbox') {
421 var domCssFn = value ? dom.addCssClass : dom.removeCssClass;
422 var isOnTooltip = el.parentElement === this.tooltipEl;
423 el.ariaChecked = value;
424 if (isOnTooltip) {
425 domCssFn(el, "ace_selected");
426 }
427 else {
428 el = el.querySelector("." + VALUE_CLASS_NAME);
429 domCssFn(el, "ace_checkmark");
430 }
431 }
432 }
433 if (commandEnabled && el.disabled) {
434 dom.removeCssClass(el, "ace_disabled");
435 el.ariaDisabled = el.disabled = false;
436 el.removeAttribute("disabled");
437 }
438 else if (!commandEnabled && !el.disabled) {
439 dom.addCssClass(el, "ace_disabled");
440 el.ariaDisabled = el.disabled = true;
441 el.setAttribute("disabled", "");
442 }
443 };
444 return CommandBarTooltip;
445 }());
446 oop.implement(CommandBarTooltip.prototype, EventEmitter);
447 dom.importCssString("\n.ace_tooltip.".concat(TOOLTIP_CLASS_NAME, "_wrapper {\n padding: 0;\n}\n\n.ace_tooltip .").concat(TOOLTIP_CLASS_NAME, " {\n padding: 1px 5px;\n display: flex;\n pointer-events: auto;\n}\n\n.ace_tooltip .").concat(TOOLTIP_CLASS_NAME, ".tooltip_more_options {\n padding: 1px;\n flex-direction: column;\n}\n\ndiv.").concat(BUTTON_CLASS_NAME, " {\n display: inline-flex;\n cursor: pointer;\n margin: 1px;\n border-radius: 2px;\n padding: 2px 5px;\n align-items: center;\n}\n\ndiv.").concat(BUTTON_CLASS_NAME, ".ace_selected,\ndiv.").concat(BUTTON_CLASS_NAME, ":hover:not(.ace_disabled) {\n background-color: rgba(0, 0, 0, 0.1);\n}\n\ndiv.").concat(BUTTON_CLASS_NAME, ".ace_disabled {\n color: #777;\n pointer-events: none;\n}\n\ndiv.").concat(BUTTON_CLASS_NAME, " .ace_icon_svg {\n height: 12px;\n background-color: #000;\n}\n\ndiv.").concat(BUTTON_CLASS_NAME, ".ace_disabled .ace_icon_svg {\n background-color: #777;\n}\n\n.").concat(TOOLTIP_CLASS_NAME, ".tooltip_more_options .").concat(BUTTON_CLASS_NAME, " {\n display: flex;\n}\n\n.").concat(TOOLTIP_CLASS_NAME, ".").concat(VALUE_CLASS_NAME, " {\n display: none;\n}\n\n.").concat(TOOLTIP_CLASS_NAME, ".tooltip_more_options .").concat(VALUE_CLASS_NAME, " {\n display: inline-block;\n width: 12px;\n}\n\n.").concat(CAPTION_CLASS_NAME, " {\n display: inline-block;\n}\n\n.").concat(KEYBINDING_CLASS_NAME, " {\n margin: 0 2px;\n display: inline-block;\n font-size: 8px;\n}\n\n.").concat(TOOLTIP_CLASS_NAME, ".tooltip_more_options .").concat(KEYBINDING_CLASS_NAME, " {\n margin-left: auto;\n}\n\n.").concat(KEYBINDING_CLASS_NAME, " div {\n display: inline-block;\n min-width: 8px;\n padding: 2px;\n margin: 0 1px;\n border-radius: 2px;\n background-color: #ccc;\n text-align: center;\n}\n\n.ace_dark.ace_tooltip .").concat(TOOLTIP_CLASS_NAME, " {\n background-color: #373737;\n color: #eee;\n}\n\n.ace_dark div.").concat(BUTTON_CLASS_NAME, ".ace_disabled {\n color: #979797;\n}\n\n.ace_dark div.").concat(BUTTON_CLASS_NAME, ".ace_selected,\n.ace_dark div.").concat(BUTTON_CLASS_NAME, ":hover:not(.ace_disabled) {\n background-color: rgba(255, 255, 255, 0.1);\n}\n\n.ace_dark div.").concat(BUTTON_CLASS_NAME, " .ace_icon_svg {\n background-color: #eee;\n}\n\n.ace_dark div.").concat(BUTTON_CLASS_NAME, ".ace_disabled .ace_icon_svg {\n background-color: #979797;\n}\n\n.ace_dark .").concat(BUTTON_CLASS_NAME, ".ace_disabled {\n color: #979797;\n}\n\n.ace_dark .").concat(KEYBINDING_CLASS_NAME, " div {\n background-color: #575757;\n}\n\n.ace_checkmark::before {\n content: '\u2713';\n}\n"), "commandbar.css", false);
448 exports.CommandBarTooltip = CommandBarTooltip;
449 exports.TOOLTIP_CLASS_NAME = TOOLTIP_CLASS_NAME;
450 exports.BUTTON_CLASS_NAME = BUTTON_CLASS_NAME;
451
452 }); (function() {
453 ace.require(["ace/ext/command_bar"], function(m) {
454 if (typeof module == "object" && typeof exports == "object" && module) {
455 module.exports = m;
456 }
457 });
458 })();
459