@blocklet/pages-kit-inner-components 0.4.98 → 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,418 +0,0 @@
1
- import { jsx as P } 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 I, 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: t } = window.location, o = t.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(t) {
28
- return `${t}:pages:version`;
29
- }
30
- function qt(t, o) {
31
- return o === "yaml" ? Z(t) ? "" : Y.stringify(t, { indent: 2 }) : o === "json" ? JSON.parse(JSON.stringify(t || "{}")) : o === "array" ? JSON.parse(JSON.stringify(t || "[]")) : t;
32
- }
33
- function Kt({ error: t }) {
34
- return /* @__PURE__ */ P(x, { children: /* @__PURE__ */ P(B, { severity: "error", children: t.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 = (t) => I(`${pt}://${window.location.hostname}`, O, `api/${t}/ws/pages`), St = "z8ia1mAXo8ZE7ytGF36L5uBf9kD2kenhqFGp9";
40
- var M;
41
- (M = blocklet == null ? void 0 : blocklet.componentMountPoints.find((t) => t.did === St)) != null && M.mountPoint || I(O, "/image-bin");
42
- function Xt(t) {
43
- return t && !/^(https?:\/\/|\/)/.test(t) ? window.location.origin + I(O, "uploads", t) : t;
44
- }
45
- const wt = window.innerWidth <= 750;
46
- function Zt(t, o, u) {
47
- if (!t || /\.gif/.test(t)) return t;
48
- const a = (wt ? o / 1.5 : o) * (u === "quality" ? 1.5 : 1);
49
- return ht(t, { imageFilter: "resize", w: a, f: "webp" });
50
- }
51
- function ht(t, o) {
52
- return !t || !o ? t : Q(t, o);
53
- }
54
- function gt(t) {
55
- if (t != null && t.trim())
56
- try {
57
- return q.parse(t), !0;
58
- } catch {
59
- return !1;
60
- }
61
- return !1;
62
- }
63
- function Yt(t) {
64
- return typeof t != "string" ? !1 : !!(gt(t) || K(t).isValid());
65
- }
66
- async function Pt(t) {
67
- return F.get(`/api/projects/${t}`).then((o) => o.data);
68
- }
69
- async function yt(t) {
70
- return F.post(`/api/projects/${t.id}/update`, t).then((o) => o.data);
71
- }
72
- const jt = 500, N = rt("abcdefghijklmnopqrstuvwxyz0123456789", 16), J = st(null);
73
- function Ht({
74
- url: t,
75
- name: o,
76
- children: u,
77
- waitingSynced: a
78
- }) {
79
- const [l, d] = E(!1), S = V(), y = `${S}:MakerLocalState`, e = `${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(e, i), [e, i]);
96
- const w = g(
97
- () => new lt(t, e, i, { params: { token: nt.get("login_token") } }),
98
- [t, e, i]
99
- ), j = g(
100
- () => new ft(
101
- [i.getArray("pageIds"), i.getMap("pages"), i.getArray("supportedLocales"), i.getMap("config")],
102
- { doc: i }
103
- ),
104
- [i]
105
- ), [k, C] = E(null), [A, L] = E(!1), [_, $] = E(void 0), D = U(async () => {
106
- if (S) {
107
- L(!0);
108
- try {
109
- const f = await Pt(S);
110
- C(f);
111
- } catch (f) {
112
- console.error("Failed to fetch project:", f);
113
- } finally {
114
- L(!1);
115
- }
116
- }
117
- }, [S]), T = U(
118
- async (f, m) => {
119
- var h;
120
- if ($(void 0), !!S) {
121
- m != null && m.optimisticUpdate && k && C((p) => ({
122
- ...p,
123
- ...f
124
- }));
125
- try {
126
- const p = await yt({
127
- ...f
128
- });
129
- C(p);
130
- } catch (p) {
131
- console.error("Failed to update project:", p), m != null && m.optimisticUpdate && C(k), $((h = p == null ? void 0 : p.response) == null ? void 0 : h.data);
132
- }
133
- }
134
- },
135
- [S, k]
136
- );
137
- v(() => {
138
- S && D();
139
- }, [S, D]);
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: j,
147
- projectState: {
148
- project: k,
149
- isLoadingProject: A,
150
- error: _
151
- },
152
- updateProject: T
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
- j,
162
- k,
163
- A,
164
- T,
165
- _
166
- ]
167
- );
168
- return v(() => {
169
- j.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
- }), j.on("stack-item-popped", ({ stackItem: f }) => {
173
- const m = f.meta.get("localState");
174
- m && n((h) => ({ ...h, ...m }));
175
- });
176
- }, [j, 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
- }, jt);
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__ */ P(J.Provider, { value: z, children: u });
197
- }
198
- const kt = () => {
199
- const t = ct(J);
200
- if (!t)
201
- throw new Error("Context is null");
202
- const o = et(t.state), { setLocalState: u, localState: a } = t, l = dt(), d = V(), S = g(
203
- () => ({
204
- setCurrentSectionId: (e, r) => {
205
- var n, s;
206
- e === a.currentPageId && r === ((s = (n = a.pages) == null ? void 0 : n[e]) == null ? void 0 : s.currentSectionId) || (l(I("/admin/maker", d ?? "", "pages", e)), u(
207
- (c) => b(c, (i) => {
208
- var w;
209
- i.currentPageId = e, i.currentComponentId = void 0, i.componentDebugProps = {}, r !== void 0 && (i.pages ?? (i.pages = {}), (w = i.pages)[e] ?? (w[e] = {}), i.pages[e].currentSectionId = r ?? void 0), r ? window.sessionStorage.setItem("iframe_show_id", `"${r}"`) : window.sessionStorage.removeItem("iframe_show_id");
210
- })
211
- ));
212
- },
213
- setCurrentComponentId: (e) => {
214
- e !== a.currentComponentId && (l(I("/admin/maker", d ?? "", "components", e)), u(
215
- (r) => b(r, (n) => {
216
- n.currentComponentId = e, n.componentDebugProps = {};
217
- })
218
- ));
219
- },
220
- setComponentDebugProps: (() => {
221
- const e = {}, r = W(() => {
222
- u(
223
- (n) => b(n, (s) => {
224
- s.componentDebugProps = {
225
- ...s == null ? void 0 : s.componentDebugProps,
226
- ...e
227
- }, Object.keys(e).forEach((c) => delete e[c]);
228
- })
229
- );
230
- }, 5);
231
- return (n) => {
232
- Object.assign(e, n), r();
233
- };
234
- })()
235
- }),
236
- [u, l, d]
237
- ), y = g(
238
- () => ({
239
- addPage: (e, r) => {
240
- const n = (e == null ? void 0 : e.id) ?? N(), s = {
241
- ...e,
242
- id: n,
243
- createdAt: (e == null ? void 0 : e.createdAt) ?? (/* @__PURE__ */ new Date()).toISOString(),
244
- updatedAt: (e == null ? void 0 : e.updatedAt) ?? (/* @__PURE__ */ new Date()).toISOString(),
245
- publishedAt: (/* @__PURE__ */ new Date(0)).toISOString(),
246
- slug: (e == null ? void 0 : e.slug) ?? `/${n}`,
247
- sections: (e == null ? void 0 : e.sections) ?? {},
248
- sectionIds: (e == null ? void 0 : e.sectionIds) ?? [],
249
- isPublic: (e == null ? void 0 : e.isPublic) ?? !0,
250
- templateConfig: (e == null ? void 0 : e.templateConfig) ?? {
251
- isTemplate: !1,
252
- displayTemplateId: void 0,
253
- dataSourceId: void 0,
254
- dataSourceParameters: {}
255
- }
256
- };
257
- return t.doc.transact(() => {
258
- o.pages[s.id] = s, o.pageIds.splice(r ?? o.pageIds.length, 0, s.id);
259
- }), s;
260
- },
261
- deletePage: (e) => {
262
- const r = o.pageIds.indexOf(e);
263
- r !== -1 && t.doc.transact(() => {
264
- o.pageIds.splice(r, 1), delete o.pages[e];
265
- });
266
- },
267
- movePage: (e, r) => {
268
- const { pageIds: n } = o, s = n.indexOf(e);
269
- s >= 0 && n.splice(r, 0, ...n.splice(s, 1));
270
- },
271
- addSection: (e, r, n) => {
272
- const s = o.pages[e];
273
- if (!s)
274
- throw new Error(`Page ${e} is not exists`);
275
- const c = {
276
- ...r,
277
- id: r.id ?? N(),
278
- isTemplateSection: r.isTemplateSection ?? !1
279
- };
280
- return t.doc.transact(() => {
281
- s.sections[c.id] = c, s.sectionIds.splice(n ?? s.sectionIds.length, 0, c.id);
282
- }), c;
283
- },
284
- deleteSection: (e, r) => {
285
- const n = o.pages[e];
286
- if (!n)
287
- throw new Error(`Page ${e} is not exists`);
288
- const s = n.sectionIds.indexOf(r);
289
- s !== -1 && t.doc.transact(() => {
290
- n.sectionIds.splice(s, 1), delete n.sections[r];
291
- });
292
- },
293
- toggleSectionVisibility: (e, r, n) => {
294
- const s = o.pages[e];
295
- if (!s)
296
- throw new Error(`Page ${e} is not exists`);
297
- const c = s.sections[r];
298
- c && (c.visibility = n);
299
- },
300
- moveSection: (e, r, n) => {
301
- const s = o.pages[e];
302
- if (!s)
303
- throw new Error(`Page ${e} is not exists`);
304
- const { sectionIds: c } = s, i = c.indexOf(r);
305
- i >= 0 && c.splice(n, 0, ...c.splice(i, 1));
306
- }
307
- }),
308
- [o, t.doc]
309
- );
310
- return {
311
- ...t,
312
- state: o,
313
- actions: y,
314
- localActions: S,
315
- get currentPage() {
316
- const {
317
- localState: { currentPageId: e }
318
- } = t;
319
- return e ? o.pages[e] : void 0;
320
- },
321
- get currentSection() {
322
- var s, c, i;
323
- const {
324
- localState: { currentPageId: e, ...r }
325
- } = t;
326
- if (!e)
327
- return;
328
- const n = (c = (s = r.pages) == null ? void 0 : s[e]) == null ? void 0 : c.currentSectionId;
329
- if (n)
330
- return (i = o.pages[e]) == null ? void 0 : i.sections[n];
331
- }
332
- };
333
- }, V = () => {
334
- const { projectId: t } = at();
335
- return t ?? mt();
336
- };
337
- function It(t) {
338
- var a, l;
339
- const { resources: o } = t;
340
- if (!o.components)
341
- return {};
342
- const u = {};
343
- return (l = (a = window == null ? void 0 : window.blocklet) == null ? void 0 : a.componentMountPoints) == null || l.forEach((d) => {
344
- u[d.did] = d;
345
- }), Object.fromEntries(
346
- Object.entries(o.components).map(([d, { blockletId: S, component: y }]) => {
347
- var e;
348
- return [
349
- d,
350
- { data: y, blockletId: S, blockletTitle: ((e = u[S]) == null ? void 0 : e.title) || S }
351
- ];
352
- })
353
- );
354
- }
355
- function Ct(t) {
356
- const { components: o } = t;
357
- return o || {};
358
- }
359
- function Et(t) {
360
- return {
361
- ...It(t),
362
- ...Ct(t)
363
- };
364
- }
365
- const te = () => Et(kt().state), vt = [
366
- /\.\./,
367
- // Directory traversal
368
- /<[^>]*>/,
369
- // HTML/XML tags
370
- /%[0-9a-f]{2}/i,
371
- // Percent encoding
372
- /[<>'"%;{}()\\]/,
373
- // Special characters
374
- // eslint-disable-next-line no-control-regex
375
- /\x00/,
376
- // Null byte
377
- /\n|\r|\t|\v|\f/,
378
- // Control characters
379
- /[^a-zA-Z0-9-_@/]/
380
- // Disallow any characters except alphanumeric, dash, underscore and forward slash
381
- ];
382
- function ee(t, o, u) {
383
- const a = [];
384
- {
385
- let l;
386
- o.slug ? o.slug.startsWith("/") ? o.slug !== "/" && o.slug.endsWith("/") ? l = t("maker.properties.pathCheckEndWithoutSlash") : /\/{2,}/.test(o.slug) ? l = t("maker.properties.pathCheckConsecutiveSlash") : /\s/.test(o.slug) ? l = t("maker.properties.pathCheckWhitespace") : vt.some((d) => d.test(o.slug)) ? l = t("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 = t("maker.properties.pathCheckAlreadyExist")) : l = t("maker.properties.pathCheckStartWithSlash") : l = t("maker.properties.pathCheckRequired"), l && a.push({ path: ["path"], message: l });
387
- }
388
- return a.length > 0 ? a : null;
389
- }
390
- function oe({ errors: t, ...o }) {
391
- return t != null && t.length ? /* @__PURE__ */ P(
392
- G,
393
- {
394
- ...o,
395
- title: /* @__PURE__ */ P(x, { children: t.map((u, a) => /* @__PURE__ */ P(x, { children: u.message }, a)) })
396
- }
397
- ) : null;
398
- }
399
- export {
400
- Kt as E,
401
- oe as P,
402
- Ht as S,
403
- Zt as a,
404
- Yt as b,
405
- F as c,
406
- mt as d,
407
- Et as e,
408
- Gt as f,
409
- Xt as g,
410
- kt as h,
411
- gt as i,
412
- te as j,
413
- Qt as k,
414
- ee as l,
415
- N as n,
416
- qt as t,
417
- V as u
418
- };