@kodezen/editor 1.0.0-beta.1 → 1.1.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.
Files changed (4) hide show
  1. package/README.md +111 -81
  2. package/dist/index.js +14 -9
  3. package/package.json +22 -9
  4. package/LICENSE +0 -674
package/README.md CHANGED
@@ -1,9 +1,15 @@
1
1
  # @kodezen/editor
2
2
 
3
- Embeddable drag-and-drop **block email editor**, extracted from Easy Mail Builder.
4
- A drop-in replacement for a Quill/TinyMCE editor: it produces the email HTML
5
- **client-side** (no DB table, CPT, REST endpoint, or the Easy Mail Builder plugin
6
- required of the host) and ships **zero global CSS** (every pane is inline-styled).
3
+ Embeddable drag-and-drop **block editor**. Framework-agnostic, zero
4
+ server/CMS dependency. A drop-in replacement for a Quill/TinyMCE editor: it
5
+ produces HTML **client-side** (no DB table, backend endpoint, or any other
6
+ package required beyond React) and ships **zero global CSS** (every pane is
7
+ inline-styled).
8
+
9
+ One component (`EmailEditor`) covers four modes — email builder, plain
10
+ rich-text, form builder, and certificate/print layout — plus two convenience
11
+ wrappers (`RichTextEditor`, `FormEditor`) for call sites that only ever want
12
+ one mode.
7
13
 
8
14
  ## Install
9
15
 
