targetj 1.0.260 → 1.1.1

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.
@@ -29,6 +29,7 @@ import { $Dom } from "./$Dom.js";
29
29
  import { TargetUtil } from "./TargetUtil.js";
30
30
  import { TModelUtil } from "./TModelUtil.js";
31
31
  import { TModel } from "./TModel.js";
32
+ import { ParticleTModel } from "./ParticleTModel.js";
32
33
  import { StateUtil } from "./StateUtil.js";
33
34
  /**
34
35
  * It enables storing and restoring state.
@@ -37,6 +38,7 @@ var StateManager = /*#__PURE__*/function () {
37
38
  function StateManager() {
38
39
  _classCallCheck(this, StateManager);
39
40
  this.stateCheckpoints = {};
41
+ this.restoreQueue = Promise.resolve();
40
42
  }
41
43
  return _createClass(StateManager, [{
42
44
  key: "store",
@@ -46,6 +48,7 @@ var StateManager = /*#__PURE__*/function () {
46
48
  runSnapshot,
47
49
  $pageDom,
48
50
  checkpoint,
51
+ template,
49
52
  _args = arguments;
50
53
  return _regeneratorRuntime().wrap(function _callee$(_context) {
51
54
  while (1) switch (_context.prev = _context.next) {
@@ -69,6 +72,10 @@ var StateManager = /*#__PURE__*/function () {
69
72
  return this.captureGpuVisuals();
70
73
  case 17:
71
74
  _context.t11 = _context.sent;
75
+ _context.next = 20;
76
+ return this.captureExplodeSources();
77
+ case 20:
78
+ _context.t12 = _context.sent;
72
79
  checkpoint = {
73
80
  key: _context.t0,
74
81
  capturedAt: _context.t1,
@@ -81,12 +88,16 @@ var StateManager = /*#__PURE__*/function () {
81
88
  scrollLeft: _context.t8,
82
89
  scrollTop: _context.t9,
83
90
  runSnapshot: _context.t10,
84
- gpuVisuals: _context.t11
91
+ gpuVisuals: _context.t11,
92
+ explodeSources: _context.t12
85
93
  };
86
- this.releaseGpuVisuals(this.stateCheckpoints[key]);
94
+ template = document.createElement("template");
95
+ template.innerHTML = checkpoint.html;
96
+ checkpoint.domTemplate = template;
97
+ this.releaseGpuResources(this.stateCheckpoints[key]);
87
98
  this.stateCheckpoints[key] = checkpoint;
88
99
  return _context.abrupt("return", checkpoint);
89
- case 22:
100
+ case 28:
90
101
  case "end":
91
102
  return _context.stop();
92
103
  }
@@ -99,8 +110,21 @@ var StateManager = /*#__PURE__*/function () {
99
110
  }()
100
111
  }, {
101
112
  key: "restore",
113
+ value: function restore() {
114
+ var _this = this;
115
+ var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "default";
116
+ var promise = this.restoreQueue.then(function () {
117
+ return _this.restoreCheckpoint(key);
118
+ });
119
+ this.restoreQueue = promise.catch(function (error) {
120
+ console.error("State restoration failed:", error);
121
+ });
122
+ return promise;
123
+ }
124
+ }, {
125
+ key: "restoreCheckpoint",
102
126
  value: function () {
103
- var _restore = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
127
+ var _restoreCheckpoint = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
104
128
  var key,
105
129
  checkpoint,
106
130
  timeOffset,
@@ -109,9 +133,12 @@ var StateManager = /*#__PURE__*/function () {
109
133
  tmodelIdMap,
110
134
  models,
111
135
  $pageDom,
136
+ fragment,
112
137
  visibleModels,
113
138
  restoredWithDom,
114
139
  $restoredPageDom,
140
+ gpuModels,
141
+ index,
115
142
  _args2 = arguments;
116
143
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
117
144
  while (1) switch (_context2.prev = _context2.next) {
@@ -128,11 +155,16 @@ var StateManager = /*#__PURE__*/function () {
128
155
  case 6:
129
156
  getLoader().clear();
130
157
  _context2.next = 9;
131
- return tApp.stop();
158
+ return tApp.stop({
159
+ discardState: true
160
+ });
132
161
  case 9:
133
162
  getLocationManager().cancelCurrentCalculation();
134
163
  _context2.next = 12;
135
- return tApp.reset();
164
+ return tApp.reset({
165
+ discardState: true,
166
+ skipSchedulerReset: true
167
+ });
136
168
  case 12:
137
169
  App.oids = {};
138
170
  App.tmodelIdMap = {};
@@ -145,8 +177,9 @@ var StateManager = /*#__PURE__*/function () {
145
177
  App.tmodelIdMap = tmodelIdMap;
146
178
  getLoader().restoreRuntimeSnapshot(checkpoint.loaderSnapshot, tmodelIdMap);
147
179
  $pageDom = TModelUtil.getPageDom();
148
- $pageDom.innerHTML(checkpoint.html);
149
- TModelUtil.restoreDomState($pageDom, checkpoint.domState);
180
+ fragment = checkpoint.domTemplate.content.cloneNode(true);
181
+ $pageDom.getElement().replaceChildren(fragment);
182
+ TModelUtil.restoreMountedDom($pageDom, checkpoint.domState);
150
183
  this.showGpuRestoreVisuals(checkpoint.gpuVisuals);
151
184
  tApp.tRoot.$dom = TModelUtil.getRootDom();
152
185
  visibleModels = checkpoint.visibleOids.map(function (oid) {
@@ -157,36 +190,43 @@ var StateManager = /*#__PURE__*/function () {
157
190
  restoredDoneTargets: true
158
191
  });
159
192
  tApp.tRoot.markLayoutDirty("checkpointRestore");
160
- _context2.next = 31;
161
- return tApp.start();
162
- case 31:
193
+ _context2.next = 32;
194
+ return tApp.start({
195
+ skipSchedulerReset: true
196
+ });
197
+ case 32:
163
198
  $restoredPageDom = TModelUtil.getPageDom();
164
- TModelUtil.restoreDomState($restoredPageDom, checkpoint.domState);
165
- _context2.next = 35;
199
+ TModelUtil.restoreMountedDom($restoredPageDom, checkpoint.domState);
200
+ _context2.next = 36;
166
201
  return TModelUtil.restoreScroll(checkpoint);
167
- case 35:
168
- _context2.next = 37;
202
+ case 36:
203
+ _context2.next = 38;
169
204
  return TModelUtil.restoreDomInteractionState($restoredPageDom, checkpoint.domState);
170
- case 37:
171
- _context2.next = 39;
172
- return Promise.all(models.map(function (tmodel) {
173
- var _tmodel$restoreRuntim;
174
- return (_tmodel$restoreRuntim = tmodel.restoreRuntimeDomDerivedState) === null || _tmodel$restoreRuntim === void 0 ? void 0 : _tmodel$restoreRuntim.call(tmodel);
205
+ case 38:
206
+ gpuModels = models.filter(function (tmodel) {
207
+ return tmodel instanceof ParticleTModel;
208
+ });
209
+ _context2.next = 41;
210
+ return Promise.all(gpuModels.map(function (tmodel) {
211
+ return tmodel.restoreRuntimeDomDerivedState(checkpoint.explodeSources[tmodel.oid]);
175
212
  }));
176
- case 39:
213
+ case 41:
214
+ for (index = 0; index < gpuModels.length; index++) {
215
+ gpuModels[index].particleRuntime.resumeRestored();
216
+ }
177
217
  this.hideGpuRestoreVisuals();
178
218
  getRunScheduler().restoreSnapshot(checkpoint.runSnapshot);
179
219
  return _context2.abrupt("return", true);
180
- case 42:
220
+ case 45:
181
221
  case "end":
182
222
  return _context2.stop();
183
223
  }
184
224
  }, _callee2, this);
185
225
  }));
186
- function restore() {
187
- return _restore.apply(this, arguments);
226
+ function restoreCheckpoint() {
227
+ return _restoreCheckpoint.apply(this, arguments);
188
228
  }
189
- return restore;
229
+ return restoreCheckpoint;
190
230
  }()
191
231
  }, {
192
232
  key: "syncAnimationsForCheckpoint",
@@ -261,8 +301,8 @@ var StateManager = /*#__PURE__*/function () {
261
301
  }, {
262
302
  key: "connectRestoredDoms",
263
303
  value: function connectRestoredDoms(visibleModels, allModels) {
264
- var newVisibles = DomInit.initCacheDoms(visibleModels);
265
- var restoredWithDom = TUtil.uniqueTModels([].concat(_toConsumableArray(visibleModels), _toConsumableArray(newVisibles)));
304
+ var restoredDoms = DomInit.initCacheDoms(visibleModels);
305
+ var restoredWithDom = TUtil.uniqueTModels([].concat(_toConsumableArray(visibleModels), _toConsumableArray(restoredDoms)));
266
306
  this.normalizeRestoredModels(allModels);
267
307
  tApp.manager.visibleOidMap = {};
268
308
  var _iterator4 = _createForOfIteratorHelper(restoredWithDom),
@@ -270,7 +310,7 @@ var StateManager = /*#__PURE__*/function () {
270
310
  try {
271
311
  for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
272
312
  var tmodel = _step4.value;
273
- if (tmodel.isIncluded()) {
313
+ if (tmodel.isIncluded() && tmodel.isVisible()) {
274
314
  tApp.manager.visibleOidMap[tmodel.oid] = tmodel;
275
315
  }
276
316
  }
@@ -281,11 +321,48 @@ var StateManager = /*#__PURE__*/function () {
281
321
  }
282
322
  return restoredWithDom;
283
323
  }
324
+ }, {
325
+ key: "getGpuVisualClipRect",
326
+ value: function getGpuVisualClipRect(element) {
327
+ var rect = element.getBoundingClientRect();
328
+ var left = Math.max(0, rect.left);
329
+ var top = Math.max(0, rect.top);
330
+ var right = Math.min(window.innerWidth, rect.right);
331
+ var bottom = Math.min(window.innerHeight, rect.bottom);
332
+ for (var parent = element.parentElement; parent; parent = parent.parentElement) {
333
+ var style = getComputedStyle(parent);
334
+ var parentRect = parent.getBoundingClientRect();
335
+ var clipsX = ["hidden", "clip", "auto", "scroll"].includes(style.overflowX);
336
+ var clipsY = ["hidden", "clip", "auto", "scroll"].includes(style.overflowY);
337
+ if (clipsX) {
338
+ left = Math.max(left, parentRect.left);
339
+ right = Math.min(right, parentRect.right);
340
+ }
341
+ if (clipsY) {
342
+ top = Math.max(top, parentRect.top);
343
+ bottom = Math.min(bottom, parentRect.bottom);
344
+ }
345
+ if (right <= left || bottom <= top) {
346
+ return {
347
+ left: left,
348
+ top: top,
349
+ width: 0,
350
+ height: 0
351
+ };
352
+ }
353
+ }
354
+ return {
355
+ left: left,
356
+ top: top,
357
+ width: right - left,
358
+ height: bottom - top
359
+ };
360
+ }
284
361
  }, {
285
362
  key: "captureGpuVisuals",
286
363
  value: function () {
287
364
  var _captureGpuVisuals = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
288
- var visuals, _i, _Object$values, tmodel, renderer, canvas, rect, bitmap;
365
+ var visuals, _i, _Object$values, tmodel, renderer, canvas, rect, clipRect, bitmap;
289
366
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
290
367
  while (1) switch (_context3.prev = _context3.next) {
291
368
  case 0:
@@ -293,7 +370,7 @@ var StateManager = /*#__PURE__*/function () {
293
370
  _i = 0, _Object$values = Object.values(App.tmodelIdMap);
294
371
  case 2:
295
372
  if (!(_i < _Object$values.length)) {
296
- _context3.next = 18;
373
+ _context3.next = 21;
297
374
  break;
298
375
  }
299
376
  tmodel = _Object$values[_i];
@@ -303,43 +380,101 @@ var StateManager = /*#__PURE__*/function () {
303
380
  _context3.next = 8;
304
381
  break;
305
382
  }
306
- return _context3.abrupt("continue", 15);
383
+ return _context3.abrupt("continue", 18);
307
384
  case 8:
308
385
  rect = canvas.getBoundingClientRect();
309
- _context3.next = 11;
386
+ clipRect = this.getGpuVisualClipRect(canvas);
387
+ if (!(clipRect.width <= 0 || clipRect.height <= 0)) {
388
+ _context3.next = 12;
389
+ break;
390
+ }
391
+ return _context3.abrupt("continue", 18);
392
+ case 12:
393
+ _context3.next = 14;
310
394
  return renderer.captureBitmap();
311
- case 11:
395
+ case 14:
312
396
  bitmap = _context3.sent;
313
397
  if (bitmap) {
314
- _context3.next = 14;
398
+ _context3.next = 17;
315
399
  break;
316
400
  }
317
- return _context3.abrupt("continue", 15);
318
- case 14:
401
+ return _context3.abrupt("continue", 18);
402
+ case 17:
319
403
  visuals[tmodel.oid] = {
320
404
  bitmap: bitmap,
321
405
  left: rect.left,
322
406
  top: rect.top,
323
407
  width: rect.width,
324
- height: rect.height
408
+ height: rect.height,
409
+ clipLeft: clipRect.left,
410
+ clipTop: clipRect.top,
411
+ clipWidth: clipRect.width,
412
+ clipHeight: clipRect.height
325
413
  };
326
- case 15:
414
+ case 18:
327
415
  _i++;
328
416
  _context3.next = 2;
329
417
  break;
330
- case 18:
418
+ case 21:
331
419
  return _context3.abrupt("return", visuals);
332
- case 19:
420
+ case 22:
333
421
  case "end":
334
422
  return _context3.stop();
335
423
  }
336
- }, _callee3);
424
+ }, _callee3, this);
337
425
  }));
338
426
  function captureGpuVisuals() {
339
427
  return _captureGpuVisuals.apply(this, arguments);
340
428
  }
341
429
  return captureGpuVisuals;
342
430
  }()
431
+ }, {
432
+ key: "captureExplodeSources",
433
+ value: function () {
434
+ var _captureExplodeSources = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
435
+ var sources, _i2, _Object$values2, _renderer$captureExpl, tmodel, renderer, source;
436
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
437
+ while (1) switch (_context4.prev = _context4.next) {
438
+ case 0:
439
+ sources = {};
440
+ _i2 = 0, _Object$values2 = Object.values(App.tmodelIdMap);
441
+ case 2:
442
+ if (!(_i2 < _Object$values2.length)) {
443
+ _context4.next = 14;
444
+ break;
445
+ }
446
+ tmodel = _Object$values2[_i2];
447
+ renderer = tmodel.particleRenderer;
448
+ if (!(!renderer || !tmodel.explodeChildrenState)) {
449
+ _context4.next = 7;
450
+ break;
451
+ }
452
+ return _context4.abrupt("continue", 11);
453
+ case 7:
454
+ _context4.next = 9;
455
+ return (_renderer$captureExpl = renderer.captureExplodeSourceBitmap) === null || _renderer$captureExpl === void 0 ? void 0 : _renderer$captureExpl.call(renderer);
456
+ case 9:
457
+ source = _context4.sent;
458
+ if (source) {
459
+ sources[tmodel.oid] = source;
460
+ }
461
+ case 11:
462
+ _i2++;
463
+ _context4.next = 2;
464
+ break;
465
+ case 14:
466
+ return _context4.abrupt("return", sources);
467
+ case 15:
468
+ case "end":
469
+ return _context4.stop();
470
+ }
471
+ }, _callee4);
472
+ }));
473
+ function captureExplodeSources() {
474
+ return _captureExplodeSources.apply(this, arguments);
475
+ }
476
+ return captureExplodeSources;
477
+ }()
343
478
  }, {
344
479
  key: "showGpuRestoreVisuals",
345
480
  value: function showGpuRestoreVisuals(visuals) {
@@ -358,25 +493,34 @@ var StateManager = /*#__PURE__*/function () {
358
493
  pointerEvents: "none",
359
494
  zIndex: "2147483647"
360
495
  });
361
- for (var _i2 = 0, _Object$values2 = Object.values(visuals); _i2 < _Object$values2.length; _i2++) {
362
- var visual = _Object$values2[_i2];
363
- if (!(visual !== null && visual !== void 0 && visual.bitmap)) {
496
+ for (var _i3 = 0, _Object$values3 = Object.values(visuals); _i3 < _Object$values3.length; _i3++) {
497
+ var visual = _Object$values3[_i3];
498
+ if (!(visual !== null && visual !== void 0 && visual.bitmap) || visual.clipWidth <= 0 || visual.clipHeight <= 0) {
364
499
  continue;
365
500
  }
501
+ var clip = document.createElement("div");
502
+ Object.assign(clip.style, {
503
+ position: "absolute",
504
+ left: "".concat(visual.clipLeft, "px"),
505
+ top: "".concat(visual.clipTop, "px"),
506
+ width: "".concat(visual.clipWidth, "px"),
507
+ height: "".concat(visual.clipHeight, "px"),
508
+ overflow: "hidden"
509
+ });
366
510
  var preview = document.createElement("canvas");
367
511
  preview.width = visual.bitmap.width;
368
512
  preview.height = visual.bitmap.height;
369
513
  Object.assign(preview.style, {
370
514
  position: "absolute",
371
- left: "".concat(visual.left, "px"),
372
- top: "".concat(visual.top, "px"),
515
+ left: "".concat(visual.left - visual.clipLeft, "px"),
516
+ top: "".concat(visual.top - visual.clipTop, "px"),
373
517
  width: "".concat(visual.width, "px"),
374
518
  height: "".concat(visual.height, "px"),
375
519
  display: "block"
376
520
  });
377
- var context = preview.getContext("2d");
378
- context.drawImage(visual.bitmap, 0, 0);
379
- overlay.appendChild(preview);
521
+ preview.getContext("2d").drawImage(visual.bitmap, 0, 0);
522
+ clip.appendChild(preview);
523
+ overlay.appendChild(clip);
380
524
  }
381
525
  document.body.appendChild(overlay);
382
526
  return overlay;
@@ -388,13 +532,18 @@ var StateManager = /*#__PURE__*/function () {
388
532
  (_document$querySelect = document.querySelector('[data-targetjs-gpu-restore-overlay="true"]')) === null || _document$querySelect === void 0 || _document$querySelect.remove();
389
533
  }
390
534
  }, {
391
- key: "releaseGpuVisuals",
392
- value: function releaseGpuVisuals(checkpoint) {
393
- for (var _i3 = 0, _Object$values3 = Object.values((checkpoint === null || checkpoint === void 0 ? void 0 : checkpoint.gpuVisuals) || {}); _i3 < _Object$values3.length; _i3++) {
535
+ key: "releaseGpuResources",
536
+ value: function releaseGpuResources(checkpoint) {
537
+ for (var _i4 = 0, _Object$values4 = Object.values((checkpoint === null || checkpoint === void 0 ? void 0 : checkpoint.gpuVisuals) || {}); _i4 < _Object$values4.length; _i4++) {
394
538
  var _visual$bitmap, _visual$bitmap$close, _visual$bitmap2;
395
- var visual = _Object$values3[_i3];
539
+ var visual = _Object$values4[_i4];
396
540
  visual === null || visual === void 0 || (_visual$bitmap = visual.bitmap) === null || _visual$bitmap === void 0 || (_visual$bitmap$close = (_visual$bitmap2 = _visual$bitmap).close) === null || _visual$bitmap$close === void 0 || _visual$bitmap$close.call(_visual$bitmap2);
397
541
  }
542
+ for (var _i5 = 0, _Object$values5 = Object.values((checkpoint === null || checkpoint === void 0 ? void 0 : checkpoint.explodeSources) || {}); _i5 < _Object$values5.length; _i5++) {
543
+ var _source$close;
544
+ var source = _Object$values5[_i5];
545
+ source === null || source === void 0 || (_source$close = source.close) === null || _source$close === void 0 || _source$close.call(source);
546
+ }
398
547
  }
399
548
  }, {
400
549
  key: "has",
@@ -422,16 +571,16 @@ var StateManager = /*#__PURE__*/function () {
422
571
  if (!checkpoint) {
423
572
  return false;
424
573
  }
425
- this.releaseGpuVisuals(checkpoint);
574
+ this.releaseGpuResources(checkpoint);
426
575
  delete this.stateCheckpoints[key];
427
576
  return true;
428
577
  }
429
578
  }, {
430
579
  key: "clearAll",
431
580
  value: function clearAll() {
432
- for (var _i4 = 0, _Object$values4 = Object.values(this.stateCheckpoints); _i4 < _Object$values4.length; _i4++) {
433
- var checkpoint = _Object$values4[_i4];
434
- this.releaseGpuVisuals(checkpoint);
581
+ for (var _i6 = 0, _Object$values6 = Object.values(this.stateCheckpoints); _i6 < _Object$values6.length; _i6++) {
582
+ var checkpoint = _Object$values6[_i6];
583
+ this.releaseGpuResources(checkpoint);
435
584
  }
436
585
  this.stateCheckpoints = {};
437
586
  }