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.
@@ -4,11 +4,13 @@
4
4
  import { imageSource } from "tess_edit/core/media"
5
5
  import { getBlock, label } from "tess_edit/core/registry"
6
6
  import { find, getValue } from "tess_edit/core/document"
7
+ import { loopScope } from "tess_edit/core/variables"
7
8
  import { codeInput } from "tess_edit/ui/code_input"
8
9
  import { h } from "tess_edit/ui/dom"
9
10
  import { icon } from "tess_edit/ui/icons"
11
+ import { inputAdapter, suggestIn } from "tess_edit/ui/variable_menu"
10
12
 
11
- const INPUT = "w-full min-w-0 rounded-md border border-gray-300 bg-white px-2 py-1 text-[13px] text-gray-900 shadow-xs outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
13
+ const INPUT = "w-full min-w-0 rounded-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 px-2 py-1 text-[13px] text-gray-900 dark:text-gray-100 shadow-xs outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
12
14
 
13
15
  // "12" → "12px"; anything else is left alone.
14
16
  export function withUnit(value) {
@@ -36,6 +38,7 @@ export class Inspector {
36
38
  this.editor = editor
37
39
  this.element = element
38
40
  this.openGroups = new Map()
41
+ this.adapters = new Map()
39
42
  }
40
43
 
41
44
  render() {
@@ -43,6 +46,8 @@ export class Inspector {
43
46
  const hit = find(doc, selectedId) || { node: doc }
44
47
  const node = hit.node
45
48
  const definition = getBlock(node.type)
49
+ this.editor.variableMenu?.close()
50
+ this.adapters = new Map()
46
51
 
47
52
  const actions =
48
53
  !this.editor.isFree(node.id)
@@ -52,23 +57,23 @@ export class Inspector {
52
57
  ["copy", "Duplicate", () => this.editor.duplicate(node.id)],
53
58
  ["trash", "Delete", () => this.editor.remove(node.id)]
54
59
  ].map(([name, title, onclick]) =>
55
- h("button", { type: "button", title, class: "w-7 h-7 flex items-center justify-center rounded-md text-gray-500 hover:bg-gray-100 hover:text-gray-900", html: icon(name), onclick })
60
+ h("button", { type: "button", title, class: "w-7 h-7 flex items-center justify-center rounded-md text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 hover:text-gray-900 dark:hover:text-gray-100", html: icon(name), onclick })
56
61
  )
57
62
 
58
63
  const header = h(
59
64
  "div",
60
- { class: "flex items-center gap-2 px-4 h-12 border-b border-gray-200 shrink-0" },
61
- h("span", { class: "text-gray-400", html: icon(definition.icon, "w-4 h-4") }),
62
- h("h2", { class: "font-semibold text-gray-900 truncate" }, label(node)),
65
+ { class: "flex items-center gap-2 px-4 h-12 border-b border-gray-200 dark:border-gray-700 shrink-0" },
66
+ h("span", { class: "text-gray-400 dark:text-gray-500", html: icon(definition.icon, "w-4 h-4") }),
67
+ h("h2", { class: "font-semibold text-gray-900 dark:text-gray-100 truncate" }, label(node)),
63
68
  h("div", { class: "ml-auto flex items-center gap-0.5" }, actions)
64
69
  )
65
70
 
66
71
  const groups = (definition.fields || []).map((group) => this.group(node, group))
67
72
  const locked = this.editor.lock && node.id !== doc.id && !this.editor.isFree(node.id)
68
- ? h("p", { class: "mx-4 mt-3 flex items-center gap-2 rounded-md bg-amber-50 px-3 py-2 text-xs text-amber-800" }, h("span", { html: icon("lock", "w-3.5 h-3.5") }), "Part of the layout")
73
+ ? h("p", { class: "mx-4 mt-3 flex items-center gap-2 rounded-md bg-amber-50 dark:bg-amber-900/30 px-3 py-2 text-xs text-amber-800 dark:text-amber-200" }, h("span", { html: icon("lock", "w-3.5 h-3.5") }), "Part of the layout")
69
74
  : null
70
75
  // A block's hint explains how to edit it, which a locked block can't be.
71
- const hint = definition.hint && !locked ? h("p", { class: "mx-4 mt-3 rounded-md bg-gray-50 px-3 py-2 text-xs text-gray-600" }, definition.hint) : null
76
+ const hint = definition.hint && !locked ? h("p", { class: "mx-4 mt-3 rounded-md bg-gray-50 dark:bg-gray-700/50 px-3 py-2 text-xs text-gray-600 dark:text-gray-300" }, definition.hint) : null
72
77
 
73
78
  this.element.replaceChildren(header, h("div", { class: "flex-1 overflow-y-auto pb-6" }, locked, hint, groups))
74
79
  }
@@ -78,10 +83,10 @@ export class Inspector {
78
83
  const open = this.openGroups.has(key) ? this.openGroups.get(key) : !group.collapsed
79
84
  const details = h(
80
85
  "details",
81
- { class: "group/field border-b border-gray-100", open, ontoggle: (event) => this.openGroups.set(key, event.currentTarget.open) },
86
+ { class: "group/field border-b border-gray-100 dark:border-gray-700", open, ontoggle: (event) => this.openGroups.set(key, event.currentTarget.open) },
82
87
  h(
83
88
  "summary",
84
- { class: "flex items-center gap-1.5 px-4 py-2.5 cursor-pointer select-none text-xs font-semibold uppercase tracking-wide text-gray-500 hover:text-gray-800 list-none [&::-webkit-details-marker]:hidden" },
89
+ { class: "flex items-center gap-1.5 px-4 py-2.5 cursor-pointer select-none text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 list-none [&::-webkit-details-marker]:hidden" },
85
90
  h("span", { html: icon("chevron", "w-3 h-3 transition-transform group-open/field:rotate-90") }),
86
91
  group.title
87
92
  ),
@@ -100,23 +105,27 @@ export class Inspector {
100
105
  ? this.control(node, field, value, set)
101
106
  : h("fieldset", { disabled: true, class: "opacity-60" }, this.control(node, field, value, set))
102
107
  const expand = field.type === "code" && editable
103
- ? h("button", { type: "button", title: `Edit ${field.label} in a larger window`, class: "absolute right-0 top-0 rounded px-1 text-[11px] font-medium text-blue-600 hover:bg-blue-50", onclick: () => this.expand(node, field) }, "Expand")
108
+ ? h("button", { type: "button", title: `Edit ${field.label} in a larger window`, class: "absolute right-0 top-0 rounded px-1 text-[11px] font-medium text-blue-600 dark:text-blue-400 hover:bg-blue-50 dark:hover:bg-blue-900/40", onclick: () => this.expand(node, field) }, "Expand")
109
+ : null
110
+ const adapter = editable && field.liquid && this.adapters.get(field.key)
111
+ const variables = adapter
112
+ ? h("button", { type: "button", title: "Insert a variable", class: "absolute right-0 top-0 rounded px-1 font-mono text-[11px] font-medium text-blue-600 dark:text-blue-400 hover:bg-blue-50 dark:hover:bg-blue-900/40", onclick: (event) => this.editor.variableMenu.browse(adapter, event.currentTarget) }, "{ }")
104
113
  : null
105
114
  // A div, not a label: a label would forward clicks to the first button or color picker inside it.
106
115
  return h(
107
116
  "div",
108
117
  { class: "relative block" },
109
- h("span", { class: "mb-1 block text-xs font-medium text-gray-700" }, field.label),
110
- expand,
118
+ h("span", { class: "mb-1 block text-xs font-medium text-gray-700 dark:text-gray-300" }, field.label),
119
+ expand || variables,
111
120
  control,
112
- field.hint ? h("span", { class: "mt-1 block text-[11px] leading-snug text-gray-500" }, field.hint) : null
121
+ field.hint ? h("span", { class: "mt-1 block text-[11px] leading-snug text-gray-500 dark:text-gray-400" }, field.hint) : null
113
122
  )
114
123
  }
115
124
 
116
125
  control(node, field, value, set) {
117
126
  switch (field.type) {
118
127
  case "textarea":
119
- return h("textarea", { class: `${INPUT} min-h-32`, rows: 4, value, oninput: (event) => set(event.target.value) })
128
+ return this.variables(h("textarea", { class: `${INPUT} min-h-32`, rows: 4, value, oninput: (event) => set(event.target.value) }), node, field)
120
129
 
121
130
  case "code":
122
131
  return codeInput({ value, language: field.language, oninput: set, class: "h-60" })
@@ -148,14 +157,14 @@ export class Inspector {
148
157
  case "segmented":
149
158
  return h(
150
159
  "div",
151
- { class: "flex rounded-md border border-gray-300 overflow-hidden" },
160
+ { class: "flex rounded-md border border-gray-300 dark:border-gray-600 overflow-hidden" },
152
161
  field.options.map((option) =>
153
162
  h(
154
163
  "button",
155
164
  {
156
165
  type: "button",
157
166
  title: value === option.value ? "Click again to use the default" : "",
158
- class: `flex-1 px-2 py-1 text-xs border-l first:border-l-0 border-gray-300 ${value === option.value ? "bg-blue-600 text-white" : "bg-white text-gray-700 hover:bg-gray-50"}`,
167
+ class: `flex-1 px-2 py-1 text-xs border-l first:border-l-0 border-gray-300 dark:border-gray-600 ${value === option.value ? "bg-blue-600 text-white" : "bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-600"}`,
159
168
  onclick: (event) => {
160
169
  event.preventDefault()
161
170
  set(value === option.value ? "" : option.value)
@@ -173,11 +182,11 @@ export class Inspector {
173
182
  { class: "flex items-center gap-2" },
174
183
  h("input", {
175
184
  type: "checkbox",
176
- class: "h-4 w-4 rounded border-gray-300 text-blue-600",
185
+ class: "h-4 w-4 rounded border-gray-300 dark:border-gray-600 dark:bg-gray-700 text-blue-600 dark:text-blue-400",
177
186
  checked: value === field.on,
178
187
  onchange: (event) => set(event.target.checked ? field.on : field.off)
179
188
  }),
180
- h("span", { class: "text-xs text-gray-600" }, value === field.on ? "On" : "Off")
189
+ h("span", { class: "text-xs text-gray-600 dark:text-gray-300" }, value === field.on ? "On" : "Off")
181
190
  )
182
191
 
183
192
  case "spacing":
@@ -190,14 +199,27 @@ export class Inspector {
190
199
  return this.image(node, field, value, set)
191
200
 
192
201
  default:
193
- return h("input", { type: "text", class: INPUT, value, placeholder: field.placeholder || "", oninput: (event) => set(event.target.value) })
202
+ return this.variables(h("input", { type: "text", class: INPUT, value, placeholder: field.placeholder || "", oninput: (event) => set(event.target.value) }), node, field)
194
203
  }
195
204
  }
196
205
 
206
+ // Suggests the host's Liquid variables (editor.variables) while typing {{ in
207
+ // a text field. A field's `liquid` also gives it a { } button to browse
208
+ // them: true for Liquid in text, "expression" for a condition, "collection"
209
+ // for what a Loop goes over.
210
+ variables(input, node, field) {
211
+ if (!this.editor.variables?.enabled) return input
212
+ const mode = field.liquid === "expression" || field.liquid === "collection" ? field.liquid : "output"
213
+ const adapter = inputAdapter(input, { mode, scope: loopScope(this.editor.doc, node.id) })
214
+ suggestIn(this.editor.variableMenu, input, adapter)
215
+ this.adapters.set(field.key, adapter)
216
+ return input
217
+ }
218
+
197
219
  // An image URL, or with the host's images (editor.media, core/media.js) a
198
220
  // choice between one of those, a new upload, and a URL
199
221
  image(node, field, value, set) {
200
- const url = h("input", { type: "text", class: INPUT, value, placeholder: field.placeholder || "https://…", oninput: (event) => set(event.target.value) })
222
+ const url = this.variables(h("input", { type: "text", class: INPUT, value, placeholder: field.placeholder || "https://…", oninput: (event) => set(event.target.value) }), node, field)
201
223
  if (!this.editor.media.enabled) return url
202
224
 
203
225
  const source = imageSource(node, this.editor, field)
@@ -238,11 +260,11 @@ export class Inspector {
238
260
  "div",
239
261
  { class: "flex items-center gap-1.5" },
240
262
  select,
241
- h("button", { type: "button", class: "shrink-0 rounded-md border border-gray-300 bg-white px-2 py-1 text-xs text-gray-700 hover:bg-gray-50 disabled:opacity-50", disabled: uploading, onclick: () => file.click() }, uploading ? "Uploading…" : "Upload"),
263
+ h("button", { type: "button", class: "shrink-0 rounded-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 px-2 py-1 text-xs text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-600 disabled:opacity-50", disabled: uploading, onclick: () => file.click() }, uploading ? "Uploading…" : "Upload"),
242
264
  file
243
265
  ),
244
- error ? h("p", { class: "mt-1 text-[11px] text-red-600", role: "alert" }, error) : null,
245
- current ? h("img", { src: current.url, alt: "", class: "mt-2 max-h-24 rounded border border-gray-200" }) : null
266
+ error ? h("p", { class: "mt-1 text-[11px] text-red-600 dark:text-red-400", role: "alert" }, error) : null,
267
+ current ? h("img", { src: current.url, alt: "", class: "mt-2 max-h-24 rounded border border-gray-200 dark:border-gray-700" }) : null
246
268
  )
247
269
  }
248
270
 
@@ -258,7 +280,7 @@ export class Inspector {
258
280
  const dialog = h(
259
281
  "div",
260
282
  {
261
- class: "absolute inset-0 z-40 flex items-center justify-center bg-gray-900/40 p-6",
283
+ class: "absolute inset-0 z-40 flex items-center justify-center bg-gray-900/40 dark:bg-black/60 p-6",
262
284
  onmousedown: (event) => event.target === dialog && close(),
263
285
  // Escape closes the dialog, not anything behind it
264
286
  onkeydown: (event) => {
@@ -269,13 +291,13 @@ export class Inspector {
269
291
  },
270
292
  h(
271
293
  "div",
272
- { role: "dialog", "aria-label": field.label, class: "flex h-full max-h-[800px] w-full max-w-5xl flex-col gap-3 rounded-xl bg-white p-4 shadow-xl" },
294
+ { role: "dialog", "aria-label": field.label, class: "flex h-full max-h-[800px] w-full max-w-5xl flex-col gap-3 rounded-xl bg-white dark:bg-gray-800 p-4 shadow-xl" },
273
295
  h(
274
296
  "div",
275
297
  { class: "flex items-center gap-3" },
276
- h("h3", { class: "text-sm font-semibold text-gray-900" }, label(node) === field.label ? field.label : `${label(node)} · ${field.label}`),
277
- field.hint ? h("span", { class: "text-xs text-gray-500" }, field.hint) : null,
278
- h("button", { type: "button", title: "Close", class: "ml-auto w-8 h-8 flex items-center justify-center rounded-md text-gray-600 hover:bg-gray-100", html: icon("close"), onclick: close })
298
+ h("h3", { class: "text-sm font-semibold text-gray-900 dark:text-gray-100" }, label(node) === field.label ? field.label : `${label(node)} · ${field.label}`),
299
+ field.hint ? h("span", { class: "text-xs text-gray-500 dark:text-gray-400" }, field.hint) : null,
300
+ h("button", { type: "button", title: "Close", class: "ml-auto 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", html: icon("close"), onclick: close })
279
301
  ),
280
302
  input
281
303
  )
@@ -293,7 +315,7 @@ export class Inspector {
293
315
  })
294
316
  const swatch = h("input", {
295
317
  type: "color",
296
- class: "h-7 w-8 shrink-0 cursor-pointer rounded border border-gray-300 bg-white p-0.5",
318
+ class: "h-7 w-8 shrink-0 cursor-pointer rounded border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 p-0.5",
297
319
  value: /^#[0-9a-f]{6}$/i.test(value) ? value : "#ffffff",
298
320
  oninput: (event) => {
299
321
  text.value = event.target.value
@@ -320,7 +342,7 @@ export class Inspector {
320
342
  set(formatSpacing(values))
321
343
  }
322
344
  }),
323
- h("span", { class: "block text-center text-[10px] text-gray-400" }, side)
345
+ h("span", { class: "block text-center text-[10px] text-gray-400 dark:text-gray-500" }, side)
324
346
  )
325
347
  )
326
348
  return h("span", { class: "grid grid-cols-4 gap-1" }, inputs)
@@ -334,7 +356,7 @@ export class Inspector {
334
356
  const rows = items.map((item, index) =>
335
357
  h(
336
358
  "div",
337
- { class: "rounded-md border border-gray-200 bg-gray-50 p-2 space-y-1.5" },
359
+ { class: "rounded-md border border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-700/50 p-2 space-y-1.5" },
338
360
  field.item.map((part) =>
339
361
  part.type === "select"
340
362
  ? h(
@@ -342,7 +364,7 @@ export class Inspector {
342
364
  { class: INPUT, onchange: (event) => update(items.map((it, i) => (i === index ? { ...it, [part.name]: event.target.value } : it))) },
343
365
  part.options.map((option) => h("option", { value: option, selected: option === item[part.name] }, option))
344
366
  )
345
- : h("input", {
367
+ : this.variables(h("input", {
346
368
  type: "text",
347
369
  class: INPUT,
348
370
  placeholder: part.label,
@@ -351,19 +373,19 @@ export class Inspector {
351
373
  items = items.map((it, i) => (i === index ? { ...it, [part.name]: event.target.value } : it))
352
374
  set(items)
353
375
  }
354
- })
376
+ }), node, { key: `${field.key}.${index}.${part.name}` })
355
377
  ),
356
378
  h(
357
379
  "div",
358
380
  { class: "flex justify-end gap-1" },
359
381
  h("button", {
360
- type: "button", title: "Move up", class: "w-6 h-6 flex items-center justify-center rounded text-gray-500 hover:bg-gray-200 disabled:opacity-30", disabled: index === 0, html: icon("up", "w-3.5 h-3.5"), onclick: () => {
382
+ type: "button", title: "Move up", class: "w-6 h-6 flex items-center justify-center rounded text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-600 disabled:opacity-30", disabled: index === 0, html: icon("up", "w-3.5 h-3.5"), onclick: () => {
361
383
  const next = [...items]
362
384
  ;[next[index - 1], next[index]] = [next[index], next[index - 1]]
363
385
  update(next)
364
386
  }
365
387
  }),
366
- h("button", { type: "button", title: "Remove", class: "w-6 h-6 flex items-center justify-center rounded text-gray-500 hover:bg-gray-200 hover:text-red-600", html: icon("trash", "w-3.5 h-3.5"), onclick: () => update(items.filter((_, i) => i !== index)) })
388
+ h("button", { type: "button", title: "Remove", class: "w-6 h-6 flex items-center justify-center rounded text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-600 hover:text-red-600 dark:hover:text-red-400", html: icon("trash", "w-3.5 h-3.5"), onclick: () => update(items.filter((_, i) => i !== index)) })
367
389
  )
368
390
  )
369
391
  )
@@ -371,7 +393,7 @@ export class Inspector {
371
393
  "div",
372
394
  { class: "space-y-2" },
373
395
  rows,
374
- h("button", { type: "button", class: "w-full rounded-md border border-dashed border-gray-300 py-1.5 text-xs text-gray-600 hover:border-blue-400 hover:text-blue-600", onclick: () => update([...items, { ...field.newItem }]) }, "Add")
396
+ h("button", { type: "button", class: "w-full rounded-md border border-dashed border-gray-300 dark:border-gray-600 py-1.5 text-xs text-gray-600 dark:text-gray-300 hover:border-blue-400 hover:text-blue-600 dark:hover:text-blue-400", onclick: () => update([...items, { ...field.newItem }]) }, "Add")
375
397
  )
376
398
  }
377
399
  }
@@ -1,16 +1,19 @@
1
1
  // Inline rich-text editing with Tiptap, mounted inside the canvas iframe on the
2
- // text block's own element, plus the formatting toolbar above the canvas.
2
+ // text block's own element, plus the formatting toolbar above the canvas. With
3
+ // the host's variables (`menu`, a ui/variable_menu.js VariableMenu) typing {{
4
+ // suggests them, and the toolbar's { } button browses them.
3
5
 
4
6
  import { Color, Editor, StarterKit, TextAlign, TextStyle } from "tess_edit/vendor/tiptap"
5
7
  import { h } from "tess_edit/ui/dom"
6
8
  import { icon } from "tess_edit/ui/icons"
7
9
 
8
- const BUTTON = "w-7 h-7 flex items-center justify-center rounded-md text-gray-600 hover:bg-gray-100 hover:text-gray-900"
9
- const ACTIVE = "bg-blue-50 text-blue-700"
10
+ const BUTTON = "w-7 h-7 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"
11
+ const ACTIVE = "bg-blue-50 dark:bg-blue-900/40 text-blue-700 dark:text-blue-300"
10
12
 
11
13
  export class TextEditing {
12
- constructor(toolbar) {
14
+ constructor(toolbar, { menu = null } = {}) {
13
15
  this.toolbar = toolbar
16
+ this.menu = menu
14
17
  this.tiptap = null
15
18
  }
16
19
 
@@ -18,7 +21,8 @@ export class TextEditing {
18
21
  return !!this.tiptap
19
22
  }
20
23
 
21
- start(container, content, onUpdate) {
24
+ // scope: the loop variables of the block (core/variables.js loopScope)
25
+ start(container, content, onUpdate, { scope = [] } = {}) {
22
26
  this.stop()
23
27
  container.replaceChildren()
24
28
  this.tiptap = new Editor({
@@ -37,9 +41,16 @@ export class TextEditing {
37
41
  TextAlign.configure({ types: ["heading", "paragraph"] })
38
42
  ],
39
43
  autofocus: "end",
40
- onUpdate: ({ editor }) => onUpdate(editor.getJSON()),
44
+ editorProps: { handleKeyDown: (view, event) => this.menu?.key(event) || false },
45
+ onUpdate: ({ editor, transaction }) => {
46
+ onUpdate(editor.getJSON())
47
+ if (this.menu && !transaction.getMeta("variable")) this.menu.suggest(this.adapter)
48
+ },
49
+ onSelectionUpdate: () => this.menu?.open && !this.menu.state?.browse && this.menu.suggest(this.adapter),
50
+ onBlur: () => this.menu?.open && !this.menu.state?.browse && this.menu.close(),
41
51
  onTransaction: () => this.renderToolbar()
42
52
  })
53
+ this.adapter = this.menu && tiptapAdapter(this.tiptap, scope)
43
54
  this.renderToolbar()
44
55
  return this.tiptap
45
56
  }
@@ -47,6 +58,7 @@ export class TextEditing {
47
58
  stop() {
48
59
  if (!this.tiptap) return null
49
60
  const json = this.tiptap.getJSON()
61
+ this.menu?.close()
50
62
  this.tiptap.destroy()
51
63
  this.tiptap = null
52
64
  this.toolbar.replaceChildren()
@@ -67,13 +79,13 @@ export class TextEditing {
67
79
  onmousedown: (event) => event.preventDefault(),
68
80
  onclick: run
69
81
  })
70
- const separator = () => h("span", { class: "mx-1 h-5 w-px bg-gray-200" })
82
+ const separator = () => h("span", { class: "mx-1 h-5 w-px bg-gray-200 dark:bg-gray-700" })
71
83
 
72
84
  const level = [1, 2, 3].find((l) => editor.isActive("heading", { level: l }))
73
85
  const style = h(
74
86
  "select",
75
87
  {
76
- class: "h-7 rounded-md border border-gray-300 bg-white px-1.5 text-xs text-gray-800",
88
+ class: "h-7 rounded-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 px-1.5 text-xs text-gray-800 dark:text-gray-200",
77
89
  title: "Paragraph style",
78
90
  onchange: (event) => {
79
91
  const value = Number(event.target.value)
@@ -88,7 +100,7 @@ export class TextEditing {
88
100
  const color = h("input", {
89
101
  type: "color",
90
102
  title: "Text color",
91
- class: "h-7 w-8 cursor-pointer rounded border border-gray-300 bg-white p-0.5",
103
+ class: "h-7 w-8 cursor-pointer rounded border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 p-0.5",
92
104
  value: /^#[0-9a-f]{6}$/i.test(currentColor || "") ? currentColor : "#000000",
93
105
  onchange: (event) => chain().setColor(event.target.value).run()
94
106
  })
@@ -117,8 +129,51 @@ export class TextEditing {
117
129
  button("list-bullet", "Bulleted list", editor.isActive("bulletList"), () => chain().toggleBulletList().run()),
118
130
  button("list-ordered", "Numbered list", editor.isActive("orderedList"), () => chain().toggleOrderedList().run()),
119
131
  separator(),
120
- button("clear", "Clear formatting", false, () => chain().unsetAllMarks().clearNodes().run())
132
+ button("clear", "Clear formatting", false, () => chain().unsetAllMarks().clearNodes().run()),
133
+ this.menu
134
+ ? h("button", {
135
+ type: "button",
136
+ title: "Insert a variable",
137
+ class: `${BUTTON} w-auto px-1.5 font-mono text-xs`,
138
+ onmousedown: (event) => event.preventDefault(),
139
+ onclick: (event) => this.menu.browse(this.adapter, event.currentTarget)
140
+ }, "{ }")
141
+ : null
121
142
  ].filter(Boolean))
122
143
  this.toolbar.classList.remove("hidden")
123
144
  }
124
145
  }
146
+
147
+ // A variable menu adapter for Tiptap (see ui/variable_menu.js): offsets are in
148
+ // the paragraph with the caret, in which every character and inline node
149
+ // takes one position.
150
+ function tiptapAdapter(editor, scope) {
151
+ const text = ($pos, from, to) => $pos.parent.textBetween(from, to, undefined, "\ufffc")
152
+ const dispatch = (from, to, value) => {
153
+ editor.view.dispatch(editor.state.tr.insertText(value, from, to).setMeta("variable", true))
154
+ editor.view.focus()
155
+ }
156
+ return {
157
+ mode: "output",
158
+ scope,
159
+ before: () => text(editor.state.selection.$from, 0, editor.state.selection.$from.parentOffset),
160
+ after: () => {
161
+ const $to = editor.state.selection.$to
162
+ return text($to, $to.parentOffset, $to.parent.content.size)
163
+ },
164
+ replace(start, end, value) {
165
+ const base = editor.state.selection.$from.start()
166
+ dispatch(base + start, base + end, value)
167
+ },
168
+ insert(value) {
169
+ const { from, to } = editor.state.selection
170
+ dispatch(from, to, value)
171
+ },
172
+ // The caret's place in the iframe, in the page's viewport
173
+ rect() {
174
+ const caret = editor.view.coordsAtPos(editor.state.selection.from)
175
+ const frame = editor.view.dom.ownerDocument.defaultView.frameElement?.getBoundingClientRect() || { left: 0, top: 0 }
176
+ return { left: caret.left + frame.left, right: caret.right + frame.left, top: caret.top + frame.top, bottom: caret.bottom + frame.top }
177
+ }
178
+ }
179
+ }
@@ -32,7 +32,7 @@ export class Tree {
32
32
  "button",
33
33
  {
34
34
  type: "button",
35
- class: `w-4 h-4 shrink-0 flex items-center justify-center text-gray-400 hover:text-gray-700 ${children.length ? "" : "invisible"}`,
35
+ class: `w-4 h-4 shrink-0 flex items-center justify-center text-gray-400 dark:text-gray-500 hover:text-gray-700 dark:hover:text-gray-200 ${children.length ? "" : "invisible"}`,
36
36
  onclick: (event) => {
37
37
  event.stopPropagation()
38
38
  open ? this.collapsed.add(node.id) : this.collapsed.delete(node.id)
@@ -46,7 +46,7 @@ export class Tree {
46
46
  ? h("button", {
47
47
  type: "button",
48
48
  title: "Add a block inside",
49
- class: "ml-auto hidden group-hover:flex w-5 h-5 items-center justify-center rounded text-gray-500 hover:bg-gray-200 hover:text-gray-900",
49
+ class: "ml-auto hidden group-hover:flex w-5 h-5 items-center justify-center rounded text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-600 hover:text-gray-900 dark:hover:text-gray-100",
50
50
  html: icon("plus", "w-3.5 h-3.5"),
51
51
  onclick: (event) => {
52
52
  event.stopPropagation()
@@ -58,7 +58,7 @@ export class Tree {
58
58
  const line = h(
59
59
  "div",
60
60
  {
61
- class: `group relative flex items-center gap-1.5 pr-2 h-7 cursor-pointer select-none rounded-md mx-1 ${selected ? "bg-blue-50 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`,
61
+ class: `group relative flex items-center gap-1.5 pr-2 h-7 cursor-pointer select-none rounded-md mx-1 ${selected ? "bg-blue-50 dark:bg-blue-900/40 text-blue-700 dark:text-blue-300" : "text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"}`,
62
62
  style: `padding-left:${depth * 14 + 4}px`,
63
63
  role: "treeitem",
64
64
  "aria-selected": selected ? "true" : "false",
@@ -79,9 +79,9 @@ export class Tree {
79
79
  onmouseleave: () => this.editor.canvas?.setHover(null)
80
80
  },
81
81
  toggle,
82
- h("span", { class: `shrink-0 ${getBlock(node.type)?.accepts === "inherit" ? "text-amber-600" : "text-gray-400"}`, html: icon(getBlock(node.type)?.icon, "w-4 h-4") }),
83
- h("span", { class: `truncate text-[13px] ${locked ? "text-gray-400" : ""}` }, label(node)),
84
- locked ? h("span", { class: "ml-auto shrink-0 text-gray-300", title: "Part of the layout", html: icon("lock", "w-3.5 h-3.5") }) : add,
82
+ h("span", { class: `shrink-0 ${getBlock(node.type)?.accepts === "inherit" ? "text-amber-600 dark:text-amber-400" : "text-gray-400 dark:text-gray-500"}`, html: icon(getBlock(node.type)?.icon, "w-4 h-4") }),
83
+ h("span", { class: `truncate text-[13px] ${locked ? "text-gray-400 dark:text-gray-500" : ""}` }, label(node)),
84
+ locked ? h("span", { class: "ml-auto shrink-0 text-gray-300 dark:text-gray-600", title: "Part of the layout", html: icon("lock", "w-3.5 h-3.5") }) : add,
85
85
  h("div", { class: "tess-tree-marker pointer-events-none absolute left-1 right-1 hidden" })
86
86
  )
87
87