vue-mount-plugin 3.0.0 → 4.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,120 +1,926 @@
1
- import { isVue2, Vue2, createVNode, render } from 'vue-demi';
1
+ import { Vue2, isVue2, h, ref, onUnmounted } from 'vue-demi';
2
2
 
3
- function _classCallCheck(instance, Constructor) {
4
- if (!(instance instanceof Constructor)) {
5
- throw new TypeError("Cannot call a class as a function");
3
+ function _arrayLikeToArray(r, a) {
4
+ (null == a || a > r.length) && (a = r.length);
5
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
6
+ return n;
7
+ }
8
+ function _arrayWithHoles(r) {
9
+ if (Array.isArray(r)) return r;
10
+ }
11
+ function _arrayWithoutHoles(r) {
12
+ if (Array.isArray(r)) return _arrayLikeToArray(r);
13
+ }
14
+ function _assertThisInitialized(e) {
15
+ if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
16
+ return e;
17
+ }
18
+ function asyncGeneratorStep(n, t, e, r, o, a, c) {
19
+ try {
20
+ var i = n[a](c),
21
+ u = i.value;
22
+ } catch (n) {
23
+ return void e(n);
6
24
  }
25
+ i.done ? t(u) : Promise.resolve(u).then(r, o);
7
26
  }
8
- function _defineProperties(target, props) {
9
- for (var i = 0; i < props.length; i++) {
10
- var descriptor = props[i];
11
- descriptor.enumerable = descriptor.enumerable || false;
12
- descriptor.configurable = true;
13
- if ("value" in descriptor) descriptor.writable = true;
14
- Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
27
+ function _asyncToGenerator(n) {
28
+ return function () {
29
+ var t = this,
30
+ e = arguments;
31
+ return new Promise(function (r, o) {
32
+ var a = n.apply(t, e);
33
+ function _next(n) {
34
+ asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
35
+ }
36
+ function _throw(n) {
37
+ asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
38
+ }
39
+ _next(void 0);
40
+ });
41
+ };
42
+ }
43
+ function _callSuper(t, o, e) {
44
+ return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e));
45
+ }
46
+ function _classCallCheck(a, n) {
47
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
48
+ }
49
+ function _defineProperties(e, r) {
50
+ for (var t = 0; t < r.length; t++) {
51
+ var o = r[t];
52
+ o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
15
53
  }
16
54
  }
17
- function _createClass(Constructor, protoProps, staticProps) {
18
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
19
- if (staticProps) _defineProperties(Constructor, staticProps);
20
- Object.defineProperty(Constructor, "prototype", {
55
+ function _createClass(e, r, t) {
56
+ return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
57
+ writable: false
58
+ }), e;
59
+ }
60
+ function _defineProperty(e, r, t) {
61
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
62
+ value: t,
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true
66
+ }) : e[r] = t, e;
67
+ }
68
+ function _getPrototypeOf(t) {
69
+ return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
70
+ return t.__proto__ || Object.getPrototypeOf(t);
71
+ }, _getPrototypeOf(t);
72
+ }
73
+ function _inherits(t, e) {
74
+ if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
75
+ t.prototype = Object.create(e && e.prototype, {
76
+ constructor: {
77
+ value: t,
78
+ writable: true,
79
+ configurable: true
80
+ }
81
+ }), Object.defineProperty(t, "prototype", {
21
82
  writable: false
83
+ }), e && _setPrototypeOf(t, e);
84
+ }
85
+ function _isNativeReflectConstruct() {
86
+ try {
87
+ var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
88
+ } catch (t) {}
89
+ return (_isNativeReflectConstruct = function () {
90
+ return !!t;
91
+ })();
92
+ }
93
+ function _iterableToArray(r) {
94
+ if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
95
+ }
96
+ function _iterableToArrayLimit(r, l) {
97
+ var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
98
+ if (null != t) {
99
+ var e,
100
+ n,
101
+ i,
102
+ u,
103
+ a = [],
104
+ f = true,
105
+ o = false;
106
+ try {
107
+ if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
108
+ } catch (r) {
109
+ o = true, n = r;
110
+ } finally {
111
+ try {
112
+ if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
113
+ } finally {
114
+ if (o) throw n;
115
+ }
116
+ }
117
+ return a;
118
+ }
119
+ }
120
+ function _nonIterableRest() {
121
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
122
+ }
123
+ function _nonIterableSpread() {
124
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
125
+ }
126
+ function ownKeys(e, r) {
127
+ var t = Object.keys(e);
128
+ if (Object.getOwnPropertySymbols) {
129
+ var o = Object.getOwnPropertySymbols(e);
130
+ r && (o = o.filter(function (r) {
131
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
132
+ })), t.push.apply(t, o);
133
+ }
134
+ return t;
135
+ }
136
+ function _objectSpread2(e) {
137
+ for (var r = 1; r < arguments.length; r++) {
138
+ var t = null != arguments[r] ? arguments[r] : {};
139
+ r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
140
+ _defineProperty(e, r, t[r]);
141
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
142
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
143
+ });
144
+ }
145
+ return e;
146
+ }
147
+ function _possibleConstructorReturn(t, e) {
148
+ if (e && ("object" == typeof e || "function" == typeof e)) return e;
149
+ if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
150
+ return _assertThisInitialized(t);
151
+ }
152
+ function _setPrototypeOf(t, e) {
153
+ return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
154
+ return t.__proto__ = e, t;
155
+ }, _setPrototypeOf(t, e);
156
+ }
157
+ function _slicedToArray(r, e) {
158
+ return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
159
+ }
160
+ function _toConsumableArray(r) {
161
+ return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
162
+ }
163
+ function _toPrimitive(t, r) {
164
+ if ("object" != typeof t || !t) return t;
165
+ var e = t[Symbol.toPrimitive];
166
+ if (void 0 !== e) {
167
+ var i = e.call(t, r);
168
+ if ("object" != typeof i) return i;
169
+ throw new TypeError("@@toPrimitive must return a primitive value.");
170
+ }
171
+ return ("string" === r ? String : Number)(t);
172
+ }
173
+ function _toPropertyKey(t) {
174
+ var i = _toPrimitive(t, "string");
175
+ return "symbol" == typeof i ? i : i + "";
176
+ }
177
+ function _unsupportedIterableToArray(r, a) {
178
+ if (r) {
179
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
180
+ var t = {}.toString.call(r).slice(8, -1);
181
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
182
+ }
183
+ }
184
+
185
+ var singletonInstances = new Map();
186
+ var componentKeys = new WeakMap();
187
+ function getComponentKey(component) {
188
+ if (!componentKeys.has(component)) {
189
+ componentKeys.set(component, Symbol(component.name || 'anonymous'));
190
+ }
191
+ return componentKeys.get(component);
192
+ }
193
+ function getSingletonKey(component, singleton) {
194
+ if (singleton === false) return undefined;
195
+ if (typeof singleton === 'string') return singleton;
196
+ if (singleton === true) return getComponentKey(component);
197
+ return undefined;
198
+ }
199
+ function getSingleton(key) {
200
+ return singletonInstances.get(key);
201
+ }
202
+ function setSingleton(key, instance) {
203
+ var existing = singletonInstances.get(key);
204
+ if (existing && existing !== instance) {
205
+ existing.unmount();
206
+ }
207
+ singletonInstances.set(key, instance);
208
+ }
209
+ function removeSingleton(key) {
210
+ singletonInstances.delete(key);
211
+ }
212
+ function clearSingletons() {
213
+ singletonInstances.forEach(function (instance) {
214
+ return instance.unmount();
215
+ });
216
+ singletonInstances.clear();
217
+ }
218
+ function hasSingleton(key) {
219
+ return singletonInstances.has(key);
220
+ }
221
+ function getSingletonKeys() {
222
+ return Array.from(singletonInstances.keys());
223
+ }
224
+
225
+ var globalConfig = {
226
+ installed: false
227
+ };
228
+ function setGlobalConfig(config) {
229
+ Object.assign(globalConfig, config);
230
+ }
231
+ function getGlobalConfig() {
232
+ return _objectSpread2({}, globalConfig);
233
+ }
234
+ function resetGlobalConfig() {
235
+ Object.keys(globalConfig).forEach(function (key) {
236
+ delete globalConfig[key];
22
237
  });
23
- return Constructor;
24
- }
25
- function _defineProperty(obj, key, value) {
26
- key = _toPropertyKey(key);
27
- if (key in obj) {
28
- Object.defineProperty(obj, key, {
29
- value: value,
30
- enumerable: true,
31
- configurable: true,
32
- writable: true
238
+ globalConfig.installed = false;
239
+ }
240
+ function mergeWithGlobalConfig(options) {
241
+ return _objectSpread2(_objectSpread2({}, globalConfig), options);
242
+ }
243
+
244
+ function isBrowser() {
245
+ return typeof document !== 'undefined';
246
+ }
247
+ function isNodeEnv() {
248
+ var _process$versions;
249
+ return typeof process !== 'undefined' && !!((_process$versions = process.versions) !== null && _process$versions !== void 0 && _process$versions.node);
250
+ }
251
+ function resolveTarget(target) {
252
+ if (!target) return null;
253
+ if (typeof target === 'string') {
254
+ if (!isBrowser()) return null;
255
+ return document.querySelector(target);
256
+ }
257
+ return target;
258
+ }
259
+ function createElement() {
260
+ var tagName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'div';
261
+ var attributes = arguments.length > 1 ? arguments[1] : undefined;
262
+ if (!isBrowser()) {
263
+ throw new TypeError('This plugin works in browser');
264
+ }
265
+ var el = document.createElement(tagName);
266
+ if (attributes) {
267
+ Object.entries(attributes).forEach(function (_ref) {
268
+ var _ref2 = _slicedToArray(_ref, 2),
269
+ key = _ref2[0],
270
+ value = _ref2[1];
271
+ el.setAttribute(key, value);
33
272
  });
273
+ }
274
+ return el;
275
+ }
276
+ function appendTo(element, target) {
277
+ if (!isBrowser()) return;
278
+ var resolvedTarget = resolveTarget(target);
279
+ if (resolvedTarget) {
280
+ resolvedTarget.appendChild(element);
34
281
  } else {
35
- obj[key] = value;
282
+ document.body.appendChild(element);
36
283
  }
37
- return obj;
38
284
  }
39
- function _toPrimitive(input, hint) {
40
- if (typeof input !== "object" || input === null) return input;
41
- var prim = input[Symbol.toPrimitive];
42
- if (prim !== undefined) {
43
- var res = prim.call(input, hint || "default");
44
- if (typeof res !== "object") return res;
45
- throw new TypeError("@@toPrimitive must return a primitive value.");
285
+ function removeElement(element) {
286
+ if (element.parentNode) {
287
+ element.parentNode.removeChild(element);
46
288
  }
47
- return (hint === "string" ? String : Number)(input);
48
289
  }
49
- function _toPropertyKey(arg) {
50
- var key = _toPrimitive(arg, "string");
51
- return typeof key === "symbol" ? key : String(key);
290
+ function isInDOM(element) {
291
+ return document.body.contains(element);
292
+ }
293
+ function setZIndex(element, zIndex) {
294
+ if (zIndex !== undefined) {
295
+ element.style.zIndex = String(zIndex);
296
+ }
297
+ }
298
+ function addClass(element, className) {
299
+ if (className) {
300
+ var _element$classList;
301
+ (_element$classList = element.classList).add.apply(_element$classList, _toConsumableArray(className.split(/\s+/).filter(Boolean)));
302
+ }
303
+ }
304
+ function removeClass(element, className) {
305
+ if (className) {
306
+ var _element$classList2;
307
+ (_element$classList2 = element.classList).remove.apply(_element$classList2, _toConsumableArray(className.split(/\s+/).filter(Boolean)));
308
+ }
52
309
  }
53
310
 
54
- var Mount = /*#__PURE__*/function () {
55
- function Mount(component) {
56
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
57
- _classCallCheck(this, Mount);
58
- _defineProperty(this, "vNode", null);
59
- _defineProperty(this, "options", {});
60
- _defineProperty(this, "seed", 1);
61
- if (typeof document === 'undefined') throw new Error('This plugin works in browser');
62
- this.options = options;
63
- this.target = (typeof options.target === 'string' ? document.querySelector(options.target) : options.target) || document.createElement(options.tagName || 'div');
64
- this.vNode = this.createVM(component, options);
65
- }
66
- _createClass(Mount, [{
67
- key: "createVM",
68
- value: function createVM(component) {
69
- var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
70
- props = _ref.props,
71
- children = _ref.children,
72
- patchFlag = _ref.patchFlag,
73
- dynamicProps = _ref.dynamicProps,
74
- isBlockNode = _ref.isBlockNode,
75
- app = _ref.app,
76
- context = _ref.context,
77
- parent = _ref.parent;
78
- var vNode;
79
- if (isVue2) {
80
- var VueConstructor = Vue2.extend(Object.assign({}, context || {}, component));
81
- vNode = new VueConstructor({
82
- parent: parent,
83
- propsData: props
84
- });
85
- vNode.id = 'mount-plugin-' + this.seed++;
86
- return vNode;
87
- } else {
88
- vNode = createVNode(component, props, children, patchFlag, dynamicProps, isBlockNode);
89
- if (app !== null && app !== void 0 && app._context) vNode.appContext = app._context;
90
- return vNode;
311
+ var EventEmitter = /*#__PURE__*/function () {
312
+ function EventEmitter() {
313
+ _classCallCheck(this, EventEmitter);
314
+ _defineProperty(this, "events", new Map());
315
+ }
316
+ return _createClass(EventEmitter, [{
317
+ key: "on",
318
+ value: function on(event, handler) {
319
+ if (!this.events.has(event)) {
320
+ this.events.set(event, new Set());
91
321
  }
322
+ this.events.get(event).add(handler);
323
+ return this;
92
324
  }
93
325
  }, {
94
- key: "mount",
95
- value: function mount() {
96
- !this.options.target && document.body.appendChild(this.target);
97
- if (isVue2) {
98
- this.vNode && this.vNode.$mount(this.target);
326
+ key: "off",
327
+ value: function off(event, handler) {
328
+ if (handler) {
329
+ var _this$events$get;
330
+ (_this$events$get = this.events.get(event)) === null || _this$events$get === void 0 || _this$events$get.delete(handler);
99
331
  } else {
100
- render(this.vNode, this.target);
332
+ this.events.delete(event);
333
+ }
334
+ return this;
335
+ }
336
+ }, {
337
+ key: "emit",
338
+ value: function emit(event) {
339
+ var _this$events$get2;
340
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
341
+ args[_key - 1] = arguments[_key];
101
342
  }
343
+ (_this$events$get2 = this.events.get(event)) === null || _this$events$get2 === void 0 || _this$events$get2.forEach(function (handler) {
344
+ try {
345
+ handler.apply(void 0, args);
346
+ } catch (error) {
347
+ console.error("Error in event handler for \"".concat(event, "\":"), error);
348
+ }
349
+ });
350
+ return this;
351
+ }
352
+ }, {
353
+ key: "has",
354
+ value: function has(event) {
355
+ return this.events.has(event) && this.events.get(event).size > 0;
356
+ }
357
+ }, {
358
+ key: "clear",
359
+ value: function clear() {
360
+ this.events.clear();
361
+ }
362
+ }, {
363
+ key: "eventNames",
364
+ value: function eventNames() {
365
+ return Array.from(this.events.keys());
366
+ }
367
+ }]);
368
+ }();
369
+ var instanceCounter = 0;
370
+ function createInstanceId() {
371
+ return "mount-".concat(++instanceCounter, "-").concat(Date.now().toString(36));
372
+ }
373
+ function capitalize(str) {
374
+ return str.charAt(0).toUpperCase() + str.slice(1);
375
+ }
376
+ function eventToPropName(event) {
377
+ return "on".concat(capitalize(event));
378
+ }
379
+
380
+ var instances = new Set();
381
+ var MountBase = /*#__PURE__*/function () {
382
+ function MountBase(component) {
383
+ var _this = this;
384
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
385
+ _classCallCheck(this, MountBase);
386
+ _defineProperty(this, "mounted", false);
387
+ _defineProperty(this, "el", null);
388
+ _defineProperty(this, "_eventEmitter", new EventEmitter());
389
+ _defineProperty(this, "_autoCreatedTarget", false);
390
+ _defineProperty(this, "_hidden", false);
391
+ _defineProperty(this, "_destroyed", false);
392
+ _defineProperty(this, "destroy", this.unmount);
393
+ _defineProperty(this, "remove", this.unmount);
394
+ this.id = createInstanceId();
395
+ this._component = component;
396
+ this.options = mergeWithGlobalConfig(options);
397
+ this._promise = new Promise(function (resolve, reject) {
398
+ _this._resolve = resolve;
399
+ _this._reject = reject;
400
+ });
401
+ this._singletonKey = getSingletonKey(component, options.singleton);
402
+ if (this._singletonKey) {
403
+ setSingleton(this._singletonKey, this);
404
+ }
405
+ instances.add(this);
406
+ }
407
+ return _createClass(MountBase, [{
408
+ key: "show",
409
+ value: function show() {
410
+ var _this$options$onBefor, _this$options, _this$options$onMount, _this$options2;
411
+ if (this.mounted && !this._hidden) return this;
412
+ (_this$options$onBefor = (_this$options = this.options).onBeforeMount) === null || _this$options$onBefor === void 0 || _this$options$onBefor.call(_this$options, this);
413
+ this._ensureElement();
414
+ this._mountVM();
415
+ if (this.options.zIndex && this.el) {
416
+ setZIndex(this.el, this.options.zIndex);
417
+ }
418
+ this.mounted = true;
419
+ this._hidden = false;
420
+ (_this$options$onMount = (_this$options2 = this.options).onMounted) === null || _this$options$onMount === void 0 || _this$options$onMount.call(_this$options2, this);
421
+ this.emit('show', this);
422
+ return this;
423
+ }
424
+ }, {
425
+ key: "hide",
426
+ value: function hide() {
427
+ if (!this.mounted || this._hidden) return this;
428
+ this.emit('beforeHide', this);
429
+ if (this.el && isInDOM(this.el)) {
430
+ removeElement(this.el);
431
+ }
432
+ this._hidden = true;
433
+ this.emit('hide', this);
434
+ return this;
102
435
  }
103
436
  }, {
104
437
  key: "unmount",
105
438
  value: function unmount() {
106
- if (isVue2) {
107
- this.vNode.$destroy();
108
- document.body.removeChild(this.vNode.$el);
439
+ var _this$options$onUnmou, _this$options4;
440
+ if (this._destroyed) return;
441
+ this._destroyed = true;
442
+ if (this.mounted || this._hidden) {
443
+ var _this$options$onBefor2, _this$options3;
444
+ (_this$options$onBefor2 = (_this$options3 = this.options).onBeforeUnmount) === null || _this$options$onBefor2 === void 0 || _this$options$onBefor2.call(_this$options3, this);
445
+ }
446
+ this._eventEmitter.emit('beforeUnmount', this);
447
+ this._unmountVM();
448
+ if (this._autoCreatedTarget && this.el) {
449
+ removeElement(this.el);
450
+ }
451
+ this.mounted = false;
452
+ this._hidden = false;
453
+ this.el = null;
454
+ this.vm = null;
455
+ instances.delete(this);
456
+ if (this._singletonKey) {
457
+ removeSingleton(this._singletonKey);
458
+ }
459
+ (_this$options$onUnmou = (_this$options4 = this.options).onUnmounted) === null || _this$options$onUnmou === void 0 || _this$options$onUnmou.call(_this$options4, this);
460
+ this._eventEmitter.clear();
461
+ }
462
+ }, {
463
+ key: "setProps",
464
+ value: function setProps(props) {
465
+ this.options = _objectSpread2(_objectSpread2({}, this.options), {}, {
466
+ props: _objectSpread2(_objectSpread2({}, this.options.props), props)
467
+ });
468
+ if (this.mounted) {
469
+ this._updateVM();
470
+ }
471
+ return this;
472
+ }
473
+ }, {
474
+ key: "on",
475
+ value: function on(event, handler) {
476
+ this._eventEmitter.on(event, handler);
477
+ return this;
478
+ }
479
+ }, {
480
+ key: "off",
481
+ value: function off(event, handler) {
482
+ this._eventEmitter.off(event, handler);
483
+ return this;
484
+ }
485
+ }, {
486
+ key: "emit",
487
+ value: function emit(event) {
488
+ var _this$_eventEmitter;
489
+ if (this._destroyed) return this;
490
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
491
+ args[_key - 1] = arguments[_key];
492
+ }
493
+ (_this$_eventEmitter = this._eventEmitter).emit.apply(_this$_eventEmitter, [event].concat(args));
494
+ return this;
495
+ }
496
+ }, {
497
+ key: "then",
498
+ value: function then(resolve) {
499
+ return this._promise.then(resolve);
500
+ }
501
+ }, {
502
+ key: "catch",
503
+ value: function _catch(reject) {
504
+ return this._promise.catch(reject);
505
+ }
506
+ }, {
507
+ key: "finally",
508
+ value: function _finally(onFinally) {
509
+ return this._promise.finally(onFinally);
510
+ }
511
+ }, {
512
+ key: "setTarget",
513
+ value: function setTarget(target) {
514
+ this.options.target = target;
515
+ return this;
516
+ }
517
+ }, {
518
+ key: "setHooks",
519
+ value: function setHooks(hooks) {
520
+ this.options = _objectSpread2(_objectSpread2({}, this.options), hooks);
521
+ return this;
522
+ }
523
+ }, {
524
+ key: "setSlots",
525
+ value: function setSlots(slots) {
526
+ this.options = _objectSpread2(_objectSpread2({}, this.options), {}, {
527
+ slots: _objectSpread2(_objectSpread2({}, this.options.slots), slots)
528
+ });
529
+ return this;
530
+ }
531
+ }, {
532
+ key: "_ensureElement",
533
+ value: function _ensureElement() {
534
+ if (this.el) return;
535
+ var target = resolveTarget(this.options.target);
536
+ if (target instanceof Element) {
537
+ this.el = target;
538
+ this._autoCreatedTarget = false;
109
539
  } else {
110
- render(null, this.target);
111
- document.body.removeChild(this.target);
540
+ this.el = createElement(this.options.tagName || 'div', {
541
+ 'data-mount-id': this.id
542
+ });
543
+ this._autoCreatedTarget = true;
544
+ }
545
+ }
546
+ }, {
547
+ key: "_appendElementToDOM",
548
+ value: function _appendElementToDOM() {
549
+ if (!this.el) return;
550
+ if (this._autoCreatedTarget) {
551
+ appendTo(this.el, null);
112
552
  }
113
- this.vNode = null;
114
- this.target = null;
553
+ }
554
+ }, {
555
+ key: "_resolvePromise",
556
+ value: function _resolvePromise(value) {
557
+ this._resolve(value);
558
+ }
559
+ }, {
560
+ key: "_rejectPromise",
561
+ value: function _rejectPromise(error) {
562
+ this._reject(error);
563
+ }
564
+ }, {
565
+ key: "_mergeListenersToProps",
566
+ value: function _mergeListenersToProps(listeners) {
567
+ var result = {};
568
+ Object.entries(listeners).forEach(function (_ref) {
569
+ var _ref2 = _slicedToArray(_ref, 2),
570
+ event = _ref2[0],
571
+ handler = _ref2[1];
572
+ result["on".concat(capitalize(event))] = handler;
573
+ });
574
+ return result;
575
+ }
576
+ }], [{
577
+ key: "instances",
578
+ get: function get() {
579
+ return Array.from(instances);
580
+ }
581
+ }, {
582
+ key: "unmountAll",
583
+ value: function unmountAll() {
584
+ instances.forEach(function (instance) {
585
+ return instance.unmount();
586
+ });
587
+ }
588
+ }, {
589
+ key: "getById",
590
+ value: function getById(id) {
591
+ return Array.from(instances).find(function (instance) {
592
+ return instance.id === id;
593
+ });
115
594
  }
116
595
  }]);
117
- return Mount;
118
596
  }();
597
+ function getInstances() {
598
+ return Array.from(instances);
599
+ }
600
+ function unmountAll() {
601
+ MountBase.unmountAll();
602
+ }
603
+ function getInstanceById(id) {
604
+ return MountBase.getById(id);
605
+ }
606
+
607
+ function inheritVue2Context(parent, options) {
608
+ var context = options.context || {};
609
+ if (parent && (parent._isVue || parent.$options)) {
610
+ return _objectSpread2({
611
+ router: parent.$router || context.router,
612
+ store: parent.$store || context.store,
613
+ i18n: parent.$i18n || context.i18n,
614
+ route: parent.$route
615
+ }, context);
616
+ }
617
+ return context;
618
+ }
619
+ function inheritVue3Context(app, _options) {
620
+ if (app !== null && app !== void 0 && app._context) {
621
+ return {
622
+ appContext: app._context,
623
+ provides: app._context.provides
624
+ };
625
+ }
626
+ return {};
627
+ }
628
+ function getCurrentComponentContext(componentInstance) {
629
+ var _componentInstance$$;
630
+ if (!componentInstance) return {};
631
+ var context = {};
632
+ if (componentInstance.$router) context.router = componentInstance.$router;
633
+ if (componentInstance.$store) context.store = componentInstance.$store;
634
+ if (componentInstance.$i18n) context.i18n = componentInstance.$i18n;
635
+ if ((_componentInstance$$ = componentInstance.$) !== null && _componentInstance$$ !== void 0 && _componentInstance$$.appContext) {
636
+ context.appContext = componentInstance.$.appContext;
637
+ }
638
+ return context;
639
+ }
640
+
641
+ var MountVue2 = /*#__PURE__*/function (_MountBase) {
642
+ function MountVue2(component) {
643
+ var _this;
644
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
645
+ _classCallCheck(this, MountVue2);
646
+ _this = _callSuper(this, MountVue2, [component, options]);
647
+ _defineProperty(_this, "vm", null);
648
+ _defineProperty(_this, "_vue2Instance", null);
649
+ if (!isVue2) {
650
+ throw new Error('MountVue2 can only be used with Vue 2');
651
+ }
652
+ _this._createVM();
653
+ return _this;
654
+ }
655
+ _inherits(MountVue2, _MountBase);
656
+ return _createClass(MountVue2, [{
657
+ key: "_createVM",
658
+ value: function _createVM() {
659
+ var VueConstructor = Vue2;
660
+ var _this$options = this.options,
661
+ props = _this$options.props,
662
+ parent = _this$options.parent,
663
+ listeners = _this$options.listeners,
664
+ on = _this$options.on;
665
+ var mergedListeners = _objectSpread2(_objectSpread2({}, listeners), on);
666
+ var context = inheritVue2Context(parent, this.options);
667
+ var componentOptions = this._component;
668
+ if (this._component.options) {
669
+ componentOptions = this._component.options;
670
+ }
671
+ var ComponentConstructor = VueConstructor.extend(componentOptions);
672
+ var instance = new ComponentConstructor({
673
+ propsData: props || {},
674
+ parent: parent,
675
+ router: context.router,
676
+ store: context.store,
677
+ i18n: context.i18n
678
+ });
679
+ Object.entries(mergedListeners).forEach(function (_ref) {
680
+ var _ref2 = _slicedToArray(_ref, 2),
681
+ event = _ref2[0],
682
+ handler = _ref2[1];
683
+ if (Array.isArray(handler)) {
684
+ handler.forEach(function (h) {
685
+ return instance.$on(event, h);
686
+ });
687
+ } else {
688
+ instance.$on(event, handler);
689
+ }
690
+ });
691
+ instance._mountId = this.id;
692
+ this._vue2Instance = instance;
693
+ }
694
+ }, {
695
+ key: "_mountVM",
696
+ value: function _mountVM() {
697
+ if (!this._vue2Instance) return;
698
+ this._ensureElement();
699
+ this._appendElementToDOM();
700
+ this._vue2Instance.$mount(this.el);
701
+ if (this._vue2Instance.$el) {
702
+ this.el = this._vue2Instance.$el;
703
+ }
704
+ this.vm = this._vue2Instance;
705
+ this.mounted = true;
706
+ }
707
+ }, {
708
+ key: "_unmountVM",
709
+ value: function _unmountVM() {
710
+ if (this._vue2Instance) {
711
+ this._vue2Instance.$destroy();
712
+ this._vue2Instance = null;
713
+ }
714
+ this.vm = null;
715
+ this.mounted = false;
716
+ }
717
+ }, {
718
+ key: "_updateVM",
719
+ value: function _updateVM() {
720
+ if (this._vue2Instance && this.options.props) {
721
+ Object.assign(this._vue2Instance.$props, this.options.props);
722
+ }
723
+ }
724
+ }]);
725
+ }(MountBase);
726
+
727
+ var cachedRender;
728
+ function getRender() {
729
+ return _getRender.apply(this, arguments);
730
+ }
731
+ function _getRender() {
732
+ _getRender = _asyncToGenerator(function* () {
733
+ if (cachedRender) return cachedRender;
734
+ var vue = yield import('vue');
735
+ cachedRender = vue.render;
736
+ return cachedRender;
737
+ });
738
+ return _getRender.apply(this, arguments);
739
+ }
740
+ var MountVue3 = /*#__PURE__*/function (_MountBase) {
741
+ function MountVue3(component) {
742
+ var _this;
743
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
744
+ _classCallCheck(this, MountVue3);
745
+ _this = _callSuper(this, MountVue3, [component, options]);
746
+ _defineProperty(_this, "vm", null);
747
+ _defineProperty(_this, "_vNode", null);
748
+ if (isVue2) {
749
+ throw new Error('MountVue3 can only be used with Vue 3');
750
+ }
751
+ _this._createVM();
752
+ return _this;
753
+ }
754
+ _inherits(MountVue3, _MountBase);
755
+ return _createClass(MountVue3, [{
756
+ key: "_createVM",
757
+ value: function _createVM() {
758
+ var _this$options = this.options,
759
+ props = _this$options.props,
760
+ children = _this$options.children,
761
+ app = _this$options.app,
762
+ listeners = _this$options.listeners,
763
+ on = _this$options.on,
764
+ slots = _this$options.slots;
765
+ var mergedListeners = _objectSpread2(_objectSpread2({}, listeners), on);
766
+ var mergedProps = _objectSpread2(_objectSpread2({}, props), this._mergeListenersToProps(mergedListeners));
767
+ var mergedChildren = slots ? _objectSpread2(_objectSpread2({}, slots), children ? {
768
+ default: children
769
+ } : {}) : children;
770
+ var vNode = h(this._component, mergedProps, mergedChildren);
771
+ if (app !== null && app !== void 0 && app._context) {
772
+ vNode.appContext = app._context;
773
+ }
774
+ this._vNode = vNode;
775
+ }
776
+ }, {
777
+ key: "_mountVM",
778
+ value: function _mountVM() {
779
+ var _this2 = this;
780
+ if (!this._vNode) {
781
+ this._createVM();
782
+ }
783
+ this._ensureElement();
784
+ this._appendElementToDOM();
785
+ getRender().then(function (render) {
786
+ if (render && _this2._vNode && _this2.el) {
787
+ var _this2$_vNode$compone, _this2$_vNode$compone2;
788
+ render(_this2._vNode, _this2.el);
789
+ _this2.vm = (_this2$_vNode$compone = (_this2$_vNode$compone2 = _this2._vNode.component) === null || _this2$_vNode$compone2 === void 0 ? void 0 : _this2$_vNode$compone2.proxy) !== null && _this2$_vNode$compone !== void 0 ? _this2$_vNode$compone : null;
790
+ }
791
+ });
792
+ this.mounted = true;
793
+ }
794
+ }, {
795
+ key: "_unmountVM",
796
+ value: function _unmountVM() {
797
+ var _this3 = this;
798
+ if (this._vNode) {
799
+ getRender().then(function (render) {
800
+ if (render && _this3.el) {
801
+ render(null, _this3.el);
802
+ }
803
+ });
804
+ this._vNode = null;
805
+ }
806
+ this.vm = null;
807
+ this.mounted = false;
808
+ }
809
+ }, {
810
+ key: "_updateVM",
811
+ value: function _updateVM() {
812
+ var _this4 = this;
813
+ this._createVM();
814
+ if (this.mounted && this.el && this._vNode) {
815
+ getRender().then(function (render) {
816
+ if (render) {
817
+ var _this4$_vNode$compone, _this4$_vNode$compone2;
818
+ render(_this4._vNode, _this4.el);
819
+ _this4.vm = (_this4$_vNode$compone = (_this4$_vNode$compone2 = _this4._vNode.component) === null || _this4$_vNode$compone2 === void 0 ? void 0 : _this4$_vNode$compone2.proxy) !== null && _this4$_vNode$compone !== void 0 ? _this4$_vNode$compone : null;
820
+ }
821
+ });
822
+ }
823
+ }
824
+ }]);
825
+ }(MountBase);
826
+
827
+ function useMount() {
828
+ var instances = ref([]);
829
+ var mount = function mount(component, options) {
830
+ var instance = createMount(component, options);
831
+ instances.value.push(instance);
832
+ return instance;
833
+ };
834
+ var unmount = function unmount(instance) {
835
+ instance.unmount();
836
+ instances.value = instances.value.filter(function (i) {
837
+ return i !== instance;
838
+ });
839
+ };
840
+ var unmountAll = function unmountAll() {
841
+ instances.value.forEach(function (instance) {
842
+ return instance.unmount();
843
+ });
844
+ instances.value = [];
845
+ };
846
+ var getById = function getById(id) {
847
+ return instances.value.find(function (instance) {
848
+ return instance.id === id;
849
+ });
850
+ };
851
+ var hasInstances = function hasInstances() {
852
+ return instances.value.length > 0;
853
+ };
854
+ var count = function count() {
855
+ return instances.value.length;
856
+ };
857
+ if (!isVue2) {
858
+ onUnmounted(unmountAll);
859
+ }
860
+ return {
861
+ instances: instances,
862
+ mount: mount,
863
+ unmount: unmount,
864
+ unmountAll: unmountAll,
865
+ getById: getById,
866
+ hasInstances: hasInstances,
867
+ count: count
868
+ };
869
+ }
870
+ function useSingleton() {
871
+ var getOrCreate = function getOrCreate(key, component, options) {
872
+ var existingInstances = useMount().instances.value;
873
+ var existing = existingInstances.find(function (i) {
874
+ return i.id === key || i.options.singleton === key;
875
+ });
876
+ if (existing) {
877
+ return existing;
878
+ }
879
+ var instance = createMount(component, _objectSpread2(_objectSpread2({}, options), {}, {
880
+ singleton: key
881
+ }));
882
+ return instance;
883
+ };
884
+ return {
885
+ getOrCreate: getOrCreate
886
+ };
887
+ }
888
+
889
+ function createMount(component) {
890
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
891
+ if (isVue2) {
892
+ return new MountVue2(component, options);
893
+ }
894
+ return new MountVue3(component, options);
895
+ }
896
+ function mount(component) {
897
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
898
+ var instance = createMount(component, options);
899
+ return instance.show();
900
+ }
901
+ var MountPlugin = {
902
+ install: function install(app) {
903
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
904
+ setGlobalConfig(_objectSpread2(_objectSpread2({}, options), {}, {
905
+ installed: true
906
+ }));
907
+ var name = options.name || '$mount';
908
+ if (isVue2) {
909
+ app.prototype[name] = mount;
910
+ } else {
911
+ app.config.globalProperties[name] = mount;
912
+ }
913
+ }
914
+ };
915
+ function isMounted(id) {
916
+ var _instance$mounted;
917
+ var instance = getInstanceById(id);
918
+ return (_instance$mounted = instance === null || instance === void 0 ? void 0 : instance.mounted) !== null && _instance$mounted !== void 0 ? _instance$mounted : false;
919
+ }
920
+ function getActiveInstanceIds() {
921
+ return getInstances().map(function (instance) {
922
+ return instance.id;
923
+ });
924
+ }
119
925
 
120
- export { Mount as default };
926
+ export { EventEmitter, MountBase, MountPlugin, MountVue2, MountVue3, addClass, appendTo, capitalize, clearSingletons, createElement, createInstanceId, createMount, eventToPropName, getActiveInstanceIds, getCurrentComponentContext, getGlobalConfig, getInstanceById, getInstances, getSingleton, getSingletonKey, getSingletonKeys, globalConfig, hasSingleton, inheritVue2Context, inheritVue3Context, isBrowser, isInDOM, isMounted, isNodeEnv, mergeWithGlobalConfig, mount, removeClass, removeElement, removeSingleton, resetGlobalConfig, resolveTarget, setGlobalConfig, setSingleton, setZIndex, unmountAll, useMount, useSingleton };