@lystech/sections 0.4.6 → 0.5.0

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.d.ts CHANGED
@@ -9,6 +9,7 @@
9
9
  * Le paquet ne dépend PAS de @lystech/core : une clinique qui veut modifier
10
10
  * trois textes n'a aucune raison de charger un magasin, un panier et PayPal.
11
11
  */
12
+ export { CLASSE_HORS_EDITION } from './constantes';
12
13
  export { SectionsProvider, useSectionsContext } from './SectionsProvider';
13
14
  export { SignatureLystech } from './SignatureLystech';
14
15
  export { adaptateurI18next, appliquerAuxTextes } from './sourceTexte';
package/dist/index.js CHANGED
@@ -1,11 +1,13 @@
1
- import { jsx as x, jsxs as J, Fragment as q } from "react/jsx-runtime";
2
- import { useState as m, useRef as R, useEffect as S, useCallback as _, useMemo as v, useContext as H, createContext as T } from "react";
3
- import { appliquerAuxTextes as W } from "./morceaux/sourceTexte-BMPMpNg9.js";
4
- import { adaptateurI18next as se } from "./morceaux/sourceTexte-BMPMpNg9.js";
5
- const u = "lystech-edit", $ = "lystech-edit-sortie", z = T(null), Z = "https://api.lystech.ca/api";
6
- function G() {
1
+ import { s as W, a as D, b as u } from "./morceaux/constantes-BPZxUYge.js";
2
+ import { C as lt } from "./morceaux/constantes-BPZxUYge.js";
3
+ import { jsx as C, jsxs as J, Fragment as q } from "react/jsx-runtime";
4
+ import { useState as p, useRef as x, useEffect as A, useCallback as b, useMemo as L, useContext as Z, createContext as G } from "react";
5
+ import { appliquerAuxTextes as K } from "./morceaux/sourceTexte-BMPMpNg9.js";
6
+ import { adaptateurI18next as ft } from "./morceaux/sourceTexte-BMPMpNg9.js";
7
+ const M = G(null), Q = "https://api.lystech.ca/api";
8
+ function V() {
7
9
  if (typeof window > "u") return null;
8
- const e = (r) => {
10
+ const t = (r) => {
9
11
  try {
10
12
  window.localStorage.setItem(u, r);
11
13
  } catch {
@@ -17,8 +19,8 @@ function G() {
17
19
  return r;
18
20
  };
19
21
  try {
20
- if (window.localStorage.getItem($)) {
21
- window.localStorage.removeItem($), window.localStorage.removeItem(u);
22
+ if (window.localStorage.getItem(D)) {
23
+ window.localStorage.removeItem(D), window.localStorage.removeItem(u);
22
24
  try {
23
25
  window.sessionStorage.removeItem(u);
24
26
  } catch {
@@ -30,154 +32,172 @@ function G() {
30
32
  const c = new URLSearchParams(r), o = c.get(u);
31
33
  if (o) {
32
34
  c.delete(u);
33
- const s = c.toString();
35
+ const a = c.toString();
34
36
  return window.history.replaceState(
35
37
  {},
36
38
  "",
37
- window.location.pathname + window.location.search + (s ? "#" + s : "")
38
- ), e(o);
39
+ window.location.pathname + window.location.search + (a ? "#" + a : "")
40
+ ), t(o);
39
41
  }
40
42
  }
41
- const t = new URL(window.location.href), n = t.searchParams.get(u);
42
- return n ? (t.searchParams.delete(u), window.history.replaceState({}, "", t.toString()), e(n)) : window.localStorage.getItem(u) ?? window.sessionStorage.getItem(u);
43
+ const e = new URL(window.location.href), n = e.searchParams.get(u);
44
+ return n ? (e.searchParams.delete(u), window.history.replaceState({}, "", e.toString()), t(n)) : window.localStorage.getItem(u) ?? window.sessionStorage.getItem(u);
43
45
  } catch {
44
46
  return null;
45
47
  }
46
48
  }
47
- function ee({
48
- site: e,
49
+ function ot({
50
+ site: t,
49
51
  page: r,
50
- langue: t = "fr",
51
- apiBaseUrl: n = Z,
52
+ langue: e = "fr",
53
+ apiBaseUrl: n = Q,
52
54
  sectionsInitiales: c,
53
55
  sourceTexte: o,
54
- pagesPartagees: s,
55
- children: h
56
+ pagesPartagees: a,
57
+ children: m
56
58
  }) {
57
- const [l, A] = m(
59
+ const [d, E] = p(
58
60
  c ?? []
59
- ), [p, w] = m(
61
+ ), [w, g] = p(
60
62
  c?.length ? "pret" : "chargement"
61
- ), [E, g] = m(0), [y, F] = m({}), [U, M] = m(!1), D = R(null);
62
- S(() => {
63
- if (!e || !r) return;
64
- D.current?.abort();
65
- const i = new AbortController();
66
- D.current = i, w("chargement");
67
- const f = n + "/site-sections/public/" + encodeURIComponent(e) + "/" + encodeURIComponent([r, ...s ?? []].join(","));
68
- return fetch(f, { signal: i.signal }).then(
69
- (a) => a.ok ? a.json() : Promise.reject(new Error(String(a.status)))
70
- ).then((a) => {
71
- i.signal.aborted || (A(Array.isArray(a) ? a : []), w("pret"));
72
- }).catch((a) => {
73
- i.signal.aborted || a?.name === "AbortError" || w("erreur");
74
- }), () => i.abort();
75
- }, [e, r, n, E, (s ?? []).join(",")]);
76
- const I = _(() => g((i) => i + 1), []), O = R({}), P = _((i, f) => {
77
- O.current[i] = f;
63
+ ), [j, y] = p(0), [S, T] = p({}), [P, $] = p(!1), U = x(null);
64
+ A(() => {
65
+ if (!t || !r) return;
66
+ U.current?.abort();
67
+ const s = new AbortController();
68
+ U.current = s, g("chargement");
69
+ const l = n + "/site-sections/public/" + encodeURIComponent(t) + "/" + encodeURIComponent([r, ...a ?? []].join(","));
70
+ return fetch(l, { signal: s.signal }).then(
71
+ (i) => i.ok ? i.json() : Promise.reject(new Error(String(i.status)))
72
+ ).then((i) => {
73
+ s.signal.aborted || (E(Array.isArray(i) ? i : []), g("pret"));
74
+ }).catch((i) => {
75
+ s.signal.aborted || i?.name === "AbortError" || g("erreur");
76
+ }), () => s.abort();
77
+ }, [t, r, n, j, (a ?? []).join(",")]);
78
+ const R = b(() => y((s) => s + 1), []), z = x({}), k = b((s, l) => {
79
+ z.current[s] = l;
78
80
  }, []);
79
- S(() => {
80
- !o || !l.length || W(l, o);
81
- }, [l, o]);
82
- const j = _(
83
- (i, f) => F((a) => ({ ...a, [i]: f })),
81
+ A(() => {
82
+ !o || !d.length || K(d, o);
83
+ }, [d, o]);
84
+ const _ = b(
85
+ (s, l) => T((i) => ({ ...i, [s]: l })),
84
86
  []
85
- ), d = R({
86
- site: e,
87
+ ), f = x({
88
+ site: t,
87
89
  page: r,
88
- langue: t,
90
+ langue: e,
89
91
  poserApercu: () => {
90
92
  },
91
93
  recharger: () => {
92
94
  }
93
95
  });
94
- d.current.site = e, d.current.page = r, d.current.langue = t, d.current.poserApercu = j, d.current.sourceTexte = o, d.current.replis = O.current, d.current.recharger = I, S(() => {
95
- d.current.surChangementDePage?.(r);
96
- }, [r]), S(() => {
97
- const i = G();
98
- if (!i) return;
99
- let f = !0, a;
100
- return import("@lystech/sections/editeur").then((b) => {
101
- f && (M(!0), a = b.monterEditeur({
102
- jeton: i,
103
- site: e,
96
+ f.current.site = t, f.current.page = r, f.current.langue = e, f.current.poserApercu = _, f.current.sourceTexte = o, f.current.replis = z.current, f.current.recharger = R, A(() => {
97
+ f.current.surChangementDePage?.(r);
98
+ }, [r]);
99
+ const v = x(null), F = b(
100
+ async (s, l = {}) => {
101
+ const i = v.current;
102
+ if (!i) throw new Error("Aucune connexion d'édition active.");
103
+ if (!s.startsWith("/"))
104
+ throw new Error(
105
+ "appelEdition attend un chemin de l'API, ex. « /results/site/droits »."
106
+ );
107
+ const h = new Headers(l.headers);
108
+ h.set("Authorization", "Bearer " + i), typeof l.body == "string" && !h.has("Content-Type") && h.set("Content-Type", "application/json");
109
+ const H = await fetch(n + s, { ...l, headers: h });
110
+ return H.status === 401 && W(), H;
111
+ },
112
+ [n]
113
+ );
114
+ A(() => {
115
+ const s = V();
116
+ if (!s) return;
117
+ let l = !0, i;
118
+ return import("@lystech/sections/editeur").then((h) => {
119
+ l && (v.current = s, $(!0), i = h.monterEditeur({
120
+ jeton: s,
121
+ site: t,
104
122
  apiBaseUrl: n,
105
- pont: d.current
123
+ pont: f.current
106
124
  }));
107
- }).catch((b) => {
108
- console.error("[lystech] éditeur indisponible:", b?.message ?? b);
125
+ }).catch((h) => {
126
+ console.error("[lystech] éditeur indisponible:", h?.message ?? h);
109
127
  }), () => {
110
- f = !1, a?.();
128
+ l = !1, v.current = null, i?.();
111
129
  };
112
- }, [e, n]);
113
- const N = v(
130
+ }, [t, n]);
131
+ const N = L(
114
132
  () => ({
115
- site: e,
133
+ site: t,
116
134
  page: r,
117
- langue: t,
135
+ langue: e,
118
136
  apiBaseUrl: n,
119
- sections: l,
120
- etat: p,
121
- recharger: I,
122
- apercu: y,
123
- poserApercu: j,
124
- modeEdition: U,
125
- declarerRepli: P
137
+ sections: d,
138
+ etat: w,
139
+ recharger: R,
140
+ apercu: S,
141
+ poserApercu: _,
142
+ modeEdition: P,
143
+ declarerRepli: k,
144
+ appelEdition: F
126
145
  }),
127
146
  [
128
- e,
129
- r,
130
147
  t,
148
+ r,
149
+ e,
131
150
  n,
132
- l,
133
- p,
134
- I,
135
- y,
136
- j,
137
- U,
138
- P
151
+ d,
152
+ w,
153
+ R,
154
+ S,
155
+ _,
156
+ P,
157
+ k,
158
+ F
139
159
  ]
140
160
  );
141
- return /* @__PURE__ */ x(z.Provider, { value: N, children: h });
161
+ return /* @__PURE__ */ C(M.Provider, { value: N, children: m });
142
162
  }
143
- function L() {
144
- return H(z);
163
+ function O() {
164
+ return Z(M);
145
165
  }
146
- const K = "https://api.lystech.ca/api", k = "lystech-edit";
147
- function te({
148
- site: e,
166
+ const X = "https://api.lystech.ca/api";
167
+ function ct({
168
+ site: t,
149
169
  apiBaseUrl: r,
150
- className: t,
170
+ className: e,
151
171
  mention: n = "Réalisé avec Lystech",
152
172
  connexion: c = "Connexion",
153
173
  methodes: o
154
174
  }) {
155
- const s = L(), h = e ?? s?.site ?? "", l = r ?? s?.apiBaseUrl ?? K, [A, p] = m(!1);
156
- if (!h) return null;
157
- const w = async () => {
158
- if (!A) {
159
- p(!0);
175
+ const a = O(), m = t ?? a?.site ?? "", d = r ?? a?.apiBaseUrl ?? X, [E, w] = p(!1);
176
+ if (!m) return null;
177
+ const g = async () => {
178
+ if (!E) {
179
+ w(!0);
160
180
  try {
161
181
  (await import("@lystech/sections/editeur")).monterConnexion({
162
- site: h,
163
- apiBaseUrl: l,
182
+ site: m,
183
+ apiBaseUrl: d,
164
184
  ...o ? { methodes: o } : {},
165
- surJeton: (y) => {
185
+ surJeton: (S) => {
166
186
  try {
167
- window.localStorage.removeItem("lystech-edit-sortie"), window.localStorage.setItem(k, y);
187
+ window.localStorage.removeItem(D), window.localStorage.setItem(u, S);
168
188
  } catch {
169
- window.location.hash = k + "=" + encodeURIComponent(y);
189
+ window.location.hash = u + "=" + encodeURIComponent(S);
170
190
  }
171
191
  window.location.reload();
172
192
  }
173
193
  });
174
- } catch (g) {
175
- console.error("[lystech] connexion indisponible:", g?.message ?? g);
194
+ } catch (y) {
195
+ console.error("[lystech] connexion indisponible:", y?.message ?? y);
176
196
  } finally {
177
- p(!1);
197
+ w(!1);
178
198
  }
179
199
  }
180
- }, E = {
200
+ }, j = {
181
201
  color: "inherit",
182
202
  font: "inherit",
183
203
  background: "none",
@@ -190,7 +210,7 @@ function te({
190
210
  return /* @__PURE__ */ J(
191
211
  "div",
192
212
  {
193
- className: t,
213
+ className: e,
194
214
  style: {
195
215
  display: "flex",
196
216
  flexWrap: "wrap",
@@ -201,7 +221,7 @@ function te({
201
221
  lineHeight: 1.5
202
222
  },
203
223
  children: [
204
- /* @__PURE__ */ x(
224
+ n !== null && /* @__PURE__ */ C(
205
225
  "a",
206
226
  {
207
227
  href: "https://lystech.ca",
@@ -211,94 +231,95 @@ function te({
211
231
  children: n
212
232
  }
213
233
  ),
214
- !s?.modeEdition && /* @__PURE__ */ J(q, { children: [
215
- /* @__PURE__ */ x("span", { "aria-hidden": "true", children: "·" }),
216
- /* @__PURE__ */ x("button", { type: "button", style: E, onClick: w, children: A ? "…" : c })
234
+ !a?.modeEdition && /* @__PURE__ */ J(q, { children: [
235
+ n !== null && /* @__PURE__ */ C("span", { "aria-hidden": "true", children: "·" }),
236
+ /* @__PURE__ */ C("button", { type: "button", style: j, onClick: g, children: E ? "…" : c })
217
237
  ] })
218
238
  ]
219
239
  }
220
240
  );
221
241
  }
222
- function Q(e) {
223
- return !!e && typeof e == "object" && !Array.isArray(e) && Object.keys(e).every((r) => /^[a-z]{2}(-[A-Za-z]{2})?$/.test(r));
242
+ function Y(t) {
243
+ return !!t && typeof t == "object" && !Array.isArray(t) && Object.keys(t).every((r) => /^[a-z]{2}(-[A-Za-z]{2})?$/.test(r));
224
244
  }
225
- function C(e, r) {
226
- if (Array.isArray(e)) return e.map((t) => C(t, r));
227
- if (Q(e)) {
228
- const t = e, n = r.slice(0, 2);
229
- return t[r] ?? t[n] ?? Object.values(t).find((o) => o != null) ?? "";
245
+ function I(t, r) {
246
+ if (Array.isArray(t)) return t.map((e) => I(e, r));
247
+ if (Y(t)) {
248
+ const e = t, n = r.slice(0, 2);
249
+ return e[r] ?? e[n] ?? Object.values(e).find((o) => o != null) ?? "";
230
250
  }
231
- if (e && typeof e == "object") {
232
- const t = {};
233
- for (const [n, c] of Object.entries(e))
234
- t[n] = C(c, r);
235
- return t;
251
+ if (t && typeof t == "object") {
252
+ const e = {};
253
+ for (const [n, c] of Object.entries(t))
254
+ e[n] = I(c, r);
255
+ return e;
236
256
  }
237
- return e;
257
+ return t;
238
258
  }
239
- function V(e, r) {
259
+ function B(t, r) {
240
260
  if (!r || typeof r != "object" || Array.isArray(r))
241
- return e;
242
- const t = { ...e };
261
+ return t;
262
+ const e = { ...t };
243
263
  for (const [n, c] of Object.entries(r))
244
- c != null && (t[n] = c);
245
- return t;
264
+ c != null && (e[n] = c);
265
+ return e;
246
266
  }
247
- function re(e, r) {
248
- const t = L(), n = t?.apercu?.[e], c = t?.declarerRepli;
249
- return S(() => {
250
- c?.(e, r.defaut);
251
- }, [c, e, r.defaut]), v(() => {
252
- const o = t?.sections.find(
253
- (l) => l.blockType === e
254
- ), s = n ?? o?.data ?? null, h = s ? C(s, t?.langue ?? "fr") : null;
267
+ function st(t, r) {
268
+ const e = O(), n = e?.apercu?.[t], c = e?.declarerRepli;
269
+ return A(() => {
270
+ c?.(t, r.defaut);
271
+ }, [c, t, r.defaut]), L(() => {
272
+ const o = e?.sections.find(
273
+ (d) => d.blockType === t
274
+ ), a = n ?? o?.data ?? null, m = a ? I(a, e?.langue ?? "fr") : null;
255
275
  return {
256
- contenu: V(r.defaut, h),
276
+ contenu: B(r.defaut, m),
257
277
  publie: !!o?.data,
258
278
  UID: o?.UID ?? null,
259
- chargement: t?.etat === "chargement",
260
- ancrage: { "data-lystech-section": e }
279
+ chargement: e?.etat === "chargement",
280
+ ancrage: { "data-lystech-section": t }
261
281
  };
262
282
  }, [
263
- t?.sections,
264
- t?.langue,
265
- t?.etat,
283
+ e?.sections,
284
+ e?.langue,
285
+ e?.etat,
266
286
  n,
267
- e,
287
+ t,
268
288
  r.defaut
269
289
  ]);
270
290
  }
271
- function ne(e, r) {
272
- const t = L();
273
- return v(() => {
274
- const n = { "data-lystech-section": e }, c = (t?.sections ?? []).filter(
275
- (o) => o.blockType === e
291
+ function it(t, r) {
292
+ const e = O();
293
+ return L(() => {
294
+ const n = { "data-lystech-section": t }, c = (e?.sections ?? []).filter(
295
+ (o) => o.blockType === t
276
296
  );
277
297
  return c.length === 0 ? {
278
- items: (r?.defaut ?? []).map((o, s) => ({
298
+ items: (r?.defaut ?? []).map((o, a) => ({
279
299
  ...o,
280
- __uid: `defaut-${s}`
300
+ __uid: `defaut-${a}`
281
301
  })),
282
302
  publie: !1,
283
- chargement: t?.etat === "chargement",
303
+ chargement: e?.etat === "chargement",
284
304
  ancrage: n
285
305
  } : {
286
306
  items: c.map((o) => ({
287
- ...C(o.data ?? {}, t?.langue ?? "fr"),
307
+ ...I(o.data ?? {}, e?.langue ?? "fr"),
288
308
  __uid: o.UID
289
309
  })),
290
310
  publie: !0,
291
- chargement: t?.etat === "chargement",
311
+ chargement: e?.etat === "chargement",
292
312
  ancrage: n
293
313
  };
294
- }, [t?.sections, t?.langue, t?.etat, e, r?.defaut]);
314
+ }, [e?.sections, e?.langue, e?.etat, t, r?.defaut]);
295
315
  }
296
316
  export {
297
- ee as SectionsProvider,
298
- te as SignatureLystech,
299
- se as adaptateurI18next,
300
- W as appliquerAuxTextes,
301
- re as useSection,
302
- ne as useSectionList,
303
- L as useSectionsContext
317
+ lt as CLASSE_HORS_EDITION,
318
+ ot as SectionsProvider,
319
+ ct as SignatureLystech,
320
+ ft as adaptateurI18next,
321
+ K as appliquerAuxTextes,
322
+ st as useSection,
323
+ it as useSectionList,
324
+ O as useSectionsContext
304
325
  };
@@ -0,0 +1,14 @@
1
+ /** La clé du jeton, dans l'adresse comme dans le stockage. */
2
+ export declare const CLE_JETON = "lystech-edit";
3
+ /** Posée juste avant un rechargement, pour que la sortie soit certaine. */
4
+ export declare const CLE_SORTIE = "lystech-edit-sortie";
5
+ /**
6
+ * SORTIR DU MODE ÉDITION, pour de bon.
7
+ *
8
+ * On efface le jeton ET on pose un marqueur de sortie. Le marqueur est la
9
+ * ceinture : si l'effacement échoue — stockage plein, navigation privée,
10
+ * écriture concurrente — le fournisseur le verra au chargement suivant et
11
+ * nettoiera lui-même. Puis on recharge : la page redevient celle d'un
12
+ * visiteur, et le lien du pied de page permet de se reconnecter.
13
+ */
14
+ export declare function sortirDuModeEdition(): void;
@@ -0,0 +1,19 @@
1
+ const o = "lystech-edit", t = "lystech-edit-sortie";
2
+ function e() {
3
+ try {
4
+ window.localStorage.setItem(t, "1"), window.localStorage.removeItem(o);
5
+ } catch {
6
+ }
7
+ try {
8
+ window.sessionStorage.removeItem(o);
9
+ } catch {
10
+ }
11
+ window.location.reload();
12
+ }
13
+ const s = "lystech-hors-edition";
14
+ export {
15
+ s as C,
16
+ t as a,
17
+ o as b,
18
+ e as s
19
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lystech/sections",
3
- "version": "0.4.6",
3
+ "version": "0.5.0",
4
4
  "description": "Contenu de site editable par le client — lecture legere, edition chargee a la demande.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",