@blocklet/pages-kit-inner-components 0.6.72 → 0.6.73

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.
@@ -0,0 +1,873 @@
1
+ import { jsx as de } from "react/jsx-runtime";
2
+ import { joinURL as F, withQuery as ue } from "ufo";
3
+ import { createAxios as le } from "@blocklet/js-sdk";
4
+ import pe from "lodash/isEmpty";
5
+ import * as me from "yaml";
6
+ import { useLocaleContext as fe } from "@arcblock/ux/lib/Locale/context";
7
+ import { nextId as B } from "@blocklet/pages-kit/utils/common";
8
+ import { getPageTemplateModel as ge } from "@blocklet/pages-kit/utils/page-model";
9
+ import "@mui/material";
10
+ import Se, { getYjsDoc as Ie } from "@syncedstore/core";
11
+ import { useSyncedStore as Pe } from "@syncedstore/react";
12
+ import { useLocalStorageState as ye } from "ahooks";
13
+ import { produce as $ } from "immer";
14
+ import _ from "lodash/cloneDeep";
15
+ import ee from "lodash/debounce";
16
+ import { createContext as we, useState as W, useRef as te, useMemo as j, useCallback as M, useEffect as V, useContext as Ce } from "react";
17
+ import { useParams as he, useNavigate as ve } from "react-router-dom";
18
+ import { IndexeddbPersistence as Oe } from "y-indexeddb";
19
+ import { WebsocketProvider as be } from "y-websocket";
20
+ import { UndoManager as xe } from "yjs";
21
+ import "@blocklet/pages-kit/types";
22
+ function Ee() {
23
+ const { pathname: o } = window.location, P = o.match(/\/admin\/(maker|previewer)\/([^/]+)/)?.[2], f = window.__PROJECT_ID__;
24
+ if (!P && !f)
25
+ throw new Error("Unable to get projectId from pathname");
26
+ return P || f;
27
+ }
28
+ function st(o) {
29
+ return `${o}:pages:version`;
30
+ }
31
+ function it(o, n) {
32
+ return n === "yaml" ? pe(o) ? "" : me.stringify(o, { indent: 2 }) : n === "json" ? JSON.parse(JSON.stringify(o || {})) : n === "array" ? JSON.parse(JSON.stringify(o || [])) : o;
33
+ }
34
+ const Q = window.blocklet?.prefix || "/", oe = le({
35
+ timeout: 200 * 1e3
36
+ }), Te = window.location.protocol === "https:" ? "wss" : "ws", at = (o) => F(`${Te}://${window.location.hostname}`, Q, `api/${o}/ws/pages`), ke = "z8ia1mAXo8ZE7ytGF36L5uBf9kD2kenhqFGp9";
37
+ blocklet?.componentMountPoints.find((o) => o.did === ke)?.mountPoint || F(Q, "/image-bin");
38
+ function dt(o) {
39
+ return o && !/^(https?:\/\/|\/)/.test(o) ? window.location.origin + F(Q, "uploads", o) : o;
40
+ }
41
+ const Ne = window.innerWidth <= 750;
42
+ function ut(o, n, P) {
43
+ if (!o || /\.gif/.test(o)) return o;
44
+ const f = (Ne ? n / 1.5 : n) * (P === "quality" ? 1.5 : 1);
45
+ return Le(o, { imageFilter: "resize", w: f, f: "webp" });
46
+ }
47
+ function Le(o, n) {
48
+ return !o || !n ? o : ue(o, n);
49
+ }
50
+ async function je(o) {
51
+ return oe.get(`/api/projects/${o}`).then((n) => n.data);
52
+ }
53
+ async function Me(o) {
54
+ return oe.post(`/api/projects/${o.id}/update`, o).then((n) => n.data);
55
+ }
56
+ const q = "@COMPONENT-TEMPLATE-", X = (o) => `${q}${o}`, K = (o) => o.startsWith(q), Z = (o) => K(o) ? o.slice(q.length) : null;
57
+ function ne(o, n = {}) {
58
+ return n[o.id] = o, o.sectionIds && o.sections && o.sectionIds.forEach((P) => {
59
+ const f = o.sections?.[P];
60
+ f && ne(f, n);
61
+ }), n;
62
+ }
63
+ function Ae(o, n) {
64
+ const P = X(o.id), f = n.section, O = ne(f);
65
+ n.dataSource ??= {};
66
+ const w = n.dataSource;
67
+ return {
68
+ id: P,
69
+ createdAt: o.createdAt,
70
+ updatedAt: o.updatedAt,
71
+ publishedAt: o.updatedAt,
72
+ slug: o.id,
73
+ sections: O,
74
+ sectionIds: [f.id],
75
+ isPublic: !0,
76
+ templateConfig: { isTemplate: !0 },
77
+ locales: {},
78
+ dataSource: w
79
+ };
80
+ }
81
+ const De = 500, ce = we(null);
82
+ function lt({
83
+ url: o,
84
+ name: n,
85
+ children: P,
86
+ waitingSynced: f
87
+ }) {
88
+ const [O, w] = W(!1), { locale: A } = fe(), h = re(), k = `${h}:MakerLocalState`, b = `${h}:${n}`, [v, S] = ye(k, {
89
+ defaultValue: {},
90
+ listenStorageChange: !0
91
+ }), R = te(v);
92
+ R.current = v;
93
+ const J = j(
94
+ () => Se({
95
+ dataSourceIds: [],
96
+ dataSources: {},
97
+ pages: {},
98
+ pageIds: [],
99
+ routes: {},
100
+ routeIds: [],
101
+ components: {},
102
+ supportedLocales: [],
103
+ config: {},
104
+ resources: {}
105
+ }),
106
+ []
107
+ ), y = j(() => Ie(J), [J]);
108
+ j(() => new Oe(b, y), [b, y]);
109
+ const C = j(
110
+ () => new be(o, b, y, {
111
+ params: {},
112
+ resyncInterval: 5 * 60 * 1e3
113
+ }),
114
+ [o, b, y]
115
+ ), U = M(() => {
116
+ C && (C.wsconnected ? (C.disconnect(), setTimeout(() => C.connect(), 100)) : C.connect());
117
+ }, [C]), e = j(
118
+ () => new xe(
119
+ [
120
+ y.getArray("pageIds"),
121
+ y.getMap("pages"),
122
+ y.getArray("supportedLocales"),
123
+ y.getMap("config"),
124
+ y.getMap("dataSources"),
125
+ y.getArray("dataSourceIds"),
126
+ y.getArray("routeIds"),
127
+ y.getMap("routes")
128
+ ],
129
+ { doc: y }
130
+ ),
131
+ [y]
132
+ ), [t, c] = W(null), [r, d] = W(!1), [i, l] = W(void 0), m = M(async () => {
133
+ if (h) {
134
+ d(!0);
135
+ try {
136
+ const s = await je(h);
137
+ c(s);
138
+ } catch (s) {
139
+ console.error("Failed to fetch project:", s);
140
+ } finally {
141
+ d(!1);
142
+ }
143
+ }
144
+ }, [h]), u = M(
145
+ async (s, a) => {
146
+ if (l(void 0), !!h) {
147
+ a?.optimisticUpdate && t && c((g) => ({
148
+ ...g,
149
+ ...s
150
+ }));
151
+ try {
152
+ const g = await Me({
153
+ ...s
154
+ });
155
+ c(g);
156
+ } catch (g) {
157
+ console.error("Failed to update project:", g), a?.optimisticUpdate && c(t), l(g?.response?.data);
158
+ }
159
+ }
160
+ },
161
+ [h, t]
162
+ );
163
+ V(() => {
164
+ h && m();
165
+ }, [h, m]);
166
+ const p = j(
167
+ () => ({
168
+ doc: y,
169
+ state: J,
170
+ localState: {
171
+ ...v,
172
+ // always use the locale from the context
173
+ currentLocale: A
174
+ },
175
+ setLocalState: (s) => S(typeof s == "function" ? (a) => s(a ?? {}) : s),
176
+ undoManager: e,
177
+ projectState: {
178
+ project: t,
179
+ isLoadingProject: r,
180
+ error: i
181
+ },
182
+ updateProject: u,
183
+ forceSync: U
184
+ }),
185
+ // eslint-disable-next-line react-hooks/exhaustive-deps
186
+ [
187
+ y,
188
+ J,
189
+ JSON.stringify(v),
190
+ // resolve the problem of react warning
191
+ S,
192
+ e,
193
+ t,
194
+ r,
195
+ u,
196
+ i,
197
+ A,
198
+ U
199
+ ]
200
+ );
201
+ return V(() => {
202
+ e.on("stack-item-added", ({ stackItem: s }) => {
203
+ const { currentPageId: a, currentLocale: g, pages: I } = R.current;
204
+ s.meta.set("localState", { currentPageId: a, currentLocale: g, pages: I });
205
+ }), e.on("stack-item-popped", ({ stackItem: s }) => {
206
+ const a = s.meta.get("localState");
207
+ a && S((g) => ({ ...g, ...a }));
208
+ });
209
+ }, [e, S]), V(() => {
210
+ C.awareness.setLocalState(v);
211
+ }, [C, v]), V(() => {
212
+ C.once("synced", () => {
213
+ w(!0);
214
+ });
215
+ let s = 0, a = null;
216
+ const g = 3, I = 5e3, N = 15e3, x = () => {
217
+ s = 0, S((T) => ({
218
+ ...T,
219
+ networkStatus: void 0
220
+ }));
221
+ }, E = () => {
222
+ S((T) => ({
223
+ ...T,
224
+ networkStatus: "offline"
225
+ }));
226
+ }, L = ee(({ status: T }) => {
227
+ if (T === "connected")
228
+ x(), a && (clearTimeout(a), a = null);
229
+ else if (T === "disconnected") {
230
+ if (s++, !a) {
231
+ const se = Date.now();
232
+ let H = 0;
233
+ const ie = Math.ceil(N / I), Y = () => {
234
+ H++;
235
+ const ae = Date.now() - se;
236
+ if (C.wsconnected) {
237
+ x();
238
+ return;
239
+ }
240
+ if (ae >= N || H >= ie) {
241
+ E();
242
+ return;
243
+ }
244
+ a = setTimeout(Y, I);
245
+ };
246
+ a = setTimeout(Y, I);
247
+ }
248
+ s >= g && E();
249
+ }
250
+ }, De), D = () => {
251
+ C.wsconnected || C.connect(), x();
252
+ }, G = () => {
253
+ E();
254
+ };
255
+ return C.on("status", L), window.addEventListener("online", D), window.addEventListener("offline", G), C.wsconnected && x(), () => {
256
+ a && clearTimeout(a), C.off("status", L), window.removeEventListener("online", D), window.removeEventListener("offline", G);
257
+ };
258
+ }, [C, S]), f && !O ? null : /* @__PURE__ */ de(ce.Provider, { value: p, children: P });
259
+ }
260
+ const _e = () => {
261
+ const o = Ce(ce);
262
+ if (!o)
263
+ throw new Error("Context is null");
264
+ const n = Pe(o.state), { setLocalState: P, localState: f } = o, O = ve(), w = re(), A = te({}), h = M(
265
+ (e) => {
266
+ const t = n.components[e]?.data;
267
+ return !t || t.renderer?.type !== "component-template" ? void 0 : (A.current[e] || (A.current[e] = {
268
+ page: Ae(t, t.renderer)
269
+ }), A.current[e]?.page);
270
+ },
271
+ [n.components]
272
+ ), k = M(
273
+ (e) => {
274
+ const t = n.components[e]?.data;
275
+ if (!t || t.renderer?.type !== "component-template")
276
+ return;
277
+ const c = h(e);
278
+ if (c)
279
+ return {
280
+ componentId: e,
281
+ component: t,
282
+ renderer: t.renderer,
283
+ page: c
284
+ };
285
+ },
286
+ [h, n.components]
287
+ ), b = M(
288
+ (e) => {
289
+ const t = n.pages[e];
290
+ if (t)
291
+ return { kind: "page", pageId: e, page: t };
292
+ if (!K(e))
293
+ return;
294
+ const c = Z(e);
295
+ if (!c)
296
+ return;
297
+ const r = k(c);
298
+ if (r)
299
+ return {
300
+ kind: "component-template",
301
+ pageId: e,
302
+ page: r.page,
303
+ componentId: r.componentId,
304
+ component: r.component,
305
+ renderer: r.renderer
306
+ };
307
+ },
308
+ [k, n.pages]
309
+ ), v = M(
310
+ (e) => {
311
+ const t = b(e);
312
+ if (!t)
313
+ throw new Error(`Page ${e} is not exists`);
314
+ return t;
315
+ },
316
+ [b]
317
+ ), S = j(
318
+ () => ({
319
+ get currentPage() {
320
+ const {
321
+ localState: { currentPageId: e, currentComponentId: t }
322
+ } = o;
323
+ if (e) {
324
+ const c = b(e);
325
+ if (c)
326
+ return c.page;
327
+ }
328
+ if (t) {
329
+ const c = k(t);
330
+ if (c)
331
+ return c.page;
332
+ }
333
+ },
334
+ get currentSection() {
335
+ const {
336
+ localState: { currentPageId: e, ...t }
337
+ } = o, { currentPage: c } = this;
338
+ if (!c || !e)
339
+ return;
340
+ const r = t.pages?.[e]?.currentSectionId;
341
+ if (r)
342
+ return c.sections?.[r] ?? this.currentPageModel?.getComponentById(r)?.model;
343
+ },
344
+ get currentSectionProperties() {
345
+ const {
346
+ localState: { currentPageId: e, ...t }
347
+ } = o, { currentPage: c } = this;
348
+ if (!c || !e)
349
+ return;
350
+ const r = t.pages?.[e]?.currentSectionId;
351
+ if (!r)
352
+ return;
353
+ const d = c.dataSource?.[r];
354
+ if (!d)
355
+ return;
356
+ const { currentLocale: i } = this.currentLocaleMap;
357
+ return i ? d[i] : void 0;
358
+ },
359
+ get currentSectionPropertiesWithFallback() {
360
+ const {
361
+ localState: { currentPageId: e, ...t }
362
+ } = o, { currentPage: c } = this;
363
+ if (!c || !e)
364
+ return;
365
+ const r = t.pages?.[e]?.currentSectionId;
366
+ if (!r)
367
+ return;
368
+ const d = c.dataSource?.[r];
369
+ if (!d)
370
+ return;
371
+ const { currentLocale: i, defaultLocale: l, fallbackLocale: m } = this.currentLocaleMap;
372
+ if (i && d[i])
373
+ return d[i];
374
+ if (l && d[l])
375
+ return d[l];
376
+ if (m && d[m])
377
+ return d[m];
378
+ },
379
+ get currentLocaleMap() {
380
+ const {
381
+ localState: { currentLocale: e },
382
+ state: {
383
+ supportedLocales: t,
384
+ config: { defaultLocale: c }
385
+ }
386
+ } = o;
387
+ return {
388
+ currentLocale: e,
389
+ defaultLocale: c,
390
+ fallbackLocale: t?.[0]?.locale ?? "en"
391
+ };
392
+ },
393
+ get currentRoute() {
394
+ const {
395
+ localState: { currentRouteId: e }
396
+ } = o;
397
+ return e ? n.routes?.[e] : void 0;
398
+ },
399
+ get currentPageModel() {
400
+ const e = this.currentPage ?? null;
401
+ if (e)
402
+ return ge(e, !1);
403
+ }
404
+ }),
405
+ [JSON.stringify(f), n]
406
+ ), R = j(() => {
407
+ if (f.currentPageId) {
408
+ const e = b(f.currentPageId);
409
+ if (e?.kind === "component-template")
410
+ return e;
411
+ }
412
+ if (f.currentComponentId) {
413
+ const e = k(f.currentComponentId);
414
+ if (e)
415
+ return {
416
+ kind: "component-template",
417
+ pageId: X(e.componentId),
418
+ page: e.page,
419
+ componentId: e.componentId,
420
+ component: e.component,
421
+ renderer: e.renderer
422
+ };
423
+ }
424
+ }, [f.currentPageId, f.currentComponentId, b, k]), J = M(
425
+ () => R?.kind === "component-template" ? R : void 0,
426
+ [R]
427
+ ), y = j(
428
+ () => ({
429
+ setCurrentSectionId: (e, t) => {
430
+ const c = K(e), r = c ? Z(e) : null;
431
+ e === f.currentPageId && t === f.pages?.[e]?.currentSectionId && (!c || r === f.currentComponentId) || (O(c && r ? F("/admin/maker", w ?? "", "components", r) : F("/admin/maker", w ?? "", "pages", e)), P(
432
+ (d) => $(d, (i) => {
433
+ i.currentPageId = e, c ? r && (i.currentComponentId = r) : i.currentComponentId = void 0, i.pages ??= {}, i.pages[e] ??= {}, i.pages[e].currentSectionId = t ?? void 0, t ? window.sessionStorage.setItem("iframe_show_id", `"${t}"`) : window.sessionStorage.removeItem("iframe_show_id");
434
+ })
435
+ ));
436
+ },
437
+ setCurrentComponentId: (e) => {
438
+ const c = n.components[e]?.data?.renderer?.type === "component-template", r = c ? X(e) : void 0;
439
+ e === f.currentComponentId && (!c || f.currentPageId === r) || (O(F("/admin/maker", w ?? "", "components", e)), P(
440
+ (d) => $(d, (i) => {
441
+ i.currentComponentId = e, c && r ? (i.currentPageId = r, i.pages ??= {}, i.pages[r] ??= {}) : i.currentPageId && K(i.currentPageId) && (i.currentPageId = void 0);
442
+ })
443
+ ));
444
+ },
445
+ setCurrentRouteId: ({ routeId: e, dynamicParams: t }) => {
446
+ P(
447
+ (c) => $(c, (r) => {
448
+ r.currentRouteId = e, r.currentDynamicParams = t;
449
+ })
450
+ );
451
+ },
452
+ // 本地缓存组件调试参数,避免用户切换组件后,需要重新输入
453
+ setCustomComponentPreviewerProperties: (() => {
454
+ const e = {};
455
+ let t = "";
456
+ const c = ee(() => {
457
+ P(
458
+ (r) => $(r, (d) => {
459
+ d.customComponentPreviewerProperties ??= {}, d.customComponentPreviewerProperties[t] = {
460
+ ...d.customComponentPreviewerProperties[t],
461
+ ..._(e[t])
462
+ }, delete e[t];
463
+ })
464
+ );
465
+ }, 5);
466
+ return (r, d) => {
467
+ const i = e[r] ?? {};
468
+ Object.assign(i, d), e[r] = i, t = r, c();
469
+ };
470
+ })(),
471
+ setRouteGenerateProcess: (e) => {
472
+ P(
473
+ (t) => $(t, (c) => {
474
+ e ? c.routeGenerateProcess = {
475
+ ...c.routeGenerateProcess,
476
+ ...e
477
+ } : c.routeGenerateProcess = {};
478
+ })
479
+ );
480
+ },
481
+ resetRouteGenerateProcess: () => {
482
+ P(
483
+ (e) => $(e, (t) => {
484
+ t.routeGenerateProcess = {
485
+ progress: "complete"
486
+ };
487
+ })
488
+ );
489
+ }
490
+ }),
491
+ [P, O, w, S]
492
+ ), C = M(
493
+ (e, t, c, r, d) => {
494
+ const i = v(e), { page: l } = i, m = _(t), u = c ? _(c) : void 0, p = z(m);
495
+ return o.doc.transact(() => {
496
+ if (d && d.component === "layout-block") {
497
+ const s = S.currentPageModel?.getComponentById(d.id);
498
+ s && (s.model.sections ??= {}, s.model.sections[p.id] = p, s.model.sectionIds ??= [], s.model.sectionIds.push(p.id));
499
+ } else if (i.kind === "component-template") {
500
+ const s = i.renderer.section;
501
+ s.sections ??= {}, s.sections[p.id] = p, s.sectionIds ??= [], s.sectionIds.push(p.id);
502
+ } else
503
+ l.sections[p.id] = p, l.sectionIds.splice(r ?? l.sectionIds.length, 0, p.id);
504
+ if (u) {
505
+ l.dataSource ??= {};
506
+ const s = (a, g) => {
507
+ const I = u[a.id];
508
+ I && (l.dataSource[g.id] = _(I)), a.component === "layout-block" && a.sectionIds?.length && a.sectionIds.forEach((N, x) => {
509
+ const E = a.sections?.[N], L = g.sectionIds?.[x], D = L ? g.sections?.[L] : void 0;
510
+ E && D && s(E, D);
511
+ });
512
+ };
513
+ s(m, p);
514
+ }
515
+ }), y.setCurrentSectionId(e, p.id), p;
516
+ },
517
+ [v, S, o.doc, y]
518
+ ), U = j(
519
+ () => ({
520
+ addPage: (e, t) => {
521
+ const c = e?.id ?? B(), r = {
522
+ ...e,
523
+ id: c,
524
+ createdAt: e?.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
525
+ updatedAt: e?.updatedAt ?? (/* @__PURE__ */ new Date()).toISOString(),
526
+ publishedAt: (/* @__PURE__ */ new Date(0)).toISOString(),
527
+ slug: e?.slug ?? `/${c}`,
528
+ sections: e?.sections ?? {},
529
+ sectionIds: e?.sectionIds ?? [],
530
+ isPublic: e?.isPublic ?? !0,
531
+ templateConfig: e?.templateConfig ?? {
532
+ isTemplate: e?.templateConfig?.isTemplate ?? !0,
533
+ displayTemplateId: e?.templateConfig?.displayTemplateId ?? void 0,
534
+ dataSourceIds: e?.templateConfig?.dataSourceIds ?? void 0,
535
+ dataSourceParameters: e?.templateConfig?.dataSourceParameters ?? {},
536
+ enabledGenerate: e?.templateConfig?.enabledGenerate ?? !1,
537
+ agentId: e?.templateConfig?.agentId ?? void 0
538
+ }
539
+ };
540
+ return o.doc.transact(() => {
541
+ n.pages[r.id] = r, n.pageIds.splice(t ?? n.pageIds.length, 0, r.id);
542
+ }), r;
543
+ },
544
+ addRoute: (e, t) => {
545
+ const c = e?.id ?? B(), r = {
546
+ ...e,
547
+ id: c,
548
+ createdAt: e?.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
549
+ updatedAt: e?.updatedAt ?? (/* @__PURE__ */ new Date()).toISOString(),
550
+ publishedAt: (/* @__PURE__ */ new Date(0)).toISOString(),
551
+ path: e?.path ?? `/${c}`,
552
+ handler: e?.handler ?? "Pages Kit",
553
+ isPublic: e?.isPublic ?? !0,
554
+ params: e?.params ?? [],
555
+ enabledGenerate: e?.enabledGenerate ?? !1,
556
+ displayTemplateId: e?.displayTemplateId ?? void 0,
557
+ dataSource: e?.dataSource ?? {}
558
+ };
559
+ return o.doc.transact(() => {
560
+ n.routes ??= {}, n.routeIds ??= [], n.routes[r.id] = r, n.routeIds.splice(t ?? n.routeIds.length, 0, r.id);
561
+ }), r;
562
+ },
563
+ deleteRoute: (e) => {
564
+ if (!e)
565
+ return;
566
+ const t = n.routeIds?.indexOf(e);
567
+ t !== void 0 && t !== -1 && o.doc.transact(() => {
568
+ n.routeIds?.splice(t, 1), delete n.routes?.[e];
569
+ });
570
+ },
571
+ deletePage: (e) => {
572
+ const t = n.pageIds.indexOf(e);
573
+ t !== -1 && o.doc.transact(() => {
574
+ n.pageIds.splice(t, 1), delete n.pages[e];
575
+ });
576
+ },
577
+ movePage: (e, t) => {
578
+ const { pageIds: c } = n, r = c.indexOf(e);
579
+ r >= 0 && c.splice(t, 0, ...c.splice(r, 1));
580
+ },
581
+ addSection: (e, t, c, r) => {
582
+ const d = v(e), { page: i } = d, l = t?.config, m = typeof l?.componentId == "string" ? l.componentId : void 0;
583
+ if (t.component === "custom-component" && m) {
584
+ const a = (n.components[m]?.data ?? n.resources?.components?.[m]?.component)?.renderer;
585
+ if (a?.type === "component-template")
586
+ return C(e, a.section, a.dataSource, c, r);
587
+ }
588
+ const u = {
589
+ ...t,
590
+ id: t.id ?? B(),
591
+ isTemplateSection: t.isTemplateSection ?? !0
592
+ /** @deprecated 已经废弃,llmConfig 从 component / resources.component 中获取 */
593
+ // llmConfig: section.llmConfig ?? {},
594
+ }, p = _(u.locales);
595
+ return delete u.locales, o.doc.transact(() => {
596
+ if (r && r.component === "layout-block") {
597
+ const s = S.currentPageModel?.getComponentById(r.id);
598
+ s && (s.model.sections ??= {}, s.model.sections[u.id] = u, s.model.sectionIds ??= [], s.model.sectionIds.push(u.id));
599
+ } else if (d.kind === "component-template") {
600
+ const s = d.renderer.section;
601
+ s.sections ??= {}, s.sections[u.id] = u, s.sectionIds ??= [], s.sectionIds.push(u.id);
602
+ } else
603
+ i.sections[u.id] = u, i.sectionIds.splice(c ?? i.sectionIds.length, 0, u.id);
604
+ p && (i.dataSource ??= {}, i.dataSource[u.id] = p);
605
+ }), u;
606
+ },
607
+ deleteSection: (e, t) => {
608
+ const c = v(e), { page: r } = c;
609
+ o.doc.transact(() => {
610
+ const d = [], i = S.currentPageModel?.getComponentById(t), l = i?.parent;
611
+ if (i && (d.push(i.model.id, ...i.all().map((m) => m.model.id)), l?.model.component === "layout-block")) {
612
+ const m = l.model.sectionIds?.indexOf(t);
613
+ m !== void 0 && m !== -1 && (l.model.sectionIds?.splice(m, 1), delete l.model.sections?.[t]), l.model.config?.gridSettings && Object.keys(l.model.config.gridSettings).forEach((u) => {
614
+ try {
615
+ delete l.model.config.gridSettings[u]?.[t];
616
+ } catch {
617
+ }
618
+ }), l.model.config?.backgroundSectionId === t && (l.model.config.backgroundSectionId = void 0);
619
+ }
620
+ d.forEach((m) => {
621
+ const u = r.sectionIds.indexOf(m);
622
+ u !== -1 && r.sectionIds.splice(u, 1), r.sections[m] && delete r.sections[m], r.dataSource?.[m] && delete r.dataSource?.[m];
623
+ });
624
+ });
625
+ },
626
+ lockSection: (e, t) => {
627
+ v(e);
628
+ const c = S.currentPageModel?.getComponentById(t)?.model;
629
+ c && (c.locked = !c.locked);
630
+ },
631
+ toggleSectionVisibility: (e, t, c) => {
632
+ v(e);
633
+ const r = S.currentPageModel?.getComponentById(t)?.model;
634
+ r && (r.visibility = c);
635
+ },
636
+ moveSection: (e, t, c, r) => {
637
+ const d = v(e), { page: i } = d, l = S.currentPageModel;
638
+ if (!l)
639
+ throw new Error(`Page model for ${e} is not available`);
640
+ const m = (p) => {
641
+ const s = l.getComponentById(p);
642
+ if (!s) return null;
643
+ const a = s.model, g = s.parent;
644
+ if (!g)
645
+ return {
646
+ section: a,
647
+ container: i,
648
+ containerType: "root",
649
+ index: i.sectionIds.indexOf(p)
650
+ };
651
+ const I = g.model;
652
+ return {
653
+ section: a,
654
+ container: I,
655
+ containerType: "layout-block",
656
+ index: I.sectionIds.indexOf(p)
657
+ };
658
+ }, u = m(t);
659
+ if (!u)
660
+ throw new Error(`Section ${t} not found`);
661
+ o.doc.transact(() => {
662
+ if (c.startsWith("layout-")) {
663
+ const a = c.replace("layout-", ""), I = l.getComponentById(a)?.model;
664
+ if (!I || I.component !== "layout-block")
665
+ throw new Error(`Layout block ${a} not found`);
666
+ if (a === t || ((E, L) => {
667
+ const D = l.getComponentById(E), G = l.getComponentById(L);
668
+ if (!D || !G || D.model.component !== "layout-block")
669
+ return !1;
670
+ let T = G.parent;
671
+ for (; T; ) {
672
+ if (T.model.id === E)
673
+ return !0;
674
+ T = T.parent;
675
+ }
676
+ return !1;
677
+ })(t, a))
678
+ return;
679
+ const x = JSON.parse(JSON.stringify(u.section));
680
+ I.sections || (I.sections = {}), I.sectionIds || (I.sectionIds = []), I.sections[t] = x, I.sectionIds.push(t), u.container.sectionIds.splice(u.index, 1), u.containerType === "layout-block" && delete u.container.sections[t];
681
+ return;
682
+ }
683
+ const p = m(c);
684
+ if (!p)
685
+ throw new Error(`Target section ${c} not found`);
686
+ let s = p.index;
687
+ if (r === "bottom" && (s = p.index + 1), u.container === p.container) {
688
+ const { sectionIds: a } = u.container;
689
+ a.splice(u.index, 1), u.index < s && (s -= 1), a.splice(s, 0, t);
690
+ } else {
691
+ const a = JSON.parse(JSON.stringify(u.section));
692
+ p.containerType === "root" || (p.container.sections || (p.container.sections = {}), p.container.sections[t] = a), p.container.sectionIds.splice(s, 0, t), u.container.sectionIds.splice(u.index, 1), u.containerType === "layout-block" && delete u.container.sections[t];
693
+ }
694
+ });
695
+ },
696
+ copySection: (e, t) => {
697
+ v(e);
698
+ const c = S.currentPageModel?.getComponentById(t);
699
+ if (!c?.model)
700
+ throw new Error(`Section ${t} is not exists`);
701
+ const r = JSON.parse(JSON.stringify(c.model)), d = z(r), i = d.id, l = c?.parent;
702
+ if (l) {
703
+ if (l.model.sections[i] = d, l.model.sectionIds.push(i), S.currentPage?.dataSource?.[t] && (S.currentPage.dataSource[i] = JSON.parse(
704
+ JSON.stringify(S.currentPage.dataSource[t])
705
+ ), d.component === "layout-block")) {
706
+ const m = (u, p) => {
707
+ const s = u.sectionIds, a = p.sectionIds;
708
+ s?.forEach((g, I) => {
709
+ const N = a?.[I];
710
+ if (N) {
711
+ if (N && S.currentPage?.dataSource) {
712
+ const L = S.currentPage.dataSource[N];
713
+ L && (S.currentPage.dataSource[g] = JSON.parse(JSON.stringify(L)));
714
+ }
715
+ const x = u.sections?.[g], E = p?.sections?.[N];
716
+ x?.component === "layout-block" && x.sectionIds && E?.sectionIds && m(x, E);
717
+ }
718
+ });
719
+ };
720
+ d.sectionIds && r.sectionIds && m(d, r);
721
+ }
722
+ y.setCurrentSectionId(e, i);
723
+ }
724
+ return d;
725
+ },
726
+ extractSectionToComponentTemplate: (e, t) => {
727
+ const c = v(e), { page: r } = c, i = S.currentPageModel?.getComponentById(t)?.model;
728
+ if (!i) {
729
+ console.warn(`Section ${t} is not exists`);
730
+ return;
731
+ }
732
+ const l = JSON.parse(JSON.stringify(i));
733
+ if (l.component !== "layout-block" || !(l.sectionIds?.length ?? 0)) {
734
+ console.warn(`Section ${t} is not eligible for component template extraction.`);
735
+ return;
736
+ }
737
+ const m = (s, a = []) => (s && (a.push(s.id), s.sectionIds?.forEach((g) => {
738
+ const I = s.sections?.[g];
739
+ m(I, a);
740
+ })), a), p = m(i).reduce(
741
+ (s, a) => {
742
+ const g = r.dataSource?.[a];
743
+ return g && (s[a] = JSON.parse(JSON.stringify(g))), s;
744
+ },
745
+ {}
746
+ );
747
+ return {
748
+ section: l,
749
+ dataSource: Object.keys(p).length ? p : void 0
750
+ };
751
+ },
752
+ addComponentTemplate: ({
753
+ name: e,
754
+ description: t,
755
+ section: c,
756
+ dataSource: r
757
+ }) => {
758
+ const d = B(), i = (/* @__PURE__ */ new Date()).toISOString(), l = _(c), m = r ? _(r) : void 0;
759
+ return o.doc.transact(() => {
760
+ const u = Object.values(n.components).length;
761
+ n.components[d] = {
762
+ index: u,
763
+ data: {
764
+ id: d,
765
+ name: e.trim() || void 0,
766
+ description: t?.trim() || void 0,
767
+ createdAt: i,
768
+ updatedAt: i,
769
+ renderer: {
770
+ type: "component-template",
771
+ section: l,
772
+ dataSource: m
773
+ }
774
+ }
775
+ };
776
+ }), n.components[d];
777
+ },
778
+ addDataSource: (e) => {
779
+ const t = e.id ?? B(), c = {
780
+ ...e ?? {},
781
+ id: t,
782
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
783
+ };
784
+ return o.doc.transact(() => {
785
+ n.dataSourceIds ??= [], n.dataSources ??= {}, n.dataSourceIds.push(t), n.dataSources[t] = c;
786
+ }), n.dataSources[t];
787
+ },
788
+ deleteDataSource: (e) => {
789
+ const t = n.dataSourceIds.indexOf(e);
790
+ t !== -1 && o.doc.transact(() => {
791
+ n.dataSourceIds.splice(t, 1), delete n.dataSources[e];
792
+ });
793
+ }
794
+ }),
795
+ [n, o.doc, S]
796
+ );
797
+ return {
798
+ ...o,
799
+ state: n,
800
+ actions: U,
801
+ localActions: y,
802
+ ...S,
803
+ getComponentTemplateContext: J
804
+ };
805
+ }, re = () => {
806
+ const { projectId: o } = he();
807
+ return o ?? Ee() ?? "";
808
+ }, pt = () => window.__PROJECT_SLUG__ ?? "";
809
+ function Re(o) {
810
+ const { resources: n } = o;
811
+ if (!n.components)
812
+ return {};
813
+ const P = {};
814
+ return window?.blocklet?.componentMountPoints?.forEach((f) => {
815
+ P[f.did] = f;
816
+ }), Object.fromEntries(
817
+ Object.entries(n.components).map(([f, { blockletId: O, component: w }]) => [
818
+ f,
819
+ { data: w, blockletId: O, blockletTitle: P[O]?.title || O }
820
+ ])
821
+ );
822
+ }
823
+ function Je(o) {
824
+ const { components: n } = o;
825
+ return n || {};
826
+ }
827
+ function $e(o) {
828
+ return {
829
+ ...Re(o),
830
+ ...Je(o)
831
+ };
832
+ }
833
+ const mt = () => $e(_e().state);
834
+ function z(o) {
835
+ const n = JSON.parse(JSON.stringify(o)), P = B();
836
+ if (n.component === "layout-block") {
837
+ const f = { ...n.config ?? {} }, O = n.sectionIds?.map((w) => {
838
+ const A = n.sections?.[w], h = z(n.sections?.[w]), k = h.id;
839
+ return A?.id === n.config?.backgroundSectionId && (f.backgroundSectionId = h.id), n.config?.gridSettings && Object.keys(n.config.gridSettings).forEach((b) => {
840
+ f.gridSettings[b] = {
841
+ ...f.gridSettings[b] ?? {},
842
+ [k]: {
843
+ ...n.config?.gridSettings?.[b]?.[w],
844
+ id: k
845
+ }
846
+ }, delete n.config?.gridSettings?.[b]?.[w];
847
+ }), h;
848
+ });
849
+ n.sections = Object.fromEntries(O?.map((w) => [w.id, w]) ?? []), n.sectionIds = O?.map((w) => w.id) ?? [], n.config = f;
850
+ }
851
+ return {
852
+ ...n,
853
+ id: P
854
+ };
855
+ }
856
+ export {
857
+ lt as S,
858
+ _e as a,
859
+ st as b,
860
+ $e as c,
861
+ pt as d,
862
+ ut as e,
863
+ dt as f,
864
+ at as g,
865
+ mt as h,
866
+ oe as i,
867
+ Ae as j,
868
+ Ee as k,
869
+ Je as l,
870
+ Re as m,
871
+ it as t,
872
+ re as u
873
+ };