tess_edit 0.1.1 → 0.2.1

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: 50c19876ccbb3401a38a0f73b8ae1806e80f4c79cccdd98a5242b7dc8d5e87b9
4
- data.tar.gz: 5178aa9ca3d9ea111742b0aadd623b35fb453921bdd6a15b91d2a150d291a77b
3
+ metadata.gz: 46b2b641773a8bea80c00c24e8aa0988627ad530f5122898bb660d6a9a43d7a7
4
+ data.tar.gz: 131766e1ec91c4ba8b68f1d9bfeaf71b9103522b12203161f9d1c349e76fa3c6
5
5
  SHA512:
6
- metadata.gz: b7dc4f718aa387537e33c7a8b02cae8dfd5ca57a0e0e9681270bdb2b1f6e52bca4e63bdd914ad9d15cbfece7217964cc16366b45e12f89eb54068efe849af286
7
- data.tar.gz: edf5c6f4d1c0c164ab355caf88311ab4992ce1615ccd612c751adda75c575c693453b480ad212ac8527456a6d7058ba76ae750b6c615965f1b75527de7a4e307
6
+ metadata.gz: 73717af04235a460190ba466c6ba16dd54242bbd6f0dd85969c96c53cc23553732d9065ede759d07e2800a0824a36834e673fcaa1957a16aa60c6ae51055c437
7
+ data.tar.gz: 857820205087bde1fb46878e20d1e25d03a988257de588c21ab0e8ef4d004ada1dfb8c8681c0bfae07e9d411bc7bb708f7932081f2edcc0026e1fdadf1a2c38a
data/README.md CHANGED
@@ -6,15 +6,20 @@ The interface follows familiar layout: blocks and a layers tree on the left, the
6
6
 
7
7
  ![TessEdit interface](docs/overview.png)
8
8
 
9
+ It completes Liquid tags and variables as you type, making it easier to work with dynamic content.
10
+ ![Liquid completion](docs/liquid.png)
11
+
9
12
  ## Features
10
13
 
11
14
  - Blocks: Section (1-4 columns), Column, Wrapper, Hero, Text, Heading, Image, Button, Divider, Spacer, Social, Navbar and raw HTML.
12
15
  - Liquid blocks: **Loop** (`{% for line in order.lines %}`) and **Condition** (`{% if order.total > 100 %}`) wrap other blocks.
13
16
  - Drag from the Blocks panel onto the canvas or into the Layers tree; move blocks on the canvas (drag handle) or in the tree; plus buttons and empty-column placeholders open an "Add block" menu.
