@vtj/core 0.1.2 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1,1281 @@
1
+ import { uid as f, timestamp as k, upperFirstCamelCase as P, merge as b, cloneDeep as H } from "@vtj/base";
2
+ /**!
3
+ * Copyright (c) 2024, VTJ.PRO All rights reserved.
4
+ * @name @vtj/core
5
+ * @author CHC chenhuachun1549@dingtalk.com
6
+ * @version 0.7.0
7
+ * @license <a href="https://vtj.pro/license.html">MIT License</a>
8
+ */
9
+ const F = "0.7.0";
10
+ class j {
11
+ }
12
+ function w(o) {
13
+ return { all: o = o || /* @__PURE__ */ new Map(), on: function(e, t) {
14
+ var i = o.get(e);
15
+ i ? i.push(t) : o.set(e, [t]);
16
+ }, off: function(e, t) {
17
+ var i = o.get(e);
18
+ i && (t ? i.splice(i.indexOf(t) >>> 0, 1) : o.set(e, []));
19
+ }, emit: function(e, t) {
20
+ var i = o.get(e);
21
+ i && i.slice().map(function(s) {
22
+ s(t);
23
+ }), (i = o.get("*")) && i.slice().map(function(s) {
24
+ s(e, t);
25
+ });
26
+ } };
27
+ }
28
+ class L {
29
+ listeners = [];
30
+ isReady = !1;
31
+ triggerReady() {
32
+ this.isReady = !0;
33
+ for (const e of this.listeners)
34
+ e();
35
+ this.listeners = [];
36
+ }
37
+ ready(e) {
38
+ this.isReady ? e() : this.listeners.push(e);
39
+ }
40
+ resetReady() {
41
+ this.isReady = !1;
42
+ }
43
+ }
44
+ class x {
45
+ constructor(e, t, i) {
46
+ this.name = e, this.value = t, this.defaultValue = i, this.setValue(t);
47
+ }
48
+ /**
49
+ * 标识是否设置了值, 设置的值与默认值一致,表示未设置,在转换成dsl时会排查该属性
50
+ */
51
+ isUnset = !1;
52
+ setValue(e) {
53
+ this.value = e, this.isUnset = this.value === this.defaultValue;
54
+ }
55
+ getValue() {
56
+ return this.value ?? this.defaultValue;
57
+ }
58
+ static toDsl(e = {}) {
59
+ return Object.entries(e).reduce((t, [i, s]) => (s.isUnset || (t[i] = s.getValue()), t), {});
60
+ }
61
+ static parse(e = {}) {
62
+ return Object.entries(e).reduce((t, [i, s]) => (t[i] = new x(i, s), t), {});
63
+ }
64
+ }
65
+ class y {
66
+ constructor(e) {
67
+ this.schema = e;
68
+ const { name: t, handler: i } = this.schema;
69
+ this.name = t, this.handler = i, this.update(e);
70
+ }
71
+ name;
72
+ handler;
73
+ modifiers = {};
74
+ update(e) {
75
+ Object.assign(this.schema, e);
76
+ const { handler: t, modifiers: i = {} } = this.schema;
77
+ this.handler = t, this.modifiers = i;
78
+ }
79
+ static toDsl(e) {
80
+ return Object.entries(e).reduce((t, [i, s]) => {
81
+ const { handler: r, modifiers: a } = s;
82
+ return t[i] = { name: i, handler: r, modifiers: a }, t;
83
+ }, {});
84
+ }
85
+ static parse(e = {}) {
86
+ return Object.entries(e).reduce(
87
+ (t, [i, s]) => (t[i] = new y(s), t),
88
+ {}
89
+ );
90
+ }
91
+ }
92
+ class _ {
93
+ constructor(e) {
94
+ this.schema = e, this.id = e.id || f(), this.update(e);
95
+ }
96
+ /**
97
+ * 标识
98
+ */
99
+ id;
100
+ /**
101
+ * 指令名称
102
+ */
103
+ name = "";
104
+ /**
105
+ * 参数
106
+ */
107
+ arg;
108
+ /**
109
+ * 修饰符
110
+ */
111
+ modifiers;
112
+ /**
113
+ * 指令值
114
+ */
115
+ value;
116
+ /**
117
+ * v-for 迭代器
118
+ */
119
+ iterator;
120
+ update(e) {
121
+ Object.assign(this.schema, e);
122
+ const { name: t, arg: i, modifiers: s, value: r, iterator: a } = this.schema;
123
+ this.name = t, this.arg = i, this.modifiers = s, this.value = r, this.iterator = a;
124
+ }
125
+ static parse(e = []) {
126
+ return e.map((t) => new _(t));
127
+ }
128
+ static toDsl(e = []) {
129
+ return e.map((t) => {
130
+ const { name: i, arg: s, modifiers: r, value: a, iterator: h, id: m } = t;
131
+ return {
132
+ id: m,
133
+ name: i,
134
+ arg: s,
135
+ modifiers: r,
136
+ value: a,
137
+ iterator: h
138
+ };
139
+ });
140
+ }
141
+ }
142
+ const l = "EVENT_NODE_CHANGE";
143
+ class p {
144
+ constructor(e, t = null) {
145
+ this.parent = t;
146
+ const { id: i = f(), name: s, from: r = "" } = e;
147
+ this.id = i, this.name = s, this.from = r, this.update(e, !0), p.nodes[this.id] = this;
148
+ }
149
+ /**
150
+ * 标记
151
+ */
152
+ __VTJ_NODE__ = !0;
153
+ /**
154
+ * 锁定
155
+ */
156
+ locked = !1;
157
+ /**
158
+ * 记录所有节点的实例
159
+ */
160
+ static nodes = {};
161
+ /**
162
+ * 节点唯一标识
163
+ */
164
+ id;
165
+ /**
166
+ * 名称,即组件的名称或html的标签名
167
+ */
168
+ name;
169
+ /**
170
+ * 组件来源
171
+ */
172
+ from;
173
+ /**
174
+ * 是否不可见
175
+ */
176
+ invisible = !1;
177
+ /**
178
+ * 子节点
179
+ */
180
+ children = "";
181
+ /**
182
+ * 放置在父组件的插槽
183
+ */
184
+ slot;
185
+ /**
186
+ * 节点属性
187
+ */
188
+ props = {};
189
+ /**
190
+ * 节点事件
191
+ */
192
+ events = {};
193
+ /**
194
+ * 指令
195
+ */
196
+ directives = [];
197
+ /**
198
+ * 销毁标识
199
+ */
200
+ disposed = !1;
201
+ /**
202
+ * 更新节点属性
203
+ * @param schema
204
+ * @param silent 是否静默,静默更新即不触发事件
205
+ */
206
+ update(e, t = !1) {
207
+ const {
208
+ invisible: i = !1,
209
+ locked: s = !1,
210
+ children: r = [],
211
+ slot: a,
212
+ props: h = {},
213
+ events: m = {},
214
+ directives: E = []
215
+ } = e;
216
+ this.invisible = i, this.locked = s, this.setChildren(r, !0), this.setSlot(a, !0), this.props = x.parse(h), this.events = y.parse(m), this.directives = _.parse(E), t || n.emit(l, this);
217
+ }
218
+ /**
219
+ * 设置子节点
220
+ * @param children
221
+ * @param silent
222
+ */
223
+ setChildren(e = "", t = !1) {
224
+ Array.isArray(e) ? this.children = e.map((i) => new p(i, this)) : this.children = e, t || n.emit(l, this);
225
+ }
226
+ /**
227
+ * 设置节点放置的插槽
228
+ * @param slot
229
+ * @param silent
230
+ */
231
+ setSlot(e, t = !1) {
232
+ this.slot = typeof e == "string" ? { name: e, params: [] } : e, t || n.emit(l, this);
233
+ }
234
+ /**
235
+ * 新增或更新属性
236
+ * @param name
237
+ * @param value
238
+ * @param defaultValue
239
+ * @param silent
240
+ */
241
+ setProp(e, t, i, s = !1) {
242
+ const r = this.props[e];
243
+ r ? r.setValue(t) : this.props[e] = new x(e, t, i), s || n.emit(l, this);
244
+ }
245
+ /**
246
+ * 删除属性
247
+ * @param name
248
+ * @param silent
249
+ */
250
+ removeProp(e, t = !1) {
251
+ delete this.props[e], t || n.emit(l, this);
252
+ }
253
+ /**
254
+ * 获取属性值
255
+ * @param name
256
+ * @returns
257
+ */
258
+ getPropValue(e) {
259
+ const t = this.props[e];
260
+ if (t)
261
+ return t.getValue();
262
+ }
263
+ /**
264
+ * 新增或更新事件
265
+ * @param scheam
266
+ * @param silent
267
+ */
268
+ setEvent(e, t = !1) {
269
+ const i = this.events[e.name];
270
+ i ? i.update(e) : this.events[e.name] = new y(e), t || n.emit(l, this);
271
+ }
272
+ /**
273
+ * 删除事件
274
+ * @param name
275
+ * @param silent
276
+ */
277
+ removeEvent(e, t = !1) {
278
+ delete this.events[e], t || n.emit(l, this);
279
+ }
280
+ /**
281
+ * 新增或更新指令
282
+ * @param scheam
283
+ * @param silent
284
+ */
285
+ setDirective(e, t = !1) {
286
+ const i = this.directives.findIndex((s) => s.id === e.id);
287
+ i >= 0 ? this.directives.splice(i, 1, new _(e)) : this.directives.push(new _(e)), t || n.emit(l, this);
288
+ }
289
+ /**
290
+ * 删除指令
291
+ * @param dirctive
292
+ * @param silent
293
+ */
294
+ removeDirective(e, t = !1) {
295
+ const i = this.directives.findIndex(
296
+ (s) => s === e || s.id === e.id
297
+ );
298
+ i >= 0 && this.directives.splice(i, 1), t || n.emit(l, this);
299
+ }
300
+ /**
301
+ * 删除子节点
302
+ * @param node
303
+ * @param silent
304
+ * @returns
305
+ */
306
+ removeChild(e, t = !1) {
307
+ const { children: i, disposed: s } = this;
308
+ if (s || !Array.isArray(i))
309
+ return;
310
+ const r = i.findIndex((a) => a === e);
311
+ e.parent = null, i.splice(r, 1), t || n.emit(l, this);
312
+ }
313
+ /**
314
+ * 追加子节点
315
+ * @param node
316
+ * @param silent
317
+ * @returns
318
+ */
319
+ appendChild(e, t = !1) {
320
+ const { children: i, disposed: s } = this;
321
+ s || (e.parent = this, Array.isArray(i) ? i.push(e) : this.children = [e], t || n.emit(l, this));
322
+ }
323
+ /**
324
+ * 在当前节点的后面插入节点
325
+ * @param node
326
+ * @param silent
327
+ * @returns
328
+ */
329
+ insertAfter(e, t = !1) {
330
+ if (!this.parent)
331
+ return;
332
+ const i = this.parent.children;
333
+ if (Array.isArray(i)) {
334
+ e.parent = this.parent;
335
+ const s = i.indexOf(this);
336
+ i.splice(s + 1, 0, e), t || n.emit(l, this.parent);
337
+ }
338
+ }
339
+ /**
340
+ * 在当前节点的前面插入节点
341
+ * @param node
342
+ * @param silent
343
+ * @returns
344
+ */
345
+ insertBefore(e, t = !1) {
346
+ if (!this.parent)
347
+ return;
348
+ const i = this.parent.children;
349
+ if (Array.isArray(i)) {
350
+ e.parent = this.parent;
351
+ const s = i.indexOf(this);
352
+ i.splice(s, 0, e), t || n.emit(l, this.parent);
353
+ }
354
+ }
355
+ movePrev(e = !1) {
356
+ const t = this.parent;
357
+ if (!t)
358
+ return;
359
+ const i = t.children;
360
+ if (Array.isArray(i)) {
361
+ const s = i.indexOf(this);
362
+ s > 0 && (i.splice(s, 1), i.splice(s - 1, 0, this), e || n.emit(l, t));
363
+ }
364
+ }
365
+ moveNext(e = !1) {
366
+ const t = this.parent;
367
+ if (!t)
368
+ return;
369
+ const i = t.children;
370
+ if (Array.isArray(i)) {
371
+ const s = i.indexOf(this);
372
+ s > -1 && s < i.length - 1 && (i.splice(s, 1), i.splice(s + 1, 0, this), e || n.emit(l, t));
373
+ }
374
+ }
375
+ /**
376
+ * 获取DSL
377
+ * @returns
378
+ */
379
+ toDsl() {
380
+ const {
381
+ id: e,
382
+ name: t,
383
+ from: i,
384
+ invisible: s,
385
+ locked: r,
386
+ slot: a,
387
+ children: h,
388
+ props: m,
389
+ directives: E,
390
+ events: N
391
+ } = this, v = Array.isArray(h) ? h.map((R) => R.toDsl()) : h;
392
+ return {
393
+ id: e,
394
+ name: t,
395
+ from: i,
396
+ invisible: s,
397
+ locked: r,
398
+ slot: a,
399
+ children: v,
400
+ props: x.toDsl(m),
401
+ directives: _.toDsl(E),
402
+ events: y.toDsl(N)
403
+ };
404
+ }
405
+ /**
406
+ * 销毁
407
+ * @param silent
408
+ * @returns
409
+ */
410
+ dispose(e = !1) {
411
+ const { children: t, disposed: i } = this;
412
+ i || (Array.isArray(t) && t.forEach((s) => s.dispose(!0)), this.parent ? this.parent.removeChild(this, e) : e || n.emit(l, this), this.parent = null, this.disposed = !0, delete p.nodes[this.id]);
413
+ }
414
+ lock(e = !1) {
415
+ if (this.locked = !0, Array.isArray(this.children))
416
+ for (const t of this.children)
417
+ t.lock(!0);
418
+ e || n.emit(l, this);
419
+ }
420
+ unlock(e = !1) {
421
+ if (this.locked = !1, Array.isArray(this.children))
422
+ for (const t of this.children)
423
+ t.unlock(!0);
424
+ e || n.emit(l, this);
425
+ }
426
+ setVisible(e, t = !1) {
427
+ if (this.invisible = !e, Array.isArray(this.children))
428
+ for (const i of this.children)
429
+ i.setVisible(e, !0);
430
+ t || n.emit(l, this);
431
+ }
432
+ isChild(e) {
433
+ let t = !1;
434
+ if (Array.isArray(this.children)) {
435
+ for (const i of this.children)
436
+ if (e === i || e.id === i.id) {
437
+ t = !0;
438
+ break;
439
+ } else if (t = i.isChild(e), t)
440
+ break;
441
+ }
442
+ return t;
443
+ }
444
+ }
445
+ const d = "EVENT_BLOCK_CHANGE";
446
+ class u {
447
+ __VTJ_BLOCK__ = !0;
448
+ id;
449
+ name = "";
450
+ inject = [];
451
+ state = {};
452
+ lifeCycles = {};
453
+ methods = {};
454
+ computed = {};
455
+ watch = [];
456
+ css = "";
457
+ props = [];
458
+ emits = [];
459
+ slots = [];
460
+ dataSources = {};
461
+ nodes = [];
462
+ locked = !1;
463
+ disposed = !1;
464
+ static normalAttrs = [
465
+ "name",
466
+ "locked",
467
+ "inject",
468
+ "state",
469
+ "lifeCycles",
470
+ "methods",
471
+ "computed",
472
+ "watch",
473
+ "css",
474
+ "props",
475
+ "emits",
476
+ "slots",
477
+ "dataSources"
478
+ ];
479
+ constructor(e) {
480
+ const { id: t } = e;
481
+ this.id = t || f(), this.update(e, !0);
482
+ }
483
+ update(e, t = !1) {
484
+ for (const s of u.normalAttrs) {
485
+ const r = e[s];
486
+ r && (this[s] = r);
487
+ }
488
+ const { nodes: i = [] } = e;
489
+ i.length && (this.nodes = i.map((s) => new p(s))), t || n.emit(d, this);
490
+ }
491
+ /**
492
+ * 获取DSL
493
+ * @returns
494
+ */
495
+ toDsl() {
496
+ const { __VTJ_BLOCK__: e, id: t, nodes: i } = this;
497
+ return {
498
+ ...u.normalAttrs.reduce((r, a) => (r[a] = this[a], r), {}),
499
+ __VTJ_BLOCK__: e,
500
+ __VERSION__: k().toString(),
501
+ id: t,
502
+ nodes: i.map((r) => r.toDsl())
503
+ };
504
+ }
505
+ /**
506
+ * 销毁
507
+ */
508
+ dispose() {
509
+ this.nodes.map((e) => e.dispose(!0)), this.nodes = [], this.disposed = !0;
510
+ }
511
+ /**
512
+ * 设置通用函数属性
513
+ * @param type
514
+ * @param name
515
+ * @param value
516
+ * @param silent
517
+ */
518
+ setFunction(e, t, i, s = !1) {
519
+ this[e][t] = i, s || n.emit(d, this);
520
+ }
521
+ /**
522
+ * 删除通用函数属性
523
+ * @param type
524
+ * @param name
525
+ * @param silent
526
+ */
527
+ removeFunction(e, t, i = !1) {
528
+ delete this[e][t], i || n.emit(d, this);
529
+ }
530
+ /**
531
+ * 设置状态
532
+ * @param name
533
+ * @param value
534
+ * @param silent
535
+ */
536
+ setState(e, t, i = !1) {
537
+ this.state[e] = t, i || n.emit(d, this);
538
+ }
539
+ /**
540
+ * 删除状态
541
+ * @param name
542
+ * @param silent
543
+ */
544
+ removeState(e, t = !1) {
545
+ delete this.state[e], t || n.emit(d, this);
546
+ }
547
+ /**
548
+ * 更新CSS
549
+ * @param content
550
+ * @param silent
551
+ */
552
+ setCss(e, t = !1) {
553
+ this.css = e, t || n.emit(d, this);
554
+ }
555
+ /**
556
+ * 新增或更新 watch
557
+ * @param watch
558
+ * @param silent
559
+ */
560
+ setWatch(e, t = !1) {
561
+ e.id = e.id || f();
562
+ const i = this.watch.findIndex(
563
+ (s) => s.id && s.id === e.id || s === e
564
+ );
565
+ i > -1 ? this.watch.splice(i, 1, e) : this.watch.push(e), t || n.emit(d, this);
566
+ }
567
+ /**
568
+ * 删除 watch
569
+ * @param watch
570
+ * @param silent
571
+ */
572
+ removeWatch(e, t = !1) {
573
+ const i = this.watch.findIndex(
574
+ (s) => s.id && s.id === e.id || s === e
575
+ );
576
+ i > -1 && (this.watch.splice(i, 1), t || n.emit(d, this));
577
+ }
578
+ /**
579
+ * 定义属性参数
580
+ * @param prop
581
+ * @param silent
582
+ */
583
+ setProp(e, t = !1) {
584
+ const i = this.props.findIndex(
585
+ (s) => typeof s == "string" ? s === e.name : s.name === e.name
586
+ );
587
+ i > -1 ? this.props.splice(i, 1, e) : this.props.push(e), t || n.emit(d, this);
588
+ }
589
+ /**
590
+ * 删除属性
591
+ * @param prop
592
+ * @param silent
593
+ */
594
+ removeProp(e, t = !1) {
595
+ const i = this.props.findIndex(
596
+ (s) => typeof s == "string" ? s === e.name : s.name === e.name
597
+ );
598
+ i > -1 && (this.props.splice(i, 1), t || n.emit(d, this));
599
+ }
600
+ /**
601
+ * 设置事件
602
+ * @param emit
603
+ * @param silent
604
+ */
605
+ setEmit(e, t = !1) {
606
+ const i = this.emits.findIndex((s) => s === e);
607
+ i > -1 ? this.emits.splice(i, 1, e) : this.emits.push(e), t || n.emit(d, this);
608
+ }
609
+ /**
610
+ * 删除事件
611
+ * @param emit
612
+ * @param silent
613
+ */
614
+ removeEmit(e, t = !1) {
615
+ const i = this.emits.findIndex((s) => s === e);
616
+ i > -1 && (this.emits.splice(i, 1), t || n.emit(d, this));
617
+ }
618
+ /**
619
+ * 设置插槽
620
+ * @param emit
621
+ * @param silent
622
+ */
623
+ setSlot(e, t = !1) {
624
+ const i = this.slots.findIndex((s) => s === e);
625
+ i > -1 ? this.slots.splice(i, 1, e) : this.slots.push(e), t || n.emit(d, this);
626
+ }
627
+ /**
628
+ * 删除插槽
629
+ * @param emit
630
+ * @param silent
631
+ */
632
+ removeSlot(e, t = !1) {
633
+ const i = this.slots.findIndex((s) => s === e);
634
+ i > -1 && (this.slots.splice(i, 1), t || n.emit(d, this));
635
+ }
636
+ /**
637
+ * 设置注入
638
+ * @param inject
639
+ * @param silent
640
+ */
641
+ setInject(e, t = !1) {
642
+ const i = this.inject.findIndex((s) => s.name === e.name);
643
+ i > -1 ? this.inject.splice(i, 1, e) : this.inject.push(e), t || n.emit(d, this);
644
+ }
645
+ /**
646
+ * 删除注入
647
+ * @param inject
648
+ * @param silent
649
+ */
650
+ removeInject(e, t = !1) {
651
+ const i = this.inject.findIndex((s) => s.name === e.name);
652
+ i > -1 && (this.inject.splice(i, 1), t || n.emit(d, this));
653
+ }
654
+ /**
655
+ * 设置数据源
656
+ * @param source
657
+ * @param silent
658
+ */
659
+ setDataSource(e, t = !1) {
660
+ this.dataSources[e.name] = e, t || n.emit(d, this);
661
+ }
662
+ /**
663
+ * 删除数据源
664
+ * @param name
665
+ * @param silent
666
+ */
667
+ removeDataSource(e, t = !1) {
668
+ delete this.dataSources[e], t || n.emit(d, this);
669
+ }
670
+ insertAfter(e, t, i = !1) {
671
+ e.parent = null;
672
+ const s = this.nodes.indexOf(t);
673
+ this.nodes.splice(s + 1, 0, e), i || n.emit(d, this);
674
+ }
675
+ insertBefore(e, t, i = !1) {
676
+ e.parent = null;
677
+ const s = this.nodes.indexOf(t);
678
+ this.nodes.splice(s, 0, e), i || n.emit(d, this);
679
+ }
680
+ appendNode(e, t = !1) {
681
+ e.parent = null, this.nodes.push(e), t || n.emit(d, this);
682
+ }
683
+ /**
684
+ * 添加节点
685
+ * @param node
686
+ * @param target
687
+ * @param position
688
+ * @param silent
689
+ */
690
+ addNode(e, t, i = "inner", s = !1) {
691
+ t ? ["left", "top"].includes(i) ? t.parent ? t.insertAfter(e, s) : this.insertBefore(e, t, s) : ["right", "bottom"].includes(i) ? t.parent ? t.insertAfter(e, s) : this.insertAfter(e, t, s) : t.appendChild(e, s) : this.appendNode(e, s);
692
+ }
693
+ __removeNode(e, t = !1) {
694
+ const i = this.nodes.findIndex((s) => s.id === e.id);
695
+ i > -1 && (this.nodes.splice(i, 1), t || n.emit(d, this));
696
+ }
697
+ /**
698
+ * 删除节点
699
+ * @param node
700
+ * @param silent
701
+ */
702
+ removeNode(e, t = !1) {
703
+ e.parent ? e.dispose(t) : (e.dispose(!0), this.__removeNode(e, t));
704
+ }
705
+ /**
706
+ * 移动节点
707
+ * @param node
708
+ * @param target
709
+ * @param position
710
+ * @param silent
711
+ */
712
+ move(e, t, i = "inner", s = !1) {
713
+ e.parent ? e.parent.removeChild(e, !0) : this.__removeNode(e, !0), this.addNode(e, t, i, s);
714
+ }
715
+ /**
716
+ * 向前交换节点
717
+ * @param node
718
+ * @param silent
719
+ */
720
+ movePrev(e, t = !1) {
721
+ if (e.parent)
722
+ e.movePrev(t);
723
+ else {
724
+ const i = this.nodes, s = i.indexOf(e);
725
+ s > 0 && (i.splice(s, 1), i.splice(s - 1, 0, e), t || n.emit(d, this));
726
+ }
727
+ }
728
+ /**
729
+ * 向后交换节点
730
+ * @param node
731
+ * @param silent
732
+ */
733
+ moveNext(e, t = !1) {
734
+ if (e.parent)
735
+ e.moveNext(t);
736
+ else {
737
+ const i = this.nodes, s = i.indexOf(e);
738
+ s > -1 && s < i.length - 1 && (i.splice(s, 1), i.splice(s + 1, 0, e), t || n.emit(d, this));
739
+ }
740
+ }
741
+ /**
742
+ * 克隆节点
743
+ * @param target
744
+ * @param silent
745
+ * @returns
746
+ */
747
+ cloneNode(e, t = !1) {
748
+ const i = D(e.toDsl()), s = new p(i);
749
+ return this.addNode(s, e, "bottom", t), s;
750
+ }
751
+ lock(e = !1) {
752
+ this.locked = !0;
753
+ for (const t of this.nodes)
754
+ t.lock(!0);
755
+ e || n.emit(d, this);
756
+ }
757
+ unlock(e = !1) {
758
+ this.locked = !1;
759
+ for (const t of this.nodes)
760
+ t.unlock(!0);
761
+ e || n.emit(d, this);
762
+ }
763
+ isChild(e) {
764
+ let t = !1;
765
+ for (const i of this.nodes)
766
+ if (e === i || e.id === i.id) {
767
+ t = !0;
768
+ break;
769
+ } else if (t = i.isChild(e), t)
770
+ break;
771
+ return t;
772
+ }
773
+ }
774
+ const c = "EVENT_PROJECT_CHANGE", V = "EVENT_PROJECT_ACTIVED", I = "EVENT_PROJECT_DEPS_CHANGE", A = "EVENT_PROJECT_PAGES_CHANGE", C = "EVENT_PROJECT_BLOCKS_CHANGE", S = "EVENT_PROJECT_APIS_CHANGE", J = "EVENT_PROJECT_PUBLISH", B = "EVENT_PROJECT_FILE_PUBLISH";
775
+ class T {
776
+ id = "";
777
+ name = "";
778
+ description = "";
779
+ homepage = "";
780
+ dependencies = [];
781
+ pages = [];
782
+ blocks = [];
783
+ apis = [];
784
+ currentFile = null;
785
+ static attrs = [
786
+ "name",
787
+ "homepage",
788
+ "description",
789
+ "dependencies",
790
+ "pages",
791
+ "blocks",
792
+ "apis"
793
+ ];
794
+ constructor(e) {
795
+ const { id: t } = e;
796
+ this.id = t || f(), this.update(e, !0);
797
+ }
798
+ update(e, t = !1) {
799
+ for (const i of T.attrs) {
800
+ const s = e[i];
801
+ s && (this[i] = s);
802
+ }
803
+ t || n.emit(c, {
804
+ model: this,
805
+ type: "update",
806
+ data: e
807
+ });
808
+ }
809
+ isPageFile(e) {
810
+ return e.type === "page";
811
+ }
812
+ toDsl() {
813
+ const { id: e } = this, t = T.attrs.reduce(
814
+ (i, s) => (i[s] = this[s], i),
815
+ {}
816
+ );
817
+ return t.pages && (t.pages = t.pages.map((i) => (delete i.dsl, i))), t.blocks && (t.blocks = t.blocks.map((i) => (delete i.dsl, i))), {
818
+ __VTJ_PROJECT__: !0,
819
+ __VERSION__: k().toString(),
820
+ id: e,
821
+ ...t
822
+ };
823
+ }
824
+ /**
825
+ * 打开文件
826
+ * @param file
827
+ * @param silent
828
+ */
829
+ active(e, t = !1) {
830
+ this.currentFile = e, t || n.emit(V, {
831
+ model: this,
832
+ type: "update",
833
+ data: e
834
+ });
835
+ }
836
+ /**
837
+ * 关闭文件
838
+ * @param silent
839
+ */
840
+ deactivate(e = !1) {
841
+ this.currentFile = null, e || n.emit(V, {
842
+ model: this,
843
+ type: "update",
844
+ data: null
845
+ });
846
+ }
847
+ /**
848
+ * 新增或更新依赖
849
+ * @param item
850
+ * @param silent
851
+ */
852
+ setDeps(e, t = !1) {
853
+ const i = this.dependencies, s = i.findIndex((a) => a.package === e.package);
854
+ let r;
855
+ if (s > -1 ? (r = "update", i.splice(s, 1, {
856
+ ...i[s],
857
+ ...e
858
+ })) : (r = "create", i.push(e)), !t) {
859
+ const a = {
860
+ model: this,
861
+ type: r,
862
+ data: e
863
+ };
864
+ n.emit(I, a), n.emit(c, a);
865
+ }
866
+ }
867
+ /**
868
+ * 删除依赖
869
+ * @param item
870
+ * @param silent
871
+ */
872
+ removeDeps(e, t = !1) {
873
+ const i = this.dependencies, s = i.findIndex((r) => r.package === e.package);
874
+ if (s > -1 && i.splice(s, 1), !t) {
875
+ const r = {
876
+ model: this,
877
+ type: "delete",
878
+ data: e
879
+ };
880
+ n.emit(I, r), n.emit(c, r);
881
+ }
882
+ }
883
+ /**
884
+ * 根据页面id查找页面或目录
885
+ * @param id
886
+ * @returns
887
+ */
888
+ getPage(e) {
889
+ const t = (i, s = []) => {
890
+ for (const r of s) {
891
+ if (r.id === i)
892
+ return r;
893
+ if (r.children && r.children.length) {
894
+ const a = t(i, r.children);
895
+ if (a)
896
+ return a;
897
+ }
898
+ }
899
+ };
900
+ return t(e, this.pages);
901
+ }
902
+ /**
903
+ * 查找全部页面,不含目录
904
+ * @returns
905
+ */
906
+ getPages() {
907
+ const e = (t = []) => {
908
+ let i = [];
909
+ for (const s of t)
910
+ s.dir ? s.children && s.children.length && (i = i.concat(e(s.children))) : i.push(s);
911
+ return i;
912
+ };
913
+ return e(this.pages);
914
+ }
915
+ /**
916
+ * 新建页面
917
+ * @param page
918
+ * @param parentId
919
+ * @param silent
920
+ */
921
+ createPage(e, t, i = !1) {
922
+ if (e.id = e.id || f(), e.type = "page", e.dir ? e.children = [] : e.dsl = e.dsl || new u({
923
+ id: e.id,
924
+ name: P(e.name)
925
+ }).toDsl(), t) {
926
+ const s = this.getPage(t);
927
+ s ? s.children ? s.children.push(e) : s.children = [e] : console.warn(`not found PageFile for id: ${t} `);
928
+ } else
929
+ this.pages.push(e);
930
+ if (this.currentFile || this.active(e, i), !i) {
931
+ const s = {
932
+ model: this,
933
+ type: "create",
934
+ data: e
935
+ };
936
+ n.emit(A, s), n.emit(c, s);
937
+ }
938
+ }
939
+ /**
940
+ * 更新页面
941
+ * @param page
942
+ * @param silent
943
+ */
944
+ updatePage(e, t = !1) {
945
+ const i = this.getPage(e.id);
946
+ if (i ? Object.assign(i, e) : console.warn(`not found PageFile for id: ${e.id} `), !t) {
947
+ const s = {
948
+ model: this,
949
+ type: "update",
950
+ data: e
951
+ };
952
+ n.emit(A, s), n.emit(c, s);
953
+ }
954
+ }
955
+ /**
956
+ * 复制页面
957
+ * @param page
958
+ * @param parentId
959
+ * @param silent
960
+ */
961
+ clonePage(e, t, i = !1) {
962
+ const s = f(), r = `${e.name}Copy`, a = `${e.title}_副本`, h = new u({
963
+ id: s,
964
+ name: r
965
+ }).toDsl(), m = b({}, e, { id: s, name: r, title: a, dsl: h }), E = t ? this.getPage(t)?.children || [] : this.pages, N = E.findIndex((v) => v.id === e.id);
966
+ if (E.splice(N + 1, 0, m), !i) {
967
+ const v = {
968
+ model: this,
969
+ type: "clone",
970
+ data: {
971
+ page: e,
972
+ newPage: m
973
+ }
974
+ };
975
+ n.emit(A, v), n.emit(c, v);
976
+ }
977
+ }
978
+ /**
979
+ * 删除页面或目录
980
+ * @param id
981
+ * @param silent
982
+ */
983
+ removePage(e, t = !1) {
984
+ const i = (s, r) => {
985
+ const a = r.findIndex((h) => h.id === s);
986
+ if (a >= 0) {
987
+ r.splice(a, 1);
988
+ return;
989
+ }
990
+ for (const h of r)
991
+ if (h.children && h.children.length)
992
+ return i(s, h.children);
993
+ };
994
+ if (i(e, this.pages), e === this.homepage && (this.homepage = ""), this.currentFile?.id === e && this.deactivate(t), !t) {
995
+ const s = {
996
+ model: this,
997
+ type: "delete",
998
+ data: e
999
+ };
1000
+ n.emit(A, s), n.emit(c, s);
1001
+ }
1002
+ }
1003
+ /**
1004
+ * 获取区块文件
1005
+ * @param id
1006
+ * @returns
1007
+ */
1008
+ getBlock(e) {
1009
+ return this.blocks.find((t) => t.id === e);
1010
+ }
1011
+ /**
1012
+ * 创建区块
1013
+ * @param block
1014
+ * @param silent
1015
+ */
1016
+ createBlock(e, t = !1) {
1017
+ const i = e.id || f(), s = P(e.name);
1018
+ if (e.id = i, e.type = "block", e.dsl = new u({ id: i, name: s }).toDsl(), this.blocks.push(e), this.currentFile || this.active(e, t), !t) {
1019
+ const r = {
1020
+ model: this,
1021
+ type: "create",
1022
+ data: e
1023
+ };
1024
+ n.emit(C, r), n.emit(c, r);
1025
+ }
1026
+ }
1027
+ /**
1028
+ *
1029
+ * @param block 更新区块
1030
+ * @param silent
1031
+ */
1032
+ updateBlock(e, t = !1) {
1033
+ const i = this.getBlock(e.id);
1034
+ if (i ? (Object.assign(i, e), i.dsl && (i.dsl.name = e.name)) : console.warn(`not found PageFile for id: ${e.id} `), !t) {
1035
+ const s = {
1036
+ model: this,
1037
+ type: "update",
1038
+ data: e
1039
+ };
1040
+ n.emit(C, s), n.emit(c, s);
1041
+ }
1042
+ }
1043
+ /**
1044
+ * 删除区块
1045
+ * @param id
1046
+ * @param silent
1047
+ */
1048
+ removeBlock(e, t = !1) {
1049
+ const i = this.blocks, s = i.findIndex((r) => r.id === e);
1050
+ if (s > -1 ? (i.splice(s, 1), this.currentFile?.id === e && this.deactivate(t)) : console.warn(`not found PageFile for id: ${e} `), !t) {
1051
+ const r = {
1052
+ model: this,
1053
+ type: "delete",
1054
+ data: e
1055
+ };
1056
+ n.emit(C, r), n.emit(c, r);
1057
+ }
1058
+ }
1059
+ /**
1060
+ * 检查是否存在名称的区块
1061
+ * @param name
1062
+ * @param excludes
1063
+ * @returns
1064
+ */
1065
+ existBlockName(e, t = []) {
1066
+ return this.blocks.some((i) => i.name === e && !t.includes(i.id));
1067
+ }
1068
+ /**
1069
+ * 检测是否存在名称的页面
1070
+ * @param name
1071
+ * @param excludes
1072
+ * @returns
1073
+ */
1074
+ existPageName(e, t = []) {
1075
+ return this.getPages().some((s) => s.name === e && !t.includes(s.id));
1076
+ }
1077
+ /**
1078
+ * 新增或更新api
1079
+ * @param item
1080
+ * @param silent
1081
+ */
1082
+ setApi(e, t = !1) {
1083
+ const i = this.apis.find(
1084
+ (r) => r.name === e.name || r.id === e.id
1085
+ );
1086
+ let s;
1087
+ if (i ? (s = "update", Object.assign(i, e)) : (s = "create", e.id = f(), this.apis.push(e)), !t) {
1088
+ const r = {
1089
+ model: this,
1090
+ type: s,
1091
+ data: e
1092
+ };
1093
+ n.emit(S, r), n.emit(c, r);
1094
+ }
1095
+ }
1096
+ /**
1097
+ * 删除api
1098
+ * @param name
1099
+ * @param silent
1100
+ */
1101
+ removeApi(e, t = !1) {
1102
+ const i = this.apis.findIndex((s) => s.name === e || s.id === e);
1103
+ if (i > -1 ? this.apis.splice(i, 1) : console.warn(`not found Api for name: ${e} `), !t) {
1104
+ const s = {
1105
+ model: this,
1106
+ type: "delete",
1107
+ data: e
1108
+ };
1109
+ n.emit(S, s), n.emit(c, s);
1110
+ }
1111
+ }
1112
+ existApiName(e, t = []) {
1113
+ return this.apis.some((i) => i.name === e && !t.includes(i.id));
1114
+ }
1115
+ setHomepage(e, t = !1) {
1116
+ if (this.homepage = e, !t) {
1117
+ const i = {
1118
+ model: this,
1119
+ type: "update",
1120
+ data: e
1121
+ };
1122
+ n.emit(c, i);
1123
+ }
1124
+ }
1125
+ publish(e) {
1126
+ const t = {
1127
+ model: this,
1128
+ type: "publish",
1129
+ data: e || this
1130
+ };
1131
+ e ? n.emit(B, t) : n.emit(J, t);
1132
+ }
1133
+ }
1134
+ const g = "EVENT_HISTORY_CHANGE", O = "EVENT_HISTORY_LOAD";
1135
+ class $ {
1136
+ options = { max: 50 };
1137
+ index = -1;
1138
+ id;
1139
+ items;
1140
+ constructor(e, t = {}) {
1141
+ Object.assign(this.options, t);
1142
+ const { id: i, items: s = [] } = e;
1143
+ this.id = i, this.items = s;
1144
+ }
1145
+ toDsl() {
1146
+ const { id: e, items: t } = this;
1147
+ return {
1148
+ id: e,
1149
+ items: t.map((i) => ({ id: i.id, label: i.label }))
1150
+ };
1151
+ }
1152
+ /**
1153
+ * 获取历史项
1154
+ * @param id
1155
+ * @returns
1156
+ */
1157
+ get(e) {
1158
+ return this.items.find((t) => t.id === e);
1159
+ }
1160
+ /**
1161
+ * 增加历史记录
1162
+ * @param dsl
1163
+ * @param silent
1164
+ */
1165
+ add(e, t = !1) {
1166
+ const { max: i } = this.options, s = {
1167
+ id: f(),
1168
+ label: (/* @__PURE__ */ new Date()).toLocaleString(),
1169
+ dsl: H(e)
1170
+ };
1171
+ if (this.items.unshift(s), this.items.length > i) {
1172
+ const r = this.items.splice(i);
1173
+ t || n.emit(g, {
1174
+ model: this,
1175
+ type: "delete",
1176
+ data: r.map((a) => a.id)
1177
+ });
1178
+ }
1179
+ this.index = -1, t || n.emit(g, {
1180
+ model: this,
1181
+ type: "create",
1182
+ data: s
1183
+ });
1184
+ }
1185
+ /**
1186
+ * 删除历史记录
1187
+ * @param id
1188
+ * @param silent
1189
+ */
1190
+ remove(e, t = !1) {
1191
+ const i = this.items.findIndex((s) => s.id === e);
1192
+ i > -1 ? (this.items.splice(i, 1), i === this.index ? this.index = -1 : this.index >= this.items.length && (this.index = this.items.length - 1)) : console.warn(`not found HistoryItem for id: ${e} `), t || n.emit(g, {
1193
+ model: this,
1194
+ type: "delete",
1195
+ data: [e]
1196
+ });
1197
+ }
1198
+ forward(e = !1) {
1199
+ const { index: t, items: i } = this;
1200
+ if (t < 0)
1201
+ return;
1202
+ --this.index;
1203
+ const s = i[this.index];
1204
+ s && !e && n.emit(O, {
1205
+ model: this,
1206
+ type: "load",
1207
+ data: s
1208
+ });
1209
+ }
1210
+ backward(e = !1) {
1211
+ const { index: t, items: i } = this;
1212
+ if (t >= i.length - 1)
1213
+ return;
1214
+ t < 0 && (this.index = 0), ++this.index;
1215
+ const s = i[this.index];
1216
+ s && !e && n.emit(O, {
1217
+ model: this,
1218
+ type: "load",
1219
+ data: s
1220
+ });
1221
+ }
1222
+ load(e, t = !1) {
1223
+ const i = this.items.findIndex((s) => s.id === e);
1224
+ i >= 0 && (this.index = i, t || n.emit(O, {
1225
+ model: this,
1226
+ type: "load",
1227
+ data: this.items[i]
1228
+ }));
1229
+ }
1230
+ clear(e = !1) {
1231
+ this.index = -1;
1232
+ const t = this.items.map((i) => i.id);
1233
+ this.items = [], e || n.emit(g, {
1234
+ model: this,
1235
+ type: "clear",
1236
+ data: t
1237
+ });
1238
+ }
1239
+ }
1240
+ const n = w();
1241
+ function K(o) {
1242
+ return o instanceof u;
1243
+ }
1244
+ function U(o) {
1245
+ return o instanceof p;
1246
+ }
1247
+ function Y(o) {
1248
+ return !!o.__VTJ_BLOCK__;
1249
+ }
1250
+ function D(o) {
1251
+ return delete o.id, Array.isArray(o.children) && (o.children = o.children.map((e) => D(e))), o;
1252
+ }
1253
+ export {
1254
+ L as Base,
1255
+ u as BlockModel,
1256
+ _ as DirectiveModel,
1257
+ d as EVENT_BLOCK_CHANGE,
1258
+ g as EVENT_HISTORY_CHANGE,
1259
+ O as EVENT_HISTORY_LOAD,
1260
+ l as EVENT_NODE_CHANGE,
1261
+ V as EVENT_PROJECT_ACTIVED,
1262
+ S as EVENT_PROJECT_APIS_CHANGE,
1263
+ C as EVENT_PROJECT_BLOCKS_CHANGE,
1264
+ c as EVENT_PROJECT_CHANGE,
1265
+ I as EVENT_PROJECT_DEPS_CHANGE,
1266
+ B as EVENT_PROJECT_FILE_PUBLISH,
1267
+ A as EVENT_PROJECT_PAGES_CHANGE,
1268
+ J as EVENT_PROJECT_PUBLISH,
1269
+ y as EventModel,
1270
+ $ as HistoryModel,
1271
+ p as NodeModel,
1272
+ T as ProjectModel,
1273
+ x as PropModel,
1274
+ j as Service,
1275
+ F as VTJ_CORE_VERSION,
1276
+ D as cloneDsl,
1277
+ n as emitter,
1278
+ K as isBlock,
1279
+ Y as isBlockSchema,
1280
+ U as isNode
1281
+ };