1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __assign, __extends, __rest } from "tslib";
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | import classNames from "classnames";
|
23 | import * as React from "react";
|
24 | import * as ReactDOM from "react-dom";
|
25 | import { AbstractPureComponent, Classes } from "../common";
|
26 | import { Popover } from "../components/popover/popover";
|
27 | var TRANSITION_DURATION = 100;
|
28 |
|
29 |
|
30 | var ContextMenuLegacy = (function (_super) {
|
31 | __extends(ContextMenuLegacy, _super);
|
32 | function ContextMenuLegacy() {
|
33 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
34 | _this.state = {
|
35 | isDarkTheme: false,
|
36 | isOpen: false,
|
37 | };
|
38 | _this.cancelContextMenu = function (e) { return e.preventDefault(); };
|
39 | _this.handleBackdropContextMenu = function (e) {
|
40 |
|
41 | e.persist();
|
42 | e.preventDefault();
|
43 |
|
44 |
|
45 | _this.setTimeout(function () {
|
46 |
|
47 |
|
48 |
|
49 | var newTarget = document.elementFromPoint(e.clientX, e.clientY);
|
50 | var view = e.view, newEventInit = __rest(e, ["view"]);
|
51 | newTarget === null || newTarget === void 0 ? void 0 : newTarget.dispatchEvent(new MouseEvent("contextmenu", newEventInit));
|
52 | }, TRANSITION_DURATION);
|
53 | };
|
54 | _this.handlePopoverInteraction = function (nextOpenState) {
|
55 | if (!nextOpenState) {
|
56 |
|
57 |
|
58 | _this.requestAnimationFrame(function () { return _this.hide(); });
|
59 | }
|
60 | };
|
61 | return _this;
|
62 | }
|
63 | ContextMenuLegacy.prototype.render = function () {
|
64 | var _a;
|
65 |
|
66 | var content = React.createElement("div", { onContextMenu: this.cancelContextMenu }, this.state.menu);
|
67 | var popoverClassName = classNames((_a = {}, _a[Classes.DARK] = this.state.isDarkTheme, _a));
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | var key = this.state.offset === undefined ? "" : "".concat(this.state.offset.left, "x").concat(this.state.offset.top);
|
73 |
|
74 |
|
75 | return (React.createElement("div", { className: Classes.CONTEXT_MENU, style: this.state.offset },
|
76 | React.createElement(Popover, __assign({}, this.props, { backdropProps: { onContextMenu: this.handleBackdropContextMenu }, content: content, enforceFocus: false, key: key, hasBackdrop: true, isOpen: this.state.isOpen, minimal: true, rootBoundary: "viewport", onInteraction: this.handlePopoverInteraction, placement: "right-start", popoverClassName: popoverClassName, transitionDuration: TRANSITION_DURATION }),
|
77 | React.createElement("div", null))));
|
78 | };
|
79 | ContextMenuLegacy.prototype.show = function (menu, offset, onClose, isDarkTheme) {
|
80 | if (isDarkTheme === void 0) { isDarkTheme = false; }
|
81 | this.setState({ isOpen: true, menu: menu, offset: offset, onClose: onClose, isDarkTheme: isDarkTheme });
|
82 | };
|
83 | ContextMenuLegacy.prototype.hide = function () {
|
84 | var _a, _b;
|
85 | (_b = (_a = this.state).onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
|
86 | this.setState({ isOpen: false, onClose: undefined });
|
87 | };
|
88 | return ContextMenuLegacy;
|
89 | }(AbstractPureComponent));
|
90 | var contextMenuElement;
|
91 | var contextMenu;
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | export function show(menu, offset, onClose, isDarkTheme) {
|
100 | if (contextMenuElement === undefined) {
|
101 | contextMenuElement = document.createElement("div");
|
102 | contextMenuElement.classList.add(Classes.CONTEXT_MENU);
|
103 | document.body.appendChild(contextMenuElement);
|
104 | contextMenu = ReactDOM.render(React.createElement(ContextMenuLegacy, { onClosed: remove }), contextMenuElement);
|
105 | }
|
106 | contextMenu.show(menu, offset, onClose, isDarkTheme);
|
107 | }
|
108 |
|
109 | export function hide() {
|
110 | contextMenu === null || contextMenu === void 0 ? void 0 : contextMenu.hide();
|
111 | }
|
112 |
|
113 | export function isOpen() {
|
114 | return contextMenu != null && contextMenu.state.isOpen;
|
115 | }
|
116 | function remove() {
|
117 | if (contextMenuElement != null) {
|
118 | ReactDOM.unmountComponentAtNode(contextMenuElement);
|
119 | contextMenuElement.remove();
|
120 | contextMenuElement = undefined;
|
121 | contextMenu = undefined;
|
122 | }
|
123 | }
|
124 |
|
\ | No newline at end of file |