@netless/appliance-plugin 1.1.42 → 1.1.43-beta.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.
Files changed (112) hide show
  1. package/READMA.zh-CN.md +23 -1
  2. package/README.md +28 -1
  3. package/dist/appliance-plugin.js +1 -1
  4. package/dist/appliance-plugin.mjs +33 -30
  5. package/dist/{bridge-C8yT2GqS.js → bridge-CDqtVTPN.js} +1 -1
  6. package/dist/{bridge-DLGOhX8U.mjs → bridge-nmqVpnZ5.mjs} +1 -1
  7. package/dist/bridge.js +1 -1
  8. package/dist/bridge.mjs +1 -1
  9. package/dist/collector/collector.d.ts +50 -2
  10. package/dist/collector/types.d.ts +6 -0
  11. package/dist/component/svg/manager.d.ts +1 -0
  12. package/dist/component/svg/snapshot.d.ts +4 -1
  13. package/dist/component/textEditor/manager.d.ts +3 -0
  14. package/dist/core/backGroundThread/index.d.ts +6 -1
  15. package/dist/core/cameraTask.d.ts +45 -0
  16. package/dist/core/const.d.ts +4 -0
  17. package/dist/core/enum.d.ts +7 -1
  18. package/dist/core/error.d.ts +1 -0
  19. package/dist/core/fontFace.d.ts +5 -3
  20. package/dist/core/mainEngine.d.ts +236 -31
  21. package/dist/core/mainThread/base.d.ts +23 -2
  22. package/dist/core/mainThread/canvas2DReadback.d.ts +27 -0
  23. package/dist/core/mainThread/index.d.ts +12 -1
  24. package/dist/core/mainThread/snapshotThread.d.ts +11 -2
  25. package/dist/core/mainThread/subLocalThread.d.ts +15 -6
  26. package/dist/core/mainThread/subServiceThread.d.ts +5 -3
  27. package/dist/core/msgEvent/base.d.ts +4 -1
  28. package/dist/core/msgEvent/baseForWorker.d.ts +1 -1
  29. package/dist/core/msgEvent/copyNode/forWorker.d.ts +1 -1
  30. package/dist/core/msgEvent/forWorker.d.ts +1 -1
  31. package/dist/core/msgEvent/index.d.ts +5 -3
  32. package/dist/core/msgEvent/rotateNode/forMain.d.ts +1 -0
  33. package/dist/core/msgEvent/rotateNode/forWorker.d.ts +1 -1
  34. package/dist/core/msgEvent/scaleNode/forMain.d.ts +1 -0
  35. package/dist/core/msgEvent/scaleNode/forWorker.d.ts +1 -1
  36. package/dist/core/msgEvent/setColor/forMain.d.ts +1 -0
  37. package/dist/core/msgEvent/setColor/forWorker.d.ts +1 -1
  38. package/dist/core/msgEvent/setFont/forWorker.d.ts +1 -1
  39. package/dist/core/msgEvent/setLock/forWorker.d.ts +1 -1
  40. package/dist/core/msgEvent/setPoint/forWorker.d.ts +1 -1
  41. package/dist/core/msgEvent/setShape/forWorker.d.ts +1 -1
  42. package/dist/core/msgEvent/setZIndex/forWorker.d.ts +1 -1
  43. package/dist/core/msgEvent/translateNode/forMain.d.ts +1 -0
  44. package/dist/core/msgEvent/translateNode/forWorker.d.ts +1 -1
  45. package/dist/core/renderRuntimeFallback.d.ts +2 -0
  46. package/dist/core/rendererBusinessTask.d.ts +30 -0
  47. package/dist/core/serviceRenderScheduler.d.ts +33 -0
  48. package/dist/core/tools/base.d.ts +1 -1
  49. package/dist/core/tools/bitmapEraserFastPath.d.ts +8 -0
  50. package/dist/core/tools/eraserFaces.d.ts +24 -9
  51. package/dist/core/tools/image.d.ts +1 -0
  52. package/dist/core/tools/pencilEraserBitMap.d.ts +66 -14
  53. package/dist/core/tools/text.d.ts +2 -1
  54. package/dist/core/types.d.ts +66 -1
  55. package/dist/core/utils/indexDB.d.ts +3 -3
  56. package/dist/core/vNodeManager.d.ts +7 -0
  57. package/dist/core/worker/base.d.ts +52 -7
  58. package/dist/core/worker/fullWorkerLocal.d.ts +10 -2
  59. package/dist/core/worker/fullWorkerService.d.ts +7 -3
  60. package/dist/core/worker/renderCapabilityProbe.d.ts +54 -0
  61. package/dist/core/worker/simpleWorker.d.ts +2 -2
  62. package/dist/core/worker/snapshotWork.d.ts +1 -0
  63. package/dist/core/worker/workerManager.d.ts +17 -4
  64. package/dist/displayer/floatBar/selectorGesture.d.ts +2 -0
  65. package/dist/fullWorker.js +138 -139
  66. package/dist/index-B44o9ggV.mjs +56070 -0
  67. package/dist/{index-D8p2ph99.mjs → index-BF310Wzc.mjs} +401 -263
  68. package/dist/index-BJOPyRJn.js +1 -0
  69. package/dist/index-BiqhUSZ0.js +570 -0
  70. package/dist/index-Cjaia8CY.mjs +4107 -0
  71. package/dist/index-Cu0f4kha.js +570 -0
  72. package/dist/index-DDd-30M7.js +1 -0
  73. package/dist/index-DdbAM7Xi.mjs +4106 -0
  74. package/dist/index-DhCFlEfW.mjs +56062 -0
  75. package/dist/index-mcBiJOLo.mjs +931 -0
  76. package/dist/index-o1TaWHww.js +1 -0
  77. package/dist/index-puxStU6w.js +1 -0
  78. package/dist/plugin/applianceMultiPlugin.d.ts +18 -3
  79. package/dist/plugin/applianceSinglePlugin.d.ts +18 -4
  80. package/dist/plugin/baseApplianceManager.d.ts +138 -12
  81. package/dist/plugin/baseViewContainerManager.d.ts +40 -3
  82. package/dist/plugin/bitmapEraserOptions.d.ts +7 -0
  83. package/dist/plugin/const.d.ts +4 -0
  84. package/dist/plugin/displayerView.d.ts +7 -7
  85. package/dist/plugin/initLogAggregator.d.ts +45 -0
  86. package/dist/plugin/multi/applianceMultiManager.d.ts +1 -0
  87. package/dist/plugin/multi/containerManager.d.ts +19 -1
  88. package/dist/plugin/pluginInstanceOwner.d.ts +1 -0
  89. package/dist/plugin/renderImageBitmapRetirement.d.ts +100 -0
  90. package/dist/plugin/renderRuntimeCache.d.ts +18 -0
  91. package/dist/plugin/renderSurface.d.ts +15 -0
  92. package/dist/plugin/renderingCompatibility.d.ts +21 -5
  93. package/dist/plugin/roomLifecycle.d.ts +12 -0
  94. package/dist/plugin/selectorApi.d.ts +8 -0
  95. package/dist/plugin/single/applianceSingleManager.d.ts +4 -0
  96. package/dist/plugin/types.d.ts +9 -16
  97. package/dist/plugin/utils.d.ts +8 -0
  98. package/dist/subWorker.js +136 -137
  99. package/dist/undo/index.d.ts +12 -3
  100. package/dist/undo/timing.d.ts +9 -4
  101. package/package.json +23 -8
  102. package/dist/index--wqREfHK.js +0 -570
  103. package/dist/index-B-tFucpi.mjs +0 -48995
  104. package/dist/index-BOx854fU.mjs +0 -3266
  105. package/dist/index-BpiBM5wZ.js +0 -1
  106. package/dist/index-ByjVgxJm.js +0 -1
  107. package/dist/index-CSVCH-SQ.mjs +0 -789
  108. package/dist/index-CfIWmlMn.js +0 -570
  109. package/dist/index-D6gXcNbF.js +0 -1
  110. package/dist/index-HAZlgByL.mjs +0 -48987
  111. package/dist/index-OZXjSPr1.js +0 -1
  112. package/dist/index-z7deO9Md.mjs +0 -3267
