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