14
17
  - Rich text editing on the canvas with [Tiptap](https://tiptap.dev): bold, italic, underline, strikethrough, colour, links (Liquid allowed in the URL), alignment, lists and headings.
18
+ - Your app's Liquid variables suggested while typing `{{`, in text and in settings, and browsable from `{ }` buttons, including those of the Loop blocks around a block.
15
19
  - Undo/redo, duplicate, delete, keyboard shortcuts (⌘Z, ⇧⌘Z, ⌘D, Delete, Enter to edit text, Esc).
16
20
  - Desktop and mobile preview, and a preview of the HTML as sent.
17
21
  - Panels on both sides, or combined as tabs in one sidebar on the left or right to give the canvas more room.
22
+ - Dark mode that follows the host app's.
18
23
  - Code dialog to view, copy or download the JSON, MJML and HTML, and to load JSON.
19
24
 
20
25
  ## Installation
@@ -58,6 +63,10 @@ Import the editor's styles after Tailwind itself:
58
63
 
59
64
  `app/assets/builds/tailwind/tess_edit.css` is generated by `bin/rails tailwindcss:build` (and `tailwindcss:watch`, `bin/dev`) from the gem's `app/assets/tailwind/tess_edit/engine.css`. It points to wherever the gem is installed, so the line above is the same on every machine. Leave `app/assets/builds` out of version control, as Rails apps do by default. If the import fails because the file is missing, run `bin/rails tailwindcss:engines` or a build once.
60
65
 
66
+ ### Dark mode
67
+
68
+ The editor's panels, menus and dialogs have `dark:` variants, so they follow your app's dark mode: by default the OS setting (`prefers-color-scheme`), or whatever your app makes `dark:` mean with `@custom-variant dark`, such as a `.dark` class on `<html>`. The email on the canvas keeps its own colours, as it is sent.
69
+
61
70
  ## Usage
62
71
 
63
72
  Give the controller an element with a height and, optionally, hidden inputs to keep up to date:
@@ -146,6 +155,24 @@ A block stores the image's `url` in the attribute (`src`, `background-url`) and
146
155
 
147
156
  A block of your own gets the same with an image field, `imageField("attributes.background-url", "Background image", "data.background")`, imported from `tess_edit`.
148
157
 
158
+ ### Variables
159
+
160
+ To suggest the variables a template can use, give the editor an endpoint with `data-tess-edit-variables-url-value`. Typing `{{` in text on the canvas, or in a setting such as a link, subject or image address, then shows them, one level at a time: `order.` shows the order's variables, `order.customer.` the customer's. Arrow keys and Enter or Tab choose, Escape closes. The text toolbar, and settings that usually hold Liquid, have a `{ }` button to browse them instead. A Loop's collection and a Condition are suggested without the braces.
161
+
162
+ The endpoint answers `GET` with the variables at `path` (the roots without one), as JSON:
163
+
164
+ ```json
165
+ {"path": "order", "variables": [
166
+ {"name": "customer", "label": "Ordered by", "type": "object", "collection": false, "children": true},
167
+ {"name": "order_lines", "label": "Order lines", "type": "object", "collection": true, "children": true},
168
+ {"name": "total", "label": "Total", "type": "money", "hint": "Including VAT", "collection": false, "children": false}
169
+ ]}
170
+ ```
171
+
172
+ `children` says a variable has variables of its own, at the path with its name added; a `collection` is a list of them, and its path goes into its elements (`order.order_lines.product`). The editor offers a collection's `first`, `last` and `size`, and inside a Loop block (`{% for line in order.order_lines %}`) its variable, `line`, asking for `order.order_lines.…` for it. An unknown path answers 404. [Liquidum](https://github.com/entdec/liquidum)'s `/liquidum/variables` answers this way from your drops; the dummy app's `VariablesController` is a fixed example (`/?variables=1`).
173
+
174
+ A field of your own gets the `{ }` button with `liquid: true`, or `liquid: "expression"` (a condition) or `liquid: "collection"` (what a loop goes over) for Liquid without braces.
175
+
149
176
  ### Blocks and custom blocks
150
177
 
151
178
  Every block is a plugin: one module in `app/javascript/tess_edit/blocks/` with its defaults, its Inspect fields, its Blocks panel tiles and a `render.mjml` hook. The built-in blocks are registered the same way a host app registers its own, so register yours before the editor connects:
@@ -186,7 +213,9 @@ cd test/dummy && bin/dev
186
213
 
187
214
  `/?lock=1` opens a locked layout (`test/dummy/config/demo_layout.json`) with only its Content block, subject and preview text open.
188
215
 
189
- `/?media=1` gives the editor the demo's image endpoint (`MediaController`), so images can be chosen from a list, uploaded, or dragged onto the canvas from the desktop; uploads go to `test/dummy/tmp/media`. Without it, images are URLs only. The options combine, e.g. `/?lock=1&media=1`.
216
+ `/?media=1` gives the editor the demo's image endpoint (`MediaController`), so images can be chosen from a list, uploaded, or dragged onto the canvas from the desktop; uploads go to `test/dummy/tmp/media`. Without it, images are URLs only.
217
+
218
+ `/?variables=1` gives the editor the demo's variables (`VariablesController`), an order's, to try the suggestions. The options combine, e.g. `/?lock=1&media=1&variables=1`.
190
219
 
191
220
  Tests:
192
221
 
@@ -14,8 +14,8 @@ export default {
14
14
  {
15
15
  title: "Button",
16
16
  fields: [
17
- { key: "data.content", label: "Label", type: "text" },
18
- { key: "attributes.href", label: "Link", type: "text" },
17
+ { key: "data.content", label: "Label", type: "text", liquid: true },
18
+ { key: "attributes.href", label: "Link", type: "text", liquid: true },
19
19
  { key: "attributes.target", label: "Open in", type: "segmented", options: [{ value: "_blank", label: "New tab" }, { value: "_self", label: "Same tab" }] },
20
20
  { key: "attributes.background-color", label: "Button color", type: "color" },
21
21
  { key: "attributes.width", label: "Width", type: "size", placeholder: "Auto" },
@@ -6,7 +6,7 @@ export default {
6
6
  roles: ["body", "content"],
7
7
  accepts: "inherit",
8
8
  defaults: () => ({ data: { expression: "customer.vip" } }),
9
- fields: [{ title: "Condition", fields: [{ key: "data.expression", label: "Show when", type: "text", hint: "A Liquid condition, e.g. order.total > 100." }] }],
9
+ fields: [{ title: "Condition", fields: [{ key: "data.expression", label: "Show when", type: "text", liquid: "expression", hint: "A Liquid condition, e.g. order.total > 100." }] }],
10
10
  hint: "Shows its blocks only when the condition is true: {% if expression %}.",
11
11
  label: (node) => `If ${node.data.expression || "…"}`,
12
12
  render: {
@@ -1,7 +1,8 @@
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.
4
+ // spacing and list; see ui/inspector.js. `liquid` gives a field a button to
5
+ // insert the host's variables (see Inspector#variables).
5
6
 
6
7
  export const ALIGN = [
7
8
  { value: "left", label: "Left" },
@@ -14,9 +14,9 @@ export default {
14
14
  {
15
15
  title: "Image",
16
16
  fields: [
17
- { ...IMAGE_FIELD, label: "Image", type: "image" },
18
- { key: "attributes.alt", label: "Alt text", type: "text" },
19
- { key: "attributes.href", label: "Link", type: "text" },
17
+ { ...IMAGE_FIELD, label: "Image", type: "image", liquid: true },
18
+ { key: "attributes.alt", label: "Alt text", type: "text", liquid: true },
19
+ { key: "attributes.href", label: "Link", type: "text", liquid: true },
20
20
  { key: "attributes.width", label: "Width", type: "size", placeholder: "Full" },
21
21
  { key: "attributes.height", label: "Height", type: "size", placeholder: "Auto" },
22
22
  align,
@@ -11,7 +11,7 @@ export default {
11
11
  title: "Loop",
12
12
  fields: [
13
13
  { key: "data.variable", label: "Variable", type: "text", hint: "The name each item gets, e.g. line." },
14
- { key: "data.collection", label: "Collection", type: "text", hint: "What to loop over, e.g. invoice.lines." }
14
+ { key: "data.collection", label: "Collection", type: "text", liquid: "collection", hint: "What to loop over, e.g. invoice.lines." }
15
15
  ]
16
16
  }
17
17
  ],
@@ -24,8 +24,8 @@ export default {
24
24
  {
25
25
  title: "Email",
26
26
  fields: [
27
- { key: "data.subject", label: "Subject", type: "text" },
28
- { key: "data.preheader", label: "Preview text", type: "text", hint: "Shown after the subject in most inboxes." }
27
+ { key: "data.subject", label: "Subject", type: "text", liquid: true },
28
+ { key: "data.preheader", label: "Preview text", type: "text", liquid: true, hint: "Shown after the subject in most inboxes." }
29
29
  ]
30
30
  },
31
31
  {
@@ -12,6 +12,9 @@
12
12
  // the engine mounted at /tess_edit)
13
13
  // mediaUrl the host's images, chosen and uploaded in Image blocks (see
14
14
  // core/media.js); without it images only have a URL
15
+ // variablesUrl the variables a template can use, suggested while typing {{
16
+ // and browsed with { } buttons (see core/variables.js), e.g.
17
+ // Liquidum's /liquidum/variables?roots=…
15
18
  // lock JSON: only the insides of some blocks and some root fields can
16
19
  // change, e.g. {"except": ["content"], "fields": ["data.subject"]}
17
20
  // (see core/lock.js)
@@ -30,15 +33,17 @@ import { starterDocument } from "tess_edit/core/starter"
30
33
  import { toMjml } from "tess_edit/export/mjml"
31
34
  import { COMPILE_URL, compile } from "tess_edit/export/compile"
32
35
  import { IMAGE_FIELD, fetchMedia, uploadMedia } from "tess_edit/core/media"
36
+ import { Variables, loopScope } from "tess_edit/core/variables"
33
37
  import { Canvas } from "tess_edit/ui/canvas"
34
38
  import { Tree } from "tess_edit/ui/tree"
35
39
  import { Inspector } from "tess_edit/ui/inspector"
36
40
  import { TextEditing } from "tess_edit/ui/text_editing"
41
+ import { VariableMenu } from "tess_edit/ui/variable_menu"
37
42
  import { renderBlocksPanel } from "tess_edit/ui/blocks_panel"
38
43
  import { debounce, h } from "tess_edit/ui/dom"
39
44
  import { icon } from "tess_edit/ui/icons"
40
45
 
41
- const TOOL = "w-8 h-8 flex items-center justify-center rounded-md text-gray-600 hover:bg-gray-100 hover:text-gray-900 disabled:opacity-30 disabled:hover:bg-transparent"
46
+ const TOOL = "w-8 h-8 flex items-center justify-center rounded-md text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 hover:text-gray-900 dark:hover:text-gray-100 disabled:opacity-30 disabled:hover:bg-transparent"
42
47
  const TAB = "px-3 h-12 text-[13px] font-medium border-b-2 -mb-px"
43
48
  const PANELS = ["left", "split", "right"]
44
49
  const PANELS_KEY = "tess_edit:panels"
@@ -46,7 +51,7 @@ const PANELS_KEY = "tess_edit:panels"
46
51
  const NARROW_WIDTH = 1000
47
52
 
48
53
  export default class extends Controller {
49
- static values = { document: String, sample: { type: Boolean, default: true }, panels: { type: String, default: "split" }, compileUrl: { type: String, default: COMPILE_URL }, mediaUrl: String, lock: String }
54
+ static values = { document: String, sample: { type: Boolean, default: true }, panels: { type: String, default: "split" }, compileUrl: { type: String, default: COMPILE_URL }, mediaUrl: String, variablesUrl: String, lock: String }
50
55
  static targets = ["jsonInput", "mjmlInput", "htmlInput"]
51
56
 
52
57
  connect() {
@@ -63,6 +68,8 @@ export default class extends Controller {
63
68
  this.narrow = false
64
69
  this.requests = {}
65
70
  this.media = { enabled: Boolean(this.mediaUrlValue), images: [], uploading: false, error: null }
71
+ this.variables = new Variables(this.variablesUrlValue)
72
+ this.variableMenu = this.variables.enabled ? new VariableMenu(this) : null
66
73
 
67
74
  this.scheduleCanvas = debounce(() => this.renderCanvas(), 60)
68
75
  this.scheduleOutputs = debounce(() => this.writeOutputs(), 300)
@@ -127,6 +134,7 @@ export default class extends Controller {
127
134
  this.scheduleOutputs.cancel()
128
135
  Object.values(this.requests).forEach((request) => request.abort())
129
136
  this.textEditing?.stop()
137
+ this.variableMenu?.close()
130
138
  this.closeMenu()
131
139
  this.resizeObserver?.disconnect()
132
140
  document.removeEventListener("mousedown", this.outsideClick)
@@ -192,31 +200,31 @@ export default class extends Controller {
192
200
  build() {
193
201
  this.blocksElement = h("div", { class: "flex-1 overflow-y-auto pb-4" })
194
202
  this.treeElement = h("div", { class: "flex-1 overflow-y-auto" })
195
- this.leftTabs = h("div", { class: "flex border-b border-gray-200 px-2 shrink-0" })
203
+ this.leftTabs = h("div", { class: "flex border-b border-gray-200 dark:border-gray-700 px-2 shrink-0" })
196
204
  this.inspectorElement = h("div", { class: "flex flex-1 flex-col min-h-0", dataset: { tessEditPanel: "inspect" } })
197
205
  this.sidePanel = h("aside")
198
206
  this.inspectPanel = h("aside")
199
- this.textToolbar = h("div", { class: "hidden absolute left-1/2 top-3 z-20 -translate-x-1/2 flex items-center gap-0.5 rounded-lg border border-gray-200 bg-white px-1.5 py-1 shadow-md" })
207
+ this.textToolbar = h("div", { class: "hidden absolute left-1/2 top-3 z-20 -translate-x-1/2 flex items-center gap-0.5 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 px-1.5 py-1 shadow-md" })
200
208
  this.frame = h("iframe", { class: "block h-full w-full border-0 bg-white transition-[width] duration-200", title: "Email canvas" })
201
209
  this.frameWrap = h("div", { class: "relative h-full mx-auto" }, this.frame)
202
- this.topBar = h("div", { class: "flex items-center gap-1 h-12 px-3 border-b border-gray-200 bg-white shrink-0" })
210
+ this.topBar = h("div", { class: "flex items-center gap-1 h-12 px-3 border-b border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shrink-0" })
203
211
  this.body = h("div", { class: "flex flex-1 min-h-0" })
204
212
 
205
213
  this.root = h(
206
214
  "div",
207
- { class: "tess-edit relative flex flex-col h-full min-h-[560px] overflow-hidden rounded-lg border border-gray-200 bg-gray-50 text-sm text-gray-800 antialiased", tabindex: "-1", onkeydown: (event) => this.onKeydown(event) },
215
+ { class: "tess-edit relative flex flex-col h-full min-h-[560px] overflow-hidden rounded-lg border border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900 text-sm text-gray-800 dark:text-gray-200 antialiased dark:[color-scheme:dark]", tabindex: "-1", onkeydown: (event) => this.onKeydown(event) },
208
216
  this.topBar,
209
217
  this.body
210
218
  )
211
- this.errorBar = h("div", { class: "hidden absolute inset-x-3 bottom-3 z-20 rounded-md border border-red-200 bg-red-50 px-3 py-2 text-xs text-red-700 shadow-sm", role: "alert" })
212
- this.main = h("main", { class: "relative flex-1 min-w-0 bg-gray-100", ondragover: (event) => this.drag && event.preventDefault() }, this.textToolbar, this.frameWrap, this.errorBar)
219
+ this.errorBar = h("div", { class: "hidden absolute inset-x-3 bottom-3 z-20 rounded-md border border-red-200 dark:border-red-800 bg-red-50 dark:bg-red-900/40 px-3 py-2 text-xs text-red-700 dark:text-red-300 shadow-sm", role: "alert" })
220
+ this.main = h("main", { class: "relative flex-1 min-w-0 bg-gray-100 dark:bg-gray-900", ondragover: (event) => this.drag && event.preventDefault() }, this.textToolbar, this.frameWrap, this.errorBar)
213
221
  this.arrangePanels()
214
222
  this.element.append(this.root)
215
223
 
216
224
  this.canvas = new Canvas(this, this.frame)
217
225
  this.tree = new Tree(this, this.treeElement)
218
226
  this.inspector = new Inspector(this, this.inspectorElement)
219
- this.textEditing = new TextEditing(this.textToolbar)
227
+ this.textEditing = new TextEditing(this.textToolbar, { menu: this.variableMenu })
220
228
  renderBlocksPanel(this, this.blocksElement)
221
229
  this.observeWidth()
222
230
 
@@ -241,7 +249,7 @@ export default class extends Controller {
241
249
  // Split: Blocks and Layers on the left, Inspect on the right. Left or right:
242
250
  // one sidebar on that side with Blocks, Layers and Inspect as tabs.
243
251
  arrangePanels() {
244
- const panelClass = (border, width) => `${width} shrink-0 flex flex-col ${border} border-gray-200 bg-white min-h-0`
252
+ const panelClass = (border, width) => `${width} shrink-0 flex flex-col ${border} border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 min-h-0`
245
253
  // Only the sidebars move: moving <main> would reload the canvas iframe and blank it.
246
254
  if (!this.main.isConnected) this.body.append(this.main)
247
255
  this.sidePanel.replaceChildren(this.leftTabs, this.blocksElement, this.treeElement)
@@ -318,13 +326,13 @@ export default class extends Controller {
318
326
  const toggle = (current, options, onchange) =>
319
327
  h(
320
328
  "div",
321
- { class: "flex rounded-lg bg-gray-100 p-0.5" },
329
+ { class: "flex rounded-lg bg-gray-100 dark:bg-gray-900 p-0.5" },
322
330
  options.map(([value, name, title, disabled = false]) =>
323
331
  h("button", {
324
332
  type: "button",
325
333
  title,
326
334
  disabled,
327
- class: `w-8 h-7 flex items-center justify-center rounded-md disabled:opacity-40 disabled:pointer-events-none ${current === value ? "bg-white text-gray-900 shadow-sm" : "text-gray-500 hover:text-gray-800"}`,
335
+ class: `w-8 h-7 flex items-center justify-center rounded-md disabled:opacity-40 disabled:pointer-events-none ${current === value ? "bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 shadow-sm" : "text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200"}`,
328
336
  html: icon(name),
329
337
  onclick: () => onchange(value)
330
338
  })
@@ -332,7 +340,7 @@ export default class extends Controller {
332
340
  )
333
341
 
334
342
  this.topBar.replaceChildren(
335
- h("span", { class: "font-semibold text-gray-900 mr-3" }, "Email"),
343
+ h("span", { class: "font-semibold text-gray-900 dark:text-gray-100 mr-3" }, "Email"),
336
344
  toggle(this.mode, [["edit", "edit", "Edit"], ["preview", "eye", "Preview the email as sent, with Liquid tags"]], (mode) => this.setMode(mode)),
337
345
  toggle(this.device, [["desktop", "desktop", "Desktop"], ["mobile", "mobile", "Mobile"]], (device) => this.setDevice(device)),
338
346
  h("div", { class: "flex-1" }),
@@ -345,12 +353,12 @@ export default class extends Controller {
345
353
  ],
346
354
  (panels) => this.setPanels(panels)
347
355
  ),
348
- h("span", { class: "mx-1 h-5 w-px bg-gray-200" }),
356
+ h("span", { class: "mx-1 h-5 w-px bg-gray-200 dark:bg-gray-700" }),
349
357
  h("button", { type: "button", title: "Undo (⌘Z)", class: TOOL, disabled: !this.history.canUndo, html: icon("undo"), onclick: () => this.undo() }),
350
358
  h("button", { type: "button", title: "Redo (⇧⌘Z)", class: TOOL, disabled: !this.history.canRedo, html: icon("redo"), onclick: () => this.redo() }),
351
359
  h(
352
360
  "button",
353
- { type: "button", class: "ml-2 flex items-center gap-1.5 h-8 rounded-md bg-gray-900 px-3 text-[13px] font-medium text-white hover:bg-gray-700", onclick: () => this.openCodeDialog() },
361
+ { type: "button", class: "ml-2 flex items-center gap-1.5 h-8 rounded-md bg-gray-900 px-3 text-[13px] font-medium text-white hover:bg-gray-700 dark:bg-gray-100 dark:text-gray-900 dark:hover:bg-gray-300", onclick: () => this.openCodeDialog() },
354
362
  h("span", { html: icon("export") }),
355
363
  "Code"
356
364
  )
@@ -359,7 +367,7 @@ export default class extends Controller {
359
367
 
360
368
  renderLeftTabs() {
361
369
  const tab = (key, name) =>
362
- h("button", { type: "button", class: `${TAB} ${this.leftTab === key ? "border-blue-600 text-blue-700" : "border-transparent text-gray-500 hover:text-gray-800"}`, onclick: () => this.setLeftTab(key) }, name)
370
+ h("button", { type: "button", class: `${TAB} ${this.leftTab === key ? "border-blue-600 dark:border-blue-400 text-blue-700 dark:text-blue-300" : "border-transparent text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200"}`, onclick: () => this.setLeftTab(key) }, name)
363
371
  // replaceChildren would turn a null into the text "null", so only pass real tabs.
364
372
  const tabs = [tab("blocks", "Blocks"), tab("layers", "Layers")]
365
373
  if (this.combined) tabs.push(tab("inspect", "Inspect"))
@@ -500,7 +508,7 @@ export default class extends Controller {
500
508
  this.tree.render()
501
509
  this.canvas.refresh()
502
510
  this.scheduleOutputs()
503
- })
511
+ }, { scope: loopScope(this.doc, id) })
504
512
  this.canvas.refresh()
505
513
  }
506
514
 
@@ -607,21 +615,21 @@ export default class extends Controller {
607
615
  if (!items.length) return
608
616
  this.menu = h(
609
617
  "div",
610
- { class: "fixed z-50 w-56 max-h-80 overflow-y-auto rounded-lg border border-gray-200 bg-white p-1 shadow-lg", role: "menu", "data-anchor": anchor, "data-tess-edit-menu": "insert" },
611
- h("div", { class: "px-2 py-1.5 text-[11px] font-semibold uppercase tracking-wide text-gray-500" }, "Add block"),
618
+ { class: "fixed z-50 w-56 max-h-80 overflow-y-auto rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 p-1 shadow-lg", role: "menu", "data-anchor": anchor, "data-tess-edit-menu": "insert" },
619
+ h("div", { class: "px-2 py-1.5 text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400" }, "Add block"),
612
620
  items.map((item) =>
613
621
  h(
614
622
  "button",
615
623
  {
616
624
  type: "button",
617
625
  role: "menuitem",
618
- class: "flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-[13px] text-gray-700 hover:bg-blue-50 hover:text-blue-700",
626
+ class: "flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-[13px] text-gray-700 dark:text-gray-300 hover:bg-blue-50 dark:hover:bg-blue-900/40 hover:text-blue-700 dark:hover:text-blue-300",
619
627
  onclick: () => {
620
628
  this.closeMenu()
621
629
  this.insertAt(item.key, parentId, index)
622
630
  }
623
631
  },
624
- h("span", { class: "text-gray-400", html: icon(item.icon) }),
632
+ h("span", { class: "text-gray-400 dark:text-gray-500", html: icon(item.icon) }),
625
633
  item.label
626
634
  )
627
635
  )
@@ -642,7 +650,7 @@ export default class extends Controller {
642
650
  }
643
651
 
644
652
  flash(message) {
645
- const note = h("div", { class: "absolute bottom-4 left-1/2 z-30 -translate-x-1/2 rounded-md bg-gray-900 px-3 py-2 text-xs text-white shadow-lg" }, message)
653
+ const note = h("div", { class: "absolute bottom-4 left-1/2 z-30 -translate-x-1/2 rounded-md bg-gray-900 px-3 py-2 text-xs text-white shadow-lg dark:bg-gray-700 dark:ring-1 dark:ring-white/10" }, message)
646
654
  this.root.append(note)
647
655
  setTimeout(() => note.remove(), 3500)
648
656
  }
@@ -696,8 +704,8 @@ export default class extends Controller {
696
704
  this.stopEditing()
697
705
  const { json, mjml, html, errors } = await this.export()
698
706
  const contents = { json: JSON.stringify(JSON.parse(json), null, 2), mjml, html }
699
- const textarea = h("textarea", { class: "flex-1 w-full resize-none rounded-md border border-gray-300 p-3 font-mono text-xs text-gray-800 outline-none focus:border-blue-500", spellcheck: false })
700
- const status = h("span", { class: "text-xs text-gray-500" })
707
+ const textarea = h("textarea", { class: "flex-1 w-full resize-none rounded-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-900 p-3 font-mono text-xs text-gray-800 dark:text-gray-200 outline-none focus:border-blue-500", spellcheck: false })
708
+ const status = h("span", { class: "text-xs text-gray-500 dark:text-gray-400" })
701
709
  const tabs = h("div", { class: "flex gap-1" })
702
710
  const apply = h("button", { type: "button", class: "rounded-md bg-blue-600 px-3 py-1.5 text-xs font-medium text-white hover:bg-blue-700", onclick: () => {
703
711
  try {
@@ -705,7 +713,7 @@ export default class extends Controller {
705
713
  close()
706
714
  } catch (error) {
707
715
  status.textContent = error.message
708
- status.className = "text-xs text-red-600"
716
+ status.className = "text-xs text-red-600 dark:text-red-400"
709
717
  }
710
718
  } }, "Load this JSON")
711
719
 
@@ -716,12 +724,12 @@ export default class extends Controller {
716
724
  const loadable = key === "json" && !this.lock
717
725
  textarea.readOnly = !loadable
718
726
  apply.classList.toggle("hidden", !loadable)
719
- status.className = "text-xs text-gray-500"
727
+ status.className = "text-xs text-gray-500 dark:text-gray-400"
720
728
  status.textContent =
721
729
  key === "json" ? (this.lock ? "The document. Parts of it are locked, so it can't be loaded here." : "The document. Edit and load it, or paste one in.") : key === "mjml" ? "Liquid loops and conditions are in <mj-raw> tags." : errors.length ? `The MJML could not be compiled: ${errors.join("; ")}` : "Liquid is left in place for rendering at send time."
722
730
  tabs.replaceChildren(
723
731
  ...["json", "mjml", "html"].map((name) =>
724
- h("button", { type: "button", class: `rounded-md px-3 py-1.5 text-xs font-medium ${name === tab ? "bg-gray-900 text-white" : "text-gray-600 hover:bg-gray-100"}`, onclick: () => show(name) }, name.toUpperCase())
732
+ h("button", { type: "button", class: `rounded-md px-3 py-1.5 text-xs font-medium ${name === tab ? "bg-gray-900 text-white dark:bg-gray-100 dark:text-gray-900" : "text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"}`, onclick: () => show(name) }, name.toUpperCase())
725
733
  )
726
734
  )
727
735
  }
@@ -736,17 +744,17 @@ export default class extends Controller {
736
744
 
737
745
  const dialog = h(
738
746
  "div",
739
- { class: "absolute inset-0 z-40 flex items-center justify-center bg-gray-900/40 p-6", onmousedown: (event) => event.target === dialog && close(), onkeydown: (event) => event.key === "Escape" && close() },
747
+ { class: "absolute inset-0 z-40 flex items-center justify-center bg-gray-900/40 dark:bg-black/60 p-6", onmousedown: (event) => event.target === dialog && close(), onkeydown: (event) => event.key === "Escape" && close() },
740
748
  h(
741
749
  "div",
742
- { class: "flex h-full max-h-[720px] w-full max-w-4xl flex-col gap-3 rounded-xl bg-white p-4 shadow-xl" },
750
+ { class: "flex h-full max-h-[720px] w-full max-w-4xl flex-col gap-3 rounded-xl bg-white dark:bg-gray-800 p-4 shadow-xl" },
743
751
  h("div", { class: "flex items-center gap-3" }, tabs, status, h("button", { type: "button", class: "ml-auto " + TOOL, title: "Close", html: icon("close"), onclick: close })),
744
752
  textarea,
745
753
  h(
746
754
  "div",
747
755
  { class: "flex items-center justify-end gap-2" },
748
- h("button", { type: "button", class: "rounded-md border border-gray-300 px-3 py-1.5 text-xs font-medium text-gray-700 hover:bg-gray-50", onclick: () => navigator.clipboard?.writeText(textarea.value).then(() => (status.textContent = "Copied.")) }, "Copy"),
749
- h("button", { type: "button", class: "rounded-md border border-gray-300 px-3 py-1.5 text-xs font-medium text-gray-700 hover:bg-gray-50", onclick: download }, "Download"),
756
+ h("button", { type: "button", class: "rounded-md border border-gray-300 dark:border-gray-600 px-3 py-1.5 text-xs font-medium text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700", onclick: () => navigator.clipboard?.writeText(textarea.value).then(() => (status.textContent = "Copied.")) }, "Copy"),
757
+ h("button", { type: "button", class: "rounded-md border border-gray-300 dark:border-gray-600 px-3 py-1.5 text-xs font-medium text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700", onclick: download }, "Download"),
750
758
  apply
751
759
  )
752
760
  )
@@ -38,7 +38,7 @@ export const IMAGE_FIELD = { key: "attributes.src", image: "data.image", source:
38
38
  // An image field for an image attribute, e.g. a background:
39
39
  // imageField("attributes.background-url", "Background image", "data.background")
40
40
  export function imageField(key, label, image) {
41
- return { key, label, type: "image", image, source: `${image}Source` }
41
+ return { key, label, type: "image", image, source: `${image}Source`, liquid: true }
42
42
  }
43
43
 
44
44
  // The image fields of a block definition
@@ -0,0 +1,146 @@
1
+ // The variables a template can use, from the host app. The editor's
2
+ // variablesUrl answers one level at a time:
3
+ //
4
+ // GET ?path=order.customer → { path, variables: [{ name, label, type, hint, collection, children }] }
5
+ //
6
+ // with the roots (order, …) for an empty path. `children` says a variable has
7
+ // variables of its own; a `collection` is a list of them. The path goes into
8
+ // a collection's elements: order.order_lines.product. Liquidum's
9
+ // VariablesController answers this way.
10
+ //
11
+ // In Liquid a collection's elements are reached through a Loop block's
12
+ // variable ({% for line in order.order_lines %} → line.product) or with
13
+ // first and last (order.order_lines.first.product), so those are resolved to
14
+ // such a path before asking.
15
+
16
+ import { ancestors } from "tess_edit/core/document"
17
+
18
+ // The members Liquid gives every collection
19
+ export const COLLECTION_MEMBERS = [
20
+ { name: "first", label: "First", type: "object", children: true },
21
+ { name: "last", label: "Last", type: "object", children: true },
22
+ { name: "size", label: "Number of items", type: "integer", children: false }
23
+ ]
24
+
25
+ const ELEMENT = new Set(["first", "last"])
26
+
27
+ export class Variables {
28
+ constructor(url, { fetch: fetcher = (...args) => fetch(...args) } = {}) {
29
+ this.url = url
30
+ this.fetch = fetcher
31
+ this.levels = new Map()
32
+ }
33
+
34
+ get enabled() {
35
+ return Boolean(this.url)
36
+ }
37
+
38
+ // The host's variables at a path (as the endpoint takes it), cached
39
+ at(path) {
40
+ if (!this.levels.has(path)) {
41
+ const request = this.request(path)
42
+ request.catch(() => this.levels.delete(path))
43
+ this.levels.set(path, request)
44
+ }
45
+ return this.levels.get(path)
46
+ }
47
+
48
+ async request(path) {
49
+ const url = new URL(this.url, window.location.href)
50
+ if (path) url.searchParams.set("path", path)
51
+ const response = await this.fetch(url, { headers: { Accept: "application/json" }, credentials: "same-origin" })
52
+ if (response.status === 404) return []
53
+ if (!response.ok) throw new Error(`Loading the variables failed (${response.status} ${response.statusText})`)
54
+ return (await response.json()).variables || []
55
+ }
56
+
57
+ // The variables at a path as written in the template, e.g. "line.product"
58
+ // in a loop over order.order_lines, or "order.order_lines" (a collection:
59
+ // first, last and size). scope is from loopScope.
60
+ async options(path, scope = []) {
61
+ const names = path ? path.split(".") : []
62
+ if (!names.length) return [...scope.map(loopVariable), ...(await this.at(""))]
63
+
64
+ const variable = await this.variable(names, scope)
65
+ if (!variable?.children) return []
66
+ if (variable.collection) return COLLECTION_MEMBERS
67
+ return this.at(resolve(names, scope))
68
+ }
69
+
70
+ // What the last name of a path is: a variable with children, a collection, …
71
+ async variable(names, scope) {
72
+ const last = names[names.length - 1]
73
+ const parent = names.slice(0, -1)
74
+ if (!parent.length) {
75
+ const own = scope.find((entry) => entry.variable === last)
76
+ if (own) return loopVariable(own)
77
+ } else if (ELEMENT.has(last)) {
78
+ const collection = await this.variable(parent, scope)
79
+ return collection?.collection ? { name: last, children: true } : null
80
+ }
81
+ const siblings = parent.length ? await this.options(parent.join("."), scope) : await this.options("", scope)
82
+ return siblings.find((variable) => variable.name === last)
83
+ }
84
+ }
85
+
86
+ function loopVariable({ variable, collection }) {
87
+ return { name: variable, label: `Each of ${collection}`, type: "object", children: true, loop: true }
88
+ }
89
+
90
+ // The path the host knows a template's path by: loop variables become their
91
+ // collection, and first and last go into the collection's elements.
92
+ export function resolve(names, scope = []) {
93
+ let resolved = [...names]
94
+ // Inner loops first: their collection can start with an outer loop's variable
95
+ for (let i = 0; i < scope.length; i++) {
96
+ for (const { variable, collection } of scope) {
97
+ if (resolved[0] === variable) resolved = [...collection.split("."), ...resolved.slice(1)]
98
+ }
99
+ }
100
+ return resolved.filter((name) => !ELEMENT.has(name) && !/^\d+$/.test(name)).join(".")
101
+ }
102
+
103
+ // The loop variables a block can use: its Loop blocks, the innermost first.
104
+ export function loopScope(doc, id) {
105
+ return ancestors(doc, id)
106
+ .filter((node) => node.type === "loop")
107
+ .map((node) => ({ variable: node.data.variable || "item", collection: node.data.collection || "items" }))
108
+ .filter(({ variable, collection }) => /^\w+$/.test(variable) && collection)
109
+ }
110
+
111
+ // The variable path being written at the caret, if any:
112
+ // "Hello {{ order.cus" → { start: 9, path: "order.cus", tag: "{{" }
113
+ // In "output" mode only inside {{ … }} or {% … %} (tag says which); in
114
+ // "expression" mode (a Loop's collection, a Condition) anywhere, with no tag.
115
+ // Not after a filter's | or in a quoted string.
116
+ export function liquidToken(before, mode = "output") {
117
+ let expression = before
118
+ let tag = null
119
+ if (mode === "output") {
120
+ const output = before.lastIndexOf("{{")
121
+ const open = Math.max(output, before.lastIndexOf("{%"))
122
+ if (open < 0 || Math.max(before.lastIndexOf("}}"), before.lastIndexOf("%}")) > open) return null
123
+ tag = open === output ? "{{" : "{%"
124
+ expression = before.slice(open + 2)
125
+ }
126
+ if ((expression.match(/["']/g) || []).length % 2) return null
127
+
128
+ const match = expression.match(/(?:^|[^\w.])([A-Za-z_][\w]*(?:\.[\w]*)*)?$/)
129
+ if (!match) return null
130
+ const path = match[1] || ""
131
+ const start = before.length - path.length
132
+ if (/\|\s*$/.test(before.slice(0, start))) return null
133
+ return { start, path, tag }
134
+ }
135
+
136
+ // The text that replaces a token's last name for a chosen variable: its name,
137
+ // then a dot to go on with its variables, or the end of a {{ tag. `before` is
138
+ // the text before the token, `after` the text after the caret. For a Loop's
139
+ // collection (`collection: true`) a collection is where the path ends.
140
+ export function completion(variable, { tag = null, before = "", after = "", collection = false } = {}) {
141
+ // "{{order" reads better as "{{ order"
142
+ const space = /\{[{%]-?$/.test(before) ? " " : ""
143
+ if (variable.children && !(collection && variable.collection)) return { text: `${space}${variable.name}.`, more: true }
144
+ const close = tag === "{{" && !/^\s*-?\}\}/.test(after) && !/^\s*\|/.test(after)
145
+ return { text: `${space}${variable.name}${close ? " }}" : ""}`, more: false }
146
+ }
@@ -11,7 +11,7 @@ export function renderBlocksPanel(editor, element) {
11
11
  h(
12
12
  "section",
13
13
  { class: "px-3 pt-3" },
14
- h("h3", { class: "mb-2 text-[11px] font-semibold uppercase tracking-wide text-gray-500" }, category.label),
14
+ h("h3", { class: "mb-2 text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400" }, category.label),
15
15
  h(
16
16
  "div",
17
17
  { class: "grid grid-cols-3 gap-1.5" },
@@ -24,7 +24,7 @@ export function renderBlocksPanel(editor, element) {
24
24
  type: "button",
25
25
  draggable: true,
26
26
  title: `Drag onto the canvas, or click to add`,
27
- class: `flex flex-col items-center justify-center gap-1 h-16 rounded-lg border border-gray-200 bg-white text-gray-600 hover:border-blue-400 hover:text-blue-600 hover:shadow-sm cursor-grab active:cursor-grabbing ${category.key === "liquid" ? "text-amber-700" : ""}`,
27
+ class: `flex flex-col items-center justify-center gap-1 h-16 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-600 dark:text-gray-300 hover:border-blue-400 hover:text-blue-600 dark:hover:text-blue-400 hover:shadow-sm cursor-grab active:cursor-grabbing ${category.key === "liquid" ? "text-amber-700 dark:text-amber-400" : ""}`,
28
28
  ondragstart: (event) => {
29
29
  event.dataTransfer.effectAllowed = "copy"
30
30
  event.dataTransfer.setData("text/plain", item.key)
@@ -40,5 +40,5 @@ export function renderBlocksPanel(editor, element) {
40
40
  )
41
41
  )
42
42
  )
43
- element.replaceChildren(...groups, h("p", { class: "px-3 py-4 text-xs text-gray-500" }, "Drag blocks onto the canvas or into the Layers tree. Liquid blocks wrap other blocks in {% for %} or {% if %}."))
43
+ element.replaceChildren(...groups, h("p", { class: "px-3 py-4 text-xs text-gray-500 dark:text-gray-400" }, "Drag blocks onto the canvas or into the Layers tree. Liquid blocks wrap other blocks in {% for %} or {% if %}."))
44
44
  }
@@ -13,13 +13,13 @@ const BOX = "m-0 w-full rounded-md border px-2 py-1.5 font-mono text-xs leading-
13
13
  const LIQUID = /\{\{[\s\S]*?\}\}|\{%[\s\S]*?%\}/
14
14
  const COLORS = {
15
15
  liquid: "text-purple-700",
16
- comment: "text-gray-400 italic",
17
- tag: "text-blue-700",
18
- attribute: "text-amber-700",
19
- string: "text-green-700",
20
- property: "text-blue-700",
16
+ comment: "text-gray-400 dark:text-gray-500 italic",
17
+ tag: "text-blue-700 dark:text-blue-300",
18
+ attribute: "text-amber-700 dark:text-amber-400",
19
+ string: "text-green-700 dark:text-green-400",
20
+ property: "text-blue-700 dark:text-blue-300",
21
21
  at: "text-purple-700",
22
- number: "text-amber-700"
22
+ number: "text-amber-700 dark:text-amber-400"
23
23
  }
24
24
 
25
25
  function escape(text) {
@@ -110,9 +110,9 @@ function onKeydown(event) {
110
110
  // value, language ("html" or "css"), oninput(value); class sizes the field
111
111
  export function codeInput({ value = "", language = "html", oninput, class: className = "min-h-32", readOnly = false }) {
112
112
  const highlight = HIGHLIGHT[language] || escape
113
- const colored = h("pre", { "aria-hidden": "true", class: `${BOX} pointer-events-none absolute inset-0 h-full overflow-hidden border-transparent bg-white text-gray-800` })
113
+ const colored = h("pre", { "aria-hidden": "true", class: `${BOX} pointer-events-none absolute inset-0 h-full overflow-hidden border-transparent bg-white dark:bg-gray-700 text-gray-800 dark:text-gray-200` })
114
114
  const textarea = h("textarea", {
115
- class: `${BOX} ${className} relative block resize-y overflow-y-auto border-gray-300 bg-transparent text-transparent caret-gray-900 outline-none selection:bg-blue-500/25 focus:border-blue-500 focus:ring-1 focus:ring-blue-500`,
115
+ class: `${BOX} ${className} relative block resize-y overflow-y-auto border-gray-300 dark:border-gray-600 bg-transparent text-transparent caret-gray-900 dark:caret-gray-100 outline-none selection:bg-blue-500/25 focus:border-blue-500 focus:ring-1 focus:ring-blue-500`,
116
116
  spellcheck: false,
117
117
  autocapitalize: "off",
118
118
  autocomplete: "off",