leiho 1.0.0-beta.12 → 1.0.0-beta.13

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/README.md CHANGED
@@ -61,6 +61,17 @@ The standalone file starts itself from its `data-wp-origin` attribute.
61
61
 
62
62
  ## Annotate
63
63
 
64
+ In a JavaScript framework, let this package write the attributes: `field()`, `block()` and `entry()` return an object to spread on the element (JSX, Astro, Svelte; `v-bind` in Vue).
65
+
66
+ ```jsx
67
+ import { field, block } from 'leiho';
68
+
69
+ <h1 {...field(page, 'hero_title')}>{page.fields.hero_title}</h1>
70
+ <section {...block(page, 'sections', i, 'Hero')}>…</section>
71
+ ```
72
+
73
+ `field(page, 'intro', { html: true })` adds `data-leiho-apply="html"`; `{ refresh: true }` adds `data-leiho-refresh="save"`. They are pure functions: use them in server components and at build time. In plain HTML, write the attributes below by hand.
74
+
64
75
  ```html
65
76
  <main data-wp-post="12">
66
77
  <h1 data-wp-field="_title">About us</h1>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leiho",
3
- "version": "1.0.0-beta.12",
3
+ "version": "1.0.0-beta.13",
4
4
  "description": "Pont postMessage de l'éditeur visuel Leiho : rend n'importe quel site (React, Next.js, Remix, Vite, Nuxt, SvelteKit, Astro, HTML) éditable depuis WordPress. Zéro dépendance, inactif hors de l'éditeur.",
5
5
  "keywords": [
6
6
  "wordpress",
@@ -31,6 +31,9 @@
31
31
  "./standalone": "./dist/leiho-bridge.js",
32
32
  "./content": {
33
33
  "import": "./src/content.js"
34
+ },
35
+ "./attrs": {
36
+ "import": "./src/attrs.js"
34
37
  }
35
38
  },
36
39
  "scripts": {
package/src/attrs.js ADDED
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Attributs d'annotation Leiho, à étaler sur un élément au lieu de les écrire
3
+ * à la main :
4
+ *
5
+ * import { field, block } from 'leiho';
6
+ *
7
+ * <section {...block( page, 'sections', i, 'Hero' )}>
8
+ * <h1 {...field( page, 'hero_title' )}>{ page.fields.hero_title }</h1>
9
+ * </section>
10
+ *
11
+ * JSX (React, Next.js, Astro, Solid), Vue (v-bind="field( page, 'x' )") et
12
+ * Svelte ({...field( page, 'x' )}) acceptent tous un objet d'attributs.
13
+ * Les fonctions sont pures : aucun accès au DOM, utilisables au rendu
14
+ * serveur comme au build.
15
+ */
16
+
17
+ /**
18
+ * Identifiant WordPress d'une entrée de l'export (ou l'identifiant lui-même).
19
+ *
20
+ * @param {object|number|string} entry
21
+ * @returns {string} '' si introuvable.
22
+ */
23
+ function postId( entry ) {
24
+ const id = entry && 'object' === typeof entry ? entry.id : entry;
25
+ return 'number' === typeof id || ( 'string' === typeof id && /^\d+$/.test( id ) ) ? String( id ) : '';
26
+ }
27
+
28
+ /**
29
+ * @param {object|number|string} entry
30
+ * @returns {Record<string,string>}
31
+ */
32
+ function withPost( entry ) {
33
+ const id = postId( entry );
34
+ return id ? { 'data-wp-post': id } : {};
35
+ }
36
+
37
+ /**
38
+ * Élément qui affiche un champ.
39
+ *
40
+ * @param {object|number|string} entry Entrée de l'export (ou son id WordPress).
41
+ * @param {string} path Chemin du champ : 'hero_title', 'cards.0.title', '_title', '_thumbnail'.
42
+ * @param {object} [options]
43
+ * @param {boolean} [options.html] La valeur contient du balisage (injectée en HTML filtré).
44
+ * @param {boolean|string} [options.refresh] Pas d'aperçu en direct : la page est rechargée après l'enregistrement.
45
+ * @param {string} [options.style] Champ couleur : propriété CSS à modifier (défaut : color).
46
+ * @returns {Record<string,string>}
47
+ */
48
+ export function field( entry, path, options = {} ) {
49
+ const attrs = { ...withPost( entry ), 'data-wp-field': String( path ) };
50
+ if ( options.html ) {
51
+ attrs[ 'data-leiho-apply' ] = 'html';
52
+ }
53
+ if ( options.refresh ) {
54
+ attrs[ 'data-leiho-refresh' ] = true === options.refresh ? 'save' : String( options.refresh );
55
+ }
56
+ if ( options.style ) {
57
+ attrs[ 'data-leiho-style' ] = String( options.style );
58
+ }
59
+ return attrs;
60
+ }
61
+
62
+ /**
63
+ * Ligne d'un contenu flexible (ou bloc d'une liste de blocs) : l'éditeur peut
64
+ * l'ajouter, la déplacer, la dupliquer et la supprimer.
65
+ *
66
+ * @param {object|number|string} entry Entrée de l'export (ou son id WordPress).
67
+ * @param {string} path Champ de blocs : 'sections'.
68
+ * @param {number} index Rang de la ligne.
69
+ * @param {string} [label] Nom affiché dans l'arbre de structure.
70
+ * @returns {Record<string,string>}
71
+ */
72
+ export function block( entry, path, index, label ) {
73
+ const attrs = { ...withPost( entry ), 'data-leiho-layout': String( path ) + '.' + Number( index ) };
74
+ if ( label ) {
75
+ attrs[ 'data-leiho-section' ] = String( label );
76
+ }
77
+ return attrs;
78
+ }
79
+
80
+ /**
81
+ * Conteneur d'une entrée (les champs à l'intérieur peuvent alors se passer
82
+ * de l'entrée : field( null, 'x' ) n'écrit que data-wp-field).
83
+ *
84
+ * @param {object|number|string} entry
85
+ * @param {string} [label] Section nommée dans l'arbre de structure.
86
+ * @returns {Record<string,string>}
87
+ */
88
+ export function entry( entry, label ) {
89
+ const attrs = withPost( entry );
90
+ if ( label ) {
91
+ attrs[ 'data-leiho-section' ] = String( label );
92
+ }
93
+ return attrs;
94
+ }
package/src/content.js CHANGED
@@ -100,7 +100,13 @@ export async function loadLeihoContent( options = {} ) {
100
100
  throw new Error( 'Leiho: unexpected response from ' + url + ' (is Leiho 1.0.0-beta.6+ active?)' );
101
101
  }
102
102
  if ( snapshotPath ) {
103
- await writeSnapshot( snapshotPath, data );
103
+ // Un serveur en lecture seule (serverless, à la requête) ne peut
104
+ // pas écrire l'instantané : le contenu reçu reste bon, on le sert.
105
+ try {
106
+ await writeSnapshot( snapshotPath, data );
107
+ } catch ( writeError ) {
108
+ logger.warn( 'Leiho: snapshot not written (' + writeError.message + '). Pass snapshot: false to skip it.' );
109
+ }
104
110
  }
105
111
  return { data, source: 'network', stale: false };
106
112
  } catch ( error ) {
@@ -181,3 +187,6 @@ function sleep( ms ) {
181
187
  export const loadOknoContent = loadLeihoContent;
182
188
  /** @deprecated Utiliser indexLeihoContent. */
183
189
  export const indexOknoContent = indexLeihoContent;
190
+
191
+ // Attributs d'annotation, à côté du contenu qu'ils décrivent.
192
+ export { field, block, entry } from './attrs.js';
package/src/index.js CHANGED
@@ -576,3 +576,6 @@ export function initLeihoBridge( options ) {
576
576
 
577
577
  /** @deprecated Nom d'avant le renommage d'Okno en Leiho, gardé jusqu'à la 2.0. */
578
578
  export const initOknoBridge = initLeihoBridge;
579
+
580
+ // Attributs d'annotation (field, block, entry) : aussi importables depuis 'leiho'.
581
+ export { field, block, entry } from './attrs.js';