@blocklet/pages-kit-inner-components 0.4.33 → 0.4.35

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