@netless/appliance-plugin 1.1.42-beta.3 → 1.1.43-beta.0

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