@@ -11,104 +17,128 @@ required of the host) and ships **zero global CSS** (every pane is inline-styled
11
17
  npm install @kodezen/editor
12
18
  ```
13
19
 
14
- The package is **pre-built** (transpiled ESM) so it works with `@wordpress/scripts`,
15
- whose Babel loader skips `node_modules`.
20
+ The package is **pre-built** (transpiled ESM) so it works regardless of the
21
+ consumer's bundler/transform setup.
16
22
 
17
23
  ### Peer dependencies (host-supplied)
18
24
 
19
- `react`, `react-dom`, `@wordpress/i18n`. These are left external so the consumer's
20
- single React instance and `wp.i18n` global are used. `@dnd-kit/*`, `react-icons`,
21
- and `just-debounce-it` are bundled in. For the default media picker, the host page
22
- must have run `wp_enqueue_media()`.
25
+ `react`, `react-dom`. These are left external so the consumer's single React
26
+ instance is reused. `@dnd-kit/*`, `react-icons`, and `just-debounce-it` are
27
+ bundled in.
23
28
 
24
- ## Usage
29
+ ### Media picker
25
30
 
26
- A full example: an email-template editor screen that loads an existing record,
27
- lets the user edit it, and saves both `html` and `tree` on submit.
31
+ The default `mediaUpload` handler opens a native `<input type="file">` picker
32
+ and resolves the chosen file as a `data:` URL — it works in any browser with
33
+ no configuration, but it's a testing baseline, not a production upload
34
+ pipeline (`data:` URLs bloat stored HTML and are unreliable in several email
35
+ clients). A host with real storage (S3, its own media library, etc.) should
36
+ pass its own `mediaUpload` prop instead — see below.
37
+
38
+ ## Usage
28
39
 
29
40
  ```jsx
30
- import { useEffect, useRef, useState } from 'react';
31
- import { EmailEditor, BLANK_TREE } from '@kodezen/editor';
32
-
33
- function EmailTemplateScreen( { templateId } ) {
34
- const ref = useRef();
35
- const [ initialTree, setInitialTree ] = useState( null );
36
- const [ saving, setSaving ] = useState( false );
37
-
38
- // Load once — initialTree is read only at mount.
39
- useEffect( () => {
40
- fetchTemplate( templateId ).then( ( record ) => {
41
- setInitialTree( record.tree ?? BLANK_TREE );
42
- } );
43
- }, [ templateId ] );
44
-
45
- const handleSave = async () => {
46
- setSaving( true );
47
- await persistTemplate( templateId, {
48
- html: ref.current.getHtml(), // pull latest value past the debounce
49
- tree: ref.current.getTree(),
50
- } );
51
- setSaving( false );
52
- };
53
-
54
- if ( ! initialTree ) {
55
- return <p>Loading…</p>;
56
- }
57
-
58
- return (
59
- <>
60
- <EmailEditor
61
- // Remount when switching records (initialTree is read once at mount).
62
- key={ templateId }
63
- ref={ ref }
64
- initialTree={ initialTree }
65
- mergeTags={ [ { tag: '{{first_name}}', label: 'First name' } ] }
66
- mediaUpload={ async ( { allowedTypes } ) => ( { url, id, alt } ) } // optional; defaults to wp.media
67
- onChange={ ( { tree, html } ) => autosave( templateId, { tree, html } ) } // debounced
68
- renderMode="document" // 'document' (full email) | 'fragment' (host wraps in its own shell)
69
- header // show a slim Preview bar
70
- />
71
- <button onClick={ handleSave } disabled={ saving }>
72
- { saving ? 'Saving…' : 'Save' }
73
- </button>
74
- </>
75
- );
76
- }
41
+ import { EmailEditor, renderContent, renderDocument, BLANK_TREE } from '@kodezen/editor';
42
+
43
+ const ref = useRef();
44
+
45
+ <EmailEditor
46
+ ref={ref}
47
+ initialTree={savedTree} // { version, root } — read once at mount; remount via React `key`
48
+ mergeTags={[{ tag: '{{first_name}}', label: 'First name' }]}
49
+ mediaUpload={async ({ allowedTypes }) => ({ url, id, alt })} // optional; defaults to a native file picker
50
+ onChange={({ tree, html }) => persist(tree, html)} // debounced
51
+ renderMode="document" // 'document' (full email) | 'fragment' (host wraps in its own shell)
52
+ header // show a slim Preview bar
53
+ />
54
+
55
+ // On submit, pull the latest value past the debounce:
56
+ const html = ref.current.getHtml();
57
+ const tree = ref.current.getTree();
77
58
  ```
78
59
 
60
+ ### Modes
61
+
62
+ `EmailEditor` takes a `mode` prop; `RichTextEditor` and `FormEditor` are thin
63
+ wrappers that hard-code it (`<EmailEditor mode="richtext" />` /
64
+ `<EmailEditor mode="form" />` respectively):
65
+
66
+ | `mode` | What |
67
+ | --- | --- |
68
+ | `builder` (default) | Full drag-and-drop block email builder. |
69
+ | `richtext` | A simple Quill-like rich-text field (persistent toolbar, single editable surface). Takes `value` (HTML string) instead of `initialTree`; `onChange` still fires `{ tree, html }` (`tree` is a single Custom HTML block wrapping the field's HTML). |
70
+ | `form` | Drag-and-drop form builder (semantic, responsive `<form>` output). `onChange` fires `{ tree, html, schema }`. |
71
+ | `certificate` | The block builder laid out on a fixed landscape print page with a background, for certificates/awards. |
72
+
73
+ ### Props
74
+
75
+ | Prop | Modes | What |
76
+ | --- | --- | --- |
77
+ | `mode` | all | `'builder'` (default) \| `'richtext'` \| `'form'` \| `'certificate'`. |
78
+ | `initialTree` | builder, form, certificate | `{ version, root }` — saved block tree, or omit for a blank starter. Read **once** at mount (like `defaultValue`); remount via a React `key` to load a different record. |
79
+ | `value` | richtext | HTML string — the saved content. Re-applied if it changes while the field is unfocused (async form loads). |
80
+ | `mergeTags` | all | `[{ tag: '{{first_name}}', label: 'First name' }, ...]` — host personalization tags, inserted verbatim and resolved at the host's send time. |
81
+ | `mediaUpload` | all | `({ allowedTypes }) => Promise<{ url, id, alt } | null>`. Optional; defaults to `defaultMediaUpload` (native file picker, `data:` URL). |
82
+ | `onChange` | all | Debounced. `{ tree, html }` (`form` mode also includes `schema`). |
83
+ | `header` | builder, richtext, form | `false` (default) \| `true` — shows a slim Preview bar. Always on for `certificate`. |
84
+ | `height` | all | Editor height in px. Omit to use the stylesheet defaults (640 builder/form/certificate, 320 richtext), which hosts can override by class. |
85
+ | `renderMode` | builder | `'document'` (full `<!doctype html>…` email) \| `'fragment'` (content only — host wraps its own `<html>/<body>` shell). |
86
+ | `blocks` | builder, form, certificate | Optional manifest override, replacing the mode's default block set entirely. |
87
+ | `blockPlugins` | builder, form | `[{ type, spec, render, controls, schema }, ...]` — teach the editor host-specific blocks without forking the core. `render(node, ctx, helpers)` returns an HTML string; `helpers` exposes the built-in renderers and style utilities so a plugin can delegate to them. See `dev_emb/library/blockPlugins.js`. |
88
+ | `hideBlocks` | builder, form, certificate | `string[]` of core block types to drop from the palette/quick-add. Blocks already present in a saved tree still render. |
89
+ | `templates` | builder, certificate | Array of starter templates shown in the template picker. |
90
+ | `onTemplatesClick` | builder, certificate | Callback when the host's own "browse templates" affordance is used. |
91
+ | `suffix` | all | Theming hook, e.g. `suffix="zaplane"`. Every shell chrome piece gets a stable `--{suffix}` modifier class, and hosts can override the `--emb-*` custom properties on `.emb-embed-root--{suffix}` to theme the chrome (e.g. dark mode) without `!important`. |
92
+ | `config` | all | `{ 'English string': 'Host-translated string', ... }` — plain lookup object. The package does no translation logic of its own; the host resolves everything and hands over final strings. Anything missing falls back to the English text as-is. Omit for plain English. See [i18n](#i18n). |
93
+
94
+ Ref handle (all modes): `{ getTree, getHtml, getSchema, setTree, setHtml }`.
95
+ Not every method is meaningful in every mode (e.g. `setHtml` is for
96
+ `richtext`) — calling one that doesn't apply is a no-op. Read
97
+ `getHtml()`/`getTree()` on the host's submit to capture the latest value even
98
+ if the debounce hasn't fired.
99
+
79
100
  ### Exports
80
101
 
81
102
  | Export | What |
82
103
  | --- | --- |
83
- | `EmailEditor` | The controlled editor component (forwardRef → `{ getTree, getHtml, setTree }`). Takes a `mode` prop: `"email"` (default), `"richtext"`, or `"form"`. |
84
- | `RichTextEditor` | `<EmailEditor mode="richtext" />` — the plain Quill-like rich-text surface (HTML in / HTML out). |
85
- | `FormEditor` | `<EmailEditor mode="form" />` — drag-and-drop form builder producing a semantic, responsive `<form>`. `onChange` emits `{ tree, html, schema }`. |
104
+ | `EmailEditor` | The controlled editor component (forwardRef, all modes — see above). |
105
+ | `RichTextEditor` | Convenience wrapper = `<EmailEditor mode="richtext" />`. |
106
+ | `FormEditor` | Convenience wrapper = `<EmailEditor mode="form" />`. |
86
107
  | `renderDocument(tree)` | Tree → full email HTML document (doctype + MSO shell). |
87
- | `renderContent(tree)` | Tree → content fragment (no doctype/body) for hosts that wrap in their own email shell. |
88
- | `renderFormDocument(tree)` / `renderFormContent(tree)` | Form-mode equivalents of the two above. |
89
- | `extractSchema(tree)` | Form tree → JSON schema describing the fields (name, type, required, options). |
90
- | `BLANK_TREE` | Starter tree used when `initialTree` is absent. |
91
- | `treeFromHtml(html)` | Best-effort HTML → tree, for importing existing content into the editor. |
92
- | `htmlToBlocks(html)` | Lower-level HTML → block-array conversion used by `treeFromHtml`. |
93
- | `defaultMediaUpload` | The `wp.media`-backed default media handler. |
108
+ | `renderContent(tree)` | Tree → email content fragment (no doctype/body) for hosts that wrap in their own email shell. |
109
+ | `renderFormDocument(tree)` / `renderFormContent(tree)` | Same, for a `form`-mode tree. |
110
+ | `renderCertificateDocument(tree)` / `renderCertificateContent(tree)` | Same, for a `certificate`-mode tree. |
111
+ | `extractSchema(tree)` | Form tree → field schema (also emitted as `schema` in `form`-mode `onChange`). |
112
+ | `pageDims` | Fixed page dimensions used by the certificate layout. |
113
+ | `treeFromHtml(html)` / `htmlToBlocks(html)` | Best-effort HTML → block tree, for importing existing content. |
114
+ | `BLANK_TREE` | Starter tree used when `initialTree` is absent (`builder`/`richtext`). |
115
+ | `BLANK_CERT_TREE` | Starter tree for `certificate` mode. |
116
+ | `defaultMediaUpload` | The native file-picker default media handler (see Media picker above). |
117
+
118
+ ## i18n
119
+
120
+ Every UI string is untranslated (plain English) by default — there's no
121
+ bundled translation catalog and no runtime translate hook. To localize, pass
122
+ a plain lookup object as the `config` prop:
94
123
 
95
- ### `renderMode`
124
+ ```jsx
125
+ <EmailEditor config={{ 'Add block': 'Ajouter un bloc' }} />
126
+ ```
96
127
 
97
- - `document` — store the full `<!doctype html>…` email (standalone sending).
98
- - `fragment` — store just the content table + blocks; use when the host wraps the
99
- body in its own `<html>/<body>` shell (e.g. GemCRM injects header/footer), to
100
- avoid double-wrapping.
128
+ The package does no lookup/interpolation logic beyond `config[text] ?? text`
129
+ — the host is responsible for resolving strings up front.
101
130
 
102
131
  ## Storage contract
103
132
 
104
- The host owns persistence. `onChange` gives `{ tree, html }`:
133
+ The host owns persistence. `onChange` gives `{ tree, html }` (plus `schema`
134
+ in `form` mode):
105
135
 
106
136
  - `html` → the field your send pipeline already uses.
107
137
  - `tree` (JSON) → a sibling field, so the editor round-trips on reopen.
108
138
 
109
- (`tree` holds HTML inside richtext attributes; if your storage sanitizes with
110
- `sanitize_text_field`/kses, base64-encode it first.)
139
+ ## Building from source
111
140
 
112
- ## License
113
-
114
- GPL-3.0-or-later. See [`LICENSE`](./LICENSE).
141
+ ```bash
142
+ npm install
143
+ npm run build # → dist/index.js
144
+ ```