@@ -0,0 +1,4106 @@
1
+ var ye = Object.defineProperty;
2
+ var we = (M, e, t) => e in M ? ye(M, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : M[e] = t;
3
+ var u = (M, e, t) => we(M, typeof e != "symbol" ? e + "" : e, t);
4
+ import { D as K, e as b, c as m, r as $, b as R, f as Z, t as V, a as h, h as de, M as ve, i as D, j, k as ke, C as H, l as z, m as Se, n as te, o as Ie, p as se, q as ge, s as Te, u as re, v as ie, w as Y, x as _, E as N, y as ee, z as Q, A as We, B as Re, F as Ee, G as be, H as Le, I as pe, J as ue, T as oe, K as ae, L as Ce, N as Ne, P as fe, Q as me, R as ne, U as Oe, S as ce, W as q, X as Be, V as he, Y as De } from "./index-DhCFlEfW.mjs";
5
+ import { cloneDeep as J, xor as Me, isNumber as G, isEqual as le } from "lodash";
6
+ import "clipper-lib";
7
+ import "lineclip";
8
+ import "rbush";
9
+ import "xss";
10
+ import "react-dom";
11
+ import "react";
12
+ class Pe {
13
+ constructor(e) {
14
+ u(this, "vNodes");
15
+ u(this, "thread");
16
+ u(this, "serviceWorkShapes", /* @__PURE__ */ new Map());
17
+ u(this, "localWorkShapes", /* @__PURE__ */ new Map());
18
+ u(this, "tmpOpt");
19
+ u(this, "animationId");
20
+ u(this, "syncUnitTime", K.syncOpt.interval);
21
+ this.vNodes = e.vNodes, this.thread = e.thread;
22
+ }
23
+ createLocalWork(e) {
24
+ const { workId: t, opt: s, toolsType: r } = e;
25
+ if (t && s) {
26
+ const i = t.toString();
27
+ !this.getToolsOpt() && r && this.setToolsOpt({
28
+ toolsType: r,
29
+ toolsOpt: s
30
+ }), this.setWorkOptions(i, s);
31
+ }
32
+ }
33
+ getLocalWorkShape(e) {
34
+ return this.localWorkShapes.get(e);
35
+ }
36
+ createLocalWorkShape(e, t) {
37
+ if (e && this.tmpOpt) {
38
+ const s = {
39
+ toolsType: this.tmpOpt.toolsType,
40
+ toolsOpt: t || this.tmpOpt.toolsOpt
41
+ }, r = this.createWorkShapeNode({ ...s, workId: e });
42
+ return r && this.localWorkShapes.set(e, {
43
+ node: r,
44
+ toolsType: r.toolsType,
45
+ workState: b.Start
46
+ }), r;
47
+ }
48
+ }
49
+ canUseTopLayer(e) {
50
+ return e === m.LaserPen;
51
+ }
52
+ destroy() {
53
+ this.clearAll();
54
+ }
55
+ clearAll() {
56
+ this.thread.topLayer.children.length && (this.thread.topLayer.parent.children.forEach((e) => {
57
+ e.name !== "viewport" && e.remove();
58
+ }), $(
59
+ this.thread.serviceLayer,
60
+ this.thread.serviceLayer.parent
61
+ )), this.serviceWorkShapes.clear(), this.localWorkShapes.clear();
62
+ }
63
+ consumeDraw(e) {
64
+ const { workId: t, dataType: s } = e;
65
+ if (s === R.Service)
66
+ this.activeServiceWorkShape(e);
67
+ else {
68
+ const r = t == null ? void 0 : t.toString(), i = r && this.localWorkShapes.get(r);
69
+ if (!i)
70
+ return;
71
+ const o = i.node.consume({
72
+ data: e,
73
+ isFullWork: !1,
74
+ isSubWorker: !0
75
+ });
76
+ o.rect && (i.result = o, i.workState = b.Doing, r && this.localWorkShapes.set(r, i));
77
+ }
78
+ this.runAnimation();
79
+ }
80
+ setToolsOpt(e) {
81
+ var t;
82
+ this.tmpOpt = e, (t = e.toolsOpt) != null && t.syncUnitTime && (this.syncUnitTime = e.toolsOpt.syncUnitTime);
83
+ }
84
+ getToolsOpt() {
85
+ return this.tmpOpt;
86
+ }
87
+ createWorkShapeNode(e) {
88
+ const { toolsType: t } = e;
89
+ if (t === m.LaserPen)
90
+ return Z({
91
+ ...e,
92
+ vNodes: this.vNodes,
93
+ fullLayer: this.thread.topLayer,
94
+ drawLayer: this.thread.topLayer
95
+ });
96
+ }
97
+ setNodeKey(e, t, s, r) {
98
+ return t.toolsType = s, t.node = this.createWorkShapeNode({
99
+ workId: e,
100
+ toolsType: s,
101
+ toolsOpt: r
102
+ }), t;
103
+ }
104
+ activeServiceWorkShape(e) {
105
+ var p, f;
106
+ const { workId: t, opt: s, toolsType: r, type: i, updateNodeOpt: o, ops: a, op: n } = e;
107
+ if (!t)
108
+ return;
109
+ const c = t.toString(), d = (p = this.vNodes.get(c)) == null ? void 0 : p.rect;
110
+ if (!((f = this.serviceWorkShapes) != null && f.has(c))) {
111
+ let w = {
112
+ toolsType: r,
113
+ animationWorkData: n || [],
114
+ animationIndex: 0,
115
+ type: i,
116
+ updateNodeOpt: o,
117
+ ops: a,
118
+ oldRect: d
119
+ };
120
+ r && s && (w = this.setNodeKey(c, w, r, s)), this.serviceWorkShapes.set(c, w);
121
+ }
122
+ const l = this.serviceWorkShapes.get(c);
123
+ i && (l.type = i), a && (l.animationWorkData = V(a), l.ops = a), o && (l.updateNodeOpt = o), n && (l.animationWorkData = n), l.node && l.node.getWorkId() !== c && l.node.setWorkId(c), d && (l.oldRect = d), r && s && (l.toolsType !== r && r && s && this.setNodeKey(c, l, r, s), l.node && l.node.setWorkOptions(s));
124
+ }
125
+ computNextAnimationIndex(e, t) {
126
+ var i;
127
+ const s = ((i = e.node) == null ? void 0 : i.syncUnitTime) || this.syncUnitTime, r = Math.floor(
128
+ (e.animationWorkData || []).slice(e.animationIndex).length * 32 / t / s
129
+ ) * t;
130
+ return Math.min(
131
+ (e.animationIndex || 0) + (r || t),
132
+ (e.animationWorkData || []).length
133
+ );
134
+ }
135
+ animationDraw() {
136
+ var r, i, o, a;
137
+ this.animationId = void 0;
138
+ let e = !1;
139
+ const t = /* @__PURE__ */ new Map(), s = [];
140
+ for (const [n, c] of this.serviceWorkShapes.entries())
141
+ switch (c.toolsType) {
142
+ case m.LaserPen: {
143
+ const l = this.computNextAnimationIndex(
144
+ c,
145
+ 8
146
+ ), p = Math.max(0, c.animationIndex || 0), f = (c.animationWorkData || []).slice(
147
+ p,
148
+ l
149
+ );
150
+ if ((c.animationIndex || 0) < l && ((r = c.node) == null || r.consumeService({
151
+ op: f,
152
+ isFullWork: !1
153
+ }), c.animationIndex = l, f.length && t.set(n, {
154
+ workState: p === 0 ? b.Start : l === ((i = c.animationWorkData) == null ? void 0 : i.length) ? b.Done : b.Doing,
155
+ op: f.slice(-2)
156
+ })), c.isDel) {
157
+ (o = c.node) == null || o.clearTmpPoints(), this.serviceWorkShapes.delete(n);
158
+ break;
159
+ }
160
+ c.ops && c.animationIndex === ((a = c.animationWorkData) == null ? void 0 : a.length) && !c.isDel && (this.thread.topLayer.getElementsByName(
161
+ n.toString()
162
+ )[0] || (c.isDel = !0, this.serviceWorkShapes.set(n, c))), e = !0;
163
+ break;
164
+ }
165
+ }
166
+ for (const [n, c] of this.localWorkShapes.entries()) {
167
+ const { result: d, toolsType: l, isDel: p, workState: f } = c;
168
+ switch (l) {
169
+ case m.LaserPen: {
170
+ if (p) {
171
+ c.node.clearTmpPoints(), this.localWorkShapes.delete(n), s.push({
172
+ removeIds: [n.toString()],
173
+ type: h.RemoveNode
174
+ });
175
+ break;
176
+ }
177
+ d && ((d.op || d.ops) && s.push(d), c.result = void 0), !this.thread.topLayer.getElementsByName(
178
+ n.toString()
179
+ )[0] && f === b.Done && (c.isDel = !0, this.localWorkShapes.set(n, c)), e = !0;
180
+ break;
181
+ }
182
+ }
183
+ }
184
+ e && this.runAnimation(), t.size && t.forEach((n, c) => {
185
+ s.push({
186
+ type: h.Cursor,
187
+ uid: c.split(de)[0],
188
+ op: n.op,
189
+ workState: n.workState,
190
+ viewId: this.thread.viewId
191
+ });
192
+ }), s.length && this.thread.post({ sp: s });
193
+ }
194
+ runAnimation() {
195
+ this.animationId || (this.animationId = requestAnimationFrame(this.animationDraw.bind(this)));
196
+ }
197
+ setWorkOptions(e, t) {
198
+ var r;
199
+ let s = (r = this.localWorkShapes.get(e)) == null ? void 0 : r.node;
200
+ if (!s && this.tmpOpt) {
201
+ const { toolsType: i } = this.tmpOpt;
202
+ this.tmpOpt.toolsOpt = t, s = this.createWorkShapeNode({ workId: e, toolsType: i, toolsOpt: t }), s && this.localWorkShapes.set(e, {
203
+ node: s,
204
+ toolsType: i,
205
+ workState: b.Start
206
+ }), this.setToolsOpt(this.tmpOpt);
207
+ }
208
+ t != null && t.syncUnitTime || (t.syncUnitTime = this.syncUnitTime), s && s.setWorkOptions(t);
209
+ }
210
+ consumeDrawAll(e) {
211
+ const { workId: t, dataType: s } = e;
212
+ if (s === R.Service)
213
+ this.activeServiceWorkShape(e);
214
+ else {
215
+ const r = t == null ? void 0 : t.toString(), i = r && this.localWorkShapes.get(r);
216
+ if (!i)
217
+ return;
218
+ const o = i.node.consumeAll({
219
+ data: e
220
+ });
221
+ i.result = o, i.workState = b.Done, r && this.localWorkShapes.set(r, i);
222
+ }
223
+ this.runAnimation();
224
+ }
225
+ }
226
+ class Ae {
227
+ constructor(e) {
228
+ u(this, "vNodes");
229
+ u(this, "thread");
230
+ u(this, "workShapes", /* @__PURE__ */ new Map());
231
+ u(this, "effectSelectNodeData", /* @__PURE__ */ new Set());
232
+ u(this, "batchEraserRemoveNodes", /* @__PURE__ */ new Set());
233
+ u(this, "batchEraserWorks", /* @__PURE__ */ new Set());
234
+ u(this, "tmpOpt");
235
+ u(this, "syncUnitTime", K.syncOpt.interval);
236
+ u(this, "fullWorkerDrawCount", 0);
237
+ u(this, "drawWorkActiveId");
238
+ u(this, "consumeCount", 0);
239
+ u(this, "combineDrawActiveId");
240
+ u(this, "eraserBatchScheduler", new ve());
241
+ u(this, "disposeVNodeRevisionObserver");
242
+ u(this, "bitmapEraserRenderQueue", Promise.resolve());
243
+ u(this, "bitmapEraserRevalidationQueued", !1);
244
+ this.vNodes = e.vNodes, this.thread = e.thread, this.disposeVNodeRevisionObserver = this.vNodes.observeRevision(() => {
245
+ this.scheduleActiveBitmapEraserRevalidation();
246
+ });
247
+ }
248
+ scheduleActiveBitmapEraserRevalidation() {
249
+ const e = this.combineDrawActiveId;
250
+ if (!e) return;
251
+ const t = this.getWorkShape(e);
252
+ (t == null ? void 0 : t.toolsType) !== m.BitMapEraser || !t.shouldRevalidateVNodeRevision() || this.bitmapEraserRevalidationQueued || (this.bitmapEraserRevalidationQueued = !0, this.enqueueBitmapEraserRender(e).catch((s) => {
253
+ this.thread.master.Logger.error(
254
+ `[Appliance]: Bitmap Eraser revision revalidation failed; viewId=${this.thread.viewId}; workId=${e}; error=${s instanceof Error ? s.message : String(s)}`
255
+ );
256
+ }).finally(() => {
257
+ this.bitmapEraserRevalidationQueued = !1;
258
+ }));
259
+ }
260
+ async loadImageBitMap(e) {
261
+ return await this.thread.loadImageBitMap(e);
262
+ }
263
+ createLocalWork(e) {
264
+ const { workId: t, opt: s, toolsType: r } = e;
265
+ if (t && s) {
266
+ const i = t.toString();
267
+ !this.getToolsOpt() && r && this.setToolsOpt({
268
+ toolsType: r,
269
+ toolsOpt: s
270
+ }), this.setWorkOptions(i, s);
271
+ }
272
+ }
273
+ // async workShapesDone(scenePath: string, serviceWork: SubServiceThread) {
274
+ // for (const key of this.workShapes.keys()) {
275
+ // await this.consumeDrawAll({
276
+ // workId: key,
277
+ // scenePath,
278
+ // viewId: this.thread.viewId,
279
+ // msgType: EPostMessageType.DrawWork,
280
+ // dataType: EDataType.Local
281
+ // }, serviceWork)
282
+ // }
283
+ // }
284
+ async updateSelector(e) {
285
+ var f;
286
+ const t = this.workShapes.get(
287
+ D
288
+ );
289
+ if (!((f = t == null ? void 0 : t.selectIds) != null && f.length)) return;
290
+ const { callback: s, ...r } = e, { updateSelectorOpt: i, willSerializeData: o, smoothSync: a } = r, n = await (t == null ? void 0 : t.updateSelector({
291
+ updateSelectorOpt: i,
292
+ selectIds: J(t.selectIds),
293
+ vNodes: this.vNodes,
294
+ willSerializeData: o,
295
+ worker: this
296
+ })), c = /* @__PURE__ */ new Map();
297
+ let d;
298
+ n != null && n.selectIds && (d = Me(t.selectIds, n.selectIds), n.selectIds.forEach((w) => {
299
+ const S = this.vNodes.get(w);
300
+ if (S) {
301
+ const { toolsType: I, op: W, opt: g } = S;
302
+ c.set(w, {
303
+ opt: g,
304
+ toolsType: I,
305
+ ops: (W == null ? void 0 : W.length) && j(W) || void 0
306
+ });
307
+ }
308
+ }), t.selectIds = n.selectIds);
309
+ const l = [], p = s && s({
310
+ res: n,
311
+ workShapeNode: t,
312
+ param: r,
313
+ postData: { sp: l },
314
+ newServiceStore: c,
315
+ smoothSync: a
316
+ }) || { sp: l };
317
+ d && p.sp.push({
318
+ type: h.RemoveNode,
319
+ removeIds: d,
320
+ viewId: this.thread.viewId
321
+ }), p.sp.length && this.thread.post(p);
322
+ }
323
+ destroy() {
324
+ var e;
325
+ (e = this.disposeVNodeRevisionObserver) == null || e.call(this), this.disposeVNodeRevisionObserver = void 0, this.eraserBatchScheduler.cancel(), this.clearAll();
326
+ }
327
+ clearAll() {
328
+ if (this.eraserBatchScheduler.cancel(), this.thread.localLayer.children.length && (this.thread.topLayer.parent.children.forEach((t) => {
329
+ t.name !== "viewport" && t.remove();
330
+ }), $(
331
+ this.thread.localLayer,
332
+ this.thread.localLayer.parent
333
+ )), this.workShapes.get(
334
+ D
335
+ )) {
336
+ const t = [];
337
+ t.push({
338
+ type: h.Select,
339
+ dataType: R.Local,
340
+ selectIds: [],
341
+ willSyncService: !1
342
+ }), this.thread.post({ sp: t });
343
+ }
344
+ this.workShapes.forEach((t) => t.clearTmpPoints()), this.workShapes.clear(), this.effectSelectNodeData.clear(), this.batchEraserWorks.clear(), this.batchEraserRemoveNodes.clear();
345
+ }
346
+ async checkTextActive(e) {
347
+ const { op: t, viewId: s, dataType: r } = e;
348
+ if (t != null && t.length) {
349
+ let i;
350
+ for (const o of this.vNodes.curNodeMap.values()) {
351
+ const { rect: a, name: n, toolsType: c, opt: d } = o, l = t[0] * this.thread.fullLayer.worldScaling[0] + this.thread.fullLayer.worldPosition[0], p = t[1] * this.thread.fullLayer.worldScaling[1] + this.thread.fullLayer.worldPosition[1];
352
+ if (c === m.Text && ke([l, p], a) && d.workState === b.Done) {
353
+ i = n;
354
+ break;
355
+ }
356
+ }
357
+ i && (await this.blurSelector({
358
+ viewId: s,
359
+ msgType: h.Select,
360
+ dataType: r,
361
+ isSync: !0
362
+ }), this.thread.post({
363
+ sp: [
364
+ {
365
+ type: h.GetTextActive,
366
+ toolsType: m.Text,
367
+ workId: i
368
+ }
369
+ ]
370
+ }));
371
+ }
372
+ }
373
+ cursorHover(e) {
374
+ const { opt: t, toolsType: s, point: r } = e, i = this.setFullWork({
375
+ workId: H,
376
+ toolsType: s,
377
+ opt: t
378
+ });
379
+ i && r && i.cursorHover(r);
380
+ }
381
+ cursorBlur() {
382
+ var t;
383
+ const e = this.getWorkShape(H);
384
+ e && ((t = e.selectIds) != null && t.length) && (e.cursorBlur(), this.clearWorkShapeNodeCache(H)), this.thread.fullLayer.parent.children.forEach((s) => {
385
+ s.name === "Cursor_Hover_Id" && s.remove();
386
+ });
387
+ }
388
+ async updateFullSelectWork(e) {
389
+ var r, i, o, a, n;
390
+ const t = this.workShapes.get(
391
+ D
392
+ ), { selectIds: s } = e;
393
+ if (!(s != null && s.length)) {
394
+ await this.blurSelector(e);
395
+ return;
396
+ }
397
+ if (!t) {
398
+ this.setFullWork({
399
+ workId: D,
400
+ toolsType: m.Selector,
401
+ opt: e.opt || (((r = this.tmpOpt) == null ? void 0 : r.toolsType) === m.Selector ? this.tmpOpt.toolsOpt : {})
402
+ }) && await this.updateFullSelectWork(e);
403
+ return;
404
+ }
405
+ if (t && (s != null && s.length)) {
406
+ const { selectRect: c } = t.updateSelectIds(s), d = [
407
+ {
408
+ selectorColor: ((i = e.opt) == null ? void 0 : i.strokeColor) || t.selectorColor,
409
+ strokeColor: ((o = e.opt) == null ? void 0 : o.strokeColor) || t.strokeColor,
410
+ fillColor: ((a = e.opt) == null ? void 0 : a.fillColor) || t.fillColor,
411
+ textOpt: ((n = e.opt) == null ? void 0 : n.textOpt) || t.textOpt,
412
+ canTextEdit: t.canTextEdit,
413
+ canRotate: t.canRotate,
414
+ scaleType: t.scaleType,
415
+ type: h.Select,
416
+ viewId: this.thread.viewId,
417
+ dataType: R.Local,
418
+ selectIds: s,
419
+ selectRect: c,
420
+ points: t.getChildrenPoints(),
421
+ willSyncService: (e == null ? void 0 : e.willSyncService) || !1,
422
+ opt: (e == null ? void 0 : e.willSyncService) && t.getWorkOptions() || void 0,
423
+ canLock: t.canLock,
424
+ isLocked: t.isLocked,
425
+ toolsTypes: t.toolsTypes,
426
+ shapeOpt: t.shapeOpt,
427
+ thickness: t.thickness,
428
+ useStroke: t.useStroke,
429
+ strokeType: t.strokeType
430
+ }
431
+ ];
432
+ this.thread.post({ sp: d });
433
+ }
434
+ }
435
+ async removeSelector(e) {
436
+ const { willSyncService: t, needUndoTicker: s } = e, r = [], i = [], o = this.workShapes.get(
437
+ D
438
+ );
439
+ if (!o)
440
+ return;
441
+ const a = o.selectIds && [...o.selectIds] || [];
442
+ for (const n of a) {
443
+ const c = this.vNodes.get(n);
444
+ if (c)
445
+ switch (c.toolsType) {
446
+ case m.Text: {
447
+ r.push({
448
+ type: h.TextUpdate,
449
+ toolsType: m.Text,
450
+ workId: n,
451
+ dataType: R.Local
452
+ });
453
+ break;
454
+ }
455
+ case m.BackgroundSVG:
456
+ r.push({
457
+ type: h.BackgroundSVGDelete,
458
+ toolsType: m.BackgroundSVG,
459
+ workId: n,
460
+ dataType: R.Local,
461
+ viewId: this.thread.viewId
462
+ });
463
+ break;
464
+ }
465
+ this.removeNode(n), i.push(n);
466
+ }
467
+ i.length && r.push({
468
+ type: h.RemoveNode,
469
+ removeIds: i,
470
+ needUndoTicker: s,
471
+ emitEventType: e.emitEventType,
472
+ requestId: e.requestId
473
+ }), r.push({
474
+ type: h.Select,
475
+ selectIds: [],
476
+ willSyncService: t
477
+ }), await this.blurSelector(), s && r.push({
478
+ type: h.None,
479
+ needUndoTicker: s
480
+ }), r.length && this.thread.post({ sp: r });
481
+ }
482
+ removeWork(e) {
483
+ const { workId: t } = e, s = t == null ? void 0 : t.toString();
484
+ s && this.removeNode(s);
485
+ }
486
+ removeNode(e) {
487
+ var r;
488
+ const t = this.vNodes.get(e);
489
+ t && (t.toolsType === m.BackgroundSVG && this.thread.post({
490
+ sp: [
491
+ {
492
+ type: h.BackgroundSVGDelete,
493
+ toolsType: m.BackgroundSVG,
494
+ workId: e,
495
+ dataType: R.Local,
496
+ viewId: this.thread.viewId
497
+ }
498
+ ]
499
+ }), (r = this.thread.fullLayer) == null || r.getElementsByName(e).forEach((i) => {
500
+ i.remove(), z(i, this.thread.fullLayer.parent);
501
+ }), this.vNodes.delete(e)), this.workShapes.has(e) && (this.thread.localLayer.getElementsByName(e).forEach((i) => {
502
+ i.remove(), z(i, this.thread.localLayer.parent);
503
+ }), this.clearWorkShapeNodeCache(e));
504
+ }
505
+ setFullWork(e) {
506
+ const { workId: t, opt: s, toolsType: r } = e;
507
+ if (t && s && r) {
508
+ const i = t.toString();
509
+ let o;
510
+ return t && this.workShapes.has(i) ? (o = this.workShapes.get(i), (o == null ? void 0 : o.toolsType) !== r ? (o == null || o.clearTmpPoints(), this.workShapes.delete(i), o = void 0) : o == null || o.setWorkOptions(s)) : o = this.createWorkShapeNode({
511
+ toolsOpt: s,
512
+ toolsType: r,
513
+ workId: i
514
+ }), o || (o = this.createWorkShapeNode({
515
+ toolsOpt: s,
516
+ toolsType: r,
517
+ workId: i
518
+ })), o ? (this.workShapes.set(i, o), o) : void 0;
519
+ }
520
+ }
521
+ async consumeFull(e) {
522
+ var o, a;
523
+ const t = (o = e.workId) == null ? void 0 : o.toString(), s = (a = e.updateNodeOpt) == null ? void 0 : a.eraserGeometryDelta;
524
+ if (t) {
525
+ const n = this.vNodes.get(t), c = e.ops && V(e.ops) || e.op || [];
526
+ if (s) {
527
+ if (!n || !e.centerPos || !Se(
528
+ this.vNodes,
529
+ t,
530
+ s,
531
+ n,
532
+ { op: c, centerPos: e.centerPos }
533
+ )) {
534
+ te(this.vNodes, t);
535
+ return;
536
+ }
537
+ } else n && e.centerPos && Ie(this.vNodes, t) && se(n.op, n.centerPos) !== se(c, e.centerPos) && te(this.vNodes, t);
538
+ }
539
+ const r = this.setFullWork(e), i = e.ops && V(e.ops);
540
+ if (r) {
541
+ t && e.clearPreviewLocalWork && this.thread.localLayer.getElementsByName(t).forEach((c) => {
542
+ c.remove(), z(c, this.thread.localLayer.parent);
543
+ }), r.toolsType === m.BackgroundSVG ? r.consumeService({
544
+ isFullWork: !0,
545
+ replaceId: t
546
+ }) : r.toolsType === m.Image ? await r.consumeServiceAsync({
547
+ isFullWork: !0,
548
+ replaceId: t,
549
+ worker: this
550
+ }) : r.toolsType === m.Text ? await r.consumeServiceAsync({
551
+ isFullWork: !0,
552
+ replaceId: t,
553
+ boxRect: this.thread.getSceneRect()
554
+ }) : r.consumeService({
555
+ op: i,
556
+ isFullWork: !0,
557
+ replaceId: t
558
+ }), e != null && e.updateNodeOpt && r.updataOptService(e.updateNodeOpt);
559
+ const n = [];
560
+ e.workId && this.workShapes.delete(e.workId.toString()), e.willSyncService && n.push({
561
+ opt: e.opt,
562
+ toolsType: e.toolsType,
563
+ type: h.FullWork,
564
+ workId: e.workId,
565
+ ops: e.ops,
566
+ updateNodeOpt: e.updateNodeOpt,
567
+ viewId: this.thread.viewId,
568
+ needUndoTicker: e.needUndoTicker,
569
+ requestId: e.requestId,
570
+ emitEventType: e.emitEventType,
571
+ scenePath: e.scenePath
572
+ }), e.isLockSentEventCursor && n.push({
573
+ type: h.None,
574
+ isLockSentEventCursor: !0,
575
+ scenePath: e.scenePath
576
+ }), e.needUndoTicker && n.push({
577
+ type: h.None,
578
+ needUndoTicker: e.needUndoTicker,
579
+ emitEventType: e.emitEventType,
580
+ requestId: e.requestId,
581
+ scenePath: e.scenePath
582
+ }), n.length && this.thread.post({ sp: n });
583
+ }
584
+ }
585
+ async colloctEffectSelectWork(e) {
586
+ const t = this.workShapes.get(
587
+ D
588
+ ), { workId: s, msgType: r } = e;
589
+ if (t && s && t.selectIds && t.selectIds.includes(s.toString())) {
590
+ r === h.RemoveNode ? t.selectIds = t.selectIds.filter(
591
+ (i) => i !== s.toString()
592
+ ) : this.effectSelectNodeData.add(e), await this.runEffectSelectWork(!0);
593
+ return;
594
+ }
595
+ return e;
596
+ }
597
+ async runEffectSelectWork(e) {
598
+ var t;
599
+ for await (const s of this.effectSelectNodeData.values()) {
600
+ const r = this.setFullWork(s);
601
+ if (r) {
602
+ const i = (t = s.workId) == null ? void 0 : t.toString();
603
+ if (r.toolsType === m.BackgroundSVG)
604
+ r.consumeService({
605
+ isFullWork: !0,
606
+ replaceId: i
607
+ });
608
+ else if (r.toolsType === m.Image)
609
+ await r.consumeServiceAsync({
610
+ isFullWork: !0,
611
+ replaceId: i,
612
+ worker: this
613
+ });
614
+ else if (r.toolsType === m.Text)
615
+ await r.consumeServiceAsync({
616
+ isFullWork: !0,
617
+ replaceId: i,
618
+ boxRect: this.thread.getSceneRect()
619
+ });
620
+ else {
621
+ const o = s.ops && V(s.ops);
622
+ r.consumeService({
623
+ op: o,
624
+ isFullWork: !0,
625
+ replaceId: i
626
+ }), s != null && s.updateNodeOpt && r.updataOptService(s.updateNodeOpt);
627
+ }
628
+ s.workId && this.workShapes.delete(s.workId.toString());
629
+ }
630
+ this.effectSelectNodeData.delete(s);
631
+ }
632
+ this.reRenderSelector(e);
633
+ }
634
+ hasSelector() {
635
+ return this.workShapes.has(D);
636
+ }
637
+ getSelector() {
638
+ return this.workShapes.get(D);
639
+ }
640
+ reRenderSelector(e = !1) {
641
+ var r;
642
+ const t = this.workShapes.get(
643
+ D
644
+ );
645
+ if (!t) return;
646
+ if (t && !((r = t.selectIds) != null && r.length))
647
+ return this.blurSelector();
648
+ const s = t.reRenderSelector();
649
+ s && this.thread.post({
650
+ sp: [
651
+ {
652
+ type: h.Select,
653
+ selectIds: t.selectIds,
654
+ selectRect: s,
655
+ willSyncService: e,
656
+ viewId: this.thread.viewId,
657
+ points: t.getChildrenPoints(),
658
+ textOpt: t.textOpt,
659
+ selectorColor: t.selectorColor,
660
+ strokeColor: t.strokeColor,
661
+ fillColor: t.fillColor,
662
+ canTextEdit: t.canTextEdit,
663
+ canRotate: t.canRotate,
664
+ scaleType: t.scaleType,
665
+ opt: t.getWorkOptions() || void 0,
666
+ canLock: t.canLock,
667
+ isLocked: t.isLocked,
668
+ toolsTypes: t.toolsTypes,
669
+ shapeOpt: t.shapeOpt,
670
+ thickness: t.thickness,
671
+ useStroke: t.useStroke,
672
+ strokeType: t.strokeType
673
+ }
674
+ ]
675
+ });
676
+ }
677
+ async blurSelector(e) {
678
+ var r;
679
+ const t = this.workShapes.get(
680
+ D
681
+ ), s = t == null ? void 0 : t.blurSelector();
682
+ if (this.clearWorkShapeNodeCache(D), ((r = this.thread.fullLayer) == null ? void 0 : r.parent).children.forEach((i) => {
683
+ i.name === D && i.remove();
684
+ }), s) {
685
+ const i = [];
686
+ i.push({
687
+ ...s,
688
+ isSync: e == null ? void 0 : e.isSync,
689
+ scenePath: e == null ? void 0 : e.scenePath
690
+ }), this.thread.post({ sp: i });
691
+ }
692
+ }
693
+ clearWorkShapeNodeCache(e) {
694
+ var t;
695
+ (t = this.getWorkShape(e)) == null || t.clearTmpPoints(), this.workShapes.delete(e);
696
+ }
697
+ /** Stop queued Bitmap Eraser work and restore Doing-time hidden nodes. */
698
+ abortBitmapEraser(e) {
699
+ const t = this.workShapes.get(e);
700
+ (t == null ? void 0 : t.toolsType) === m.BitMapEraser && (t.clearTmpPoints(), this.workShapes.delete(e), this.combineDrawActiveId === e && (this.combineDrawActiveId = void 0), this.drawWorkActiveId === e && (this.drawWorkActiveId = void 0), this.bitmapEraserRenderQueue = Promise.resolve());
701
+ }
702
+ async drawBitMapEraserFull(e, t, s, r) {
703
+ const {
704
+ willUpdateNodes: i,
705
+ willDeleteNodes: o,
706
+ eraseTxnRecoveryRequired: a,
707
+ eraseTxnBeforeImages: n
708
+ } = t, c = e.getWorkId(), d = [
709
+ ...i ? Array.from(i.keys()).map((p) => String(p)) : [],
710
+ ...o ? Array.from(o) : []
711
+ ], l = [];
712
+ if (r && l.push({
713
+ type: h.None,
714
+ needUndoTicker: r,
715
+ undoScopeIds: d,
716
+ undoScopePhase: "finalize",
717
+ eraseTxnId: c,
718
+ eraseTxnRecoveryRequired: a,
719
+ eraseTxnBeforeImages: n
720
+ }), l.push({
721
+ type: h.RemoveNode,
722
+ removeIds: [c],
723
+ eraseTxnId: c,
724
+ viewId: this.thread.viewId,
725
+ needUndoTicker: r,
726
+ eraseTxnRecoveryRequired: a,
727
+ eraseTxnBeforeImages: n
728
+ }), s && l.push({
729
+ type: h.None,
730
+ isLockSentEventCursor: s
731
+ }), i != null && i.size || o != null && o.size) {
732
+ if (i != null && i.size)
733
+ for (const [p, f] of i)
734
+ l.push({
735
+ type: h.UpdateNode,
736
+ dataType: R.Local,
737
+ opt: f.opt,
738
+ workId: p,
739
+ updateNodeOpt: {
740
+ useAnimation: !1
741
+ },
742
+ eraseTxnId: c,
743
+ needUndoTicker: r
744
+ });
745
+ o != null && o.size && l.push({
746
+ type: h.RemoveNode,
747
+ removeIds: [...o],
748
+ eraseTxnId: c,
749
+ viewId: this.thread.viewId,
750
+ needUndoTicker: r
751
+ });
752
+ }
753
+ l.push({
754
+ type: h.None,
755
+ viewId: this.thread.viewId,
756
+ eraseTxnId: c,
757
+ eraseTxnPhase: t.eraseTxnCommitStatus === "failed" || a ? "failed" : "applied",
758
+ eraseTxnRecoveryRequired: a,
759
+ eraseTxnBeforeImages: n
760
+ }), l.length && this.thread.post({ sp: l });
761
+ }
762
+ drawPencilEraserFull(e, t, s, r) {
763
+ const { willNewNodes: i, willDeleteNodes: o } = t, a = e.getWorkId(), n = [
764
+ ...i ? Array.from(i.keys()).map((d) => String(d)) : [],
765
+ ...o ? Array.from(o) : []
766
+ ], c = [];
767
+ if (r && n.length && c.push({
768
+ type: h.None,
769
+ needUndoTicker: r,
770
+ undoScopeIds: n
771
+ }), c.push({
772
+ type: h.RemoveNode,
773
+ removeIds: [a],
774
+ viewId: this.thread.viewId,
775
+ needUndoTicker: r
776
+ }), s && c.push({
777
+ type: h.None,
778
+ isLockSentEventCursor: s
779
+ }), i != null && i.size || o != null && o.size) {
780
+ if (i != null && i.size)
781
+ for (const [d, l] of i)
782
+ c.push({
783
+ type: h.FullWork,
784
+ dataType: R.Local,
785
+ toolsType: l.toolsType,
786
+ ops: j(l.op),
787
+ opt: l.opt,
788
+ workId: d,
789
+ updateNodeOpt: {
790
+ useAnimation: !1
791
+ },
792
+ needUndoTicker: r
793
+ });
794
+ o != null && o.size && c.push({
795
+ type: h.RemoveNode,
796
+ removeIds: [...o],
797
+ viewId: this.thread.viewId,
798
+ needUndoTicker: r
799
+ });
800
+ }
801
+ c.length && this.thread.post({ sp: c });
802
+ }
803
+ drawEraser(e, t, s, r) {
804
+ const i = [], o = e.removeIds || [];
805
+ r && o.length && i.push({
806
+ type: h.None,
807
+ collectUndoTicker: r,
808
+ undoScopeIds: o,
809
+ undoScopePhase: "collect"
810
+ }), e.removeIds && i.push({ ...e, needUndoTicker: s, collectUndoTicker: r }), t && i.push({
811
+ type: h.None,
812
+ isLockSentEventCursor: t
813
+ }), s && i.push({
814
+ type: h.None,
815
+ needUndoTicker: s,
816
+ undoScopePhase: "finalize"
817
+ }), this.thread.post({ sp: i, consumeCount: this.consumeCount });
818
+ }
819
+ getWorkShape(e) {
820
+ return this.workShapes.get(e);
821
+ }
822
+ getWorkShapes() {
823
+ return this.workShapes;
824
+ }
825
+ async consumeDraw(e, t) {
826
+ const { op: s, workId: r, scenePath: i, postCount: o, smoothSync: a } = e;
827
+ if (s != null && s.length && r) {
828
+ const n = r.toString(), c = this.workShapes.get(n);
829
+ if (!c)
830
+ return;
831
+ const d = c.toolsType;
832
+ if (d === m.LaserPen)
833
+ return;
834
+ if (this.combineDrawActiveId && this.combineDrawActiveId !== n) {
835
+ const l = this.combineDrawActiveId;
836
+ this.eraserBatchScheduler.cancel(), this.combineDrawActiveId = void 0, await this.consumeDrawAll(
837
+ {
838
+ workId: l,
839
+ scenePath: i,
840
+ viewId: this.thread.viewId,
841
+ msgType: h.DrawWork,
842
+ dataType: R.Local
843
+ },
844
+ t
845
+ );
846
+ }
847
+ switch (this.drawWorkActiveId && this.drawWorkActiveId !== n && (await this.consumeDrawAll(
848
+ {
849
+ workId: this.drawWorkActiveId,
850
+ scenePath: i,
851
+ viewId: this.thread.viewId,
852
+ msgType: h.DrawWork,
853
+ dataType: R.Local
854
+ },
855
+ t
856
+ ), this.drawWorkActiveId = void 0), !this.drawWorkActiveId && n !== D && (this.drawWorkActiveId = n), G(o) && (this.consumeCount = o), d) {
857
+ case m.Selector: {
858
+ const l = c.consume({
859
+ data: e,
860
+ isFullWork: !0,
861
+ isMainThread: !0
862
+ });
863
+ this.fullWorkerDrawCount++;
864
+ const p = [];
865
+ l.type === h.Select && (l.selectIds && t.runReverseSelectWork(l.selectIds), p.push({ ...l, scenePath: i })), await this.thread.fullLayer.parent.prepareRender, this.thread.post({
866
+ consumeCount: this.consumeCount,
867
+ fullWorkerDrawCount: this.fullWorkerDrawCount,
868
+ sp: p
869
+ });
870
+ break;
871
+ }
872
+ case m.PencilEraser: {
873
+ c.consume({
874
+ data: e,
875
+ isFullWork: !1,
876
+ isMainThread: !0
877
+ }), this.fullWorkerDrawCount++, await this.thread.localLayer.parent.prepareRender, this.thread.post({
878
+ sp: void 0,
879
+ consumeCount: this.consumeCount,
880
+ fullWorkerDrawCount: this.fullWorkerDrawCount
881
+ }), this.combineDrawActiveId = n, this.eraserBatchScheduler.schedule(() => {
882
+ this.drawPencilEraserCombine(
883
+ n,
884
+ e.needUndoTicker,
885
+ e.collectUndoTicker
886
+ );
887
+ }, ge(this.thread.master.maxCombineEraserTime));
888
+ break;
889
+ }
890
+ case m.BitMapEraser: {
891
+ c.consume({
892
+ data: e,
893
+ isFullWork: !1,
894
+ isMainThread: !0
895
+ }), this.fullWorkerDrawCount++, this.combineDrawActiveId = n, await this.enqueueBitmapEraserRender(
896
+ n,
897
+ e.needUndoTicker,
898
+ e.collectUndoTicker,
899
+ {
900
+ consumeCount: this.consumeCount,
901
+ consumeWorkId: n
902
+ }
903
+ );
904
+ break;
905
+ }
906
+ case m.Eraser:
907
+ {
908
+ const l = c.consume({
909
+ data: e,
910
+ isFullWork: !0
911
+ });
912
+ await this.thread.fullLayer.parent.prepareRender, this.drawEraser(
913
+ l,
914
+ void 0,
915
+ e.needUndoTicker,
916
+ e.collectUndoTicker
917
+ );
918
+ }
919
+ break;
920
+ case m.Arrow:
921
+ case m.Straight:
922
+ case m.Ellipse:
923
+ case m.Rectangle:
924
+ case m.Star:
925
+ case m.Polygon:
926
+ case m.SpeechBalloon:
927
+ case m.Pencil:
928
+ {
929
+ const l = c.consume({
930
+ data: e,
931
+ isFullWork: !1,
932
+ isMainThread: !0,
933
+ smoothSync: a
934
+ });
935
+ l && (this.fullWorkerDrawCount++, await this.thread.localLayer.parent.prepareRender, this.thread.post({
936
+ consumeCount: this.consumeCount,
937
+ fullWorkerDrawCount: this.fullWorkerDrawCount,
938
+ sp: l.op && [{ ...l, scenePath: i }] || void 0
939
+ }));
940
+ }
941
+ break;
942
+ }
943
+ }
944
+ }
945
+ enqueueBitmapEraserRender(e, t, s, r) {
946
+ const i = this.bitmapEraserRenderQueue.then(async () => {
947
+ this.combineDrawActiveId !== e || !this.getWorkShape(e) || await this.postEraserBatch(
948
+ e,
949
+ t,
950
+ s,
951
+ r
952
+ );
953
+ });
954
+ return this.bitmapEraserRenderQueue = i.catch(() => {
955
+ }), i;
956
+ }
957
+ async drawPencilEraserCombine(e, t, s) {
958
+ await this.postEraserBatch(e, t, s);
959
+ }
960
+ async postEraserBatch(e, t, s, r) {
961
+ var o, a, n, c, d, l;
962
+ const i = (o = this.workShapes.get(e)) == null ? void 0 : o.combineConsume({ workerEngine: this });
963
+ if (i || r) {
964
+ const p = i == null ? void 0 : i.willDeleteNodes, f = i == null ? void 0 : i.willNewNodes, w = [
965
+ ...p ? Array.from(p) : []
966
+ ], S = {
967
+ render: [],
968
+ sp: [],
969
+ ...r
970
+ };
971
+ if (i != null && i.rect) {
972
+ const I = Te(i.rect);
973
+ (a = S.render) == null || a.push(
974
+ {
975
+ rect: I,
976
+ isClear: !0,
977
+ clearCanvas: re.Bg,
978
+ viewId: this.thread.viewId,
979
+ eraseTxnId: e
980
+ },
981
+ {
982
+ rect: I,
983
+ drawCanvas: re.Bg,
984
+ viewId: this.thread.viewId,
985
+ eraseTxnId: e
986
+ }
987
+ );
988
+ }
989
+ if (s && w.length && ((n = this.getWorkShape(e)) == null ? void 0 : n.toolsType) === m.BitMapEraser && ((c = S.sp) == null || c.push({
990
+ type: h.None,
991
+ collectUndoTicker: s,
992
+ undoScopeIds: w,
993
+ undoScopePhase: "collect"
994
+ })), p != null && p.size && ((d = S.sp) == null || d.push({
995
+ type: h.RemoveNode,
996
+ removeIds: [...p],
997
+ viewId: this.thread.viewId,
998
+ needUndoTicker: t,
999
+ collectUndoTicker: s
1000
+ })), f != null && f.size)
1001
+ for (const [I, W] of f)
1002
+ (l = S.sp) == null || l.push({
1003
+ type: h.FullWork,
1004
+ dataType: R.Local,
1005
+ toolsType: W.toolsType,
1006
+ ops: j(W.op),
1007
+ opt: W.opt,
1008
+ workId: I,
1009
+ updateNodeOpt: {
1010
+ useAnimation: !1
1011
+ },
1012
+ needUndoTicker: t,
1013
+ collectUndoTicker: s
1014
+ });
1015
+ i != null && i.rect && await this.thread.fullLayer.parent.prepareRender, this.thread.post(S);
1016
+ }
1017
+ }
1018
+ async consumeDrawAll(e, t) {
1019
+ var a, n, c;
1020
+ this.eraserBatchScheduler.cancel();
1021
+ const { workId: s, scenePath: r, isLockSentEventCursor: i } = e, o = e.needUndoTicker;
1022
+ if (s) {
1023
+ this.combineDrawActiveId === s.toString() && (this.combineDrawActiveId = void 0);
1024
+ const d = s.toString();
1025
+ this.drawWorkActiveId === d && (this.drawWorkActiveId = void 0);
1026
+ const l = this.workShapes.get(d);
1027
+ if (!l)
1028
+ return;
1029
+ const p = l.toolsType;
1030
+ if (p === m.LaserPen)
1031
+ return;
1032
+ p === m.BitMapEraser && await this.bitmapEraserRenderQueue;
1033
+ const f = this.workShapes.get(H), w = (a = f == null ? void 0 : f.selectIds) == null ? void 0 : a[0], S = await l.consumeAll({ data: e, workerEngine: this });
1034
+ switch (p) {
1035
+ case m.Selector:
1036
+ {
1037
+ S.selectIds && w && ((n = S.selectIds) != null && n.includes(w)) && f.cursorBlur();
1038
+ const I = [];
1039
+ i && I.push({
1040
+ type: h.None,
1041
+ isLockSentEventCursor: i,
1042
+ scenePath: r
1043
+ }), S.type === h.Select && (S.selectIds && t.runReverseSelectWork(S.selectIds), I.push({ ...S, scenePath: r })), I.length && this.thread.post({ sp: I }), (c = l.selectIds) != null && c.length ? l.clearTmpPoints() : this.clearWorkShapeNodeCache(d);
1044
+ }
1045
+ break;
1046
+ case m.PencilEraser:
1047
+ await this.drawPencilEraserFull(
1048
+ l,
1049
+ S,
1050
+ i,
1051
+ o
1052
+ ), this.fullWorkerDrawCount = 0, this.clearWorkShapeNodeCache(d);
1053
+ break;
1054
+ case m.BitMapEraser:
1055
+ await this.drawBitMapEraserFull(
1056
+ l,
1057
+ S,
1058
+ i,
1059
+ o
1060
+ ), this.fullWorkerDrawCount = 0, this.clearWorkShapeNodeCache(d);
1061
+ break;
1062
+ case m.Eraser:
1063
+ await this.drawEraser(
1064
+ { ...S, scenePath: r },
1065
+ i,
1066
+ o,
1067
+ e.collectUndoTicker
1068
+ ), l.clearTmpPoints();
1069
+ break;
1070
+ case m.Arrow:
1071
+ case m.Straight:
1072
+ case m.Ellipse:
1073
+ case m.Rectangle:
1074
+ case m.Star:
1075
+ case m.Polygon:
1076
+ case m.SpeechBalloon:
1077
+ case m.Pencil: {
1078
+ const I = [];
1079
+ if (i && I.push({
1080
+ type: h.None,
1081
+ isLockSentEventCursor: i,
1082
+ scenePath: r
1083
+ }), S) {
1084
+ const g = p === m.Pencil && S.type === h.RemoveNode;
1085
+ g ? $(
1086
+ this.thread.localLayer,
1087
+ this.thread.localLayer.parent
1088
+ ) : I.push({
1089
+ ...S,
1090
+ scenePath: r,
1091
+ needUndoTicker: e.needUndoTicker,
1092
+ undoScopeIds: S.undoScopeIds || (S.workId !== void 0 ? [S.workId.toString()] : void 0),
1093
+ undoScopePhase: S.undoScopePhase || "finalize"
1094
+ }), this.fullWorkerDrawCount = 0, e.needUndoTicker && I.push({
1095
+ type: h.None,
1096
+ needUndoTicker: e.needUndoTicker,
1097
+ scenePath: r,
1098
+ undoScopeIds: g ? [] : void 0,
1099
+ undoScopePhase: g ? "finalize" : void 0
1100
+ }), (g ? this.thread.localLayer : this.thread.fullLayer).parent.prepareRender.then(() => {
1101
+ this.thread.post({
1102
+ fullWorkerDrawCount: this.fullWorkerDrawCount,
1103
+ sp: I
1104
+ });
1105
+ });
1106
+ }
1107
+ this.clearWorkShapeNodeCache(d);
1108
+ break;
1109
+ }
1110
+ }
1111
+ }
1112
+ }
1113
+ getToolsOpt() {
1114
+ return this.tmpOpt;
1115
+ }
1116
+ setToolsOpt(e) {
1117
+ var t, s;
1118
+ if ((t = this.tmpOpt) != null && t.toolsType && this.tmpOpt.toolsType !== e.toolsType) {
1119
+ this.eraserBatchScheduler.cancel();
1120
+ for (const [r, i] of this.workShapes)
1121
+ r !== D && (i.clearTmpPoints(), this.workShapes.delete(r));
1122
+ }
1123
+ this.tmpOpt = e, (s = e.toolsOpt) != null && s.syncUnitTime && (this.syncUnitTime = e.toolsOpt.syncUnitTime);
1124
+ }
1125
+ setWorkOptions(e, t) {
1126
+ let s = this.workShapes.get(e);
1127
+ if (!s && this.tmpOpt) {
1128
+ const { toolsType: r } = this.tmpOpt;
1129
+ this.tmpOpt.toolsOpt = t, s = this.createWorkShapeNode({ workId: e, toolsType: r, toolsOpt: t }), s && this.workShapes.set(e, s), this.setToolsOpt(this.tmpOpt);
1130
+ }
1131
+ t.syncUnitTime || (t.syncUnitTime = this.syncUnitTime), s == null || s.setWorkOptions(t);
1132
+ }
1133
+ createWorkShapeNode(e) {
1134
+ return Z(
1135
+ {
1136
+ ...e,
1137
+ vNodes: this.vNodes,
1138
+ fullLayer: this.thread.fullLayer,
1139
+ drawLayer: this.thread.localLayer
1140
+ },
1141
+ this.thread.serviceWork
1142
+ );
1143
+ }
1144
+ }
1145
+ class Fe {
1146
+ constructor(e) {
1147
+ u(this, "vNodes");
1148
+ u(this, "thread");
1149
+ u(this, "workShapes", /* @__PURE__ */ new Map());
1150
+ u(this, "selectorWorkShapes", /* @__PURE__ */ new Map());
1151
+ u(this, "willRunEffectSelectorIds", /* @__PURE__ */ new Set());
1152
+ u(this, "syncUnitTime", K.syncOpt.interval);
1153
+ u(this, "animationScheduler");
1154
+ u(this, "effectScheduler");
1155
+ this.vNodes = e.vNodes, this.thread = e.thread, this.animationScheduler = new ie({
1156
+ run: async () => {
1157
+ try {
1158
+ await this.animationDraw();
1159
+ } catch (t) {
1160
+ throw Y(t, {
1161
+ business: "service-render",
1162
+ stage: "animation-draw"
1163
+ });
1164
+ }
1165
+ },
1166
+ schedule: (t) => requestAnimationFrame(t),
1167
+ cancel: (t) => cancelAnimationFrame(t),
1168
+ onError: (t) => this.thread.reportRendererBusinessError(t, {
1169
+ business: "service-render",
1170
+ stage: "animation-draw"
1171
+ })
1172
+ }), this.effectScheduler = new ie({
1173
+ run: async () => {
1174
+ try {
1175
+ await this.effectRunSelector();
1176
+ } catch (t) {
1177
+ throw Y(t, {
1178
+ business: "service-render",
1179
+ stage: "selector-effect"
1180
+ });
1181
+ }
1182
+ },
1183
+ schedule: (t) => setTimeout(t, 0),
1184
+ cancel: (t) => clearTimeout(t),
1185
+ onError: (t) => this.thread.reportRendererBusinessError(t, {
1186
+ business: "service-render",
1187
+ stage: "selector-effect"
1188
+ })
1189
+ });
1190
+ }
1191
+ async loadImageBitMap(e) {
1192
+ return await this.thread.loadImageBitMap(e);
1193
+ }
1194
+ destroy() {
1195
+ this.animationScheduler.destroy(), this.effectScheduler.destroy(), this.clearAll();
1196
+ }
1197
+ clearAll() {
1198
+ this.thread.serviceLayer.children.length && (this.thread.serviceLayer.parent.children.forEach((e) => {
1199
+ e.name !== "viewport" && e.remove();
1200
+ }), $(
1201
+ this.thread.serviceLayer,
1202
+ this.thread.serviceLayer.parent
1203
+ )), this.workShapes.clear(), this.selectorWorkShapes.clear(), this.willRunEffectSelectorIds.clear();
1204
+ }
1205
+ runEffect() {
1206
+ this.effectScheduler.request();
1207
+ }
1208
+ effectRunSelector() {
1209
+ this.willRunEffectSelectorIds.forEach((e) => {
1210
+ var s, r;
1211
+ const t = this.selectorWorkShapes.get(e);
1212
+ t && t.selectIds && ((s = t.node) == null || s.selectServiceNode(
1213
+ e,
1214
+ t,
1215
+ !0
1216
+ )), (r = t == null ? void 0 : t.selectIds) != null && r.length || this.selectorWorkShapes.delete(e);
1217
+ }), this.willRunEffectSelectorIds.clear();
1218
+ }
1219
+ runSelectWork(e) {
1220
+ this.activeSelectorShape(e);
1221
+ const { workId: t } = e, s = t == null ? void 0 : t.toString();
1222
+ s && this.willRunEffectSelectorIds.add(s), this.runEffect();
1223
+ }
1224
+ removeWork(e) {
1225
+ const { workId: t } = e, s = t == null ? void 0 : t.toString();
1226
+ if (s) {
1227
+ if (this.workShapes.get(s)) {
1228
+ this.workShapes.delete(s), this.removeNode(s, e);
1229
+ return;
1230
+ }
1231
+ this.removeNode(s, e);
1232
+ }
1233
+ }
1234
+ consumeFull(e) {
1235
+ if (this.activeWorkShape(e), e.refreshTextRender) {
1236
+ this.runAnimation(!0);
1237
+ return;
1238
+ }
1239
+ this.runAnimation();
1240
+ }
1241
+ runReverseSelectWork(e) {
1242
+ e.forEach((t) => {
1243
+ this.selectorWorkShapes.forEach((s, r) => {
1244
+ var i;
1245
+ if ((i = s.selectIds) != null && i.length) {
1246
+ const o = s.selectIds.indexOf(t);
1247
+ o > -1 && (s.selectIds.splice(o, 1), this.willRunEffectSelectorIds.add(r));
1248
+ }
1249
+ });
1250
+ }), this.willRunEffectSelectorIds.size && this.runEffect();
1251
+ }
1252
+ consumeDraw(e) {
1253
+ this.activeWorkShape(e), this.runAnimation();
1254
+ }
1255
+ computNextAnimationIndex(e, t) {
1256
+ const s = Math.floor(
1257
+ (e.animationWorkData || []).slice(e.animationIndex).length * 32 / t / this.syncUnitTime
1258
+ ) * t;
1259
+ return Math.min(
1260
+ (e.animationIndex || 0) + (s || t),
1261
+ (e.animationWorkData || []).length
1262
+ );
1263
+ }
1264
+ async animationDraw() {
1265
+ var r, i, o, a, n, c, d, l, p, f, w, S, I, W, g, y, v, T, C, L, A;
1266
+ let e = !1;
1267
+ const t = /* @__PURE__ */ new Map(), s = [];
1268
+ for (const [E, k] of this.workShapes.entries())
1269
+ switch (k.toolsType) {
1270
+ case m.BackgroundSVG: {
1271
+ (r = k.node) == null || r.consumeService({
1272
+ isFullWork: !0,
1273
+ replaceId: E
1274
+ }), this.selectorWorkShapes.forEach((O, B) => {
1275
+ var P;
1276
+ (P = O.selectIds) != null && P.includes(E) && (this.willRunEffectSelectorIds.add(B), this.runEffect());
1277
+ }), this.workShapes.delete(E);
1278
+ break;
1279
+ }
1280
+ case m.Image: {
1281
+ await ((i = k.node) == null ? void 0 : i.consumeServiceAsync({
1282
+ isFullWork: !0,
1283
+ worker: this
1284
+ })), this.selectorWorkShapes.forEach((O, B) => {
1285
+ var P;
1286
+ (P = O.selectIds) != null && P.includes(E) && (this.willRunEffectSelectorIds.add(B), this.runEffect());
1287
+ }), this.workShapes.delete(E);
1288
+ break;
1289
+ }
1290
+ case m.Text: {
1291
+ k.node && (await ((o = k.node) == null ? void 0 : o.consumeServiceAsync({
1292
+ isFullWork: !0,
1293
+ replaceId: E,
1294
+ boxRect: this.thread.getSceneRect()
1295
+ })), this.selectorWorkShapes.forEach((O, B) => {
1296
+ var P;
1297
+ (P = O.selectIds) != null && P.includes(E) && (this.willRunEffectSelectorIds.add(B), this.runEffect());
1298
+ }), k.refreshTextRender && k.requestId && s.push({
1299
+ type: h.RefreshTextRender,
1300
+ requestId: k.requestId,
1301
+ workId: E,
1302
+ viewId: this.thread.viewId
1303
+ }), (a = k.node) == null || a.clearTmpPoints(), this.workShapes.delete(E));
1304
+ break;
1305
+ }
1306
+ case m.Arrow:
1307
+ case m.Straight:
1308
+ case m.Rectangle:
1309
+ case m.Ellipse:
1310
+ case m.Star:
1311
+ case m.Polygon:
1312
+ case m.SpeechBalloon: {
1313
+ const O = !!k.ops;
1314
+ if ((n = k.animationWorkData) != null && n.length) {
1315
+ const B = k.oldRect;
1316
+ (c = k.node) == null || c.consumeService({
1317
+ op: k.animationWorkData,
1318
+ isFullWork: O
1319
+ }), O && (this.selectorWorkShapes.forEach((P, F) => {
1320
+ var U;
1321
+ (U = P.selectIds) != null && U.includes(E) && (this.willRunEffectSelectorIds.add(F), this.runEffect());
1322
+ }), (d = k.node) == null || d.clearTmpPoints(), this.workShapes.delete(E)), k.isEnableCursor ? t.set(E, {
1323
+ cursorTool: _(k.toolsType),
1324
+ workState: B ? k.ops ? b.Done : b.Doing : b.Start,
1325
+ op: k.animationWorkData.slice(-3, -1)
1326
+ }) : O && !k.useAnimation && ((l = k.updateNodeOpt) != null && l.useAnimation) && t.set(E, {
1327
+ cursorTool: _(k.toolsType),
1328
+ workState: b.Done,
1329
+ op: k.animationWorkData.slice(-3, -1),
1330
+ uid: (p = k.updateNodeOpt) == null ? void 0 : p.uid
1331
+ }), k.animationWorkData.length = 0;
1332
+ }
1333
+ break;
1334
+ }
1335
+ case m.Pencil: {
1336
+ if (k.useAnimation) {
1337
+ if (k.useAnimation) {
1338
+ if (k.isDel) {
1339
+ (y = k.node) == null || y.clearTmpPoints(), this.workShapes.delete(E);
1340
+ break;
1341
+ }
1342
+ const O = 3, B = this.computNextAnimationIndex(
1343
+ k,
1344
+ O
1345
+ ), P = k.isDiff ? 0 : Math.max(0, (k.animationIndex || 0) - O), F = (k.animationWorkData || []).slice(
1346
+ P,
1347
+ B
1348
+ ), U = (T = (v = k.node) == null ? void 0 : v.getWorkId()) == null ? void 0 : T.toString();
1349
+ if ((k.animationIndex || 0) < B || k.isDiff) {
1350
+ if ((C = k.node) == null || C.consumeService({
1351
+ op: F,
1352
+ isFullWork: !1
1353
+ }), k.animationIndex = B, k.isDiff && (k.isDiff = !1), F.length && k.isEnableCursor) {
1354
+ const X = F.slice(-3, -1);
1355
+ t.set(E, {
1356
+ cursorTool: _(k.toolsType),
1357
+ workState: P === 0 ? b.Start : B === ((L = k.animationWorkData) == null ? void 0 : L.length) ? b.Done : b.Doing,
1358
+ op: X
1359
+ });
1360
+ }
1361
+ } else k.ops && ((A = k.node) == null || A.consumeService({
1362
+ op: k.animationWorkData || [],
1363
+ isFullWork: !0,
1364
+ replaceId: U
1365
+ }), k.isDel = !0, k.isEnableCursor && t.set(E, {
1366
+ cursorTool: _(k.toolsType),
1367
+ workState: b.Done,
1368
+ op: F.slice(-3, -1)
1369
+ }));
1370
+ e = !0;
1371
+ break;
1372
+ }
1373
+ } else {
1374
+ const O = !!k.ops;
1375
+ if ((f = k.node) == null || f.consumeService({
1376
+ op: k.animationWorkData || [],
1377
+ isFullWork: O,
1378
+ replaceId: E
1379
+ }), (w = k.node) == null || w.updataOptService(k.updateNodeOpt), O) {
1380
+ if (!k.isEnableCursor && ((S = k.updateNodeOpt) != null && S.useAnimation) && ((I = k.animationWorkData) != null && I.length)) {
1381
+ const B = k.animationWorkData.slice(-3, -1);
1382
+ t.set(E, {
1383
+ cursorTool: _(k.toolsType),
1384
+ workState: b.Done,
1385
+ op: B,
1386
+ uid: (W = k.updateNodeOpt) == null ? void 0 : W.uid
1387
+ });
1388
+ }
1389
+ this.selectorWorkShapes.forEach((B, P) => {
1390
+ var F;
1391
+ (F = B.selectIds) != null && F.includes(E) && (this.willRunEffectSelectorIds.add(P), this.runEffect());
1392
+ }), (g = k.node) == null || g.clearTmpPoints(), this.workShapes.delete(E);
1393
+ }
1394
+ }
1395
+ break;
1396
+ }
1397
+ }
1398
+ e && this.runAnimation(), t.size && t.forEach((E, k) => {
1399
+ s.push({
1400
+ type: h.Cursor,
1401
+ uid: E.uid || k.split(de)[0],
1402
+ op: E.op,
1403
+ workState: E.workState,
1404
+ cursorTool: E.cursorTool,
1405
+ viewId: this.thread.viewId
1406
+ });
1407
+ }), s.length && this.thread.post({ sp: s });
1408
+ }
1409
+ runAnimation(e = !1) {
1410
+ this.animationScheduler.request(e);
1411
+ }
1412
+ async flush() {
1413
+ await this.animationScheduler.flush(), await this.effectScheduler.flush();
1414
+ }
1415
+ hasDiffData(e, t, s) {
1416
+ const r = e.length;
1417
+ if (t.length < r)
1418
+ return !0;
1419
+ switch (s) {
1420
+ case m.Pencil: {
1421
+ for (let i = 0; i < r; i += 3)
1422
+ if (t[i] !== e[i] || t[i + 1] !== e[i + 1])
1423
+ return !0;
1424
+ break;
1425
+ }
1426
+ case m.LaserPen: {
1427
+ for (let i = 0; i < r; i += 2)
1428
+ if (t[i] !== e[i] || t[i + 1] !== e[i + 1])
1429
+ return !0;
1430
+ break;
1431
+ }
1432
+ }
1433
+ return !1;
1434
+ }
1435
+ activeWorkShape(e) {
1436
+ var W, g, y, v;
1437
+ const {
1438
+ workId: t,
1439
+ opt: s,
1440
+ toolsType: r,
1441
+ type: i,
1442
+ updateNodeOpt: o,
1443
+ ops: a,
1444
+ op: n,
1445
+ useAnimation: c,
1446
+ imageBitmap: d,
1447
+ isEnableCursor: l,
1448
+ requestId: p,
1449
+ refreshTextRender: f
1450
+ } = e;
1451
+ if (!t)
1452
+ return;
1453
+ const w = t.toString(), S = (W = this.vNodes.get(w)) == null ? void 0 : W.rect;
1454
+ if (!((g = this.workShapes) != null && g.has(w))) {
1455
+ let T = {
1456
+ toolsType: r,
1457
+ animationWorkData: n || [],
1458
+ animationIndex: 0,
1459
+ type: i,
1460
+ updateNodeOpt: o,
1461
+ ops: a,
1462
+ useAnimation: typeof c < "u" ? c : typeof (o == null ? void 0 : o.useAnimation) < "u" ? o == null ? void 0 : o.useAnimation : !0,
1463
+ oldRect: S,
1464
+ isDiff: !1,
1465
+ imageBitmap: d,
1466
+ isEnableCursor: l
1467
+ };
1468
+ r && s && (T = this.setNodeKey(w, T, r, s)), (y = this.workShapes) == null || y.set(w, T);
1469
+ }
1470
+ const I = (v = this.workShapes) == null ? void 0 : v.get(w);
1471
+ I.isEnableCursor = l, I.requestId = p, I.refreshTextRender = !!f, i && (I.type = i), a && (I.animationWorkData = V(a), I.ops = a), o && (I.updateNodeOpt = o), n && (I.isDiff = this.hasDiffData(
1472
+ I.animationWorkData || [],
1473
+ n,
1474
+ I.toolsType
1475
+ ), I.toolsType, m.Pencil, I.animationWorkData = n), I.node && I.node.getWorkId() !== w && I.node.setWorkId(w), S && (I.oldRect = S), r && s && (s.syncUnitTime && (this.syncUnitTime = s.syncUnitTime), I.toolsType !== r && r && s && this.setNodeKey(w, I, r, s), I.node && I.node.setWorkOptions(s)), d && (I.imageBitmap = d);
1476
+ }
1477
+ removeNode(e, t) {
1478
+ e.indexOf(D) > -1 && this.removeSelectWork(t), this.thread.fullLayer.getElementsByName(e).forEach((r) => {
1479
+ r.remove(), z(r, this.thread.fullLayer.parent);
1480
+ }), this.thread.serviceLayer.getElementsByName(e).forEach((r) => {
1481
+ r.remove(), z(r, this.thread.serviceLayer.parent);
1482
+ });
1483
+ const s = this.vNodes.get(e);
1484
+ s && s.toolsType === m.BackgroundSVG && this.thread.post({
1485
+ sp: [
1486
+ {
1487
+ type: h.BackgroundSVGDelete,
1488
+ toolsType: m.BackgroundSVG,
1489
+ workId: e,
1490
+ dataType: R.Service,
1491
+ viewId: this.thread.viewId
1492
+ }
1493
+ ]
1494
+ }), this.vNodes.delete(e);
1495
+ }
1496
+ removeSelectWork(e) {
1497
+ const { workId: t } = e, s = t == null ? void 0 : t.toString();
1498
+ s && (this.activeSelectorShape(e), this.willRunEffectSelectorIds.add(s)), this.runEffect();
1499
+ }
1500
+ activeSelectorShape(e) {
1501
+ var c, d, l;
1502
+ const { workId: t, opt: s, toolsType: r, type: i, selectIds: o } = e;
1503
+ if (!t)
1504
+ return;
1505
+ const a = t.toString();
1506
+ if (!((c = this.selectorWorkShapes) != null && c.has(a))) {
1507
+ let p = {
1508
+ toolsType: r,
1509
+ selectIds: o,
1510
+ type: i,
1511
+ opt: s
1512
+ };
1513
+ r && s && (p = this.setNodeKey(a, p, r, s)), (d = this.selectorWorkShapes) == null || d.set(a, p);
1514
+ }
1515
+ const n = (l = this.selectorWorkShapes) == null ? void 0 : l.get(a);
1516
+ i && (n.type = i), n.node && n.node.getWorkId() !== a && n.node.setWorkId(a), n.selectIds = o || [];
1517
+ }
1518
+ setNodeKey(e, t, s, r) {
1519
+ return t.toolsType = s, t.node = Z(
1520
+ {
1521
+ toolsType: s,
1522
+ toolsOpt: r,
1523
+ vNodes: this.vNodes,
1524
+ fullLayer: this.thread.fullLayer,
1525
+ drawLayer: this.thread.serviceLayer,
1526
+ workId: e
1527
+ },
1528
+ this
1529
+ ), t;
1530
+ }
1531
+ }
1532
+ class x {
1533
+ constructor() {
1534
+ u(this, "localWork");
1535
+ u(this, "serviceWork");
1536
+ u(this, "threadEngine");
1537
+ }
1538
+ registerMainThread(e) {
1539
+ return this.threadEngine = e, this.localWork = e.localWork, this.serviceWork = e.serviceWork, this;
1540
+ }
1541
+ }
1542
+ class Ue extends x {
1543
+ constructor() {
1544
+ super(...arguments);
1545
+ u(this, "emitEventType", N.CopyNode);
1546
+ }
1547
+ async consume(t) {
1548
+ const { msgType: s, dataType: r, emitEventType: i } = t;
1549
+ if (s === h.FullWork && r === R.Local && i === this.emitEventType)
1550
+ return await this.consumeForLocalWorker(t), !0;
1551
+ }
1552
+ async consumeForLocalWorker(t) {
1553
+ var r;
1554
+ const { workId: s } = t;
1555
+ s && await ((r = this.localWork) == null ? void 0 : r.consumeFull(t));
1556
+ }
1557
+ }
1558
+ class xe extends x {
1559
+ constructor() {
1560
+ super(...arguments);
1561
+ u(this, "emitEventType", N.SetColorNode);
1562
+ }
1563
+ async consume(t) {
1564
+ const { msgType: s, dataType: r, emitEventType: i } = t;
1565
+ if (s === h.UpdateNode && r === R.Local && i === this.emitEventType)
1566
+ return await this.consumeForLocalWorker(t), !0;
1567
+ }
1568
+ async consumeForLocalWorker(t) {
1569
+ var l;
1570
+ const {
1571
+ workId: s,
1572
+ updateNodeOpt: r,
1573
+ willRefreshSelector: i,
1574
+ willSyncService: o,
1575
+ willSerializeData: a,
1576
+ textUpdateForWoker: n,
1577
+ needUndoTicker: c,
1578
+ requestId: d
1579
+ } = t;
1580
+ s === D && r && await ((l = this.localWork) == null ? void 0 : l.updateSelector({
1581
+ updateSelectorOpt: r,
1582
+ willRefreshSelector: i,
1583
+ willSyncService: o,
1584
+ willSerializeData: a,
1585
+ emitEventType: this.emitEventType,
1586
+ requestId: d,
1587
+ textUpdateForWoker: n,
1588
+ callback: this.updateSelectorCallback,
1589
+ needUndoTicker: c
1590
+ }));
1591
+ }
1592
+ updateSelectorCallback(t) {
1593
+ const { param: s, postData: r, newServiceStore: i } = t, {
1594
+ willSyncService: o,
1595
+ isSync: a,
1596
+ textUpdateForWoker: n,
1597
+ needUndoTicker: c,
1598
+ requestId: d,
1599
+ emitEventType: l
1600
+ } = s, p = r.sp || [];
1601
+ if (o)
1602
+ for (const [f, w] of i.entries())
1603
+ n && w.toolsType === m.Text ? p.push({
1604
+ ...w,
1605
+ workId: f,
1606
+ type: h.TextUpdate,
1607
+ dataType: R.Local,
1608
+ willSyncService: !0,
1609
+ emitEventType: l,
1610
+ requestId: d,
1611
+ needUndoTicker: c
1612
+ }) : p.push({
1613
+ ...w,
1614
+ workId: f,
1615
+ type: h.UpdateNode,
1616
+ updateNodeOpt: {
1617
+ useAnimation: !1
1618
+ },
1619
+ isSync: a,
1620
+ emitEventType: l,
1621
+ requestId: d,
1622
+ needUndoTicker: c
1623
+ });
1624
+ return c && p.push({
1625
+ type: h.None,
1626
+ needUndoTicker: c
1627
+ }), {
1628
+ sp: p
1629
+ };
1630
+ }
1631
+ }
1632
+ class Ge extends x {
1633
+ constructor() {
1634
+ super(...arguments);
1635
+ u(this, "emitEventType", N.ZIndexNode);
1636
+ }
1637
+ async consume(t) {
1638
+ const { msgType: s, dataType: r, emitEventType: i } = t;
1639
+ if (s === h.UpdateNode && r === R.Local && i === this.emitEventType)
1640
+ return await this.consumeForLocalWorker(t), !0;
1641
+ }
1642
+ async consumeForLocalWorker(t) {
1643
+ var d;
1644
+ const {
1645
+ workId: s,
1646
+ updateNodeOpt: r,
1647
+ willRefreshSelector: i,
1648
+ willSyncService: o,
1649
+ willSerializeData: a,
1650
+ needUndoTicker: n,
1651
+ requestId: c
1652
+ } = t;
1653
+ s === D && r && await ((d = this.localWork) == null ? void 0 : d.updateSelector({
1654
+ updateSelectorOpt: r,
1655
+ willRefreshSelector: i,
1656
+ willSyncService: o,
1657
+ willSerializeData: a,
1658
+ emitEventType: this.emitEventType,
1659
+ requestId: c,
1660
+ callback: this.updateSelectorCallback,
1661
+ needUndoTicker: n
1662
+ }));
1663
+ }
1664
+ updateSelectorCallback(t) {
1665
+ const { param: s, postData: r, newServiceStore: i } = t, {
1666
+ willSyncService: o,
1667
+ isSync: a,
1668
+ needUndoTicker: n,
1669
+ requestId: c,
1670
+ emitEventType: d
1671
+ } = s, l = r.sp || [];
1672
+ if (o && l)
1673
+ for (const [p, f] of i.entries())
1674
+ f.toolsType === m.BackgroundSVG && l.push({
1675
+ ...f,
1676
+ workId: p,
1677
+ type: h.BackgroundSVGUpdate,
1678
+ dataType: R.Local,
1679
+ updateNodeOpt: {
1680
+ useAnimation: !1
1681
+ },
1682
+ needUndoTicker: n,
1683
+ emitEventType: d,
1684
+ requestId: c
1685
+ }), l.push({
1686
+ ...f,
1687
+ workId: p,
1688
+ type: h.UpdateNode,
1689
+ updateNodeOpt: {
1690
+ useAnimation: !1
1691
+ },
1692
+ isSync: a,
1693
+ emitEventType: d,
1694
+ requestId: c,
1695
+ needUndoTicker: n
1696
+ });
1697
+ return n && l.push({
1698
+ type: h.None,
1699
+ needUndoTicker: n
1700
+ }), {
1701
+ sp: l
1702
+ };
1703
+ }
1704
+ }
1705
+ class qe extends x {
1706
+ constructor() {
1707
+ super(...arguments);
1708
+ u(this, "emitEventType", N.TranslateNode);
1709
+ u(this, "updateSelectorCallback", (t) => {
1710
+ var W;
1711
+ const { param: s, postData: r, newServiceStore: i, workShapeNode: o, res: a, smoothSync: n } = t, {
1712
+ willSyncService: c,
1713
+ isSync: d,
1714
+ updateSelectorOpt: l,
1715
+ textUpdateForWoker: p,
1716
+ needUndoTicker: f
1717
+ } = s, w = l.workState, S = r.sp || [];
1718
+ if (w === b.Start)
1719
+ return {
1720
+ sp: [
1721
+ {
1722
+ type: h.SelectorTransform,
1723
+ emitEventType: this.emitEventType,
1724
+ workState: w
1725
+ }
1726
+ ],
1727
+ render: []
1728
+ };
1729
+ const I = a == null ? void 0 : a.selectRect;
1730
+ if (c) {
1731
+ const g = w === b.Done ? f : void 0;
1732
+ S.push({
1733
+ type: h.Select,
1734
+ selectIds: o.selectIds,
1735
+ selectRect: I,
1736
+ willSyncService: w === b.Done ? !0 : n,
1737
+ isSync: !0,
1738
+ points: w === b.Done && o.getChildrenPoints() || void 0,
1739
+ textOpt: o.textOpt,
1740
+ needUndoTicker: g,
1741
+ emitEventType: this.emitEventType
1742
+ });
1743
+ const y = {
1744
+ workState: w,
1745
+ useAnimation: l.useAnimation || !1
1746
+ };
1747
+ l.uid && (y.uid = l.uid);
1748
+ for (const [v, T] of i.entries())
1749
+ T.toolsType === m.BackgroundSVG && S.push({
1750
+ ...T,
1751
+ workId: v,
1752
+ type: h.BackgroundSVGUpdate,
1753
+ dataType: R.Local,
1754
+ willSyncService: w === b.Done ? !0 : n,
1755
+ updateNodeOpt: y,
1756
+ needUndoTicker: g,
1757
+ emitEventType: this.emitEventType
1758
+ }), p && T.toolsType === m.Text ? S.push({
1759
+ ...T,
1760
+ workId: v,
1761
+ type: h.TextUpdate,
1762
+ dataType: R.Local,
1763
+ willSyncService: w === b.Done ? !0 : n,
1764
+ updateNodeOpt: y,
1765
+ needUndoTicker: g,
1766
+ emitEventType: this.emitEventType
1767
+ }) : (n || w === b.Done) && S.push({
1768
+ ...T,
1769
+ workId: v,
1770
+ type: h.UpdateNode,
1771
+ updateNodeOpt: {
1772
+ ...y,
1773
+ eraserGeometryDelta: ee(
1774
+ (W = this.localWork) == null ? void 0 : W.vNodes,
1775
+ v
1776
+ )
1777
+ },
1778
+ isSync: d,
1779
+ needUndoTicker: g,
1780
+ emitEventType: this.emitEventType
1781
+ });
1782
+ }
1783
+ return f && S.push({
1784
+ type: h.None,
1785
+ needUndoTicker: f
1786
+ }), S.push({
1787
+ type: h.SelectorTransform,
1788
+ emitEventType: this.emitEventType,
1789
+ workState: w
1790
+ }), {
1791
+ sp: S
1792
+ };
1793
+ });
1794
+ }
1795
+ async consume(t) {
1796
+ const { msgType: s, dataType: r, emitEventType: i } = t;
1797
+ if (s === h.UpdateNode && r === R.Local && i === this.emitEventType)
1798
+ return await this.consumeForLocalWorker(t), !0;
1799
+ }
1800
+ async consumeForLocalWorker(t) {
1801
+ var p, f;
1802
+ const {
1803
+ workId: s,
1804
+ updateNodeOpt: r,
1805
+ willRefreshSelector: i,
1806
+ willSyncService: o,
1807
+ willSerializeData: a,
1808
+ textUpdateForWoker: n,
1809
+ emitEventType: c,
1810
+ smoothSync: d,
1811
+ needUndoTicker: l
1812
+ } = t;
1813
+ s === D && r && (r.workState === b.Done && (r != null && r.translate) && (r.translate[0] || r.translate[1]) || r.workState !== b.Done ? await ((p = this.localWork) == null ? void 0 : p.updateSelector({
1814
+ updateSelectorOpt: r,
1815
+ willRefreshSelector: i,
1816
+ willSyncService: o,
1817
+ willSerializeData: a,
1818
+ isSync: !0,
1819
+ textUpdateForWoker: n,
1820
+ emitEventType: c,
1821
+ callback: this.updateSelectorCallback,
1822
+ smoothSync: d,
1823
+ needUndoTicker: l
1824
+ })) : r.workState === b.Done && ((f = this.localWork) == null || f.vNodes.deleteLastTarget()));
1825
+ }
1826
+ }
1827
+ class ze extends x {
1828
+ constructor() {
1829
+ super(...arguments);
1830
+ u(this, "emitEventType", N.ScaleNode);
1831
+ u(this, "updateSelectorCallback", (t) => {
1832
+ var I;
1833
+ const { param: s, postData: r, workShapeNode: i, res: o, newServiceStore: a, smoothSync: n } = t, { updateSelectorOpt: c, willSyncService: d, needUndoTicker: l } = s, p = c.workState, f = r.sp || [], w = o == null ? void 0 : o.selectRect, S = p === b.Done ? l : void 0;
1834
+ if (p === b.Start)
1835
+ return {
1836
+ sp: [
1837
+ {
1838
+ type: h.SelectorTransform,
1839
+ emitEventType: this.emitEventType,
1840
+ workState: p
1841
+ }
1842
+ ],
1843
+ render: []
1844
+ };
1845
+ if (d) {
1846
+ f.push({
1847
+ type: h.Select,
1848
+ selectIds: i.selectIds,
1849
+ selectRect: w,
1850
+ willSyncService: p === b.Done ? !0 : n,
1851
+ isSync: !0,
1852
+ points: p === b.Done && i.getChildrenPoints() || void 0,
1853
+ textOpt: i.textOpt
1854
+ });
1855
+ const W = {
1856
+ useAnimation: c.useAnimation || !1
1857
+ };
1858
+ c.uid && (W.uid = c.uid);
1859
+ for (const [g, y] of a.entries())
1860
+ y.toolsType === m.BackgroundSVG && f.push({
1861
+ ...y,
1862
+ workId: g,
1863
+ type: h.BackgroundSVGUpdate,
1864
+ dataType: R.Local,
1865
+ willSyncService: p === b.Done ? !0 : n,
1866
+ updateNodeOpt: W,
1867
+ needUndoTicker: S,
1868
+ emitEventType: this.emitEventType
1869
+ }), y.toolsType === m.Text ? f.push({
1870
+ ...y,
1871
+ workId: g,
1872
+ type: h.TextUpdate,
1873
+ dataType: R.Local,
1874
+ willSyncService: p === b.Done ? !0 : n,
1875
+ updateNodeOpt: W,
1876
+ needUndoTicker: S,
1877
+ emitEventType: this.emitEventType
1878
+ }) : (n || p === b.Done) && f.push({
1879
+ ...y,
1880
+ workId: g,
1881
+ type: h.UpdateNode,
1882
+ updateNodeOpt: {
1883
+ ...W,
1884
+ eraserGeometryDelta: ee(
1885
+ (I = this.localWork) == null ? void 0 : I.vNodes,
1886
+ g
1887
+ )
1888
+ },
1889
+ isSync: !0,
1890
+ needUndoTicker: S,
1891
+ emitEventType: this.emitEventType
1892
+ });
1893
+ }
1894
+ return l && f.push({
1895
+ type: h.None,
1896
+ needUndoTicker: l
1897
+ }), f.push({
1898
+ type: h.SelectorTransform,
1899
+ emitEventType: this.emitEventType,
1900
+ workState: p
1901
+ }), {
1902
+ sp: f
1903
+ };
1904
+ });
1905
+ }
1906
+ async consume(t) {
1907
+ const { msgType: s, dataType: r, emitEventType: i } = t;
1908
+ if (s === h.UpdateNode && r === R.Local && i === this.emitEventType)
1909
+ return await this.consumeForLocalWorker(t), !0;
1910
+ }
1911
+ async consumeForLocalWorker(t) {
1912
+ var c;
1913
+ const {
1914
+ workId: s,
1915
+ updateNodeOpt: r,
1916
+ willSyncService: i,
1917
+ willSerializeData: o,
1918
+ smoothSync: a,
1919
+ needUndoTicker: n
1920
+ } = t;
1921
+ s === D && r && await ((c = this.localWork) == null ? void 0 : c.updateSelector({
1922
+ updateSelectorOpt: r,
1923
+ willSyncService: i,
1924
+ willSerializeData: o,
1925
+ isSync: !0,
1926
+ callback: this.updateSelectorCallback.bind(this),
1927
+ smoothSync: a,
1928
+ needUndoTicker: n
1929
+ }));
1930
+ }
1931
+ }
1932
+ class Ve extends x {
1933
+ constructor() {
1934
+ super(...arguments);
1935
+ u(this, "emitEventType", N.RotateNode);
1936
+ u(this, "updateSelectorCallback", (t) => {
1937
+ var g;
1938
+ const { param: s, postData: r, workShapeNode: i, res: o, newServiceStore: a, smoothSync: n } = t, {
1939
+ updateSelectorOpt: c,
1940
+ willSyncService: d,
1941
+ willSerializeData: l,
1942
+ isSync: p,
1943
+ needUndoTicker: f
1944
+ } = s, w = c.workState, S = r.sp || [], I = o == null ? void 0 : o.selectRect, W = w === b.Done ? f : void 0;
1945
+ if (d) {
1946
+ l && w === b.Done && S.push({
1947
+ type: h.Select,
1948
+ selectIds: i.selectIds,
1949
+ selectRect: I,
1950
+ willSyncService: w === b.Done ? !0 : n,
1951
+ isSync: p,
1952
+ points: i.getChildrenPoints()
1953
+ });
1954
+ const y = {
1955
+ useAnimation: c.useAnimation || !1
1956
+ };
1957
+ if (c.uid && (y.uid = c.uid), n || w === b.Done)
1958
+ for (const [v, T] of a.entries())
1959
+ S.push({
1960
+ ...T,
1961
+ workId: v,
1962
+ type: h.UpdateNode,
1963
+ updateNodeOpt: {
1964
+ ...y,
1965
+ eraserGeometryDelta: ee(
1966
+ (g = this.localWork) == null ? void 0 : g.vNodes,
1967
+ v
1968
+ )
1969
+ },
1970
+ isSync: p,
1971
+ needUndoTicker: W,
1972
+ emitEventType: this.emitEventType
1973
+ });
1974
+ }
1975
+ return f && S.push({
1976
+ type: h.None,
1977
+ needUndoTicker: f
1978
+ }), S.push({
1979
+ type: h.SelectorTransform,
1980
+ emitEventType: this.emitEventType,
1981
+ workState: w
1982
+ }), {
1983
+ sp: S
1984
+ };
1985
+ });
1986
+ }
1987
+ async consume(t) {
1988
+ const { msgType: s, dataType: r, emitEventType: i } = t;
1989
+ if (s === h.UpdateNode && r === R.Local && i === this.emitEventType)
1990
+ return await this.consumeForLocalWorker(t), !0;
1991
+ }
1992
+ async consumeForLocalWorker(t) {
1993
+ var l;
1994
+ const {
1995
+ workId: s,
1996
+ updateNodeOpt: r,
1997
+ willRefreshSelector: i,
1998
+ willSyncService: o,
1999
+ willSerializeData: a,
2000
+ emitEventType: n,
2001
+ smoothSync: c,
2002
+ needUndoTicker: d
2003
+ } = t;
2004
+ s === D && r && await ((l = this.localWork) == null ? void 0 : l.updateSelector({
2005
+ updateSelectorOpt: r,
2006
+ willRefreshSelector: i,
2007
+ willSyncService: o,
2008
+ willSerializeData: a,
2009
+ emitEventType: n,
2010
+ isSync: !0,
2011
+ callback: this.updateSelectorCallback,
2012
+ smoothSync: c,
2013
+ needUndoTicker: d
2014
+ }));
2015
+ }
2016
+ }
2017
+ class $e extends x {
2018
+ constructor() {
2019
+ super(...arguments);
2020
+ u(this, "emitEventType", N.SetFontStyle);
2021
+ }
2022
+ async consume(t) {
2023
+ const { msgType: s, dataType: r, emitEventType: i } = t;
2024
+ if (s === h.UpdateNode && r === R.Local && i === this.emitEventType)
2025
+ return await this.consumeForLocalWorker(t), !0;
2026
+ }
2027
+ async consumeForLocalWorker(t) {
2028
+ var d;
2029
+ const {
2030
+ workId: s,
2031
+ updateNodeOpt: r,
2032
+ willRefreshSelector: i,
2033
+ willSyncService: o,
2034
+ willSerializeData: a,
2035
+ textUpdateForWoker: n,
2036
+ requestId: c
2037
+ } = t;
2038
+ s === D && r && await ((d = this.localWork) == null ? void 0 : d.updateSelector({
2039
+ updateSelectorOpt: r,
2040
+ willRefreshSelector: i,
2041
+ willSyncService: o,
2042
+ willSerializeData: a,
2043
+ emitEventType: this.emitEventType,
2044
+ requestId: c,
2045
+ textUpdateForWoker: n,
2046
+ callback: this.updateSelectorCallback
2047
+ }));
2048
+ }
2049
+ updateSelectorCallback(t) {
2050
+ const { param: s, postData: r, newServiceStore: i, workShapeNode: o, res: a } = t, {
2051
+ willSyncService: n,
2052
+ isSync: c,
2053
+ updateSelectorOpt: d,
2054
+ textUpdateForWoker: l,
2055
+ requestId: p,
2056
+ emitEventType: f
2057
+ } = s, w = r.sp || [], S = a == null ? void 0 : a.selectRect;
2058
+ if (n && w) {
2059
+ d.fontSize && w.push({
2060
+ type: h.Select,
2061
+ selectIds: o.selectIds,
2062
+ selectRect: S,
2063
+ willSyncService: n,
2064
+ isSync: c,
2065
+ points: o.getChildrenPoints()
2066
+ });
2067
+ for (const [I, W] of i.entries())
2068
+ l && W.toolsType === m.Text ? w.push({
2069
+ ...W,
2070
+ workId: I,
2071
+ type: h.TextUpdate,
2072
+ dataType: R.Local,
2073
+ willSyncService: !0,
2074
+ emitEventType: f,
2075
+ requestId: p
2076
+ }) : w.push({
2077
+ ...W,
2078
+ workId: I,
2079
+ type: h.UpdateNode,
2080
+ updateNodeOpt: {
2081
+ useAnimation: !1
2082
+ },
2083
+ isSync: c,
2084
+ emitEventType: f,
2085
+ requestId: p
2086
+ });
2087
+ }
2088
+ return {
2089
+ sp: w
2090
+ };
2091
+ }
2092
+ }
2093
+ class _e extends x {
2094
+ constructor() {
2095
+ super(...arguments);
2096
+ u(this, "emitEventType", N.SetPoint);
2097
+ u(this, "updateSelectorCallback", (t) => {
2098
+ const { param: s, postData: r, newServiceStore: i, workShapeNode: o, res: a } = t, { willSyncService: n, isSync: c, needUndoTicker: d } = s, l = r.sp || [], p = a == null ? void 0 : a.selectRect;
2099
+ if (n && l) {
2100
+ for (const [f, w] of i.entries())
2101
+ l.push({
2102
+ ...w,
2103
+ workId: f,
2104
+ type: h.UpdateNode,
2105
+ updateNodeOpt: {
2106
+ useAnimation: !1
2107
+ },
2108
+ isSync: c,
2109
+ needUndoTicker: d,
2110
+ emitEventType: this.emitEventType
2111
+ });
2112
+ l.push({
2113
+ type: h.Select,
2114
+ selectIds: o.selectIds,
2115
+ selectRect: p,
2116
+ willSyncService: n,
2117
+ isSync: c,
2118
+ points: o.getChildrenPoints()
2119
+ });
2120
+ }
2121
+ return d && l.push({
2122
+ type: h.None,
2123
+ needUndoTicker: d
2124
+ }), l.push({
2125
+ type: h.SelectorTransform,
2126
+ emitEventType: this.emitEventType,
2127
+ workState: s.updateSelectorOpt.workState
2128
+ }), {
2129
+ sp: l
2130
+ };
2131
+ });
2132
+ }
2133
+ async consume(t) {
2134
+ const { msgType: s, dataType: r, emitEventType: i } = t;
2135
+ if (s === h.UpdateNode && r === R.Local && i === this.emitEventType)
2136
+ return await this.consumeForLocalWorker(t), !0;
2137
+ }
2138
+ async consumeForLocalWorker(t) {
2139
+ var d;
2140
+ const {
2141
+ workId: s,
2142
+ updateNodeOpt: r,
2143
+ willRefreshSelector: i,
2144
+ willSyncService: o,
2145
+ willSerializeData: a,
2146
+ textUpdateForWoker: n,
2147
+ needUndoTicker: c
2148
+ } = t;
2149
+ s === D && r && await ((d = this.localWork) == null ? void 0 : d.updateSelector({
2150
+ updateSelectorOpt: r,
2151
+ willRefreshSelector: i,
2152
+ willSyncService: o,
2153
+ emitEventType: this.emitEventType,
2154
+ willSerializeData: a,
2155
+ isSync: !0,
2156
+ textUpdateForWoker: n,
2157
+ callback: this.updateSelectorCallback,
2158
+ needUndoTicker: c
2159
+ }));
2160
+ }
2161
+ }
2162
+ class He extends x {
2163
+ constructor() {
2164
+ super(...arguments);
2165
+ u(this, "emitEventType", N.SetLock);
2166
+ }
2167
+ async consume(t) {
2168
+ const { msgType: s, dataType: r, emitEventType: i } = t;
2169
+ if (s === h.UpdateNode && r === R.Local && i === this.emitEventType)
2170
+ return await this.consumeForLocalWorker(t), !0;
2171
+ }
2172
+ async consumeForLocalWorker(t) {
2173
+ var d;
2174
+ const {
2175
+ workId: s,
2176
+ updateNodeOpt: r,
2177
+ willRefreshSelector: i,
2178
+ willSyncService: o,
2179
+ willSerializeData: a,
2180
+ needUndoTicker: n,
2181
+ requestId: c
2182
+ } = t;
2183
+ s === D && r && await ((d = this.localWork) == null ? void 0 : d.updateSelector({
2184
+ updateSelectorOpt: r,
2185
+ willRefreshSelector: i,
2186
+ willSyncService: o,
2187
+ willSerializeData: a,
2188
+ emitEventType: this.emitEventType,
2189
+ requestId: c,
2190
+ callback: this.updateSelectorCallback,
2191
+ needUndoTicker: n
2192
+ }));
2193
+ }
2194
+ updateSelectorCallback(t) {
2195
+ const { param: s, postData: r, newServiceStore: i, workShapeNode: o, res: a } = t, {
2196
+ willSyncService: n,
2197
+ isSync: c,
2198
+ updateSelectorOpt: d,
2199
+ needUndoTicker: l,
2200
+ requestId: p,
2201
+ emitEventType: f
2202
+ } = s, w = r.sp || [], S = a == null ? void 0 : a.selectRect;
2203
+ if (n && w) {
2204
+ for (const [I, W] of i.entries())
2205
+ w.push({
2206
+ ...W,
2207
+ workId: I,
2208
+ type: h.UpdateNode,
2209
+ updateNodeOpt: {
2210
+ useAnimation: !1
2211
+ },
2212
+ isSync: c,
2213
+ emitEventType: f,
2214
+ requestId: p
2215
+ });
2216
+ w.push({
2217
+ isLocked: d.isLocked,
2218
+ selectorColor: o.selectorColor,
2219
+ scaleType: o.scaleType,
2220
+ canRotate: o.canRotate,
2221
+ type: h.Select,
2222
+ selectIds: o.selectIds,
2223
+ selectRect: S,
2224
+ willSyncService: n,
2225
+ isSync: c
2226
+ });
2227
+ }
2228
+ return l && w.push({
2229
+ type: h.None,
2230
+ needUndoTicker: l
2231
+ }), {
2232
+ sp: w
2233
+ };
2234
+ }
2235
+ }
2236
+ class Qe extends x {
2237
+ constructor() {
2238
+ super(...arguments);
2239
+ u(this, "emitEventType", N.SetShapeOpt);
2240
+ }
2241
+ async consume(t) {
2242
+ const { msgType: s, dataType: r, emitEventType: i } = t;
2243
+ if (s === h.UpdateNode && r === R.Local && i === this.emitEventType)
2244
+ return await this.consumeForLocalWorker(t), !0;
2245
+ }
2246
+ async consumeForLocalWorker(t) {
2247
+ var d;
2248
+ const {
2249
+ workId: s,
2250
+ updateNodeOpt: r,
2251
+ willRefreshSelector: i,
2252
+ willSyncService: o,
2253
+ willSerializeData: a,
2254
+ needUndoTicker: n,
2255
+ requestId: c
2256
+ } = t;
2257
+ s === D && r && await ((d = this.localWork) == null ? void 0 : d.updateSelector({
2258
+ updateSelectorOpt: r,
2259
+ willRefreshSelector: i,
2260
+ willSyncService: o,
2261
+ willSerializeData: a,
2262
+ emitEventType: this.emitEventType,
2263
+ requestId: c,
2264
+ callback: this.updateSelectorCallback,
2265
+ needUndoTicker: n
2266
+ }));
2267
+ }
2268
+ updateSelectorCallback(t) {
2269
+ const { param: s, postData: r, newServiceStore: i } = t, {
2270
+ willSyncService: o,
2271
+ isSync: a,
2272
+ needUndoTicker: n,
2273
+ requestId: c,
2274
+ emitEventType: d
2275
+ } = s, l = r.sp || [];
2276
+ if (o && l)
2277
+ for (const [p, f] of i.entries())
2278
+ l.push({
2279
+ ...f,
2280
+ workId: p,
2281
+ type: h.UpdateNode,
2282
+ updateNodeOpt: {
2283
+ useAnimation: !1
2284
+ },
2285
+ isSync: a,
2286
+ emitEventType: d,
2287
+ requestId: c
2288
+ });
2289
+ return n && l.push({
2290
+ type: h.None,
2291
+ needUndoTicker: n
2292
+ }), {
2293
+ sp: l
2294
+ };
2295
+ }
2296
+ }
2297
+ class Ke {
2298
+ constructor(e) {
2299
+ u(this, "builders", /* @__PURE__ */ new Map());
2300
+ this.builders = new Map(e.map((t) => [t, this.build(t)]));
2301
+ }
2302
+ build(e) {
2303
+ switch (e) {
2304
+ case N.TranslateNode:
2305
+ return new qe();
2306
+ case N.ZIndexNode:
2307
+ return new Ge();
2308
+ case N.CopyNode:
2309
+ return new Ue();
2310
+ case N.SetColorNode:
2311
+ return new xe();
2312
+ case N.ScaleNode:
2313
+ return new ze();
2314
+ case N.RotateNode:
2315
+ return new Ve();
2316
+ case N.SetFontStyle:
2317
+ return new $e();
2318
+ case N.SetPoint:
2319
+ return new _e();
2320
+ case N.SetLock:
2321
+ return new He();
2322
+ case N.SetShapeOpt:
2323
+ return new Qe();
2324
+ }
2325
+ }
2326
+ registerForMainThread(e) {
2327
+ return this.builders.forEach((t) => {
2328
+ t && t.registerMainThread(e);
2329
+ }), this;
2330
+ }
2331
+ async consumeForMainThread(e) {
2332
+ for (const t of this.builders.values())
2333
+ if (await (t == null ? void 0 : t.consume(e)))
2334
+ return !0;
2335
+ return !1;
2336
+ }
2337
+ }
2338
+ function Xe({
2339
+ subThreads: M,
2340
+ layers: e,
2341
+ scene: t,
2342
+ removeLayerTexture: s
2343
+ }) {
2344
+ let r;
2345
+ for (const i of M)
2346
+ try {
2347
+ i.destroy();
2348
+ } catch (o) {
2349
+ r || (r = o);
2350
+ }
2351
+ for (const i of e) {
2352
+ const o = i.parent;
2353
+ if (o)
2354
+ try {
2355
+ s(i, o);
2356
+ } catch (a) {
2357
+ r || (r = a);
2358
+ }
2359
+ try {
2360
+ i.remove();
2361
+ } catch (a) {
2362
+ r || (r = a);
2363
+ }
2364
+ }
2365
+ try {
2366
+ t.remove();
2367
+ } catch (i) {
2368
+ r || (r = i);
2369
+ }
2370
+ if (r)
2371
+ throw r;
2372
+ }
2373
+ const Ye = 4;
2374
+ class Ze {
2375
+ constructor(e = {}) {
2376
+ u(this, "pendingFrames", /* @__PURE__ */ new Map());
2377
+ u(this, "failedCanvases", /* @__PURE__ */ new Set());
2378
+ u(this, "tailFrameCount");
2379
+ u(this, "requestFrame");
2380
+ u(this, "cancelFrame");
2381
+ u(this, "onReadbackError");
2382
+ u(this, "frameHandle");
2383
+ u(this, "destroyed", !1);
2384
+ this.tailFrameCount = Math.max(
2385
+ 1,
2386
+ Math.floor(e.tailFrameCount ?? Ye)
2387
+ ), this.requestFrame = e.requestFrame ?? ((t) => requestAnimationFrame(t)), this.cancelFrame = e.cancelFrame ?? ((t) => cancelAnimationFrame(t)), this.onReadbackError = e.onReadbackError;
2388
+ }
2389
+ markRendered(e) {
2390
+ this.destroyed || (this.pendingFrames.set(e, this.tailFrameCount), this.schedule());
2391
+ }
2392
+ destroy() {
2393
+ this.destroyed || (this.destroyed = !0, this.frameHandle !== void 0 && (this.cancelFrame(this.frameHandle), this.frameHandle = void 0), this.pendingFrames.clear(), this.failedCanvases.clear());
2394
+ }
2395
+ schedule() {
2396
+ this.destroyed || this.frameHandle !== void 0 || this.pendingFrames.size === 0 || (this.frameHandle = this.requestFrame(() => this.flushFrame()));
2397
+ }
2398
+ flushFrame() {
2399
+ var e, t;
2400
+ if (this.frameHandle = void 0, !this.destroyed) {
2401
+ for (const [s, r] of this.pendingFrames) {
2402
+ if (!s.isConnected || s.width <= 0 || s.height <= 0) {
2403
+ this.pendingFrames.delete(s);
2404
+ continue;
2405
+ }
2406
+ try {
2407
+ (e = s.getContext("2d")) == null || e.getImageData(0, 0, 1, 1);
2408
+ } catch (i) {
2409
+ this.failedCanvases.has(s) || (this.failedCanvases.add(s), (t = this.onReadbackError) == null || t.call(this, i));
2410
+ }
2411
+ r <= 1 ? this.pendingFrames.delete(s) : this.pendingFrames.set(s, r - 1);
2412
+ }
2413
+ this.schedule();
2414
+ }
2415
+ }
2416
+ }
2417
+ class je {
2418
+ constructor(e, t) {
2419
+ u(this, "viewId");
2420
+ u(this, "fullLayer");
2421
+ u(this, "topLayer");
2422
+ u(this, "localLayer");
2423
+ u(this, "serviceLayer");
2424
+ u(this, "snapshotFullLayer");
2425
+ u(this, "vNodes");
2426
+ u(this, "master");
2427
+ u(this, "opt");
2428
+ u(this, "cameraOpt");
2429
+ u(this, "scene");
2430
+ u(this, "localWork");
2431
+ u(this, "serviceWork");
2432
+ u(this, "topWork");
2433
+ u(this, "taskUpdateCameraId");
2434
+ u(this, "taskUpdateCameraResolve");
2435
+ u(this, "debounceUpdateCameraId");
2436
+ u(this, "activeCameraSettledRender");
2437
+ u(this, "activeCameraTaskContext");
2438
+ u(this, "debounceUpdateCache", /* @__PURE__ */ new Set());
2439
+ u(this, "mainThreadPostId");
2440
+ u(this, "destroyed", !1);
2441
+ u(this, "readbackScheduler");
2442
+ u(this, "restoreLayerRenderCallbacks", []);
2443
+ u(this, "viewOwner");
2444
+ u(this, "viewGeneration");
2445
+ u(this, "combinePostMsg", /* @__PURE__ */ new Set());
2446
+ u(this, "methodBuilder");
2447
+ u(this, "cacheImages", /* @__PURE__ */ new Map());
2448
+ u(this, "imageResolveMap", /* @__PURE__ */ new Map());
2449
+ this.viewId = e, this.opt = t, this.viewOwner = t.viewOwner, this.viewGeneration = t.viewGeneration, this.scene = this.createScene({
2450
+ ...t.canvasOpt,
2451
+ container: t.container
2452
+ }), this.master = t.master;
2453
+ const s = K.bufferSize.full, r = K.bufferSize.sub;
2454
+ this.fullLayer = this.createLayer("fullLayer", this.scene, {
2455
+ ...t.layerOpt,
2456
+ bufferSize: this.viewId === Q ? s : r * 2
2457
+ }), this.topLayer = this.createLayer("topLayer", this.scene, {
2458
+ ...t.layerOpt,
2459
+ bufferSize: (this.viewId === Q, r),
2460
+ contextType: "2d"
2461
+ }), this.localLayer = this.createLayer("localLayer", this.scene, {
2462
+ ...t.layerOpt,
2463
+ bufferSize: (this.viewId === Q, r),
2464
+ contextType: "2d"
2465
+ }), this.serviceLayer = this.createLayer("serviceLayer", this.scene, {
2466
+ ...t.layerOpt,
2467
+ bufferSize: (this.viewId === Q, r),
2468
+ contextType: "2d"
2469
+ }), t.mainThreadRenderMode === "readback" && this.enableCanvas2DReadback([
2470
+ this.fullLayer.parent,
2471
+ this.topLayer.parent,
2472
+ this.localLayer.parent,
2473
+ this.serviceLayer.parent
2474
+ ]), this.vNodes = new We(e, this.scene);
2475
+ const i = {
2476
+ thread: this,
2477
+ vNodes: this.vNodes
2478
+ };
2479
+ this.localWork = new Ae(i), this.serviceWork = new Fe(i), this.topWork = new Pe(i), this.vNodes.init(this.fullLayer), this.methodBuilder = new Ke([
2480
+ N.CopyNode,
2481
+ N.SetColorNode,
2482
+ N.DeleteNode,
2483
+ N.RotateNode,
2484
+ N.ScaleNode,
2485
+ N.TranslateNode,
2486
+ N.ZIndexNode,
2487
+ N.SetFontStyle,
2488
+ N.SetPoint,
2489
+ N.SetLock,
2490
+ N.SetShapeOpt
2491
+ ]).registerForMainThread(this);
2492
+ }
2493
+ getCachedImages(e) {
2494
+ var t;
2495
+ return (t = this.cacheImages.get(e)) == null ? void 0 : t.imageSource;
2496
+ }
2497
+ getCachedImagesByWorkId(e) {
2498
+ for (const [t, s] of this.cacheImages.entries())
2499
+ if (t === e && s.imageSource)
2500
+ return s.imageSource;
2501
+ }
2502
+ deleteCachedImagesByWorkId(e) {
2503
+ for (const [t, s] of this.cacheImages.entries())
2504
+ s.workId === e && (this.closeImageSource(s.imageSource), this.cacheImages.delete(t));
2505
+ }
2506
+ clearCacheImages() {
2507
+ this.cacheImages.forEach((e) => this.closeImageSource(e.imageSource)), this.cacheImages.clear();
2508
+ }
2509
+ closeImageSource(e) {
2510
+ if (!(!("close" in e) || typeof e.close != "function"))
2511
+ try {
2512
+ e.close();
2513
+ } catch {
2514
+ }
2515
+ }
2516
+ clearImageResolveMap(e) {
2517
+ this.imageResolveMap.forEach(({ timer: t, resolve: s, reject: r }, i) => {
2518
+ t && clearTimeout(t), e !== void 0 ? r == null || r(e) : s == null || s(i);
2519
+ }), this.imageResolveMap.clear();
2520
+ }
2521
+ post(e) {
2522
+ this.destroyed || (this.combinePostMsg.add(e), this.runBatchPostData());
2523
+ }
2524
+ reportRendererBusinessError(e, t) {
2525
+ const s = Y(e, t);
2526
+ s.reported || (s.reported = !0, this.post({
2527
+ sp: [
2528
+ {
2529
+ type: h.ReportError,
2530
+ viewId: this.viewId,
2531
+ viewGeneration: this.viewGeneration,
2532
+ reportErrorCode: "RENDERER_RUNTIME_ERROR",
2533
+ reportStage: s.stage,
2534
+ reportError: s.message,
2535
+ reportString: Re(s, t, {
2536
+ viewId: this.viewId,
2537
+ renderer: "mainThread"
2538
+ })
2539
+ }
2540
+ ]
2541
+ }));
2542
+ }
2543
+ updateDpr(e) {
2544
+ this.scene.displayRatio = e;
2545
+ }
2546
+ async on(e) {
2547
+ var t, s, r, i, o;
2548
+ if (this.destroyed) {
2549
+ try {
2550
+ (t = e.imageBitmap) == null || t.close();
2551
+ } catch {
2552
+ }
2553
+ return;
2554
+ }
2555
+ if (!await this.methodBuilder.consumeForMainThread(e)) {
2556
+ const {
2557
+ msgType: a,
2558
+ toolsType: n,
2559
+ opt: c,
2560
+ dataType: d,
2561
+ workId: l,
2562
+ workState: p,
2563
+ imageSrc: f,
2564
+ imageBitmap: w,
2565
+ imageElement: S,
2566
+ workIds: I,
2567
+ isLockSentEventCursor: W,
2568
+ requestId: g
2569
+ } = e, y = l == null ? void 0 : l.toString();
2570
+ switch (a) {
2571
+ case h.UpdateDpr:
2572
+ G(e.dpr) && this.updateDpr(e.dpr);
2573
+ break;
2574
+ case h.AuthClear: {
2575
+ const { clearUids: v, localUid: T } = e;
2576
+ this.vNodes.setCanClearUids(v), this.vNodes.setLocalUid(T);
2577
+ break;
2578
+ }
2579
+ case h.Destroy:
2580
+ this.destroy();
2581
+ break;
2582
+ case h.CancelBitmapEraser: {
2583
+ const v = y;
2584
+ v && this.localWork.abortBitmapEraser(v);
2585
+ break;
2586
+ }
2587
+ case h.Clear:
2588
+ this.clearAll();
2589
+ break;
2590
+ case h.ServiceSyncCommit: {
2591
+ if (this.destroyed) break;
2592
+ const v = [
2593
+ (s = this.fullLayer) == null ? void 0 : s.parent,
2594
+ (r = this.topLayer) == null ? void 0 : r.parent,
2595
+ (i = this.localLayer) == null ? void 0 : i.parent,
2596
+ (o = this.serviceLayer) == null ? void 0 : o.parent
2597
+ ].filter(Boolean);
2598
+ try {
2599
+ await be(
2600
+ (async () => {
2601
+ await Le(
2602
+ this.serviceWork,
2603
+ v
2604
+ );
2605
+ })(),
2606
+ {
2607
+ business: "service-sync-commit",
2608
+ stage: "flush-and-prepare-render",
2609
+ timeoutMs: 5e3
2610
+ }
2611
+ );
2612
+ } catch (T) {
2613
+ throw Y(T, {
2614
+ business: "service-sync-commit",
2615
+ stage: "flush-and-prepare-render"
2616
+ });
2617
+ }
2618
+ if (this.destroyed) break;
2619
+ this.post({
2620
+ sp: [
2621
+ {
2622
+ type: h.ServiceSyncCommit,
2623
+ viewId: this.viewId,
2624
+ viewGeneration: this.viewGeneration,
2625
+ scenePath: e.scenePath,
2626
+ serviceSyncId: e.serviceSyncId,
2627
+ serviceRevision: e.serviceRevision
2628
+ }
2629
+ ]
2630
+ });
2631
+ break;
2632
+ }
2633
+ case h.UpdateCamera:
2634
+ try {
2635
+ await Ee(
2636
+ e.cameraTaskId,
2637
+ async (v) => {
2638
+ this.activeCameraTaskContext = v;
2639
+ try {
2640
+ await this.updateCamera(e, v);
2641
+ } finally {
2642
+ this.activeCameraTaskContext === v && (this.activeCameraTaskContext = void 0);
2643
+ }
2644
+ }
2645
+ );
2646
+ } catch (v) {
2647
+ throw this.cameraOpt = void 0, this.cancelCameraSettledRender(), v;
2648
+ }
2649
+ break;
2650
+ case h.UpdateTools:
2651
+ if (n && c) {
2652
+ const v = {
2653
+ toolsType: n,
2654
+ toolsOpt: c
2655
+ };
2656
+ this.topWork.canUseTopLayer(n) ? this.topWork.setToolsOpt(v) : this.localWork.setToolsOpt(v);
2657
+ }
2658
+ break;
2659
+ case h.CreateWork:
2660
+ if (y && c && n) {
2661
+ if (this.topWork.canUseTopLayer(n)) {
2662
+ this.topWork.getToolsOpt() || this.topWork.setToolsOpt({
2663
+ toolsType: n,
2664
+ toolsOpt: c
2665
+ }), this.topWork.setWorkOptions(y, c);
2666
+ break;
2667
+ }
2668
+ this.localWork.getToolsOpt() || this.localWork.setToolsOpt({
2669
+ toolsType: n,
2670
+ toolsOpt: c
2671
+ }), this.localWork.setWorkOptions(y, c);
2672
+ }
2673
+ break;
2674
+ case h.DrawWork:
2675
+ p === b.Done && d === R.Local ? (await this.consumeDrawAll(d, e), n === m.LaserPen && W && this.post({
2676
+ sp: [
2677
+ {
2678
+ type: h.None,
2679
+ isLockSentEventCursor: W
2680
+ }
2681
+ ]
2682
+ })) : await this.consumeDraw(d, e);
2683
+ break;
2684
+ case h.UpdateNode:
2685
+ case h.FullWork:
2686
+ if (n && this.topWork.canUseTopLayer(n)) {
2687
+ await this.consumeDrawAll(d, e);
2688
+ break;
2689
+ }
2690
+ await this.consumeFull(d, e);
2691
+ break;
2692
+ case h.RemoveNode:
2693
+ await this.removeNode(e);
2694
+ return;
2695
+ case h.Select:
2696
+ d === R.Local || l === D ? await this.localWork.updateFullSelectWork(e) : d === R.Service && this.serviceWork.runSelectWork(e);
2697
+ break;
2698
+ case h.CursorBlur:
2699
+ this.localWork.cursorBlur();
2700
+ return;
2701
+ case h.CursorHover:
2702
+ this.localWork.cursorHover(e);
2703
+ break;
2704
+ case h.GetTextActive:
2705
+ d === R.Local && this.localWork.checkTextActive(e);
2706
+ break;
2707
+ case h.GetImageBitMap:
2708
+ if (f && (w || S) && l) {
2709
+ const v = this.imageResolveMap.get(f);
2710
+ if (!v || v.requestId !== g) {
2711
+ this.closeImageSource(w || S);
2712
+ break;
2713
+ }
2714
+ const T = l.toString();
2715
+ this.deleteCachedImagesByWorkId(T), this.cacheImages.set(f, {
2716
+ imageSource: w || S,
2717
+ workId: T
2718
+ });
2719
+ const { resolve: C, timer: L } = v;
2720
+ L && clearTimeout(L), C && C(f);
2721
+ }
2722
+ break;
2723
+ case h.GetVNodeInfo:
2724
+ if (l && I) {
2725
+ const v = I.map((T) => this.vNodes.get(T));
2726
+ this.post({
2727
+ sp: [
2728
+ {
2729
+ type: h.GetVNodeInfo,
2730
+ dataType: R.Local,
2731
+ workId: l,
2732
+ vInfo: v
2733
+ }
2734
+ ]
2735
+ });
2736
+ }
2737
+ break;
2738
+ }
2739
+ }
2740
+ }
2741
+ getIconSize(e, t, s) {
2742
+ const r = e * s, i = t * s;
2743
+ return r <= 50 || i <= 50 ? [50, 50] : r <= 100 || i <= 100 ? [100, 100] : r <= 200 || i <= 200 ? [200, 200] : r <= 400 || i <= 400 ? [400, 400] : r <= 800 || i <= 800 ? [800, 800] : [1600, 1600];
2744
+ }
2745
+ async loadImageBitMap(e) {
2746
+ const { toolsType: t, opt: s, workId: r, requestId: i } = e;
2747
+ if (t === m.Image && s && r) {
2748
+ const o = r.toString(), { src: a, type: n, width: c, height: d, strokeColor: l } = s;
2749
+ if (!a || !n || !c || !d)
2750
+ return;
2751
+ let p = a;
2752
+ if (n === pe.Iconify) {
2753
+ const [y, v] = this.getIconSize(c, d, this.opt.displayer.dpr);
2754
+ p = ue(a, y, v, l);
2755
+ }
2756
+ if (this.cacheImages.has(p)) {
2757
+ const y = this.getCachedImages(p);
2758
+ if (y)
2759
+ return y;
2760
+ }
2761
+ const w = this.imageResolveMap.get(p);
2762
+ if (w)
2763
+ return await w.promise, this.getCachedImages(p);
2764
+ let S, I;
2765
+ const W = new Promise((y, v) => {
2766
+ S = y, I = v;
2767
+ }), g = {
2768
+ resolve: S,
2769
+ reject: I,
2770
+ timer: void 0,
2771
+ requestId: i,
2772
+ promise: W
2773
+ };
2774
+ return g.timer = setTimeout(() => {
2775
+ this.imageResolveMap.get(p) === g && S(p);
2776
+ }, 5e3), this.imageResolveMap.set(p, g), this.opt.post({
2777
+ sp: [
2778
+ {
2779
+ imageSrc: p,
2780
+ workId: o,
2781
+ viewId: this.viewId,
2782
+ isgl: !!this.fullLayer.parent.gl,
2783
+ isSubWorker: !1,
2784
+ requestId: i,
2785
+ type: h.GetImageBitMap
2786
+ }
2787
+ ]
2788
+ }), await W, this.imageResolveMap.get(p) === g && this.imageResolveMap.delete(p), this.getCachedImages(p);
2789
+ }
2790
+ }
2791
+ async removeNode(e) {
2792
+ const { dataType: t, workId: s, removeIds: r } = e, i = r || [];
2793
+ if (s && i.push(s.toString()), i.length)
2794
+ for (const o of i) {
2795
+ if (o === D) {
2796
+ await this.localWork.removeSelector(e);
2797
+ continue;
2798
+ }
2799
+ t === R.Local ? this.localWork.removeWork(e) : t === R.Service && this.serviceWork.removeWork(e), await this.localWork.colloctEffectSelectWork(e);
2800
+ }
2801
+ }
2802
+ async consumeFull(e, t) {
2803
+ const s = await this.localWork.colloctEffectSelectWork(t);
2804
+ s && e === R.Local && await this.localWork.consumeFull(s), s && e === R.Service && this.serviceWork.consumeFull(s);
2805
+ }
2806
+ setCameraOpt(e) {
2807
+ this.cameraOpt = e;
2808
+ const { scale: t, centerX: s, centerY: r, width: i, height: o } = e;
2809
+ (i !== this.scene.width || o !== this.scene.height) && this.updateScene({ width: i, height: o }), this.fullLayer.setAttribute("scale", [t, t]), this.fullLayer.setAttribute("translate", [-s, -r]), this.topLayer.setAttribute("scale", [t, t]), this.topLayer.setAttribute("translate", [-s, -r]), this.localLayer.setAttribute("scale", [t, t]), this.localLayer.setAttribute("translate", [-s, -r]), this.serviceLayer.setAttribute("scale", [t, t]), this.serviceLayer.setAttribute("translate", [-s, -r]);
2810
+ }
2811
+ runBatchPostData() {
2812
+ this.destroyed || this.mainThreadPostId || (this.mainThreadPostId = requestAnimationFrame(
2813
+ this.combinePost.bind(this)
2814
+ ));
2815
+ }
2816
+ combinePostData() {
2817
+ var i;
2818
+ this.mainThreadPostId = void 0;
2819
+ const e = [];
2820
+ let t, s, r;
2821
+ for (const o of this.combinePostMsg.values()) {
2822
+ if ((i = o.sp) != null && i.length)
2823
+ for (const a of o.sp) {
2824
+ const n = {
2825
+ ...a,
2826
+ viewId: a.viewId || this.viewId,
2827
+ viewGeneration: a.viewGeneration ?? this.viewGeneration
2828
+ };
2829
+ let c = !1;
2830
+ for (const d of e)
2831
+ if (le(n, d)) {
2832
+ c = !0;
2833
+ break;
2834
+ }
2835
+ c || e.push(n);
2836
+ }
2837
+ G(o.fullWorkerDrawCount) && (t = o.fullWorkerDrawCount), G(o.consumeCount) && (s = o.consumeCount, r = o.consumeWorkId);
2838
+ }
2839
+ return this.combinePostMsg.clear(), {
2840
+ sp: e,
2841
+ fullWorkerDrawCount: t,
2842
+ consumeCount: s,
2843
+ consumeWorkId: r,
2844
+ viewId: this.viewId,
2845
+ viewGeneration: this.viewGeneration,
2846
+ viewOwner: this.viewOwner
2847
+ };
2848
+ }
2849
+ combinePost() {
2850
+ var s, r;
2851
+ if (this.destroyed) {
2852
+ this.mainThreadPostId = void 0, this.combinePostMsg.clear();
2853
+ return;
2854
+ }
2855
+ const e = this.combinePostData(), t = (s = e.sp) == null ? void 0 : s.filter(
2856
+ (i) => i.type !== h.None || i.isLockSentEventCursor || i.needUndoTicker || i.collectUndoTicker || i.eraseTxnPhase
2857
+ );
2858
+ t != null && t.length ? e.sp = t.map((i) => ({
2859
+ ...i,
2860
+ viewId: i.viewId || this.viewId,
2861
+ viewGeneration: i.viewGeneration ?? this.viewGeneration
2862
+ })) : delete e.sp, e.consumeCount === void 0 && (delete e.consumeCount, delete e.consumeWorkId), e.fullWorkerDrawCount === void 0 && delete e.fullWorkerDrawCount, !this.destroyed && (e != null && e.consumeCount || e != null && e.fullWorkerDrawCount || (r = e.sp) != null && r.length) && this.opt.post(e);
2863
+ }
2864
+ clearAll() {
2865
+ this.cancelCameraSettledRender(), this.debounceUpdateCache.clear(), this.fullLayer.children.length && (this.fullLayer.parent.children.forEach((e) => {
2866
+ e.name !== "viewport" && e.remove();
2867
+ }), $(this.fullLayer, this.fullLayer.parent)), this.clearCacheImages(), this.clearImageResolveMap(), this.localWork.clearAll(), this.topWork.clearAll(), this.serviceWork.clearAll(), this.vNodes.clear(), this.post({
2868
+ sp: [
2869
+ {
2870
+ type: h.Clear
2871
+ }
2872
+ ]
2873
+ });
2874
+ }
2875
+ async consumeDrawAll(e, t) {
2876
+ const { toolsType: s, workId: r } = t;
2877
+ if (r) {
2878
+ const i = r.toString();
2879
+ if (s && this.topWork.canUseTopLayer(s)) {
2880
+ e === R.Local && (this.topWork.getLocalWorkShape(
2881
+ r.toString()
2882
+ ) || this.topWork.createLocalWork(t)), await this.topWork.consumeDrawAll(t);
2883
+ return;
2884
+ }
2885
+ e === R.Local && (this.localWork.getWorkShape(i) || this.localWork.createLocalWork(t), await this.localWork.consumeDrawAll(t, this.serviceWork));
2886
+ }
2887
+ }
2888
+ async consumeDraw(e, t) {
2889
+ const { opt: s, workId: r, toolsType: i } = t;
2890
+ if (r && i && s) {
2891
+ const o = r.toString();
2892
+ if (this.topWork.canUseTopLayer(i)) {
2893
+ e === R.Local && (this.topWork.getLocalWorkShape(o) || this.topWork.createLocalWork(t)), this.topWork.consumeDraw(t);
2894
+ return;
2895
+ }
2896
+ e === R.Local ? (this.localWork.getWorkShape(o) || this.localWork.createLocalWork(t), await this.localWork.consumeDraw(t, this.serviceWork)) : e === R.Service && this.serviceWork.consumeDraw(t);
2897
+ return;
2898
+ }
2899
+ }
2900
+ async updateCamera(e, t) {
2901
+ var d, l;
2902
+ const { cameraOpt: s, scenePath: r, cameraTaskId: i } = e;
2903
+ let o = !1;
2904
+ const a = this.cameraOpt, n = !!(s && le(a, s));
2905
+ let c = !1;
2906
+ if (s && n) {
2907
+ const p = !!this.activeCameraSettledRender;
2908
+ if (this.retargetCameraSettledRender(i)) return;
2909
+ if (!p) {
2910
+ G(i) && this.opt.post({
2911
+ viewId: this.viewId,
2912
+ viewGeneration: this.viewGeneration,
2913
+ cameraSettledTaskId: i
2914
+ });
2915
+ return;
2916
+ }
2917
+ c = !0;
2918
+ }
2919
+ if (s && (!n || c)) {
2920
+ this.cancelCameraSettledRender();
2921
+ const p = !a || a.scale !== s.scale || a.centerX !== s.centerX || a.centerY !== s.centerY || a.width !== s.width || a.height !== s.height;
2922
+ if (this.taskUpdateCameraId && (clearTimeout(this.taskUpdateCameraId), this.taskUpdateCameraId = void 0, (d = this.taskUpdateCameraResolve) == null || d.call(this, !1), this.taskUpdateCameraResolve = void 0), r) {
2923
+ let y;
2924
+ do {
2925
+ y = [];
2926
+ for (const [T, C] of this.localWork.getWorkShapes().entries())
2927
+ switch (C.toolsType) {
2928
+ case m.Text:
2929
+ case m.BitMapEraser:
2930
+ case m.PencilEraser:
2931
+ case m.Eraser:
2932
+ case m.Selector:
2933
+ case m.LaserPen:
2934
+ break;
2935
+ default:
2936
+ T !== H && T !== D && y.push(`${T}:${C.toolsType}`);
2937
+ break;
2938
+ }
2939
+ if (t && (t.stage = "blocking-works", t.blockingWorks = y), !y.length) break;
2940
+ if (!await new Promise((T) => {
2941
+ this.taskUpdateCameraResolve = T, this.taskUpdateCameraId = setTimeout(() => {
2942
+ this.taskUpdateCameraId = void 0, this.taskUpdateCameraResolve = void 0, T(!0);
2943
+ }, oe);
2944
+ }) || t != null && t.aborted) return;
2945
+ } while (y.length);
2946
+ t && (t.stage = "update-camera", t.blockingWorks = void 0);
2947
+ }
2948
+ if (t != null && t.aborted) return;
2949
+ const f = /* @__PURE__ */ new Map();
2950
+ for (const [y, v] of this.vNodes.getNodesByType(m.Text).entries()) {
2951
+ const T = v.rect;
2952
+ f.set(y, J(T));
2953
+ }
2954
+ for (const [y, v] of this.vNodes.gethasEraserNodes().entries())
2955
+ f.set(y, J(v.rect));
2956
+ const w = new Set(f.keys());
2957
+ let S = !1;
2958
+ if (this.localWork.hasSelector()) {
2959
+ const y = (l = this.localWork.getSelector()) == null ? void 0 : l.selectIds;
2960
+ if (y) {
2961
+ S = !0;
2962
+ for (const v of y)
2963
+ w.add(v);
2964
+ }
2965
+ }
2966
+ let I = !1;
2967
+ if (this.serviceWork.selectorWorkShapes.size)
2968
+ for (const y of this.serviceWork.selectorWorkShapes.values()) {
2969
+ const v = y.selectIds;
2970
+ if (v) {
2971
+ I = !0;
2972
+ for (const T of v)
2973
+ w.add(T);
2974
+ }
2975
+ }
2976
+ if (this.setCameraOpt(s), this.vNodes.curNodeMap.size) {
2977
+ this.vNodes.clearTarget(), this.vNodes.updateHighLevelNodesRect(w);
2978
+ for (const [T, C] of f.entries()) {
2979
+ const L = this.vNodes.get(T);
2980
+ if (L) {
2981
+ const A = C, E = L.rect, k = this.getSceneRect(), O = ae(A, k), B = ae(E, k);
2982
+ let P = c;
2983
+ (O !== B || A.w !== E.w || A.h !== E.h || B === Ce.intersect) && (P = !0);
2984
+ const { toolsType: F, opt: U } = L, X = U.eraserlines && U.eraserlines.length > 0 || !!U.eraserFaces;
2985
+ (P || p && X) && (F === m.Text || X) && this.debounceUpdateCache.add(T);
2986
+ }
2987
+ }
2988
+ if (S && this.localWork.reRenderSelector(), I)
2989
+ for (const [
2990
+ T,
2991
+ C
2992
+ ] of this.serviceWork.selectorWorkShapes.entries())
2993
+ this.serviceWork.runSelectWork({
2994
+ workId: T,
2995
+ selectIds: C.selectIds,
2996
+ msgType: h.Select,
2997
+ dataType: R.Service,
2998
+ viewId: this.viewId
2999
+ });
3000
+ o = !0;
3001
+ const y = { cancelled: !1, cameraTaskId: i, context: t };
3002
+ this.activeCameraSettledRender = y;
3003
+ const v = () => !this.destroyed && !y.cancelled && this.activeCameraSettledRender === y && !(t != null && t.aborted);
3004
+ this.debounceUpdateCameraId = setTimeout(async () => {
3005
+ var T;
3006
+ this.debounceUpdateCameraId = void 0;
3007
+ try {
3008
+ if (!v()) return;
3009
+ const C = [];
3010
+ for (const E of this.debounceUpdateCache.values()) {
3011
+ const k = (T = this.fullLayer) == null ? void 0 : T.getElementsByName(E)[0], O = this.vNodes.get(E);
3012
+ if (O) {
3013
+ const { toolsType: B, opt: P } = O;
3014
+ if (k || B !== m.Text) {
3015
+ const F = this.localWork.setFullWork({
3016
+ toolsType: B,
3017
+ opt: P,
3018
+ workId: E
3019
+ });
3020
+ if (F) {
3021
+ if (B === m.Text)
3022
+ C.push(
3023
+ F.consumeServiceAsync({
3024
+ isFullWork: !0,
3025
+ replaceId: E,
3026
+ boxRect: this.getSceneRect(),
3027
+ isActive: v
3028
+ })
3029
+ );
3030
+ else if (P.eraserlines && P.eraserlines.length > 0 || P.eraserFaces) {
3031
+ const U = F.consumeService({
3032
+ op: O.op,
3033
+ isFullWork: !0,
3034
+ replaceId: E,
3035
+ workState: b.Done
3036
+ });
3037
+ this.localWork.clearWorkShapeNodeCache(E), C.push(Promise.resolve(U));
3038
+ }
3039
+ }
3040
+ }
3041
+ }
3042
+ this.debounceUpdateCache.delete(E);
3043
+ }
3044
+ if (await Promise.all(C), !v()) return;
3045
+ this.vNodes.updateLowLevelNodesRect(), this.vNodes.clearHighLevelIds();
3046
+ const L = [
3047
+ this.fullLayer.parent.prepareRender,
3048
+ this.topLayer.parent.prepareRender,
3049
+ this.localLayer.parent.prepareRender,
3050
+ this.serviceLayer.parent.prepareRender
3051
+ ];
3052
+ if (y.renderLayers = [
3053
+ this.fullLayer.parent,
3054
+ this.topLayer.parent,
3055
+ this.localLayer.parent,
3056
+ this.serviceLayer.parent
3057
+ ], await Promise.all(L), y.renderLayers = void 0, !v()) return;
3058
+ const A = y.cameraTaskId;
3059
+ G(A) && this.opt.post({
3060
+ viewId: this.viewId,
3061
+ viewGeneration: this.viewGeneration,
3062
+ cameraSettledTaskId: A
3063
+ });
3064
+ } catch (C) {
3065
+ if (!v()) return;
3066
+ const L = {
3067
+ code: "RENDERER_BUSINESS_ERROR",
3068
+ business: "updateCamera",
3069
+ message: C instanceof Error ? C.message : String(C),
3070
+ stage: "camera-settled-render",
3071
+ workerType: "mainThread"
3072
+ }, A = y.cameraTaskId;
3073
+ if (G(A))
3074
+ try {
3075
+ this.opt.post({
3076
+ viewId: this.viewId,
3077
+ viewGeneration: this.viewGeneration,
3078
+ cameraTaskId: A,
3079
+ cameraTaskStatus: "failed",
3080
+ cameraTaskError: L
3081
+ });
3082
+ } catch (E) {
3083
+ console.error(
3084
+ `[cameraSettledRender] failed ACK post failed viewId=${this.viewId} taskId=${A}`,
3085
+ E
3086
+ );
3087
+ }
3088
+ } finally {
3089
+ y.renderLayers = void 0, this.activeCameraSettledRender === y && (this.activeCameraSettledRender = void 0);
3090
+ }
3091
+ }, oe);
3092
+ }
3093
+ const W = [
3094
+ this.fullLayer.parent,
3095
+ this.topLayer.parent,
3096
+ this.localLayer.parent,
3097
+ this.serviceLayer.parent
3098
+ ], g = Promise.all(
3099
+ W.map((y) => y.prepareRender)
3100
+ );
3101
+ if (t && !await Ne(
3102
+ t,
3103
+ g,
3104
+ "prepare-render"
3105
+ )) {
3106
+ W.filter(
3107
+ (v) => v.cancelPendingRender()
3108
+ ).forEach((v) => v.render()), g.catch(() => {
3109
+ });
3110
+ return;
3111
+ }
3112
+ if (t || await g, t != null && t.aborted) return;
3113
+ !o && G(i) && this.opt.post({
3114
+ viewId: this.viewId,
3115
+ viewGeneration: this.viewGeneration,
3116
+ cameraSettledTaskId: i
3117
+ });
3118
+ }
3119
+ }
3120
+ getSceneRect() {
3121
+ const { width: e, height: t } = this.scene;
3122
+ return {
3123
+ x: 0,
3124
+ y: 0,
3125
+ w: Math.floor(e),
3126
+ h: Math.floor(t)
3127
+ };
3128
+ }
3129
+ createScene(e) {
3130
+ return new fe({
3131
+ displayRatio: this.opt.displayer.dpr,
3132
+ depth: !1,
3133
+ desynchronized: !0,
3134
+ ...e,
3135
+ autoRender: !0,
3136
+ id: this.viewId,
3137
+ contextType: "2d"
3138
+ });
3139
+ }
3140
+ createLayer(e, t, s) {
3141
+ const { width: r, height: i } = s, o = `canvas-${e}`, a = t.layer(o, { ...s, offscreen: !1 }), n = new me({
3142
+ anchor: [0.5, 0.5],
3143
+ pos: [r * 0.5, i * 0.5],
3144
+ size: [r, i],
3145
+ name: "viewport",
3146
+ id: e
3147
+ });
3148
+ return a.append(n), n;
3149
+ }
3150
+ enableCanvas2DReadback(e) {
3151
+ this.readbackScheduler = new Ze({
3152
+ onReadbackError: (t) => this.opt.master.Logger.warn(
3153
+ `[Appliance]: [main-thread-readback] stage=failed viewId=${this.viewId} error=${t instanceof Error ? t.message : String(t)}`
3154
+ )
3155
+ });
3156
+ for (const t of e) {
3157
+ const s = t.render, r = (...i) => {
3158
+ var n;
3159
+ const o = s.apply(t, i), a = t.canvas;
3160
+ return a instanceof HTMLCanvasElement && ((n = this.readbackScheduler) == null || n.markRendered(a)), o;
3161
+ };
3162
+ t.render = r, this.restoreLayerRenderCallbacks.push(() => {
3163
+ t.render === r && (t.render = s);
3164
+ });
3165
+ }
3166
+ }
3167
+ disableCanvas2DReadback() {
3168
+ var e;
3169
+ (e = this.readbackScheduler) == null || e.destroy(), this.readbackScheduler = void 0;
3170
+ for (const t of this.restoreLayerRenderCallbacks.splice(0)) t();
3171
+ }
3172
+ updateScene(e) {
3173
+ this.scene.attr({ ...e });
3174
+ const { width: t, height: s } = e;
3175
+ this.scene.width = t, this.scene.height = s, this.updateLayer({ width: t, height: s });
3176
+ }
3177
+ updateLayer(e) {
3178
+ const { width: t, height: s } = e;
3179
+ this.fullLayer.parent.setAttribute("width", t), this.fullLayer.parent.setAttribute("height", s), this.fullLayer.setAttribute("size", [t, s]), this.fullLayer.setAttribute("pos", [t * 0.5, s * 0.5]), this.topLayer.parent.setAttribute("width", t), this.topLayer.parent.setAttribute("height", s), this.topLayer.setAttribute("size", [t, s]), this.topLayer.setAttribute("pos", [t * 0.5, s * 0.5]), this.localLayer.parent.setAttribute("width", t), this.localLayer.parent.setAttribute("height", s), this.localLayer.setAttribute("size", [t, s]), this.localLayer.setAttribute("pos", [t * 0.5, s * 0.5]), this.serviceLayer.parent.setAttribute("width", t), this.serviceLayer.parent.setAttribute("height", s), this.serviceLayer.setAttribute("size", [t, s]), this.serviceLayer.setAttribute("pos", [t * 0.5, s * 0.5]);
3180
+ }
3181
+ cancelCameraSettledRender() {
3182
+ var t;
3183
+ this.debounceUpdateCameraId !== void 0 && (clearTimeout(this.debounceUpdateCameraId), this.debounceUpdateCameraId = void 0);
3184
+ const e = this.activeCameraSettledRender;
3185
+ e && (e.cancelled = !0, (t = e.renderLayers) == null || t.forEach((s) => s.cancelPendingRender()), this.activeCameraSettledRender = void 0);
3186
+ }
3187
+ retargetCameraSettledRender(e) {
3188
+ var s;
3189
+ if (!G(e)) return !1;
3190
+ const t = this.activeCameraSettledRender;
3191
+ return !t || t.cancelled || (s = t.context) != null && s.aborted ? !1 : (t.cameraTaskId = e, !0);
3192
+ }
3193
+ cancelCameraBlockingWait() {
3194
+ this.taskUpdateCameraId !== void 0 && (clearTimeout(this.taskUpdateCameraId), this.taskUpdateCameraId = void 0);
3195
+ const e = this.taskUpdateCameraResolve;
3196
+ this.taskUpdateCameraResolve = void 0, e == null || e(!1);
3197
+ }
3198
+ destroy() {
3199
+ var e, t;
3200
+ this.destroyed || (this.destroyed = !0, this.disableCanvas2DReadback(), (t = (e = this.activeCameraTaskContext) == null ? void 0 : e.abort) == null || t.call(e), this.activeCameraTaskContext = void 0, this.mainThreadPostId !== void 0 && (cancelAnimationFrame(this.mainThreadPostId), this.mainThreadPostId = void 0), this.combinePostMsg.clear(), this.cancelCameraBlockingWait(), this.cancelCameraSettledRender(), this.debounceUpdateCache.clear(), this.clearCacheImages(), this.clearImageResolveMap(
3201
+ new Error(`main-thread view destroyed: ${this.viewId}`)
3202
+ ), this.vNodes.clear(), Xe({
3203
+ subThreads: [this.localWork, this.serviceWork, this.topWork],
3204
+ layers: [
3205
+ this.fullLayer,
3206
+ this.topLayer,
3207
+ this.localLayer,
3208
+ this.serviceLayer
3209
+ ],
3210
+ scene: this.scene,
3211
+ removeLayerTexture: (s, r) => z(s, r)
3212
+ }));
3213
+ }
3214
+ }
3215
+ class Je {
3216
+ constructor(e, t) {
3217
+ u(this, "viewId");
3218
+ u(this, "fullLayer");
3219
+ u(this, "master");
3220
+ u(this, "opt");
3221
+ u(this, "viewOwner");
3222
+ u(this, "viewGeneration");
3223
+ u(this, "scene");
3224
+ u(this, "mainThreadPostId");
3225
+ u(this, "combinePostMsg", /* @__PURE__ */ new Set());
3226
+ u(this, "destroyed", !1);
3227
+ u(this, "workShapes", /* @__PURE__ */ new Map());
3228
+ u(this, "cacheImages", /* @__PURE__ */ new Map());
3229
+ u(this, "imageResolveMap", /* @__PURE__ */ new Map());
3230
+ u(this, "activeRequest");
3231
+ this.viewId = e, this.opt = t, this.viewOwner = t.viewOwner, this.viewGeneration = t.viewGeneration, this.scene = this.createScene({
3232
+ ...t.canvasOpt,
3233
+ container: t.container
3234
+ }), this.master = t.master, this.fullLayer = this.createLayer("fullLayer", this.scene, {
3235
+ ...t.layerOpt,
3236
+ bufferSize: this.viewId === Q ? 6e3 : 3e3,
3237
+ contextType: "2d"
3238
+ });
3239
+ }
3240
+ getCachedImages(e) {
3241
+ var t;
3242
+ return (t = this.cacheImages.get(e)) == null ? void 0 : t.imageSource;
3243
+ }
3244
+ getCachedImagesByWorkId(e) {
3245
+ for (const [t, s] of this.cacheImages.entries())
3246
+ if (t === e && s.imageSource)
3247
+ return s.imageSource;
3248
+ }
3249
+ deleteCachedImagesByWorkId(e) {
3250
+ for (const [t, s] of this.cacheImages.entries())
3251
+ s.workId === e && (this.closeImageSource(s.imageSource), this.cacheImages.delete(t));
3252
+ }
3253
+ clearCacheImages() {
3254
+ this.cacheImages.forEach((e) => this.closeImageSource(e.imageSource)), this.cacheImages.clear();
3255
+ }
3256
+ closeImageSource(e) {
3257
+ if (e && !(!("close" in e) || typeof e.close != "function"))
3258
+ try {
3259
+ e.close();
3260
+ } catch {
3261
+ }
3262
+ }
3263
+ releaseMessageImageBitmap(e) {
3264
+ this.closeImageSource(e == null ? void 0 : e.imageBitmap);
3265
+ }
3266
+ releaseBatchImageBitmaps(e) {
3267
+ var t, s;
3268
+ (t = e == null ? void 0 : e.render) == null || t.forEach(
3269
+ (r) => this.closeImageSource(r.imageBitmap)
3270
+ ), (s = e == null ? void 0 : e.sp) == null || s.forEach((r) => this.releaseMessageImageBitmap(r));
3271
+ }
3272
+ clearImageResolveMap() {
3273
+ this.imageResolveMap.forEach(({ timer: e, resolve: t }, s) => {
3274
+ e && clearTimeout(e), t == null || t(s);
3275
+ }), this.imageResolveMap.clear();
3276
+ }
3277
+ post(e) {
3278
+ if (this.destroyed) {
3279
+ this.releaseBatchImageBitmaps(e);
3280
+ return;
3281
+ }
3282
+ this.combinePostMsg.add(e), this.runBatchPostData();
3283
+ }
3284
+ reportRequestFailure(e, t, s, r, i) {
3285
+ const o = r instanceof Error ? r.message : String(r);
3286
+ try {
3287
+ this.opt.post({
3288
+ viewId: this.viewId,
3289
+ viewGeneration: this.viewGeneration,
3290
+ viewOwner: this.viewOwner,
3291
+ sp: [
3292
+ {
3293
+ type: h.ReportError,
3294
+ viewId: this.viewId,
3295
+ scenePath: s,
3296
+ requestId: t,
3297
+ reportBusiness: e,
3298
+ reportStage: i,
3299
+ reportError: o,
3300
+ reportString: `[SnapshotThreadImpl] ${e} failed; stage=${i}; requestId=${t || "unknown"}; error=${o}`
3301
+ }
3302
+ ]
3303
+ });
3304
+ } catch (a) {
3305
+ console.error(
3306
+ `[SnapshotThreadImpl] ${e} failure report failed`,
3307
+ a
3308
+ );
3309
+ }
3310
+ }
3311
+ async on(e) {
3312
+ if (this.destroyed) {
3313
+ this.closeImageSource(e.imageBitmap);
3314
+ return;
3315
+ }
3316
+ const { msgType: t, imageSrc: s, imageBitmap: r, imageElement: i, workId: o, requestId: a } = e;
3317
+ switch (t) {
3318
+ case h.Snapshot: {
3319
+ this.activeRequest = {
3320
+ business: "snapshot",
3321
+ scenePath: e.scenePath,
3322
+ requestId: e.requestId
3323
+ };
3324
+ try {
3325
+ await this.getSnapshot(e), this.flushPendingPost();
3326
+ } catch (n) {
3327
+ this.destroyed || this.reportRequestFailure(
3328
+ "snapshot",
3329
+ e.requestId,
3330
+ e.scenePath,
3331
+ n,
3332
+ "render"
3333
+ );
3334
+ } finally {
3335
+ this.activeRequest = void 0, this.destroy();
3336
+ }
3337
+ return;
3338
+ }
3339
+ case h.BoundingBox: {
3340
+ this.activeRequest = {
3341
+ business: "bounding-box",
3342
+ scenePath: e.scenePath,
3343
+ requestId: e.requestId
3344
+ };
3345
+ try {
3346
+ await this.getBoundingRect(e), this.flushPendingPost();
3347
+ } catch (n) {
3348
+ this.destroyed || this.reportRequestFailure(
3349
+ "bounding-box",
3350
+ e.requestId,
3351
+ e.scenePath,
3352
+ n,
3353
+ "compute"
3354
+ );
3355
+ } finally {
3356
+ this.activeRequest = void 0, this.destroy();
3357
+ }
3358
+ return;
3359
+ }
3360
+ case h.GetImageBitMap: {
3361
+ if (s && (r || i) && o) {
3362
+ const n = o.toString(), c = this.imageResolveMap.get(s);
3363
+ if (!c || c.requestId !== a) {
3364
+ this.closeImageSource(r || i);
3365
+ break;
3366
+ }
3367
+ if (this.deleteCachedImagesByWorkId(n), this.cacheImages.set(s, {
3368
+ imageSource: r || i,
3369
+ workId: n
3370
+ }), c) {
3371
+ const { resolve: d, timer: l } = c;
3372
+ l && clearTimeout(l), d && d(s);
3373
+ }
3374
+ }
3375
+ break;
3376
+ }
3377
+ }
3378
+ }
3379
+ getIconSize(e, t, s) {
3380
+ const r = e * s, i = t * s;
3381
+ return r <= 50 || i <= 50 ? [50, 50] : r <= 100 || i <= 100 ? [100, 100] : r <= 200 || i <= 200 ? [200, 200] : r <= 400 || i <= 400 ? [400, 400] : r <= 800 || i <= 800 ? [800, 800] : [1600, 1600];
3382
+ }
3383
+ async loadImageBitMap(e) {
3384
+ const { toolsType: t, opt: s, workId: r, requestId: i } = e;
3385
+ if (t === m.Image && s && r) {
3386
+ const o = r.toString(), { src: a, type: n, width: c, height: d, strokeColor: l } = s;
3387
+ if (!a || !n || !c || !d)
3388
+ return;
3389
+ let p = a;
3390
+ if (n === pe.Iconify) {
3391
+ const [g, y] = this.getIconSize(c, d, this.opt.displayer.dpr);
3392
+ p = ue(a, g, y, l);
3393
+ }
3394
+ if (this.cacheImages.has(p)) {
3395
+ const g = this.getCachedImages(p);
3396
+ if (g)
3397
+ return g;
3398
+ }
3399
+ const w = this.imageResolveMap.get(p);
3400
+ if (w)
3401
+ return await w.promise, this.getCachedImages(p);
3402
+ let S;
3403
+ const I = new Promise((g) => {
3404
+ S = g;
3405
+ }), W = {
3406
+ resolve: S,
3407
+ timer: void 0,
3408
+ requestId: i,
3409
+ promise: I
3410
+ };
3411
+ return W.timer = setTimeout(() => {
3412
+ this.imageResolveMap.get(p) === W && S(p);
3413
+ }, 5e3), this.imageResolveMap.set(p, W), this.opt.post({
3414
+ viewId: this.viewId,
3415
+ viewGeneration: this.viewGeneration,
3416
+ viewOwner: this.viewOwner,
3417
+ sp: [
3418
+ {
3419
+ imageSrc: p,
3420
+ workId: o,
3421
+ viewId: this.viewId,
3422
+ isgl: !!this.fullLayer.parent.gl,
3423
+ isSubWorker: !0,
3424
+ type: h.GetImageBitMap,
3425
+ requestId: i
3426
+ }
3427
+ ]
3428
+ }), await I, this.imageResolveMap.get(p) === W && this.imageResolveMap.delete(p), this.getCachedImages(p);
3429
+ }
3430
+ }
3431
+ createWorkShapeNode(e) {
3432
+ return Z({
3433
+ ...e,
3434
+ fullLayer: this.fullLayer,
3435
+ drawLayer: void 0
3436
+ });
3437
+ }
3438
+ setFullWork(e) {
3439
+ const { workId: t, opt: s, toolsType: r } = e;
3440
+ if (t && s && r) {
3441
+ const i = t.toString();
3442
+ let o;
3443
+ return t && this.workShapes.has(i) ? (o = this.workShapes.get(i), o == null || o.setWorkOptions(s)) : o = this.createWorkShapeNode({
3444
+ toolsOpt: s,
3445
+ toolsType: r,
3446
+ workId: i
3447
+ }), o ? (this.workShapes.set(i, o), o) : void 0;
3448
+ }
3449
+ }
3450
+ async runFullWork(e) {
3451
+ var r;
3452
+ const t = this.setFullWork(e), s = e.ops && V(e.ops);
3453
+ if (t) {
3454
+ let i, o;
3455
+ const a = (r = t.getWorkId()) == null ? void 0 : r.toString();
3456
+ return t.toolsType === m.BackgroundSVG ? i = t.consumeService({
3457
+ isFullWork: !0,
3458
+ replaceId: a
3459
+ }) : t.toolsType === m.Image ? i = await t.consumeServiceAsync({
3460
+ isFullWork: !0,
3461
+ worker: this,
3462
+ requestId: e.requestId
3463
+ }) : t.toolsType === m.Text ? i = await t.consumeServiceAsync({
3464
+ isFullWork: !0,
3465
+ replaceId: a,
3466
+ isDrawLabel: !0
3467
+ }) : (i = t.consumeService({
3468
+ op: s,
3469
+ isFullWork: !0,
3470
+ replaceId: a
3471
+ }), o = (e == null ? void 0 : e.updateNodeOpt) && t.updataOptService(e.updateNodeOpt)), ne(i, o);
3472
+ }
3473
+ }
3474
+ async getSnapshot(e) {
3475
+ const { scenePath: t, scenes: s, cameraOpt: r, w: i, h: o, requestId: a } = e;
3476
+ if (!(t && s && r))
3477
+ throw new Error("snapshot input is incomplete");
3478
+ this.setCameraOpt(r);
3479
+ for (const [c, d] of Object.entries(s))
3480
+ if (d != null && d.type)
3481
+ switch (d == null ? void 0 : d.type) {
3482
+ case h.UpdateNode:
3483
+ case h.FullWork: {
3484
+ const { opt: l } = d, p = {
3485
+ ...d,
3486
+ opt: l,
3487
+ workId: c,
3488
+ msgType: h.FullWork,
3489
+ dataType: R.Service,
3490
+ viewId: this.viewId,
3491
+ requestId: a
3492
+ };
3493
+ await this.runFullWork(p);
3494
+ break;
3495
+ }
3496
+ }
3497
+ let n;
3498
+ i && o && (n = {
3499
+ resizeWidth: i,
3500
+ resizeHeight: o
3501
+ }), await this.getSnapshotRender({ scenePath: t, options: n, requestId: a });
3502
+ }
3503
+ getSceneRect() {
3504
+ const { width: e, height: t } = this.scene;
3505
+ return {
3506
+ x: 0,
3507
+ y: 0,
3508
+ w: Math.floor(e),
3509
+ h: Math.floor(t)
3510
+ };
3511
+ }
3512
+ getRectImageBitmap(e, t) {
3513
+ const s = Math.floor(e.x * this.opt.displayer.dpr), r = Math.floor(e.y * this.opt.displayer.dpr), i = e.w > 0 && Math.floor(e.w * this.opt.displayer.dpr || 1) || 1, o = e.h > 0 && Math.floor(e.h * this.opt.displayer.dpr || 1) || 1;
3514
+ return createImageBitmap(
3515
+ this.fullLayer.parent.canvas,
3516
+ s,
3517
+ r,
3518
+ i,
3519
+ o,
3520
+ t
3521
+ );
3522
+ }
3523
+ async getSnapshotRender(e) {
3524
+ var o;
3525
+ const { scenePath: t, options: s, requestId: r } = e;
3526
+ ((o = this.fullLayer) == null ? void 0 : o.parent).render();
3527
+ const i = await this.getRectImageBitmap(
3528
+ this.getSceneRect(),
3529
+ s
3530
+ );
3531
+ if (i) {
3532
+ if (this.destroyed) {
3533
+ this.closeImageSource(i);
3534
+ return;
3535
+ }
3536
+ this.post({
3537
+ sp: [
3538
+ {
3539
+ type: h.Snapshot,
3540
+ scenePath: t,
3541
+ imageBitmap: i,
3542
+ viewId: this.viewId,
3543
+ index: 0,
3544
+ requestId: r
3545
+ }
3546
+ ]
3547
+ }), this.fullLayer && !this.destroyed && $(this.fullLayer, this.fullLayer.parent);
3548
+ }
3549
+ }
3550
+ async getBoundingRect(e) {
3551
+ const { scenePath: t, scenes: s, cameraOpt: r, requestId: i } = e;
3552
+ if (!(t && s && r))
3553
+ throw new Error("bounding-box input is incomplete");
3554
+ this.setCameraOpt(r);
3555
+ let o;
3556
+ for (const [a, n] of Object.entries(s))
3557
+ if (n != null && n.type)
3558
+ switch (n == null ? void 0 : n.type) {
3559
+ case h.UpdateNode:
3560
+ case h.FullWork: {
3561
+ const c = await this.runFullWork({
3562
+ ...n,
3563
+ workId: a,
3564
+ msgType: h.FullWork,
3565
+ dataType: R.Service,
3566
+ viewId: this.viewId,
3567
+ requestId: i
3568
+ });
3569
+ o = ne(o, c);
3570
+ break;
3571
+ }
3572
+ }
3573
+ this.post({
3574
+ sp: [
3575
+ {
3576
+ type: h.BoundingBox,
3577
+ scenePath: t,
3578
+ rect: o || { x: 0, y: 0, w: 0, h: 0 },
3579
+ viewId: this.viewId,
3580
+ requestId: i
3581
+ }
3582
+ ]
3583
+ });
3584
+ }
3585
+ setCameraOpt(e) {
3586
+ const { scale: t, centerX: s, centerY: r, width: i, height: o } = e;
3587
+ this.updateScene({ width: i, height: o }), this.fullLayer.setAttribute("scale", [t, t]), this.fullLayer.setAttribute("translate", [-s, -r]);
3588
+ }
3589
+ runBatchPostData() {
3590
+ this.destroyed || this.mainThreadPostId || (this.mainThreadPostId = requestAnimationFrame(
3591
+ this.combinePost.bind(this)
3592
+ ));
3593
+ }
3594
+ flushPendingPost() {
3595
+ !this.combinePostMsg.size || this.destroyed || (this.mainThreadPostId !== void 0 && (cancelAnimationFrame(this.mainThreadPostId), this.mainThreadPostId = void 0), this.combinePost());
3596
+ }
3597
+ combinePostData() {
3598
+ var t;
3599
+ this.mainThreadPostId = void 0;
3600
+ const e = [];
3601
+ for (const s of this.combinePostMsg.values())
3602
+ if ((t = s.sp) != null && t.length)
3603
+ for (const r of s.sp) {
3604
+ let i = !1;
3605
+ for (const o of e)
3606
+ if (Oe(r, o)) {
3607
+ i = !0;
3608
+ break;
3609
+ }
3610
+ i ? this.releaseMessageImageBitmap(r) : e.push(r);
3611
+ }
3612
+ return this.combinePostMsg.clear(), {
3613
+ sp: e
3614
+ };
3615
+ }
3616
+ combinePost() {
3617
+ var s, r;
3618
+ if (this.destroyed) {
3619
+ this.mainThreadPostId = void 0, this.releaseBatchImageBitmaps({
3620
+ sp: [...this.combinePostMsg].flatMap((i) => i.sp || [])
3621
+ }), this.combinePostMsg.clear();
3622
+ return;
3623
+ }
3624
+ const e = this.combinePostData(), t = (s = e.sp) == null ? void 0 : s.filter(
3625
+ (i) => i.type !== h.None || i.isLockSentEventCursor || i.needUndoTicker
3626
+ );
3627
+ if (e.sp && t) {
3628
+ const i = new Set(t);
3629
+ e.sp.filter((o) => !i.has(o)).forEach((o) => this.releaseMessageImageBitmap(o));
3630
+ }
3631
+ if (t != null && t.length ? e.sp = t.map((i) => i.viewId ? i : { ...i, viewId: this.viewId }) : delete e.sp, !this.destroyed && ((r = e.sp) != null && r.length))
3632
+ try {
3633
+ this.opt.post({
3634
+ ...e,
3635
+ viewId: this.viewId,
3636
+ viewGeneration: this.viewGeneration,
3637
+ viewOwner: this.viewOwner
3638
+ });
3639
+ } catch (i) {
3640
+ throw this.releaseBatchImageBitmaps(e), i;
3641
+ }
3642
+ else
3643
+ this.releaseBatchImageBitmaps(e);
3644
+ }
3645
+ createScene(e) {
3646
+ return new fe({
3647
+ displayRatio: this.opt.displayer.dpr,
3648
+ depth: !1,
3649
+ desynchronized: !0,
3650
+ ...e,
3651
+ autoRender: !1,
3652
+ contextType: "2d"
3653
+ });
3654
+ }
3655
+ createLayer(e, t, s) {
3656
+ const { width: r, height: i } = s, o = `canvas-${e}`, a = t.layer(o, s), n = new me({
3657
+ anchor: [0.5, 0.5],
3658
+ pos: [r * 0.5, i * 0.5],
3659
+ size: [r, i],
3660
+ name: "viewport",
3661
+ id: e
3662
+ });
3663
+ return a.append(n), n;
3664
+ }
3665
+ updateScene(e) {
3666
+ this.scene.attr({ ...e });
3667
+ const { width: t, height: s } = e;
3668
+ this.scene.width = t, this.scene.height = s, this.updateLayer({ width: t, height: s });
3669
+ }
3670
+ updateLayer(e) {
3671
+ const { width: t, height: s } = e;
3672
+ this.fullLayer.parent.setAttribute("width", t), this.fullLayer.parent.setAttribute("height", s), this.fullLayer.setAttribute("size", [t, s]), this.fullLayer.setAttribute("pos", [t * 0.5, s * 0.5]);
3673
+ }
3674
+ destroy(e) {
3675
+ if (this.destroyed) return;
3676
+ e && this.activeRequest && this.reportRequestFailure(
3677
+ this.activeRequest.business,
3678
+ this.activeRequest.requestId,
3679
+ this.activeRequest.scenePath,
3680
+ new Error(`snapshot request ${e}`),
3681
+ e
3682
+ ), this.activeRequest = void 0, this.destroyed = !0, this.mainThreadPostId !== void 0 && (cancelAnimationFrame(this.mainThreadPostId), this.mainThreadPostId = void 0), this.releaseBatchImageBitmaps({
3683
+ sp: [...this.combinePostMsg].flatMap((r) => r.sp || [])
3684
+ }), this.combinePostMsg.clear();
3685
+ const t = [];
3686
+ this.workShapes.forEach((r) => {
3687
+ try {
3688
+ r.clearTmpPoints();
3689
+ } catch (i) {
3690
+ t.push(i);
3691
+ }
3692
+ }), this.workShapes.clear();
3693
+ try {
3694
+ this.clearCacheImages();
3695
+ } catch (r) {
3696
+ t.push(r);
3697
+ }
3698
+ try {
3699
+ this.clearImageResolveMap();
3700
+ } catch (r) {
3701
+ t.push(r);
3702
+ }
3703
+ const s = this.fullLayer.parent;
3704
+ try {
3705
+ this.fullLayer.remove();
3706
+ } catch (r) {
3707
+ t.push(r);
3708
+ }
3709
+ try {
3710
+ s && z(this.fullLayer, s);
3711
+ } catch (r) {
3712
+ t.push(r);
3713
+ }
3714
+ try {
3715
+ this.scene.remove();
3716
+ } catch (r) {
3717
+ t.push(r);
3718
+ }
3719
+ if (t.length)
3720
+ throw t[0];
3721
+ }
3722
+ }
3723
+ class ht {
3724
+ constructor(e) {
3725
+ u(this, "mainThreadMap", /* @__PURE__ */ new Map());
3726
+ u(this, "cameraRenderQueueByView", /* @__PURE__ */ new Map());
3727
+ u(this, "viewGenerationById", /* @__PURE__ */ new Map());
3728
+ u(this, "viewOwnerById", /* @__PURE__ */ new Map());
3729
+ u(this, "ownerSequence", 0);
3730
+ u(this, "snapshotThreadsByRequest", /* @__PURE__ */ new Map());
3731
+ u(this, "destroyed", !1);
3732
+ u(this, "master");
3733
+ u(this, "post", (e) => {
3734
+ if (this.destroyed) {
3735
+ this.releaseBatchResources(e);
3736
+ return;
3737
+ }
3738
+ if (e.viewId) {
3739
+ const f = this.viewOwnerById.get(e.viewId), w = this.viewGenerationById.get(e.viewId);
3740
+ if (e.viewOwner !== void 0 && (!f || f !== e.viewOwner) || e.viewGeneration !== void 0 && w !== void 0 && w !== e.viewGeneration) {
3741
+ this.releaseBatchResources(e);
3742
+ return;
3743
+ }
3744
+ }
3745
+ const {
3746
+ fullWorkerDrawCount: t,
3747
+ sp: s,
3748
+ workerTasksqueueCount: r,
3749
+ cameraTaskId: i,
3750
+ cameraTaskStatus: o,
3751
+ cameraTaskError: a,
3752
+ cameraSettledTaskId: n,
3753
+ viewId: c,
3754
+ viewGeneration: d,
3755
+ consumeCount: l,
3756
+ consumeWorkId: p
3757
+ } = e;
3758
+ if (c && q(i)) {
3759
+ const f = Be(
3760
+ o,
3761
+ a,
3762
+ "mainThread"
3763
+ );
3764
+ f ? this.master.failCameraTask(
3765
+ c,
3766
+ i,
3767
+ f,
3768
+ d
3769
+ ) : this.master.ackCameraTask(c, i, d);
3770
+ }
3771
+ c && q(n) && this.master.ackCameraSettledTask(
3772
+ c,
3773
+ n,
3774
+ d
3775
+ ), this.master.isBusy && q(r) && this.master.setWorkerTasksqueueCount(r), q(t) && this.master.setMaxDrawCount(t), q(l) && this.master.setConsumeCount(l, p), s && this.master.collectorSyncData(
3776
+ e.viewOwner === void 0 ? s : s.map(
3777
+ (f) => f.type === h.GetImageBitMap && f.viewOwner === void 0 ? { ...f, viewOwner: e.viewOwner } : f
3778
+ )
3779
+ );
3780
+ });
3781
+ this.master = e;
3782
+ }
3783
+ enqueueViewTask(e, t) {
3784
+ const r = (this.cameraRenderQueueByView.get(e) || Promise.resolve()).catch(() => {
3785
+ }).then(() => this.destroyed ? void 0 : t());
3786
+ return this.cameraRenderQueueByView.set(e, r), r;
3787
+ }
3788
+ isStaleViewMessage(e) {
3789
+ if (!e.viewId || e.viewId === ce || e.viewGeneration === void 0)
3790
+ return !1;
3791
+ const t = this.viewGenerationById.get(e.viewId);
3792
+ return t !== void 0 && e.viewGeneration < t;
3793
+ }
3794
+ discardView(e) {
3795
+ for (const [s, r] of [
3796
+ ...this.snapshotThreadsByRequest.entries()
3797
+ ])
3798
+ r.thread.viewId === e && this.disposeSnapshotThread(
3799
+ s,
3800
+ r.thread,
3801
+ "view-destroyed"
3802
+ );
3803
+ const t = this.mainThreadMap.get(e);
3804
+ try {
3805
+ t == null || t.destroy();
3806
+ } catch (s) {
3807
+ this.master.Logger.error(
3808
+ `[Appliance]: MainThread View discard failed; viewId=${e}; error=${s instanceof Error ? s.message : String(s)}`
3809
+ );
3810
+ }
3811
+ this.mainThreadMap.delete(e), this.cameraRenderQueueByView.delete(e), this.viewOwnerById.delete(e), this.viewGenerationById.set(
3812
+ e,
3813
+ (this.viewGenerationById.get(e) || 0) + 1
3814
+ );
3815
+ }
3816
+ destroy() {
3817
+ if (this.destroyed) return;
3818
+ this.destroyed = !0;
3819
+ const e = [];
3820
+ for (const [t, s] of [
3821
+ ...this.snapshotThreadsByRequest.entries()
3822
+ ])
3823
+ try {
3824
+ this.disposeSnapshotThread(t, s.thread);
3825
+ } catch (r) {
3826
+ e.push(
3827
+ `snapshot-thread: ${r instanceof Error ? r.message : String(r)}`
3828
+ );
3829
+ }
3830
+ this.snapshotThreadsByRequest.clear();
3831
+ for (const t of this.mainThreadMap.values())
3832
+ try {
3833
+ t.destroy();
3834
+ } catch (s) {
3835
+ e.push(
3836
+ `view=${t.viewId}: ${s instanceof Error ? s.message : String(s)}`
3837
+ );
3838
+ }
3839
+ this.mainThreadMap.clear(), this.cameraRenderQueueByView.clear(), this.viewOwnerById.clear(), e.length && this.master.Logger.error(
3840
+ `[Appliance]: MainThread destroy completed with cleanup errors: ${e.join("; ")}`
3841
+ );
3842
+ }
3843
+ createMainThread(e, t) {
3844
+ return new je(e, t);
3845
+ }
3846
+ createSnapshotThread(e, t) {
3847
+ return new Je(e, t);
3848
+ }
3849
+ getSnapshotRequestKey(e) {
3850
+ return e.requestId || `${e.viewId}\0${e.scenePath || ""}\0${e.msgType}`;
3851
+ }
3852
+ disposeSnapshotThread(e, t, s) {
3853
+ const r = this.snapshotThreadsByRequest.get(e);
3854
+ if (!(!r || r.thread !== t)) {
3855
+ this.snapshotThreadsByRequest.delete(e);
3856
+ try {
3857
+ t.destroy(s);
3858
+ } finally {
3859
+ r.surface.remove();
3860
+ }
3861
+ }
3862
+ }
3863
+ releaseMessageBitmap(e) {
3864
+ var t;
3865
+ try {
3866
+ (t = e.imageBitmap) == null || t.close();
3867
+ } catch {
3868
+ }
3869
+ }
3870
+ releaseBatchResources(e) {
3871
+ var t, s;
3872
+ (t = e.render) == null || t.forEach((r) => this.releaseImageBitmap(r.imageBitmap)), (s = e.sp) == null || s.forEach((r) => this.releaseImageBitmap(r.imageBitmap));
3873
+ }
3874
+ releaseImageBitmap(e) {
3875
+ var t;
3876
+ try {
3877
+ (t = e == null ? void 0 : e.close) == null || t.call(e);
3878
+ } catch {
3879
+ }
3880
+ }
3881
+ async consume(e) {
3882
+ var t, s, r;
3883
+ if (this.destroyed) {
3884
+ e.forEach((i) => this.releaseMessageBitmap(i));
3885
+ return;
3886
+ }
3887
+ for (const i of e.values()) {
3888
+ if (this.destroyed) {
3889
+ this.releaseMessageBitmap(i);
3890
+ return;
3891
+ }
3892
+ const {
3893
+ msgType: o,
3894
+ viewId: a,
3895
+ tasksqueue: n,
3896
+ mainTasksqueueCount: c,
3897
+ cameraTaskId: d,
3898
+ layerOpt: l,
3899
+ offscreenCanvasOpt: p,
3900
+ cameraOpt: f,
3901
+ isSubWorker: w
3902
+ } = i;
3903
+ if (this.isStaleViewMessage(i)) {
3904
+ this.releaseMessageBitmap(i);
3905
+ continue;
3906
+ }
3907
+ if (o === h.Console) {
3908
+ console.log(this);
3909
+ continue;
3910
+ }
3911
+ if (o === h.Init) {
3912
+ const g = i.viewGeneration ?? (this.viewGenerationById.get(a) || 0) + 1;
3913
+ await this.enqueueViewTask(a, async () => {
3914
+ var E, k, O;
3915
+ const y = this.viewGenerationById.get(a);
3916
+ if (y !== void 0 && g < y)
3917
+ return;
3918
+ this.viewGenerationById.set(a, g);
3919
+ const v = i.viewOwner || `${a}:${g}:${++this.ownerSequence}`, T = (E = this.master.control.viewContainerManager.getView(a)) == null ? void 0 : E.displayer, C = T == null ? void 0 : T.canvasContainerRef.current;
3920
+ if (!T || !C || !l || !p) {
3921
+ this.viewOwnerById.delete(a);
3922
+ return;
3923
+ }
3924
+ this.viewOwnerById.set(a, v);
3925
+ const L = this.mainThreadMap.get(a);
3926
+ L == null || L.destroy();
3927
+ const A = this.createMainThread(a, {
3928
+ displayer: T,
3929
+ container: C,
3930
+ layerOpt: l,
3931
+ master: this.master,
3932
+ canvasOpt: p,
3933
+ viewOwner: v,
3934
+ viewGeneration: g,
3935
+ mainThreadRenderMode: ((O = (k = this.master.control) == null ? void 0 : k.renderingCompatibilityDecision) == null ? void 0 : O.mainThreadRenderMode) ?? "normal",
3936
+ post: (B) => this.post({
3937
+ ...B,
3938
+ viewId: B.viewId || a,
3939
+ viewGeneration: B.viewGeneration ?? g,
3940
+ viewOwner: v
3941
+ })
3942
+ });
3943
+ this.mainThreadMap.set(a, A), A && f && A.setCameraOpt(f), this.post({
3944
+ sp: [
3945
+ {
3946
+ type: h.Init,
3947
+ viewId: a,
3948
+ viewGeneration: g
3949
+ }
3950
+ ]
3951
+ });
3952
+ });
3953
+ continue;
3954
+ }
3955
+ if (o === h.Snapshot || o === h.BoundingBox) {
3956
+ const g = (t = this.master.control.viewContainerManager.getView(a)) == null ? void 0 : t.displayer, y = (s = this.master.control.viewContainerManager.mainView) == null ? void 0 : s.displayer, v = (r = y == null ? void 0 : y.snapshotContainerRef) == null ? void 0 : r.current;
3957
+ if (g && v && f) {
3958
+ const T = this.getSnapshotRequestKey(i), C = this.snapshotThreadsByRequest.get(T);
3959
+ C && this.disposeSnapshotThread(T, C.thread, "replaced");
3960
+ const L = document.createElement("div");
3961
+ L.style.width = `${f.width}px`, L.style.height = `${f.height}px`, v.append(L);
3962
+ const A = {
3963
+ ...he.defaultLayerOpt,
3964
+ offscreen: !1,
3965
+ width: f.width,
3966
+ height: f.height
3967
+ }, E = {
3968
+ ...he.defaultScreenCanvasOpt,
3969
+ width: f.width,
3970
+ height: f.height
3971
+ }, k = this.createSnapshotThread(a, {
3972
+ displayer: g,
3973
+ container: L,
3974
+ layerOpt: A,
3975
+ master: this.master,
3976
+ canvasOpt: E,
3977
+ post: this.post,
3978
+ viewOwner: this.viewOwnerById.get(a),
3979
+ viewGeneration: this.viewGenerationById.get(a)
3980
+ });
3981
+ this.snapshotThreadsByRequest.set(T, {
3982
+ thread: k,
3983
+ surface: L
3984
+ }), k.on(i).then(() => {
3985
+ this.disposeSnapshotThread(T, k);
3986
+ }).catch((O) => {
3987
+ this.master.Logger.error(
3988
+ `[Appliance]: snapshot render failed; viewId=${a}; error=${O instanceof Error ? O.message : String(O)}`
3989
+ ), this.disposeSnapshotThread(T, k);
3990
+ });
3991
+ continue;
3992
+ }
3993
+ this.post({
3994
+ viewId: a,
3995
+ viewGeneration: this.viewGenerationById.get(a),
3996
+ viewOwner: this.viewOwnerById.get(a),
3997
+ sp: [
3998
+ {
3999
+ type: h.ReportError,
4000
+ viewId: a,
4001
+ scenePath: i.scenePath,
4002
+ requestId: i.requestId,
4003
+ reportBusiness: o === h.Snapshot ? "snapshot" : "bounding-box",
4004
+ reportStage: "surface-unavailable",
4005
+ reportError: "snapshot surface is unavailable",
4006
+ reportString: `[MainThreadManager] ${o === h.Snapshot ? "snapshot" : "bounding-box"} surface is unavailable; viewId=${a}; requestId=${i.requestId || "unknown"}`
4007
+ }
4008
+ ]
4009
+ });
4010
+ continue;
4011
+ }
4012
+ if (o === h.GetImageBitMap && w) {
4013
+ const g = i.requestId ? this.snapshotThreadsByRequest.get(i.requestId) : [...this.snapshotThreadsByRequest.values()].find(
4014
+ ({ thread: y }) => y.viewId === a
4015
+ );
4016
+ g ? g.thread.on(i) : this.releaseMessageBitmap(i);
4017
+ continue;
4018
+ }
4019
+ if (o === h.GetImageBitMap) {
4020
+ const g = this.mainThreadMap.get(a), y = this.viewGenerationById.get(a), v = this.viewOwnerById.get(a);
4021
+ if (!g || i.viewGeneration !== void 0 && i.viewGeneration !== y || i.viewOwner !== void 0 && i.viewOwner !== v || this.mainThreadMap.get(a) !== g) {
4022
+ this.releaseMessageBitmap(i);
4023
+ continue;
4024
+ }
4025
+ await g.on(i);
4026
+ continue;
4027
+ }
4028
+ if (o === h.TasksQueue && (n != null && n.size)) {
4029
+ const g = n.get(a), y = this.mainThreadMap.get(a), v = this.viewGenerationById.get(a), T = this.viewOwnerById.get(a);
4030
+ let C;
4031
+ if (g && y)
4032
+ try {
4033
+ await this.enqueueViewTask(a, async () => {
4034
+ this.viewGenerationById.get(a) !== v || this.viewOwnerById.get(a) !== T || this.mainThreadMap.get(a) !== y || await y.on(g);
4035
+ });
4036
+ } catch (L) {
4037
+ if (g.msgType !== h.UpdateCamera) throw L;
4038
+ const A = De(L);
4039
+ C = {
4040
+ code: A ? "RENDERER_BUSINESS_TIMEOUT" : "RENDERER_BUSINESS_ERROR",
4041
+ business: "updateCamera",
4042
+ message: L instanceof Error ? L.message : String(L),
4043
+ elapsedMs: A ? L.elapsedMs : void 0,
4044
+ stage: A ? L.stage : "update-camera",
4045
+ blockingWorks: A ? L.blockingWorks : void 0,
4046
+ workerType: "mainThread"
4047
+ };
4048
+ }
4049
+ if (C && q(d)) {
4050
+ this.post({
4051
+ viewId: a,
4052
+ viewGeneration: v,
4053
+ viewOwner: T,
4054
+ cameraTaskId: d,
4055
+ cameraTaskStatus: C.code === "RENDERER_BUSINESS_TIMEOUT" ? "timeout" : "failed",
4056
+ cameraTaskError: C
4057
+ });
4058
+ continue;
4059
+ }
4060
+ c && q(d) && this.post({
4061
+ workerTasksqueueCount: c,
4062
+ cameraTaskId: d,
4063
+ viewId: a,
4064
+ viewGeneration: v,
4065
+ viewOwner: T
4066
+ });
4067
+ continue;
4068
+ }
4069
+ if (a === ce) {
4070
+ const g = [...this.mainThreadMap.entries()];
4071
+ for (const [y, v] of g) {
4072
+ const T = this.enqueueViewTask(y, async () => {
4073
+ this.mainThreadMap.get(y) === v && (o === h.Destroy ? (v.destroy(), this.mainThreadMap.delete(y), this.viewOwnerById.delete(y), this.viewGenerationById.set(
4074
+ y,
4075
+ (this.viewGenerationById.get(y) || 0) + 1
4076
+ )) : await v.on(i));
4077
+ });
4078
+ await T, this.cameraRenderQueueByView.get(y) === T && this.cameraRenderQueueByView.delete(y);
4079
+ }
4080
+ continue;
4081
+ }
4082
+ const S = this.mainThreadMap.get(a);
4083
+ if (o === h.Destroy && !S) {
4084
+ await this.enqueueViewTask(a, async () => {
4085
+ this.viewGenerationById.set(
4086
+ a,
4087
+ i.viewGeneration ?? (this.viewGenerationById.get(a) || 0) + 1
4088
+ ), this.viewOwnerById.delete(a);
4089
+ });
4090
+ continue;
4091
+ }
4092
+ if (!S)
4093
+ continue;
4094
+ const I = this.viewGenerationById.get(a), W = this.enqueueViewTask(a, async () => {
4095
+ this.viewGenerationById.get(a) !== I || this.mainThreadMap.get(a) !== S || await S.on(i);
4096
+ });
4097
+ o === h.Destroy && (await W, this.mainThreadMap.get(a) === S && this.mainThreadMap.delete(a), this.viewOwnerById.delete(a), this.cameraRenderQueueByView.get(a) === W && this.cameraRenderQueueByView.delete(a), this.viewGenerationById.set(
4098
+ a,
4099
+ i.viewGeneration ?? (I || 0) + 1
4100
+ ));
4101
+ }
4102
+ }
4103
+ }
4104
+ export {
4105
+ ht as MainThreadManagerImpl
4106
+ };