UNPKG

@wener/console

Version:
630 lines (629 loc) 22.8 kB
function _array_like_to_array(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _array_with_holes(arr) { if (Array.isArray(arr)) return arr; } function _assert_this_initialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _call_super(_this, derived, args) { derived = _get_prototype_of(derived); return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args)); } function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _construct(Parent, args, Class) { if (_is_native_reflect_construct()) { _construct = Reflect.construct; } else { _construct = function construct(Parent, args, Class) { var a = [ null ]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _set_prototype_of(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _get_prototype_of(o) { _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _get_prototype_of(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _set_prototype_of(subClass, superClass); } function _is_native_function(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } function _iterable_to_array_limit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){ _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally{ try { if (!_n && _i["return"] != null) _i["return"](); } finally{ if (_d) throw _e; } } return _arr; } function _non_iterable_rest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _object_spread(target) { for(var i = 1; i < arguments.length; i++){ var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === "function") { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function(key) { _define_property(target, key, source[key]); }); } return target; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function(sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _object_spread_props(target, source) { source = source != null ? source : {}; if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function(key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _possible_constructor_return(self, call) { if (call && (_type_of(call) === "object" || typeof call === "function")) { return call; } return _assert_this_initialized(self); } function _set_prototype_of(o, p) { _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _set_prototype_of(o, p); } function _sliced_to_array(arr, i) { return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest(); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } function _wrap_native_super(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrap_native_super = function wrapNativeSuper(Class) { if (Class === null || !_is_native_function(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _get_prototype_of(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _set_prototype_of(Wrapper, Class); }; return _wrap_native_super(Class); } function _is_native_reflect_construct() { try { var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {})); } catch (_) {} return (_is_native_reflect_construct = function() { return !!result; })(); } import { createContext, useContext } from 'react'; import { clamp, getGlobalStates, randomUUID } from '@wener/utils'; import { createStore } from 'zustand'; import { mutative } from 'zustand-mutative'; export var WindowContext = /*#__PURE__*/ createContext(undefined); export function useRootWindow() { return getRootWindow(); } export function useWindow() { var win = useContext(WindowContext); if (!win) { console.trace("useWindow used outside of WindowContext, fallback to root window"); } return win || getRootWindow(); } export function useWindowContext() { var win = useContext(WindowContext); if (!win) { throw new Error("useWindowContext must be used within a WindowContext"); } return win; } export function getRootWindow() { return getGlobalStates('ReactRootWindow', function() { return new ReactRootWindow(); }); } function createRootWindowStore() { var init = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}; return createStore(mutative(function(setState, getState, store) { return _object_spread({ maximized: undefined, windows: [] }, init); })); } var WindowSizes = { xs: { width: 200, height: 200 }, sm: { width: 400, height: 300 }, md: { width: 600, height: 400 }, lg: { width: 800, height: 600 }, xl: { width: 1000, height: 800 }, xxl: { width: 1200, height: 800 } }; var FrameSize = { title: 28, border: 1, width: 2, height: 28 + 2 }; function normalize(init) { return _object_spread({ zIndex: 0, minimized: false, maximized: false, canMaximize: true, canMinimize: true, canResize: true, canDrag: true, minWidth: 200, minHeight: 200, metadata: {}, attributes: {}, properties: {}, frameless: false, canFullscreen: true, fullscreen: false, windows: [] }, init, normalizeCoordinate(init)); } function normalizeCoordinate(param) { var _param_width = param.width, width = _param_width === void 0 ? WindowSizes.md.width : _param_width, _param_height = param.height, height = _param_height === void 0 ? WindowSizes.md.height : _param_height, x = param.x, y = param.y, center = (arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}).center; var _ref = typeof window === 'undefined' ? { innerWidth: 800, innerHeight: 600 } : window, ww = _ref.innerWidth, wh = _ref.innerHeight; width = clamp(width, WindowSizes.xs.width, ww); height = clamp(height, WindowSizes.xs.height, wh); var cx = (ww - width) / 2; var cy = (wh - height) / 2; if (center) { x = cx; y = cy; } x = clamp(x || cx, 0, ww - width); y = clamp(y || cy, 0, wh - height); return { x: x, y: y, width: width, height: height }; } function createWindowStore() { var init = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}; return createStore(mutative(function(setState, getState, store) { return normalize(init); })); } export var ReactWindow = /*#__PURE__*/ function(EventTarget1) { "use strict"; _inherits(ReactWindow, EventTarget1); function ReactWindow(param) { var id = param.id, key = param.key, store = param.store, parent = param.parent; _class_call_check(this, ReactWindow); var _this; _this = _call_super(this, ReactWindow), _define_property(_this, "id", void 0), _define_property(_this, "key", void 0), _define_property(_this, "store", void 0), _define_property(_this, "parent", void 0), _define_property(_this, "setBody", function(ref) { var current = _this.state.bodyElement; if (current === ref) { return; } // do initial focus if (!current) { ref === null || ref === void 0 ? void 0 : ref.focus(); } _this.store.setState({ bodyElement: ref }); }), _define_property(_this, "close", function(data) { _this.dispatchEvent(new CustomEvent('close', { detail: data })); }), _define_property(_this, "focus", function() { var ele = _this.state.bodyElement; if (!ele) { return; } if (!document.activeElement || !(ele === null || ele === void 0 ? void 0 : ele.contains(document.activeElement))) { ele === null || ele === void 0 ? void 0 : ele.focus(); _this.dispatchEvent(new Event('focus')); } }), _define_property(_this, "minimize", function(minimize) { var _this_state = _this.state, current = _this_state.minimized, canMinimize = _this_state.canMinimize; if (!canMinimize) { return; } minimize = minimize !== null && minimize !== void 0 ? minimize : !current; if (minimize === current) { return; } _this.store.setState(function(s) { s.minimized = minimize; s.maximized = false; }); _this.dispatchEvent(new Event('minimize')); }), _define_property(_this, "maximize", function(maximize) { var _this_state = _this.state, current = _this_state.maximized, canMaximize = _this_state.canMaximize; if (!canMaximize) { return; } maximize = maximize !== null && maximize !== void 0 ? maximize : !current; if (maximize === current) { return; } _this.store.setState(function(s) { if (maximize && !s.maximized) { s.maximized = true; s.minimized = false; s.properties['last'] = [ s.x, s.y, s.width, s.height ]; s.width = window.innerWidth; s.height = window.innerHeight; s.x = 0; s.y = 0; ReactWindow.MaximizedWindow = _this; } else if (!maximize && s.maximized) { s.maximized = false; s.minimized = false; var _s_properties_last; var _ref = _sliced_to_array((_s_properties_last = s.properties['last']) !== null && _s_properties_last !== void 0 ? _s_properties_last : [], 4), x = _ref[0], y = _ref[1], width = _ref[2], height = _ref[3]; s.width = width; s.height = height; s.x = x; s.y = y; Object.assign(s, normalizeCoordinate(s)); ReactWindow.MaximizedWindow = undefined; } }); if (maximize) { _this.dispatchEvent(new Event('maximize')); } else { _this.dispatchEvent(new Event('restore')); } }), _define_property(_this, "center", function() { _this.store.setState(normalizeCoordinate(_this.store.getState(), { center: true })); }), _define_property(_this, "open", function(opts) { getRootWindow().open(opts); }); _this.id = id; _this.key = key || id; _this.parent = parent; _this.store = store || createWindowStore(); return _this; } _create_class(ReactWindow, [ { key: "state", get: function get() { return this.store.getState(); } }, { key: "body", get: function get() { return this.state.bodyElement; } } ]); return ReactWindow; }(_wrap_native_super(EventTarget)); _define_property(ReactWindow, "MaximizedWindow", void 0); var ReactRootWindow = /*#__PURE__*/ function(ReactWindow1) { "use strict"; _inherits(ReactRootWindow, ReactWindow1); function ReactRootWindow() { _class_call_check(this, ReactRootWindow); var _this; _this = _call_super(this, ReactRootWindow, [ { id: 'root' } ]), _define_property(_this, "zIndex", 1), _define_property(_this, "current", void 0), _define_property(_this, "handleFocusIn", function(e, win) { _this.setActive(win); }), _define_property(_this, "handleFocusOut", function(e, win) { if (win === _this.current) { _this.current = undefined; } }), _define_property(_this, "toggle", function(opts) { var found = _this.find(opts); if (found) { found.close(); return; } return _this.open(opts); }), _define_property(_this, "open", function(opts) { if (opts.key) { var existing = _this.windows.find(function(v) { return v.key === opts.key; }); if (existing) { _this.setActive(existing); return existing; } } var id = randomUUID(); var key = opts.key || id; if (!opts.frameless) { var fw = FrameSize.width, fh = FrameSize.height; var wkeys = [ 'width', 'maxWidth', 'minWidth' ]; var hkeys = [ 'height', 'maxHeight', 'minHeight' ]; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; try { for(var _iterator = wkeys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ var key1 = _step.value; if (opts[key1]) { opts[key1] += fw; } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally{ try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally{ if (_didIteratorError) { throw _iteratorError; } } } var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined; try { for(var _iterator1 = hkeys[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){ var key2 = _step1.value; if (opts[key2]) { opts[key2] += fh; } } } catch (err) { _didIteratorError1 = true; _iteratorError1 = err; } finally{ try { if (!_iteratorNormalCompletion1 && _iterator1.return != null) { _iterator1.return(); } } finally{ if (_didIteratorError1) { throw _iteratorError1; } } } } var root = (_this.parent || getRootWindow()).store; var store = createWindowStore(_object_spread_props(_object_spread({}, opts), { zIndex: _this.zIndex++ })); var child = new ReactWindow({ id: id, key: key, store: store }); child.addEventListener('close', function() { root.setState(function(s) { s.windows = s.windows.filter(function(v) { return v !== child; }); _this.current === child && (_this.current = undefined); }); }); child.addEventListener('focusin', function(e) { return _this.handleFocusIn(e, child); }); child.addEventListener('focusout', function(e) { return _this.handleFocusOut(e, child); }); root.setState(function(s) { s.windows.push(child); }); _this.setActive(child); return child; }), _define_property(_this, "close", function() { _this.windows.forEach(function(v) { return v.close(); }); }); return _this; } _create_class(ReactRootWindow, [ { key: "top", get: function get() { if (this.current) { return this.current; } return this.windows.filter(function(v) { return !v.state.minimized; }).toSorted(function(a, b) { return a.state.zIndex - b.state.zIndex; })[0]; } }, { key: "windows", get: function get() { return this.state.windows; } }, { key: "setActive", value: function setActive(win) { try { var zIndex = win.state.zIndex; if (zIndex === this.zIndex) { this.current = win; win.minimize(false); return; } win.store.setState({ zIndex: ++this.zIndex }); this.current = win; } finally{ win.focus(); } } }, { key: "find", value: function find(s) { if (s.key) return this.windows.find(function(v) { return v.key === s.key; }); } } ]); return ReactRootWindow; }(ReactWindow);