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