UNPKG

dockview-core

Version:

Zero dependency layout manager supporting tabs, groups, grids and splitviews for vanilla TypeScript

133 lines (132 loc) 6.07 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.PopupService = void 0; var dom_1 = require("../../dom"); var events_1 = require("../../events"); var lifecycle_1 = require("../../lifecycle"); function isCoarsePrimaryInput(win) { if (!win.matchMedia) { return false; } var coarse = win.matchMedia('(pointer: coarse)').matches; var fine = win.matchMedia('(pointer: fine)').matches; return coarse && !fine; } var PopupService = /** @class */ (function (_super) { __extends(PopupService, _super); function PopupService(root, win) { if (win === void 0) { win = window; } var _this = _super.call(this) || this; _this._active = null; _this._activeDisposable = new lifecycle_1.MutableDisposable(); _this._root = root; _this._window = win; _this._element = win.document.createElement('div'); _this._element.className = 'dv-popover-anchor'; _this._element.style.position = 'relative'; _this._root.prepend(_this._element); _this.addDisposables(lifecycle_1.Disposable.from(function () { _this.close(); }), _this._activeDisposable); return _this; } /** * Move the popup anchor into a new root element. Call this when a shell * wraps the dockview component so that edge-group overflow dropdowns * position correctly relative to the full layout area. */ PopupService.prototype.updateRoot = function (newRoot) { newRoot.prepend(this._element); this._root = newRoot; }; PopupService.prototype.openPopover = function (element, position) { var _this = this; var _a; this.close(); var wrapper = this._window.document.createElement('div'); wrapper.style.position = 'absolute'; wrapper.style.zIndex = (_a = position.zIndex) !== null && _a !== void 0 ? _a : 'var(--dv-overlay-z-index)'; wrapper.appendChild(element); var anchorBox = this._element.getBoundingClientRect(); var offsetX = anchorBox.left; var offsetY = anchorBox.top; wrapper.style.top = "".concat(position.y - offsetY, "px"); wrapper.style.left = "".concat(position.x - offsetX, "px"); this._element.appendChild(wrapper); this._active = wrapper; // Outside-pointerdown dismissal is suppressed for a short grace // window after opening. Touch long-press callers (chip / tab context // menus) open the popover while the user's finger is still pressing // the source element — Android Chrome can dispatch a follow-up // synthetic pointerdown tied to the gesture, and the release-then- // retap motion can land just outside the wrapper. Either would // dismiss the popover before the user can see or interact with it. // The grace window is short enough that intentional outside taps // still feel responsive. var openedAt = Date.now(); var POINTERDOWN_GRACE_MS = 200; this._activeDisposable.value = new lifecycle_1.CompositeDisposable((0, events_1.addDisposableListener)(this._window, 'pointerdown', function (event) { var _a; if (Date.now() - openedAt < POINTERDOWN_GRACE_MS) { return; } var target = event.target; if (!(target instanceof HTMLElement)) { return; } var el = target; while (el && el !== wrapper) { el = (_a = el === null || el === void 0 ? void 0 : el.parentElement) !== null && _a !== void 0 ? _a : null; } if (el) { return; // clicked within popover } _this.close(); }), (0, events_1.addDisposableListener)(this._window, 'keydown', function (event) { if (event.key === 'Escape' || event.key === 'Enter') { _this.close(); } }), (0, events_1.addDisposableListener)(this._window, 'resize', function () { // On touch-primary devices, common interactions resize the // window: on-screen keyboard pop, orientation change, browser // address-bar collapse. None of these mean "the user wants // the popover dismissed". Specifically, focusing the chip // context menu's rename input pops the keyboard, which would // otherwise close the menu the moment the user goes to edit // it. Desktop / hybrid input keeps the existing behaviour — // there a resize genuinely means the user has resized the // window and the popover position is now stale. if (isCoarsePrimaryInput(_this._window)) { return; } _this.close(); })); this._window.requestAnimationFrame(function () { (0, dom_1.shiftAbsoluteElementIntoView)(wrapper, _this._root); }); }; PopupService.prototype.close = function () { if (this._active) { this._active.remove(); this._activeDisposable.dispose(); this._active = null; } }; return PopupService; }(lifecycle_1.CompositeDisposable)); exports.PopupService = PopupService;