tess_edit 0.3.0 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: daa06d2ede42eac7f2f25875e0b91f7694a917bbeb49ef674cc36e7bf92ba92c
4
- data.tar.gz: 9d3ca9a21d6fcfa3dffe6891c72b86db197ef322d78fe49977ffc0203e969ee8
3
+ metadata.gz: a7edf50f5750b4cfa068076d5bce42ba51c8ff068e3b272c1b882a27ca573fec
4
+ data.tar.gz: d521a49419a260dcf5066274511588e5f602715fa6ceaf2fb5cd1fcfb3dea983
5
5
  SHA512:
6
- metadata.gz: 2a4e727b8a4da2050b83902f08513cb1a4034e56c070f02964de408d04efeb59be3d03c58e14b80f232950f0cd43c268bab7824763b4db0868bf217769df7b3c
7
- data.tar.gz: 24112a3299bf75b1595be478ecf5ea6b47ce8cf408e9f3525cde257bbeaf7a19da32a79620b1edc7e3ba3156c9f72f241ad4be17ac403f44c168df2b5ae4936b
6
+ metadata.gz: ad669b198bad3b4beb556629b880448d22c8cd54c5b45cbf5d0b7c34cbdb3513d9d9e7644fe6630b60bc8ffc93e8ffa3563ace8aae009c6c1dd327b2d0b706ba
7
+ data.tar.gz: 89feda7ff43627f7d8df5ea18d0fb2d156ac8a07f91ae9d1dd64a6d8d71f801b8ff1c179eb08369e0d6522d24a3f5b829868714bb48ae919d3ec6b1761c81242
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  TessEdit is a drag-and-drop email editor for Rails, written in plain JavaScript with Stimulus. You build an email from blocks; the editor stores the design as JSON and exports [MJML](https://mjml.io) and responsive HTML. Blocks can hold [Liquid](https://shopify.github.io/liquid/), which survives into the HTML so it can be rendered at send time.
4
4
 
5
+ What it makes is a kit: the email is built in, and your app can add its own, such as the demo's Note, with blocks of its own and TessEdit's (see [Kits](#kits)).
6
+
5
7
  The interface follows familiar layout: blocks and a layers tree on the left, the email in the middle, and Inspect, with the selected block's settings, on the right.
6
8
 
7
9
  ![TessEdit interface](docs/overview.png)
@@ -22,6 +24,9 @@ It completes Liquid tags and variables as you type, making it easier to work wit
22
24
  - Panels on both sides, or combined as tabs in one sidebar on the left or right to give the canvas more room.
23
25
  - Dark mode that follows the host app's.
24
26
  - Code dialog to view, copy or download the JSON, MJML and HTML, and to load JSON.
27
+ - Kits: other kinds of documents, registered by your app, with its own blocks and output and TessEdit's blocks brought in.
28
+ - Layouts: a Content block marks where the documents using a layout put their own blocks, and a lock keeps the rest of the layout as it is.
29
+ - Palette sources: tiles from your app in the Blocks panel, such as saved snippets, inserted as a copy that remembers where it came from.
25
30
 
26
31
  ## Installation
27
32
 
@@ -117,7 +122,7 @@ TessEdit.parent_controller = "Admin::BaseController"
117
122
 
118
123
  ```json
119
124
  {
120
- "version": 1, "id": "b1x2y3z", "type": "root",
125
+ "version": 1, "id": "b1x2y3z", "type": "email",
121
126
  "attributes": { "width": "600px", "background-color": "#f3f4f6" },
122
127
  "data": { "subject": "...", "preheader": "...", "fontFamily": "..." },
123
128
  "children": [
@@ -130,7 +135,7 @@ TessEdit.parent_controller = "Admin::BaseController"
130
135
  }
131
136
  ```
132
137
 
133
- The top node is the `root` block, which renders the `<mjml>` document with the subject, preview text and theme. Documents from before its rename, with `"type": "page"`, still load. `attributes` map onto MJML attributes; `data` holds everything else. Text content is Tiptap (ProseMirror) JSON, so other renderers, such as a PDF renderer, can walk it.
138
+ The top node is the `email` block, which renders the `<mjml>` document with the subject, preview text and theme. Documents from before its renames, with `"type": "page"` or `"type": "root"`, still load. `attributes` map onto MJML attributes; `data` holds everything else. Text content is Tiptap (ProseMirror) JSON, so other renderers, such as a kit's own, can walk it.
134
139
 
135
140
  ### Locking a layout
136
141
 
@@ -141,7 +146,48 @@ To let people fill in one part of a design that is otherwise fixed, such as a te
141
146
  data-tess-edit-lock-value='<%= {except: ["content"], fields: ["data.subject", "data.preheader"]}.to_json %>'>
142
147
  ```
143
148
 
144
- Only what is inside blocks of the types in `except` can change: those blocks take new blocks, and everything in them can be edited, moved, duplicated and deleted. The blocks themselves stay where they are. Of the root, only the listed `fields` can change. Everything else still shows on the canvas and can be selected, with its settings disabled in Inspect and a lock in Layers. A tile clicked while a locked block is selected goes at the end of the first block it is allowed in, and the Code dialog shows the JSON without loading it. The editor still stores the whole document; taking the open part out of it is up to the host app. The `except` types are usually a custom block, like the demo's Content block in `test/dummy/app/javascript/blocks/content.js`.
149
+ Only what is inside blocks of the types in `except` can change: those blocks take new blocks, and everything in them can be edited, moved, duplicated and deleted. The blocks themselves stay where they are. Of the root, only the listed `fields` can change. Everything else still shows on the canvas and can be selected, with its settings disabled in Inspect and a lock in Layers. A tile clicked while a locked block is selected goes at the end of the first block it is allowed in, and the Code dialog shows the JSON without loading it. The editor still stores the whole document; taking the open part out of it is up to the host app. The `except` type is usually `content`, the Content block (see Layouts).
150
+
151
+ ### Layouts
152
+
153
+ The Content block (in the Layout category of the Blocks panel) marks where the documents using a layout put their own blocks; a layout has one. What a layout has inside it is example content to design the layout with. Outside the editor its contents are between two comments, in `<mj-raw>` tags in MJML:
154
+
155
+ ```html
156
+ <!-- tess-edit:content --> … <!-- /tess-edit:content -->
157
+ ```
158
+
159
+ A document using a layout stores its own blocks. `TessEdit::Content` puts them in and takes them out again, and puts a document's output into its layout's, so a document follows its layout when the layout changes:
160
+
161
+ ```ruby
162
+ document = TessEdit::Content.document_with(layout.json, template.blocks) # for the editor, locked to the Content block
163
+ blocks = TessEdit::Content.nodes(JSON.parse(params[:json])) # what to store when it is saved
164
+ html = TessEdit::Content.merge(layout.html, TessEdit::Content.content_of(params[:html]))
165
+ ```
166
+
167
+ ### Palette sources
168
+
169
+ A palette source adds a section of tiles from your app to the Blocks panel, such as saved snippets. People drag a tile onto the canvas or click it, and the source creates the node that goes in:
170
+
171
+ ```js
172
+ import { registerPaletteSource, renumber } from "tess_edit"
173
+
174
+ registerPaletteSource({
175
+ key: "snippets",
176
+ label: "Snippets",
177
+ // null leaves the section out, e.g. without the setting it needs
178
+ items: async (editor) => {
179
+ if (!editor.options.snippetsUrl) return null
180
+ const response = await fetch(editor.options.snippetsUrl)
181
+ return (await response.json()).snippets // [{ key, label, type, … }]
182
+ },
183
+ // The node to insert, here a copy of the snippet's block
184
+ create: async (item, editor) => renumber(item.block, { origin: true })
185
+ })
186
+ ```
187
+
188
+ An item's `type` says where the tile can go; a container with `accepts: "inherit"` goes where its children go, so a snippet with sections can't go into a column, and the editor says so. `renumber(node, { origin: true })` gives every copied block an `origin`, the id of the block it is a copy of, so your app can compare the copy with what it came from, block by block. A duplicated block is new content and has no origin. `data-tess-edit-options-value` passes settings such as `snippetsUrl` to sources and blocks, as `editor.options`.
189
+
190
+ `data-tess-edit-blocks-value` limits the blocks people can add, e.g. `["text", "image"]`; blocks already in the document, and tiles from sources, aren't affected.
145
191
 
146
192
  ### Images
147
193
 
@@ -226,7 +272,32 @@ registerBlock({
226
272
  registerTessEdit(application)
227
273
  ```
228
274
 
229
- Roles say where a block may go: `body` (in the root or a wrapper, like sections and heroes), `wrapper`, `column` (in a section) and `content` (in a column or hero). A container lists the roles it takes in `accepts`; Loop and Condition use `"inherit"`. The full definition, and the helpers a render hook gets (`element`, `container`, `children`, `liquid`, `attrs`, `richText`, ...), are described at the top of `core/registry.js` and `export/mjml.js`. Blocks render only through their `render` hooks, so a later renderer (PDF) adds a hook per block next to `mjml`. Stored documents don't change; a document with a block type that isn't registered fails `normalize`.
275
+ Roles say where a block may go: `body` (in the email or a wrapper, like sections and heroes), `wrapper`, `column` (in a section) and `content` (in a column or hero). A container lists the roles it takes in `accepts`; Loop and Condition use `"inherit"`. The full definition, and the helpers a render hook gets (`element`, `container`, `children`, `liquid`, `attrs`, `richText`, ...), are described at the top of `core/registry.js` and `export/mjml.js`. Blocks render only through their `render` hooks: `mjml` for emails, and a hook of its own for each kit. Stored documents don't change; a document with a block type that isn't registered fails `normalize`.
276
+
277
+ A select field's `options` can be a function of the editor, `options: (editor) => [...]`, for choices from your app. Give the editor those with `data-tess-edit-options-value` (JSON), which blocks read as `editor.options`.
278
+
279
+ ## Kits
280
+
281
+ What the editor makes is a kit: a root block, the blocks that name the kit in their `kits`, and what the canvas shows and the editor outputs. The email is the built-in kit and its reference. Your app registers its own with `registerKit` before its root block, and picks it with `data-tess-edit-kit-value`:
282
+
283
+ ```js
284
+ import { createDocument, extendBlock, registerBlock, registerKit } from "tess_edit"
285
+
286
+ registerKit({
287
+ key: "note", name: "Note", root: "note",
288
+ starter: () => createDocument("note"),
289
+ canvas: async (doc, { editor }) => ({ html: toNoteHtml(doc, { editor }), errors: [] }), // with tess-<id> classes in the editor
290
+ outputs: async (doc) => ({ outputs: { html: toNoteHtml(doc) }, errors: [] }) // into the htmlInput
291
+ })
292
+ registerBlock({ type: "note", name: "Note", kits: ["note"], accepts: ["body", "content"], render: { note: … } })
293
+
294
+ // TessEdit's own blocks, in the kit, with how they render in it
295
+ extendBlock("text", { kits: ["note"], render: { note: (node, ctx) => … } })
296
+ ```
297
+
298
+ A block is part of the email kit unless it says otherwise. `extendBlock` adds kits, render hooks and field groups to a block that is registered, also when another gem replaced it, so the kit can use TessEdit's Text, Image, Section, Loop, Condition, Content and others. The kit renders its documents itself, through its blocks' hooks; the canvas needs every block's element to have the class `tess-<id>`, and an empty container a placeholder with `tessph-<id>`. `core/registry.js` describes the definition. `kits/email.js` is the reference, and the demo's Note kit (`test/dummy/app/javascript/kits/note.js`) a small one of a host app's own.
299
+
300
+ Loading a document into the editor of another kit fails with a message saying which editor it is for.
230
301
 
231
302
  ## Development
232
303
 
@@ -242,17 +313,23 @@ cd test/dummy && bin/dev
242
313
 
243
314
  `/?variables=1` gives the editor the demo's variables (`VariablesController`), an order's, to try the suggestions.
244
315
 
316
+ `/?kit=note` designs a note: the demo's own kit (`app/javascript/kits/note.js`).
317
+
318
+ `/?snippets=1` adds the demo's snippets source (`app/javascript/snippets.js`, `SnippetsController`): a greeting and a footer section.
319
+
320
+ `/?blocks=text,image` only offers those blocks.
321
+
245
322
  `/?translations=1` gives the editor the demo's translations (`TranslationsController`, in memory, in English, Dutch and German). The options combine, e.g. `/?lock=1&media=1&variables=1&translations=1`.
246
323
 
247
324
  Tests:
248
325
 
249
326
  ```bash
250
- npm test # JavaScript: document model, export, MJML snapshots (Node 22+)
327
+ npm test # JavaScript: document model, kits, export and MJML snapshots (Node 22+)
251
328
  bin/rails test # Rails: engine, compile endpoint, MRML and Liquid rendering of the snapshots
252
329
  bin/rails test:system # The editor in Chrome, through Selenium
253
330
  ```
254
331
 
255
- `npm test` compares the MJML of every block, and of the sample email, with the files in `test/javascript/snapshots`. The Rails tests compile those files with MRML and render the sample with Liquid. After an intended change in output, regenerate them and review the diff:
332
+ `npm test` compares the MJML of every email block, and of the sample email, with the files in `test/javascript/snapshots`. The Rails tests compile those files with MRML and render the sample with Liquid. After an intended change in output, regenerate them and review the diff:
256
333
 
257
334
  ```bash
258
335
  UPDATE_SNAPSHOTS=1 npm test
@@ -1,7 +1,8 @@
1
1
  // The built-in blocks, registered by core/registry.js in this order. The
2
2
  // order is also the order of tiles in the Blocks panel.
3
3
 
4
- import root from "tess_edit/blocks/root"
4
+ import email from "tess_edit/blocks/email"
5
+ import content from "tess_edit/blocks/content"
5
6
  import wrapper from "tess_edit/blocks/wrapper"
6
7
  import section from "tess_edit/blocks/section"
7
8
  import column from "tess_edit/blocks/column"
@@ -23,4 +24,4 @@ export const builtinCategories = [
23
24
  { key: "liquid", label: "Liquid", order: 30 }
24
25
  ]
25
26
 
26
- export const builtinBlocks = [root, section, column, wrapper, hero, text, image, button, divider, spacer, social, navbar, raw, loop, condition]
27
+ export const builtinBlocks = [email, section, column, wrapper, hero, content, text, image, button, divider, spacer, social, navbar, raw, loop, condition]
@@ -28,5 +28,7 @@ export default {
28
28
  border
29
29
  ],
30
30
  label: (node) => `Button · ${node.data.content || ""}`,
31
- render: { mjml: (node, { element, escapeText }) => element("mj-button", node, escapeText(node.data.content)) }
31
+ render: {
32
+ mjml: (node, { element, escapeText }) => element("mj-button", node, escapeText(node.data.content))
33
+ }
32
34
  }
@@ -20,5 +20,7 @@ export default {
20
20
  },
21
21
  border
22
22
  ],
23
- render: { mjml: (node, { container }) => container("mj-column", node) }
23
+ render: {
24
+ mjml: (node, { container }) => container("mj-column", node)
25
+ }
24
26
  }
@@ -1,3 +1,5 @@
1
+ const ifTag = (node) => `{% if ${node.data.expression || "true"} %}`
2
+
1
3
  export default {
2
4
  type: "condition",
3
5
  name: "Condition",
@@ -10,6 +12,6 @@ export default {
10
12
  hint: "Shows its blocks only when the condition is true: {% if expression %}.",
11
13
  label: (node) => `If ${node.data.expression || "…"}`,
12
14
  render: {
13
- mjml: (node, { liquid }) => liquid(node, `{% if ${node.data.expression || "true"} %}`, "{% endif %}")
15
+ mjml: (node, { liquid }) => liquid(node, ifTag(node), "{% endif %}")
14
16
  }
15
17
  }
@@ -0,0 +1,36 @@
1
+ // Where the documents using a layout put their own blocks. What a layout has
2
+ // inside it is example content to design the layout with; a document using
3
+ // the layout replaces it with its own. A layout has one.
4
+ //
5
+ // Outside the editor its contents are marked with two comments, so the host
6
+ // can put a document's content into the layout's output (see
7
+ // TessEdit::Content):
8
+ //
9
+ // <!-- tess-edit:content --> … <!-- /tess-edit:content -->
10
+ //
11
+ // In MJML the comments are in <mj-raw> tags, which MJML keeps. A kit that
12
+ // uses it renders the same comments (see extendBlock in core/registry.js).
13
+
14
+ export const CONTENT_START = "<!-- tess-edit:content -->"
15
+ export const CONTENT_END = "<!-- /tess-edit:content -->"
16
+
17
+ const mjRaw = (comment) => `<mj-raw>${comment}</mj-raw>`
18
+
19
+ export default {
20
+ type: "content",
21
+ name: "Content",
22
+ icon: "content",
23
+ category: "layout",
24
+ unique: true,
25
+ // Like Loop and Condition it adds no HTML of its own and takes what its
26
+ // parent takes, so it works at the top level and inside a column.
27
+ roles: ["body", "content"],
28
+ accepts: "inherit",
29
+ hint: "The content of the documents using this layout goes here. What you put inside is example content for designing the layout.",
30
+ render: {
31
+ mjml(node, { editor, children }) {
32
+ const inner = children(node)
33
+ return editor ? inner.join("\n") : [mjRaw(CONTENT_START), ...inner, mjRaw(CONTENT_END)].join("\n")
34
+ }
35
+ }
36
+ }
@@ -20,5 +20,7 @@ export default {
20
20
  ]
21
21
  }
22
22
  ],
23
- render: { mjml: (node, { element }) => element("mj-divider", node) }
23
+ render: {
24
+ mjml: (node, { element }) => element("mj-divider", node)
25
+ }
24
26
  }
@@ -1,9 +1,9 @@
1
- // The root of every document: renders the whole <mjml> document, with the
1
+ // The root of an email: renders the whole <mjml> document, with the
2
2
  // email's subject, preview text and theme in <mj-head>.
3
3
 
4
4
  export default {
5
- type: "root",
6
- name: "Root",
5
+ type: "email",
6
+ name: "Email",
7
7
  icon: "root",
8
8
  roles: [],
9
9
  accepts: ["body", "wrapper"],
@@ -1,8 +1,10 @@
1
1
  // Field definitions shared by the built-in blocks, and usable by custom ones.
2
2
  // A field: { key: "attributes.padding" | "data.subject", label, type, ... }.
3
3
  // Types: text, textarea, code, size, color, select, segmented, toggle,
4
- // spacing and list; see ui/inspector.js. `liquid` gives a field a button to
5
- // insert the host's variables (see Inspector#variables).
4
+ // spacing and list; see ui/inspector.js. A select's options can be a
5
+ // function of the editor, options(editor), for choices the host gives.
6
+ // `liquid` gives a field a button to insert the host's variables (see
7
+ // Inspector#variables).
6
8
 
7
9
  export const ALIGN = [
8
10
  { value: "left", label: "Left" },
@@ -25,5 +25,7 @@ export default {
25
25
  },
26
26
  { title: "Border", collapsed: true, fields: [{ key: "attributes.border", label: "Border", type: "text" }, { key: "attributes.border-radius", label: "Radius", type: "size" }, { key: "attributes.container-background-color", label: "Background", type: "color" }] }
27
27
  ],
28
- render: { mjml: (node, { element }) => element("mj-image", node) }
28
+ render: {
29
+ mjml: (node, { element }) => element("mj-image", node)
30
+ }
29
31
  }
@@ -1,3 +1,5 @@
1
+ const forTag = (node) => `{% for ${node.data.variable || "item"} in ${node.data.collection || "items"} %}`
2
+
1
3
  export default {
2
4
  type: "loop",
3
5
  name: "Loop",
@@ -18,6 +20,6 @@ export default {
18
20
  hint: "Repeats its blocks for each item: {% for variable in collection %}.",
19
21
  label: (node) => `For ${node.data.variable || "item"} in ${node.data.collection || "…"}`,
20
22
  render: {
21
- mjml: (node, { liquid }) => liquid(node, `{% for ${node.data.variable || "item"} in ${node.data.collection || "items"} %}`, "{% endfor %}")
23
+ mjml: (node, { liquid }) => liquid(node, forTag(node), "{% endfor %}")
22
24
  }
23
25
  }
@@ -27,7 +27,7 @@ export default {
27
27
  fields: [
28
28
  background,
29
29
  imageField("attributes.background-url", "Background image", "data.background"),
30
- { key: "attributes.full-width", label: "Full width", type: "toggle", on: "full-width", off: "" },
30
+ { key: "attributes.full-width", label: "Full width", type: "toggle", on: "full-width", off: "", visible: (node, editor) => editor?.kit?.key === "email" },
31
31
  { key: "attributes.direction", label: "Column order", type: "segmented", options: [{ value: "ltr", label: "Left to right" }, { value: "rtl", label: "Right to left" }] },
32
32
  { key: "attributes.text-align", label: "Align", type: "segmented", options: ALIGN },
33
33
  padding
@@ -36,5 +36,7 @@ export default {
36
36
  border
37
37
  ],
38
38
  palette: layouts.map(([key, label, icon, widths]) => ({ key, label, icon, category: "layout", create: ({ createNode }) => section(createNode, widths) })),
39
- render: { mjml: (node, { container }) => container("mj-section", node) }
39
+ render: {
40
+ mjml: (node, { container }) => container("mj-section", node)
41
+ }
40
42
  }
@@ -6,5 +6,7 @@ export default {
6
6
  roles: ["content"],
7
7
  defaults: () => ({ attributes: { height: "24px" } }),
8
8
  fields: [{ title: "Spacer", fields: [{ key: "attributes.height", label: "Height", type: "size" }, { key: "attributes.container-background-color", label: "Background", type: "color" }] }],
9
- render: { mjml: (node, { element }) => element("mj-spacer", node) }
9
+ render: {
10
+ mjml: (node, { element }) => element("mj-spacer", node)
11
+ }
10
12
  }
@@ -35,5 +35,7 @@ export default {
35
35
  create: ({ createNode }) => createNode("text", { attributes: { "font-size": "24px", "font-weight": "700", "line-height": "1.3" }, data: { content: doc("Heading") } })
36
36
  }
37
37
  ],
38
- render: { mjml: (node, { element, richText }) => element("mj-text", node, richText(node.data.content)) }
38
+ render: {
39
+ mjml: (node, { element, richText }) => element("mj-text", node, richText(node.data.content))
40
+ }
39
41
  }