dockview-core
Version:
Zero dependency layout manager supporting tabs, groups, grids and splitviews for vanilla TypeScript
262 lines (261 loc) • 10.8 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PopoutWindowModule = exports.PopoutWindowService = void 0;
var events_1 = require("../events");
var array_1 = require("../array");
var modules_1 = require("./modules");
var PopoutWindowService = /** @class */ (function () {
function PopoutWindowService(host) {
this._entries = [];
this._popupServices = new Map();
this._restorationCleanups = new Set();
this._restorationPromise = Promise.resolve();
this._onDidRemove = new events_1.Emitter();
this.onDidRemove = this._onDidRemove.event;
this._host = host;
}
Object.defineProperty(PopoutWindowService.prototype, "entries", {
get: function () {
return this._entries;
},
enumerable: false,
configurable: true
});
Object.defineProperty(PopoutWindowService.prototype, "restorationPromise", {
get: function () {
return this._restorationPromise;
},
enumerable: false,
configurable: true
});
PopoutWindowService.prototype.add = function (entry) {
this._entries.push(entry);
};
PopoutWindowService.prototype.remove = function (entry) {
// Fire only on a genuine removal, and not while the host component is
// tearing down (consumers don't want popout-removed events during
// dispose).
if ((0, array_1.remove)(this._entries, entry) && !this._host.isDisposed) {
this._onDidRemove.fire(entry);
}
};
PopoutWindowService.prototype.findByGroup = function (group) {
// A popout window may host several groups in a nested gridview, so
// match by membership (DOM containment) rather than only the anchor.
return this._entries.find(function (entry) {
return entry.popoutGroup === group ||
entry.gridview.element.contains(group.element);
});
};
PopoutWindowService.prototype.findReferenceGroupId = function (group) {
var _a;
return (_a = this._entries.find(function (entry) { return entry.popoutGroup === group; })) === null || _a === void 0 ? void 0 : _a.referenceGroup;
};
/**
* The popout window's innerWidth/innerHeight are often 0/stale until it has
* painted, and the nested gridview lays its children out to the size passed
* to layout() (a plain group fills via CSS instead). To stop content
* rendering into a zero box until a manual resize — and to avoid the race a
* fixed number of animation frames had — observe the gridview element with
* a ResizeObserver created in the POPOUT window's OWN realm. A parent-realm
* observer fires unreliably across the window boundary; a same-realm one
* fires reliably, including the initial observation once the window is
* sized.
*
* @returns a disposable that disconnects the observer, or `undefined` when
* the popout realm has no ResizeObserver (e.g. jsdom).
*/
PopoutWindowService.prototype.observeGridviewSize = function (popoutWindow, gridview, overlayRenderContainer) {
var _this = this;
var _a;
var PopoutResizeObserver = (_a = popoutWindow.window) === null || _a === void 0 ? void 0 : _a.ResizeObserver;
if (!PopoutResizeObserver) {
return undefined;
}
var lastWidth = -1;
var lastHeight = -1;
var relayout = function () {
var win = popoutWindow.window;
if (_this._host.isDisposed || !win || win.closed) {
return;
}
var width = Math.round(gridview.element.clientWidth);
var height = Math.round(gridview.element.clientHeight);
if (width === lastWidth && height === lastHeight) {
return;
}
lastWidth = width;
lastHeight = height;
if (width > 0 && height > 0) {
gridview.layout(width, height);
}
overlayRenderContainer.updateAllPositions();
};
var observer = new PopoutResizeObserver(function () {
var _a;
// Defer out of the observer callback into the popout's own frame to
// size against the settled layout and to avoid resize-loop warnings.
var raf = (_a = popoutWindow.window) === null || _a === void 0 ? void 0 : _a.requestAnimationFrame;
if (raf) {
raf.call(popoutWindow.window, relayout);
}
else {
relayout();
}
});
observer.observe(gridview.element);
return { dispose: function () { return observer.disconnect(); } };
};
PopoutWindowService.prototype.getPopupService = function (groupId) {
return this._popupServices.get(groupId);
};
PopoutWindowService.prototype.setPopupService = function (groupId, service) {
this._popupServices.set(groupId, service);
};
PopoutWindowService.prototype.deletePopupService = function (groupId) {
this._popupServices.delete(groupId);
};
PopoutWindowService.prototype.scheduleRestoration = function (delayMs, work, onCancel) {
var _this = this;
return new Promise(function (resolve) {
var cleanup = function () {
_this._restorationCleanups.delete(cleanup);
clearTimeout(handle);
onCancel === null || onCancel === void 0 ? void 0 : onCancel();
resolve();
};
var handle = setTimeout(function () {
_this._restorationCleanups.delete(cleanup);
// Guard against the component being disposed before this
// timer fires. Under React StrictMode the component is
// mounted -> disposed -> remounted, and without this guard
// the first instance's queued restoration would open a
// second popout window. See issue #851.
if (_this._host.isDisposed) {
resolve();
return;
}
work();
resolve();
}, delayMs);
_this._restorationCleanups.add(cleanup);
});
};
PopoutWindowService.prototype.finishRestoration = function (promises) {
this._restorationPromise = Promise.all(promises).then(function () { return void 0; });
};
PopoutWindowService.prototype.cancelPendingRestorations = function () {
var e_1, _a;
try {
for (var _b = __values(__spreadArray([], __read(this._restorationCleanups), false)), _c = _b.next(); !_c.done; _c = _b.next()) {
var cleanup = _c.value;
cleanup();
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
this._restorationCleanups.clear();
};
PopoutWindowService.prototype.serialize = function () {
return this._entries.map(function (entry) {
var grid = entry.gridview.serialize();
var root = grid.root;
var url = entry.popoutGroup.api.location.type === 'popout'
? entry.popoutGroup.api.location.popoutUrl
: undefined;
var base = {
gridReferenceGroup: entry.referenceGroup,
position: entry.window.dimensions(),
url: url,
};
// Single-group window keeps the legacy `data` shape so layouts
// round-trip byte-stably and older readers keep working.
if (root.type === 'branch' &&
root.data.length === 1 &&
root.data[0].type === 'leaf') {
return __assign(__assign({}, base), { data: root.data[0].data });
}
return __assign(__assign({}, base), { grid: grid });
});
};
PopoutWindowService.prototype.disposeAll = function () {
var e_2, _a;
try {
for (var _b = __values(__spreadArray([], __read(this._entries), false)), _c = _b.next(); !_c.done; _c = _b.next()) {
var entry = _c.value;
entry.disposable.dispose();
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
};
PopoutWindowService.prototype.dispose = function () {
this.cancelPendingRestorations();
this.disposeAll();
this._onDidRemove.dispose();
};
return PopoutWindowService;
}());
exports.PopoutWindowService = PopoutWindowService;
exports.PopoutWindowModule = (0, modules_1.defineModule)({
name: 'PopoutWindow',
serviceKey: 'popoutWindowService',
create: function (host) { return new PopoutWindowService(host); },
});