@blocklet/pages-kit-inner-components 0.4.99 → 0.4.100

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.
@@ -1,420 +0,0 @@
1
- import { jsx as C } from "react/jsx-runtime";
2
- import { Box as x, Alert as B, Tooltip as G } from "@mui/material";
3
- import q from "gradient-parser";
4
- import K from "tinycolor2";
5
- import { joinURL as j, withQuery as Q } from "ufo";
6
- import { createAxios as X } from "@blocklet/js-sdk";
7
- import Z from "lodash/isEmpty";
8
- import * as Y from "yaml";
9
- import H, { getYjsDoc as tt } from "@syncedstore/core";
10
- import { useSyncedStore as et } from "@syncedstore/react";
11
- import { useLocalStorageState as ot } from "ahooks";
12
- import { produce as b } from "immer";
13
- import nt from "js-cookie";
14
- import W from "lodash/debounce";
15
- import { customAlphabet as rt } from "nanoid";
16
- import { createContext as st, useState as E, useRef as it, useMemo as g, useCallback as U, useEffect as v, useContext as ct } from "react";
17
- import { useParams as at, useNavigate as dt } from "react-router-dom";
18
- import { IndexeddbPersistence as ut } from "y-indexeddb";
19
- import { WebsocketProvider as lt } from "y-websocket";
20
- import { UndoManager as ft } from "yjs";
21
- function mt() {
22
- const { pathname: e } = window.location, o = e.match(/\/admin\/(maker|previewer)\/([^/]+)/), u = o == null ? void 0 : o[2], a = window.__PROJECT_ID__;
23
- if (!u && !a)
24
- throw new Error("Unable to get projectId from pathname");
25
- return u || a;
26
- }
27
- function Gt(e) {
28
- return `${e}:pages:version`;
29
- }
30
- function qt(e, o) {
31
- return o === "yaml" ? Z(e) ? "" : Y.stringify(e, { indent: 2 }) : o === "json" ? JSON.parse(JSON.stringify(e || "{}")) : o === "array" ? JSON.parse(JSON.stringify(e || "[]")) : e;
32
- }
33
- function Kt({ error: e }) {
34
- return /* @__PURE__ */ C(x, { children: /* @__PURE__ */ C(B, { severity: "error", children: e.message }) });
35
- }
36
- var R;
37
- const O = ((R = window.blocklet) == null ? void 0 : R.prefix) || "/", F = X({
38
- timeout: 200 * 1e3
39
- }), pt = window.location.protocol === "https:" ? "wss" : "ws", Qt = (e) => j(`${pt}://${window.location.hostname}`, O, `api/${e}/ws/pages`), St = "z8ia1mAXo8ZE7ytGF36L5uBf9kD2kenhqFGp9";
40
- var M;
41
- (M = blocklet == null ? void 0 : blocklet.componentMountPoints.find((e) => e.did === St)) != null && M.mountPoint || j(O, "/image-bin");
42
- function Xt(e) {
43
- return e && !/^(https?:\/\/|\/)/.test(e) ? window.location.origin + j(O, "uploads", e) : e;
44
- }
45
- const wt = window.innerWidth <= 750;
46
- function Zt(e, o, u) {
47
- if (!e || /\.gif/.test(e)) return e;
48
- const a = (wt ? o / 1.5 : o) * (u === "quality" ? 1.5 : 1);
49
- return ht(e, { imageFilter: "resize", w: a, f: "webp" });
50
- }
51
- function ht(e, o) {
52
- return !e || !o ? e : Q(e, o);
53
- }
54
- function Pt(e) {
55
- if (e != null && e.trim())
56
- try {
57
- return q.parse(e), !0;
58
- } catch {
59
- return !1;
60
- }
61
- return !1;
62
- }
63
- function Yt(e) {
64
- return typeof e != "string" ? !1 : !!(Pt(e) || K(e).isValid());
65
- }
66
- async function gt(e) {
67
- return F.get(`/api/projects/${e}`).then((o) => o.data);
68
- }
69
- async function Ct(e) {
70
- return F.post(`/api/projects/${e.id}/update`, e).then((o) => o.data);
71
- }
72
- const yt = 500, N = rt("abcdefghijklmnopqrstuvwxyz0123456789", 16), J = st(null);
73
- function Ht({
74
- url: e,
75
- name: o,
76
- children: u,
77
- waitingSynced: a
78
- }) {
79
- const [l, d] = E(!1), S = V(), y = `${S}:MakerLocalState`, t = `${S}:${o}`, [r, n] = ot(y, {
80
- defaultValue: {},
81
- listenStorageChange: !0
82
- }), s = it(r);
83
- s.current = r;
84
- const c = g(
85
- () => H({
86
- pages: {},
87
- pageIds: [],
88
- components: {},
89
- supportedLocales: [],
90
- config: {},
91
- resources: {}
92
- }),
93
- []
94
- ), i = g(() => tt(c), [c]);
95
- g(() => new ut(t, i), [t, i]);
96
- const w = g(
97
- () => new lt(e, t, i, { params: { token: nt.get("login_token") } }),
98
- [e, t, i]
99
- ), P = g(
100
- () => new ft(
101
- [i.getArray("pageIds"), i.getMap("pages"), i.getArray("supportedLocales"), i.getMap("config")],
102
- { doc: i }
103
- ),
104
- [i]
105
- ), [I, k] = E(null), [A, L] = E(!1), [T, _] = E(void 0), $ = U(async () => {
106
- if (S) {
107
- L(!0);
108
- try {
109
- const f = await gt(S);
110
- k(f);
111
- } catch (f) {
112
- console.error("Failed to fetch project:", f);
113
- } finally {
114
- L(!1);
115
- }
116
- }
117
- }, [S]), D = U(
118
- async (f, m) => {
119
- var h;
120
- if (_(void 0), !!S) {
121
- m != null && m.optimisticUpdate && I && k((p) => ({
122
- ...p,
123
- ...f
124
- }));
125
- try {
126
- const p = await Ct({
127
- ...f
128
- });
129
- k(p);
130
- } catch (p) {
131
- console.error("Failed to update project:", p), m != null && m.optimisticUpdate && k(I), _((h = p == null ? void 0 : p.response) == null ? void 0 : h.data);
132
- }
133
- }
134
- },
135
- [S, I]
136
- );
137
- v(() => {
138
- S && $();
139
- }, [S, $]);
140
- const z = g(
141
- () => ({
142
- doc: i,
143
- state: c,
144
- localState: r,
145
- setLocalState: (f) => n(typeof f == "function" ? (m) => f(m ?? {}) : f),
146
- undoManager: P,
147
- projectState: {
148
- project: I,
149
- isLoadingProject: A,
150
- error: T
151
- },
152
- updateProject: D
153
- }),
154
- // eslint-disable-next-line react-hooks/exhaustive-deps
155
- [
156
- i,
157
- c,
158
- JSON.stringify(r),
159
- // resolve the problem of react warning
160
- n,
161
- P,
162
- I,
163
- A,
164
- D,
165
- T
166
- ]
167
- );
168
- return v(() => {
169
- P.on("stack-item-added", ({ stackItem: f }) => {
170
- const { currentPageId: m, currentLocale: h, pages: p } = s.current;
171
- f.meta.set("localState", { currentPageId: m, currentLocale: h, pages: p });
172
- }), P.on("stack-item-popped", ({ stackItem: f }) => {
173
- const m = f.meta.get("localState");
174
- m && n((h) => ({ ...h, ...m }));
175
- });
176
- }, [P, n]), v(() => {
177
- w.awareness.setLocalState(r);
178
- }, [w, r]), v(() => {
179
- w.once("synced", () => d(!0));
180
- const f = () => {
181
- n((p) => ({
182
- ...p,
183
- networkStatus: void 0
184
- }));
185
- }, m = () => {
186
- n((p) => ({
187
- ...p,
188
- networkStatus: "offline"
189
- }));
190
- }, h = W(({ status: p }) => {
191
- p === "connected" ? f() : p === "disconnected" && m();
192
- }, yt);
193
- return w.on("status", h), window.addEventListener("online", f), window.addEventListener("offline", m), () => {
194
- w.off("status", h), window.removeEventListener("online", f), window.removeEventListener("offline", m);
195
- };
196
- }, [w, n]), a && !l ? null : /* @__PURE__ */ C(J.Provider, { value: z, children: u });
197
- }
198
- const It = () => {
199
- const e = ct(J);
200
- if (!e)
201
- throw new Error("Context is null");
202
- const o = et(e.state), { setLocalState: u, localState: a } = e, l = dt(), d = V(), S = g(
203
- () => ({
204
- setCurrentSectionId: (t, r) => {
205
- var n, s;
206
- t === a.currentPageId && r === ((s = (n = a.pages) == null ? void 0 : n[t]) == null ? void 0 : s.currentSectionId) || (l(j("/admin/maker", d ?? "", "pages", t)), u(
207
- (c) => b(c, (i) => {
208
- var w;
209
- i.currentPageId = t, i.currentComponentId = void 0, i.componentDebugProps = {}, r !== void 0 && (i.pages ?? (i.pages = {}), (w = i.pages)[t] ?? (w[t] = {}), i.pages[t].currentSectionId = r ?? void 0), r ? window.sessionStorage.setItem("iframe_show_id", `"${r}"`) : window.sessionStorage.removeItem("iframe_show_id");
210
- })
211
- ));
212
- },
213
- setCurrentComponentId: (t) => {
214
- t !== a.currentComponentId && (l(j("/admin/maker", d ?? "", "components", t)), u(
215
- (r) => b(r, (n) => {
216
- n.currentComponentId = t, n.componentDebugProps = {};
217
- })
218
- ));
219
- },
220
- setComponentDebugProps: (() => {
221
- const t = {}, r = W(() => {
222
- u(
223
- (n) => b(n, (s) => {
224
- s.componentDebugProps = {
225
- ...s == null ? void 0 : s.componentDebugProps,
226
- ...t
227
- }, Object.keys(t).forEach((c) => delete t[c]);
228
- })
229
- );
230
- }, 5);
231
- return (n) => {
232
- Object.assign(t, n), r();
233
- };
234
- })()
235
- }),
236
- [u, l, d]
237
- ), y = g(
238
- () => ({
239
- addPage: (t, r) => {
240
- var c, i, w, P;
241
- const n = (t == null ? void 0 : t.id) ?? N(), s = {
242
- ...t,
243
- id: n,
244
- createdAt: (t == null ? void 0 : t.createdAt) ?? (/* @__PURE__ */ new Date()).toISOString(),
245
- updatedAt: (t == null ? void 0 : t.updatedAt) ?? (/* @__PURE__ */ new Date()).toISOString(),
246
- publishedAt: (/* @__PURE__ */ new Date(0)).toISOString(),
247
- slug: (t == null ? void 0 : t.slug) ?? `/${n}`,
248
- sections: (t == null ? void 0 : t.sections) ?? {},
249
- sectionIds: (t == null ? void 0 : t.sectionIds) ?? [],
250
- isPublic: (t == null ? void 0 : t.isPublic) ?? !0,
251
- templateConfig: (t == null ? void 0 : t.templateConfig) ?? {
252
- isTemplate: ((c = t == null ? void 0 : t.templateConfig) == null ? void 0 : c.isTemplate) ?? !0,
253
- displayTemplateId: ((i = t == null ? void 0 : t.templateConfig) == null ? void 0 : i.displayTemplateId) ?? void 0,
254
- dataSourceId: ((w = t == null ? void 0 : t.templateConfig) == null ? void 0 : w.dataSourceId) ?? void 0,
255
- dataSourceParameters: ((P = t == null ? void 0 : t.templateConfig) == null ? void 0 : P.dataSourceParameters) ?? {}
256
- }
257
- };
258
- return e.doc.transact(() => {
259
- o.pages[s.id] = s, o.pageIds.splice(r ?? o.pageIds.length, 0, s.id);
260
- }), s;
261
- },
262
- deletePage: (t) => {
263
- const r = o.pageIds.indexOf(t);
264
- r !== -1 && e.doc.transact(() => {
265
- o.pageIds.splice(r, 1), delete o.pages[t];
266
- });
267
- },
268
- movePage: (t, r) => {
269
- const { pageIds: n } = o, s = n.indexOf(t);
270
- s >= 0 && n.splice(r, 0, ...n.splice(s, 1));
271
- },
272
- addSection: (t, r, n) => {
273
- const s = o.pages[t];
274
- if (!s)
275
- throw new Error(`Page ${t} is not exists`);
276
- const c = {
277
- ...r,
278
- id: r.id ?? N(),
279
- isTemplateSection: r.isTemplateSection ?? !0,
280
- llmConfig: r.llmConfig ?? {}
281
- };
282
- return e.doc.transact(() => {
283
- s.sections[c.id] = c, s.sectionIds.splice(n ?? s.sectionIds.length, 0, c.id);
284
- }), c;
285
- },
286
- deleteSection: (t, r) => {
287
- const n = o.pages[t];
288
- if (!n)
289
- throw new Error(`Page ${t} is not exists`);
290
- const s = n.sectionIds.indexOf(r);
291
- s !== -1 && e.doc.transact(() => {
292
- n.sectionIds.splice(s, 1), delete n.sections[r];
293
- });
294
- },
295
- toggleSectionVisibility: (t, r, n) => {
296
- const s = o.pages[t];
297
- if (!s)
298
- throw new Error(`Page ${t} is not exists`);
299
- const c = s.sections[r];
300
- c && (c.visibility = n);
301
- },
302
- moveSection: (t, r, n) => {
303
- const s = o.pages[t];
304
- if (!s)
305
- throw new Error(`Page ${t} is not exists`);
306
- const { sectionIds: c } = s, i = c.indexOf(r);
307
- i >= 0 && c.splice(n, 0, ...c.splice(i, 1));
308
- }
309
- }),
310
- [o, e.doc]
311
- );
312
- return {
313
- ...e,
314
- state: o,
315
- actions: y,
316
- localActions: S,
317
- get currentPage() {
318
- const {
319
- localState: { currentPageId: t }
320
- } = e;
321
- return t ? o.pages[t] : void 0;
322
- },
323
- get currentSection() {
324
- var s, c, i;
325
- const {
326
- localState: { currentPageId: t, ...r }
327
- } = e;
328
- if (!t)
329
- return;
330
- const n = (c = (s = r.pages) == null ? void 0 : s[t]) == null ? void 0 : c.currentSectionId;
331
- if (n)
332
- return (i = o.pages[t]) == null ? void 0 : i.sections[n];
333
- }
334
- };
335
- }, V = () => {
336
- const { projectId: e } = at();
337
- return e ?? mt() ?? "";
338
- };
339
- function jt(e) {
340
- var a, l;
341
- const { resources: o } = e;
342
- if (!o.components)
343
- return {};
344
- const u = {};
345
- return (l = (a = window == null ? void 0 : window.blocklet) == null ? void 0 : a.componentMountPoints) == null || l.forEach((d) => {
346
- u[d.did] = d;
347
- }), Object.fromEntries(
348
- Object.entries(o.components).map(([d, { blockletId: S, component: y }]) => {
349
- var t;
350
- return [
351
- d,
352
- { data: y, blockletId: S, blockletTitle: ((t = u[S]) == null ? void 0 : t.title) || S }
353
- ];
354
- })
355
- );
356
- }
357
- function kt(e) {
358
- const { components: o } = e;
359
- return o || {};
360
- }
361
- function Et(e) {
362
- return {
363
- ...jt(e),
364
- ...kt(e)
365
- };
366
- }
367
- const te = () => Et(It().state), vt = [
368
- /\.\./,
369
- // Directory traversal
370
- /<[^>]*>/,
371
- // HTML/XML tags
372
- /%[0-9a-f]{2}/i,
373
- // Percent encoding
374
- /[<>'"%;{}()\\]/,
375
- // Special characters
376
- // eslint-disable-next-line no-control-regex
377
- /\x00/,
378
- // Null byte
379
- /\n|\r|\t|\v|\f/,
380
- // Control characters
381
- /[^a-zA-Z0-9-_@/]/
382
- // Disallow any characters except alphanumeric, dash, underscore and forward slash
383
- ];
384
- function ee(e, o, u) {
385
- const a = [];
386
- {
387
- let l;
388
- o.slug ? o.slug.startsWith("/") ? o.slug !== "/" && o.slug.endsWith("/") ? l = e("maker.properties.pathCheckEndWithoutSlash") : /\/{2,}/.test(o.slug) ? l = e("maker.properties.pathCheckConsecutiveSlash") : /\s/.test(o.slug) ? l = e("maker.properties.pathCheckWhitespace") : vt.some((d) => d.test(o.slug)) ? l = e("maker.properties.pathCheckInvalid") : Object.values(u.pages).some((d) => (d == null ? void 0 : d.id) !== o.id && (d == null ? void 0 : d.slug) === o.slug) && (l = e("maker.properties.pathCheckAlreadyExist")) : l = e("maker.properties.pathCheckStartWithSlash") : l = e("maker.properties.pathCheckRequired"), l && a.push({ path: ["path"], message: l });
389
- }
390
- return a.length > 0 ? a : null;
391
- }
392
- function oe({ errors: e, ...o }) {
393
- return e != null && e.length ? /* @__PURE__ */ C(
394
- G,
395
- {
396
- ...o,
397
- title: /* @__PURE__ */ C(x, { children: e.map((u, a) => /* @__PURE__ */ C(x, { children: u.message }, a)) })
398
- }
399
- ) : null;
400
- }
401
- export {
402
- Kt as E,
403
- oe as P,
404
- Ht as S,
405
- Zt as a,
406
- Yt as b,
407
- F as c,
408
- mt as d,
409
- Et as e,
410
- Gt as f,
411
- Xt as g,
412
- It as h,
413
- Pt as i,
414
- te as j,
415
- Qt as k,
416
- ee as l,
417
- N as n,
418
- qt as t,
419
- V as u
420
- };