This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
hterm_contextmenu.js
149 lines (132 loc) · 3.99 KB
/
hterm_contextmenu.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
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* @fileoverview Context menu handling.
*/
/**
* Manage the context menu usually shown when right clicking.
*
* @constructor
*/
hterm.ContextMenu = function() {
// The document that contains this context menu.
this.document_ = null;
// The generated context menu (i.e. HTML elements).
this.element_ = null;
// The structured menu (i.e. JS objects).
/** @type {!Array<!hterm.ContextMenu.Item>} */
this.menu_ = [];
};
/** @typedef {{name:(string|symbol), action:function(!Event)}} */
hterm.ContextMenu.Item;
/**
* Constant to add a separator to the context menu.
*/
hterm.ContextMenu.SEPARATOR = Symbol('-');
/**
* Bind context menu to a specific document element.
*
* @param {!Document} document The document to use when creating elements.
*/
hterm.ContextMenu.prototype.setDocument = function(document) {
if (this.element_) {
this.element_.remove();
this.element_ = null;
}
this.document_ = document;
this.regenerate_();
this.document_.body.appendChild(this.element_);
};
/**
* Regenerate the HTML elements based on internal menu state.
*/
hterm.ContextMenu.prototype.regenerate_ = function() {
if (!this.element_) {
this.element_ = this.document_.createElement('menu');
this.element_.id = 'hterm:context-menu';
} else {
this.hide();
}
// Clear out existing menu entries.
while (this.element_.firstChild) {
this.element_.removeChild(this.element_.firstChild);
}
this.menu_.forEach(({name, action}) => {
const menuitem = this.document_.createElement('menuitem');
if (name === hterm.ContextMenu.SEPARATOR) {
menuitem.innerHTML = '<hr>';
menuitem.className = 'separator';
} else {
menuitem.innerText = name;
menuitem.addEventListener('mousedown', function(e) {
e.preventDefault();
action(e);
});
}
this.element_.appendChild(menuitem);
});
};
/**
* Set all the entries in the context menu.
*
* This is an array of arrays. The first element in the array is the string to
* display while the second element is the function to call.
*
* The first element may also be the SEPARATOR constant to add a separator.
*
* This resets all existing menu entries.
*
* @param {!Array<!hterm.ContextMenu.Item>} items The menu entries.
*/
hterm.ContextMenu.prototype.setItems = function(items) {
this.menu_ = items;
this.regenerate_();
};
/**
* Show the context menu.
*
* The event is used to determine where to show the menu.
*
* If no menu entries are defined, then nothing will be shown.
*
* @param {!Event} e The event triggering this display.
* @param {!hterm.Terminal=} terminal The terminal object to get style info
* from.
*/
hterm.ContextMenu.prototype.show = function(e, terminal) {
// If there are no menu entries, then don't try to show anything.
if (this.menu_.length == 0) {
return;
}
// If we have the terminal, sync the style preferences over.
if (terminal) {
this.element_.style.fontSize = terminal.getFontSize();
this.element_.style.fontFamily = terminal.getFontFamily();
}
this.element_.style.top = `${e.clientY}px`;
this.element_.style.left = `${e.clientX}px`;
const docSize = this.document_.body.getBoundingClientRect();
this.element_.style.display = 'block';
// We can't calculate sizes until after it's displayed.
const eleSize = this.element_.getBoundingClientRect();
// Make sure the menu isn't clipped outside of the current element.
const minY = Math.max(0, docSize.height - eleSize.height);
const minX = Math.max(0, docSize.width - eleSize.width);
if (minY < e.clientY) {
this.element_.style.top = `${minY}px`;
}
if (minX < e.clientX) {
this.element_.style.left = `${minX}px`;
}
};
/**
* Hide the context menu.
*/
hterm.ContextMenu.prototype.hide = function() {
if (!this.element_) {
return;
}
this.element_.style.display = 'none';
};