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.
@@ -9,14 +9,18 @@
9
9
  // accepts: undefined, // roles it takes as children, "inherit", or undefined for a leaf
10
10
  // defaults: () => ({ attributes: {}, data: {} }),
11
11
  // fields: [{ title: "Product", fields: [{ key: "data.sku", label: "SKU", type: "text" }] }],
12
- // // a field's visible: (node, editor) => boolean shows it only when it applies;
12
+ // // a field's visible: (node, editor) => boolean shows it only when it applies,
13
+ // // and a group only when one of its fields does;
13
14
  // // imageField(key, label, idKey) (core/media.js) makes an image field;
14
15
  // // a "code" field takes language: "html" (default) or "css"
15
16
  // hint: "Shown at the top of Inspect.",
16
17
  // label: (node) => `Product · ${node.data.sku}`, // optional, for Layers and the canvas
17
18
  // richText: "data.content", // optional: a Tiptap JSON value edited on the canvas by double-click
18
19
  // palette: [{ key, label, icon, category, create: ({ createNode }) => node }], // optional
19
- // render: { mjml: (node, ctx) => "<mj-text …>" } // ctx: see export/mjml.js
20
+ // kits: ["email"], // the kits it is part of, default ["email"]
21
+ // unique: true, // optional: at most one per document, like Content
22
+ // render: { mjml: (node, ctx) => "<mj-text …>" } // one per format of its kits:
23
+ // // mjml (export/mjml.js), html (export/html.js)
20
24
  // })
21
25
  //
22
26
  // Roles: "body" (inside the page or a wrapper: sections, heroes), "wrapper",
@@ -26,11 +30,99 @@
26
30
  //
27
31
  // Without a `palette`, a block with a `category` gets one tile in the Blocks
28
32
  // panel; `palette: []` gives it none. Registering a type again replaces it.
33
+ //
34
+ // Kits: what the editor makes. A kit has its own root block and the blocks
35
+ // that name it in their `kits`, and decides what the canvas shows and what
36
+ // the editor outputs:
37
+ //
38
+ // registerKit({
39
+ // key: "note", // data-tess-edit-kit-value; blocks name it in `kits`
40
+ // name: "Note", // shown in the top bar
41
+ // root: "note", // the type of its root block, unique to the kit
42
+ // starter: () => doc, // the sample document
43
+ // devices: [{ key, icon, title, width }], // optional: canvas widths in the top bar
44
+ // previewTitle: "…", // the Preview button's title
45
+ // canvas: async (doc, { editor, compileUrl, signal }) => ({ html, errors }),
46
+ // // the canvas's HTML, with the tess-<id> and tessph-<id>
47
+ // // classes when editor is true; null when aborted
48
+ // outputs: async (doc, { compileUrl, signal }) => ({ outputs: { html: "…" }, errors }),
49
+ // // what the editor stores besides the JSON (the inputs,
50
+ // // the change event and the Code dialog); null when aborted
51
+ // notes: { html: "…" }, // optional: what each output is, in the Code dialog
52
+ // errors: "The email could not be compiled" // shown before the reasons
53
+ // })
54
+ //
55
+ // The built-in kit is "email" (MJML, compiled to HTML on the server, see
56
+ // kits/email.js). A host adds its own kit with its root block and blocks,
57
+ // and brings TessEdit's blocks into it with extendBlock:
58
+ //
59
+ // extendBlock("text", { kits: ["note"], render: { note: (node, ctx) => "<div>…</div>" } })
60
+ //
61
+ // which adds the kits and render hooks (and any fields, in groups of their
62
+ // own) to the block as it is registered, also when another gem replaced it.
29
63
 
30
64
  import { builtinBlocks, builtinCategories } from "tess_edit/blocks/builtin"
65
+ import { builtinKits } from "tess_edit/kits/builtin"
31
66
 
32
67
  const definitions = new Map()
33
68
  const categoryList = new Map()
69
+ const kitList = new Map()
70
+ const sourceList = new Map()
71
+
72
+ export const DEFAULT_KIT = "email"
73
+
74
+ export function registerKit(kit) {
75
+ const { key, name, root, canvas, outputs } = kit || {}
76
+ if (!key || !name) throw new Error("A kit needs a key and a name")
77
+ if (!root) throw new Error(`Kit ${key} needs a root block type`)
78
+ if (typeof canvas !== "function" || typeof outputs !== "function") throw new Error(`Kit ${key} needs canvas(doc, options) and outputs(doc, options)`)
79
+ kitList.set(key, { devices: null, notes: {}, errors: `The ${name} could not be rendered`, ...kit })
80
+ return kitList.get(key)
81
+ }
82
+
83
+ export function getKit(key = DEFAULT_KIT) {
84
+ return kitList.get(key)
85
+ }
86
+
87
+ export function kits() {
88
+ return [...kitList.values()]
89
+ }
90
+
91
+ // The kit a document belongs to, from its root block
92
+ export function kitOf(doc) {
93
+ return kits().find((kit) => kit.root === doc?.type)
94
+ }
95
+
96
+ function inKit(definition, kit) {
97
+ return !kit || definition.kits.includes(kit)
98
+ }
99
+
100
+ // Tiles the host app gives at run time, such as reusable content from its
101
+ // server, in a section of their own at the bottom of the Blocks panel:
102
+ //
103
+ // registerPaletteSource({
104
+ // key: "snippets",
105
+ // label: "Snippets",
106
+ // items: async (editor) => [{ key, label, type, description, version }],
107
+ // // null leaves the section out (e.g. not for this editor);
108
+ // // `type` is the block type create gives, to show where it fits
109
+ // create: async (item, editor) => node // the node inserted, e.g. with renumber(block, { origin: true })
110
+ // })
111
+ //
112
+ // A tile is dragged onto the canvas or clicked like any other. Its node goes
113
+ // where its type fits; a container that takes what its parent takes
114
+ // ("inherit") also needs its children to fit there.
115
+ export function registerPaletteSource(source) {
116
+ const { key, label, items, create } = source || {}
117
+ if (!key || !label) throw new Error("A palette source needs a key and a label")
118
+ if (typeof items !== "function" || typeof create !== "function") throw new Error(`Palette source ${key} needs items(editor) and create(item, editor)`)
119
+ sourceList.set(key, { ...source })
120
+ return sourceList.get(key)
121
+ }
122
+
123
+ export function paletteSources() {
124
+ return [...sourceList.values()]
125
+ }
34
126
 
35
127
  export function registerCategory({ key, label, order = 100 }) {
36
128
  if (!key || !label) throw new Error("A category needs a key and a label")
@@ -41,8 +133,8 @@ export function registerBlock(definition) {
41
133
  const { type, name, render } = definition || {}
42
134
  if (!type || typeof type !== "string") throw new Error("A block needs a type")
43
135
  if (!name) throw new Error(`Block ${type} needs a name`)
44
- if (typeof render?.mjml !== "function") throw new Error(`Block ${type} needs render.mjml(node, ctx)`)
45
- if (type !== "root" && !(definition.roles || []).length) throw new Error(`Block ${type} needs roles, e.g. ["content"]`)
136
+ if (!Object.values(render || {}).some((hook) => typeof hook === "function")) throw new Error(`Block ${type} needs render.mjml(node, ctx) or render.html(node, ctx)`)
137
+ if (!kits().some((kit) => kit.root === type) && !(definition.roles || []).length) throw new Error(`Block ${type} needs roles, e.g. ["content"]`)
46
138
 
47
139
  const palette = (definition.palette ?? (definition.category ? [{ key: type, label: name, icon: definition.icon, category: definition.category }] : [])).map((item) => ({
48
140
  icon: definition.icon,
@@ -51,7 +143,20 @@ export function registerBlock(definition) {
51
143
  ...item,
52
144
  type
53
145
  }))
54
- definitions.set(type, { roles: [], ...definition, palette })
146
+ definitions.set(type, { roles: [], kits: [DEFAULT_KIT], ...definition, palette })
147
+ return definitions.get(type)
148
+ }
149
+
150
+ // Adds kits, render hooks and field groups to a registered block
151
+ export function extendBlock(type, { kits = [], render = {}, fields = [] } = {}) {
152
+ const definition = definitions.get(type)
153
+ if (!definition) throw new Error(`No block ${type} to extend`)
154
+ definitions.set(type, {
155
+ ...definition,
156
+ kits: [...new Set([...definition.kits, ...kits])],
157
+ render: { ...definition.render, ...render },
158
+ fields: [...(definition.fields || []), ...fields]
159
+ })
55
160
  return definitions.get(type)
56
161
  }
57
162
 
@@ -67,18 +172,20 @@ export function isContainer(type) {
67
172
  return !!getBlock(type)?.accepts
68
173
  }
69
174
 
70
- // The block types that may go where the given roles are accepted.
71
- export function typesForRoles(roles) {
72
- return [...definitions.values()].filter((definition) => definition.roles.some((role) => roles.includes(role))).map((definition) => definition.type)
175
+ // The block types that may go where the given roles are accepted, of a kit's
176
+ // blocks when one is given.
177
+ export function typesForRoles(roles, kit) {
178
+ return [...definitions.values()].filter((definition) => inKit(definition, kit) && definition.roles.some((role) => roles.includes(role))).map((definition) => definition.type)
73
179
  }
74
180
 
75
181
  export function categories() {
76
182
  return [...categoryList.values()].sort((a, b) => a.order - b.order)
77
183
  }
78
184
 
79
- // Tiles for the Blocks panel, in registration order.
80
- export function paletteItems() {
81
- return [...definitions.values()].flatMap((definition) => definition.palette)
185
+ // Tiles for the Blocks panel, in registration order, of a kit's blocks when
186
+ // one is given.
187
+ export function paletteItems(kit) {
188
+ return [...definitions.values()].filter((definition) => inKit(definition, kit)).flatMap((definition) => definition.palette)
82
189
  }
83
190
 
84
191
  export function paletteItem(key) {
@@ -90,5 +197,6 @@ export function label(node) {
90
197
  return definition?.label?.(node) || definition?.name || node.type
91
198
  }
92
199
 
200
+ builtinKits.forEach(registerKit)
93
201
  builtinCategories.forEach(registerCategory)
94
202
  builtinBlocks.forEach(registerBlock)
@@ -9,9 +9,10 @@ import EditorController from "tess_edit/controllers/editor_controller"
9
9
  export { EditorController }
10
10
  export { toMjml } from "tess_edit/export/mjml"
11
11
  export { compile } from "tess_edit/export/compile"
12
- export { richTextToHtml } from "tess_edit/export/richtext"
13
- export { createDocument, createNode, normalize } from "tess_edit/core/document"
14
- export { registerBlock, registerCategory, getBlock, blockTypes, paletteItems } from "tess_edit/core/registry"
12
+ export { escapeAttribute, escapeText, richTextToHtml } from "tess_edit/export/richtext"
13
+ export { contentNode, createDocument, createNode, findType, normalize, renumber, withContent } from "tess_edit/core/document"
14
+ export { CONTENT_END, CONTENT_START } from "tess_edit/blocks/content"
15
+ export { registerBlock, registerCategory, registerKit, registerPaletteSource, extendBlock, getBlock, getKit, kitOf, kits, blockTypes, paletteItems, paletteSources } from "tess_edit/core/registry"
15
16
  export * as fields from "tess_edit/blocks/fields"
16
17
  export { imageField } from "tess_edit/core/media"
17
18
  export { starterDocument } from "tess_edit/core/starter"
@@ -0,0 +1,5 @@
1
+ // The built-in kit, registered by core/registry.js before the blocks.
2
+
3
+ import email from "tess_edit/kits/email"
4
+
5
+ export const builtinKits = [email]
@@ -0,0 +1,28 @@
1
+ // The email kit: the document becomes MJML in the browser, which the server
2
+ // compiles to HTML with MRML (export/compile.js), for the canvas and the
3
+ // html output alike. Liquid stays in both, for rendering at send time.
4
+
5
+ import { toMjml } from "tess_edit/export/mjml"
6
+ import { compile } from "tess_edit/export/compile"
7
+ import { starterDocument } from "tess_edit/core/starter"
8
+
9
+ export default {
10
+ key: "email",
11
+ name: "Email",
12
+ root: "email",
13
+ starter: starterDocument,
14
+ devices: [
15
+ { key: "desktop", icon: "desktop", title: "Desktop" },
16
+ { key: "mobile", icon: "mobile", title: "Mobile", width: "375px" }
17
+ ],
18
+ previewTitle: "Preview the email as sent, with Liquid tags",
19
+ canvas: (doc, { editor, compileUrl, signal }) => compile(toMjml(doc, { editor }), { url: compileUrl, signal }),
20
+ async outputs(doc, { compileUrl, signal }) {
21
+ const mjml = toMjml(doc)
22
+ const result = await compile(mjml, { url: compileUrl, signal })
23
+ if (!result) return null
24
+ return { outputs: { mjml, html: result.html || "" }, errors: result.errors }
25
+ },
26
+ notes: { mjml: "Liquid loops and conditions are in <mj-raw> tags.", html: "Liquid is left in place for rendering at send time." },
27
+ errors: "The email could not be compiled"
28
+ }
@@ -1,13 +1,15 @@
1
1
  // The Blocks panel: draggable tiles, grouped by category. Clicking a tile adds
2
- // it after the selected block (or at the end of the page) when that is allowed.
2
+ // it after the selected block (or at the end of the document) when that is
3
+ // allowed. The tiles of the host's palette sources (core/registry.js) follow,
4
+ // a list per source, to drag or click in the same way.
3
5
 
4
- import { categories, paletteItems } from "tess_edit/core/registry"
6
+ import { categories, getBlock } from "tess_edit/core/registry"
5
7
  import { h } from "tess_edit/ui/dom"
6
8
  import { icon } from "tess_edit/ui/icons"
7
9
 
8
10
  export function renderBlocksPanel(editor, element) {
9
- const items = paletteItems()
10
- const groups = categories().map((category) =>
11
+ const items = editor.paletteItems()
12
+ const groups = categories().filter((category) => items.some((item) => item.category === category.key)).map((category) =>
11
13
  h(
12
14
  "section",
13
15
  { class: "px-3 pt-3" },
@@ -40,5 +42,52 @@ export function renderBlocksPanel(editor, element) {
40
42
  )
41
43
  )
42
44
  )
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 %}."))
45
+ element.replaceChildren(...groups, ...sourceSections(editor), 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 %}."))
46
+ }
47
+
48
+ const SEARCH_FROM = 8
49
+
50
+ function sourceSections(editor) {
51
+ return [...editor.sources.values()].map(({ source, items, error }) => {
52
+ const rows = h("div", { class: "space-y-1" })
53
+ const show = (query = "") => {
54
+ const words = query.toLowerCase().split(/\s+/).filter(Boolean)
55
+ const matches = items.filter((item) => words.every((word) => `${item.label} ${item.key} ${item.description || ""}`.toLowerCase().includes(word)))
56
+ rows.replaceChildren(
57
+ ...matches.map((item) =>
58
+ h(
59
+ "button",
60
+ {
61
+ type: "button",
62
+ draggable: true,
63
+ title: item.description || "Drag onto the canvas, or click to add",
64
+ "data-tess-edit-source-item": item.key,
65
+ class: "flex w-full items-center gap-2 rounded-md border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 px-2 py-1.5 text-left text-[13px] text-gray-700 dark:text-gray-300 hover:border-blue-400 hover:text-blue-600 dark:hover:text-blue-400 cursor-grab active:cursor-grabbing",
66
+ ondragstart: (event) => {
67
+ event.dataTransfer.effectAllowed = "copy"
68
+ event.dataTransfer.setData("text/plain", item.key)
69
+ editor.startDrag({ kind: "source", source, item, type: item.type })
70
+ },
71
+ ondragend: () => editor.endDrag(),
72
+ onclick: () => editor.insertFromSource(source, item)
73
+ },
74
+ h("span", { class: "text-gray-400 dark:text-gray-500", html: icon(item.icon || source.icon || getBlock(item.type)?.icon) }),
75
+ h("span", { class: "min-w-0 flex-1 truncate" }, item.label || item.key),
76
+ item.version ? h("span", { class: "text-[11px] text-gray-400 dark:text-gray-500" }, `v${item.version}`) : null
77
+ )
78
+ ),
79
+ ...(matches.length ? [] : [h("p", { class: "text-xs text-gray-500 dark:text-gray-400" }, items.length ? "Nothing matches." : "There's nothing here yet.")])
80
+ )
81
+ }
82
+ show()
83
+ const search = items.length >= SEARCH_FROM ? h("input", { type: "search", placeholder: `Find in ${source.label}`, class: "mb-2 w-full rounded-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 px-2 py-1 text-[13px]", oninput: (event) => show(event.target.value) }) : null
84
+ return h(
85
+ "section",
86
+ { class: "px-3 pt-3", "data-tess-edit-source": source.key },
87
+ h("h3", { class: "mb-2 text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400" }, source.label),
88
+ error ? h("p", { class: "text-xs text-red-600 dark:text-red-400" }, error) : null,
89
+ search,
90
+ rows
91
+ )
92
+ })
44
93
  }
@@ -4,7 +4,7 @@
4
4
  // editor-mode MJML puts on every element. With the host's translations, t
5
5
  // filters show their text in the editor's language (see annotate).
6
6
 
7
- import { ancestors, canInsert, find, isDescendant, isHorizontal } from "tess_edit/core/document"
7
+ import { ancestors, find, isDescendant, isHorizontal } from "tess_edit/core/document"
8
8
  import { imageFields } from "tess_edit/core/media"
9
9
  import { getBlock, label } from "tess_edit/core/registry"
10
10
  import { missingArguments, textParts, translationCalls } from "tess_edit/core/translations"
@@ -81,7 +81,9 @@ export class Canvas {
81
81
  doc.head.append(style)
82
82
  this.overlay = doc.createElement("div")
83
83
  this.overlay.id = "tess-overlay"
84
- doc.body.append(this.overlay)
84
+ // Outside the body, which a kit may position (a sheet of paper), so the
85
+ // overlay's coordinates are the document's
86
+ doc.documentElement.append(this.overlay)
85
87
  this.win.scrollTo(0, scrollY)
86
88
  if (mode === "edit") {
87
89
  this.listen()
@@ -191,7 +193,7 @@ export class Canvas {
191
193
  rectOf(id) {
192
194
  const hit = find(this.editor.doc, id)
193
195
  if (!hit) return null
194
- if (hit.node.type === "root") {
196
+ if (hit.node === this.editor.doc) {
195
197
  const body = this.doc.body
196
198
  const rect = body.getBoundingClientRect()
197
199
  return { left: rect.left, top: rect.top, right: rect.right, bottom: rect.top + body.scrollHeight, width: rect.width, height: body.scrollHeight }
@@ -233,7 +235,7 @@ export class Canvas {
233
235
  if (this.hoverId && this.hoverId !== selectedId) {
234
236
  const hit = find(this.editor.doc, this.hoverId)
235
237
  const rect = hit && this.rectOf(this.hoverId)
236
- if (rect && hit.node.type !== "root") {
238
+ if (rect && hit.node !== this.editor.doc) {
237
239
  const box = this.box(rect, "tess-hover")
238
240
  box.append(Object.assign(this.doc.createElement("div"), { className: "tess-label", textContent: getBlock(hit.node.type)?.name || hit.node.type }))
239
241
  this.overlay.append(box)
@@ -241,12 +243,12 @@ export class Canvas {
241
243
  }
242
244
 
243
245
  const hit = selectedId && find(this.editor.doc, selectedId)
244
- if (!hit || hit.node.type === "root") return
246
+ if (!hit || hit.node === this.editor.doc) return
245
247
  const rect = this.rectOf(selectedId)
246
248
  if (!rect) return
247
249
  const box = this.box(rect, editingId === selectedId ? "tess-selected tess-editing" : "tess-selected")
248
250
  const free = this.editor.isFree(selectedId)
249
- box.append(Object.assign(this.doc.createElement("div"), { className: "tess-label", textContent: editingId === selectedId ? "Editing text · Esc to finish" : free ? label(hit.node) : `${label(hit.node)} · Layout` }))
251
+ box.append(Object.assign(this.doc.createElement("div"), { className: "tess-label", textContent: editingId === selectedId ? "Editing text · Esc to finish" : free || !this.editor.lock ? label(hit.node) : `${label(hit.node)} · Layout` }))
250
252
 
251
253
  if (editingId !== selectedId) {
252
254
  const tools = this.doc.createElement("div")
@@ -384,13 +386,14 @@ export class Canvas {
384
386
  }
385
387
 
386
388
  // The nearest block, from the one under the pointer outwards, with an image
387
- // field that can be edited
389
+ // field that can be edited. The root's (e.g. a page background) only when
390
+ // dropped on the page itself, not on a block in it.
388
391
  imageTarget(target) {
389
392
  if (!this.editor.media.enabled) return null
390
393
  const hit = this.hit(target)
391
394
  if (!hit) return null
392
395
  const doc = this.editor.doc
393
- for (const node of [find(doc, hit.id).node, ...ancestors(doc, hit.id)]) {
396
+ for (const node of [find(doc, hit.id).node, ...ancestors(doc, hit.id).filter((node) => node !== doc)]) {
394
397
  const field = imageFields(getBlock(node.type)).find((field) => this.editor.canEditField(node.id, field.key))
395
398
  if (field) return { id: node.id, field }
396
399
  }
@@ -425,7 +428,7 @@ export class Canvas {
425
428
 
426
429
  allowed(parentId, drag) {
427
430
  const doc = this.editor.doc
428
- if (!this.editor.canFill(parentId) || !canInsert(doc, parentId, drag.type)) return false
431
+ if (!this.editor.canInsert(parentId, drag.type, { moving: drag.kind !== "new" })) return false
429
432
  if (drag.kind === "move" && (parentId === drag.id || isDescendant(doc, drag.id, parentId))) return false
430
433
  return true
431
434
  }
@@ -10,6 +10,7 @@ const paths = {
10
10
  heading: '<path d="M6 4v16M18 4v16M6 12h12"/>',
11
11
  image: '<rect x="3" y="4" width="18" height="16" rx="2"/><circle cx="9" cy="10" r="2"/><path d="m21 16-5-5-9 9"/>',
12
12
  button: '<rect x="3" y="8" width="18" height="8" rx="4"/><path d="M9 12h6"/>',
13
+ content: '<rect x="3" y="4" width="18" height="16" rx="2" stroke-dasharray="3 2"/><path d="M7 9h10M7 13h10M7 17h6"/>',
13
14
  divider: '<path d="M3 12h18"/><path d="M8 7h8M8 17h8" opacity=".4"/>',
14
15
  spacer: '<path d="M12 4v16M8 8l4-4 4 4M8 16l4 4 4-4"/>',
15
16
  social: '<circle cx="6" cy="12" r="2.5"/><circle cx="18" cy="6" r="2.5"/><circle cx="18" cy="18" r="2.5"/><path d="m8.2 10.9 7.6-3.8M8.2 13.1l7.6 3.8"/>',
@@ -78,7 +78,8 @@ export class Inspector {
78
78
  h("div", { class: "ml-auto flex items-center gap-0.5" }, actions)
79
79
  )
80
80
 
81
- const groups = (definition.fields || []).map((group) => this.group(node, group))
81
+ // A group shows when one of its fields applies (see visible below)
82
+ const groups = (definition.fields || []).filter((group) => group.fields.some((field) => !field.visible || field.visible(node, this.editor))).map((group) => this.group(node, group))
82
83
  const locked = this.editor.lock && node.id !== doc.id && !this.editor.isFree(node.id)
83
84
  ? 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")
84
85
  : null
@@ -164,7 +165,8 @@ export class Inspector {
164
165
  return h(
165
166
  "select",
166
167
  { class: `${INPUT} pr-7`, onchange: (event) => set(event.target.value) },
167
- field.options.map((option) => {
168
+ // options can depend on the editor's settings: options(editor)
169
+ (typeof field.options === "function" ? field.options(this.editor) : field.options).map((option) => {
168
170
  const optionValue = typeof option === "string" ? option : option.value
169
171
  const optionLabel = typeof option === "string" ? option || "Default" : option.label
170
172
  return h("option", { value: optionValue, selected: optionValue === value }, optionLabel)
@@ -1,7 +1,7 @@
1
1
  // The Layers panel: the document as a collapsible tree that can be reordered by
2
2
  // dragging. Rows also take blocks dragged from the Blocks panel.
3
3
 
4
- import { canInsert, find, isDescendant } from "tess_edit/core/document"
4
+ import { find, isDescendant } from "tess_edit/core/document"
5
5
  import { getBlock, isContainer, label } from "tess_edit/core/registry"
6
6
  import { h } from "tess_edit/ui/dom"
7
7
  import { icon } from "tess_edit/ui/icons"
@@ -93,7 +93,7 @@ export class Tree {
93
93
  const drag = this.editor.drag
94
94
  if (!drag) return null
95
95
  const doc = this.editor.doc
96
- const ok = (parentId) => this.editor.canFill(parentId) && canInsert(doc, parentId, drag.type) && !(drag.kind === "move" && (parentId === drag.id || isDescendant(doc, drag.id, parentId)))
96
+ const ok = (parentId) => this.editor.canInsert(parentId, drag.type, { moving: drag.kind !== "new" }) && !(drag.kind === "move" && (parentId === drag.id || isDescendant(doc, drag.id, parentId)))
97
97
  const rect = event.currentTarget.getBoundingClientRect()
98
98
  const ratio = (event.clientY - rect.top) / rect.height
99
99
  const hit = find(doc, node.id)
@@ -0,0 +1,62 @@
1
+ module TessEdit
2
+ # A document's own content in its layout. The layout's Content block marks
3
+ # its place in the MJML or HTML with two comments (in <mj-raw> tags in MJML):
4
+ #
5
+ # <!-- tess-edit:content --> … the blocks inside the Content block … <!-- /tess-edit:content -->
6
+ #
7
+ # A document stores its own blocks; the host puts them in the layout's
8
+ # document for the editor (document_with), and takes them out again when it
9
+ # saves (nodes). The layout's output with the document's output between the
10
+ # markers (merge) is the whole document, also after the layout changed.
11
+ module Content
12
+ START = "<!-- tess-edit:content -->"
13
+ FINISH = "<!-- /tess-edit:content -->"
14
+ MJML_START = "<mj-raw>#{START}</mj-raw>"
15
+ MJML_FINISH = "<mj-raw>#{FINISH}</mj-raw>"
16
+ MARKED = /(#{Regexp.escape(MJML_START)}|#{Regexp.escape(START)})(.*?)(#{Regexp.escape(MJML_FINISH)}|#{Regexp.escape(FINISH)})/m
17
+
18
+ class Error < StandardError; end
19
+
20
+ module_function
21
+
22
+ # What is between the markers
23
+ def content_of(output)
24
+ matches = output.to_s.scan(MARKED)
25
+ raise Error, "The output has #{matches.size} Content blocks, it needs exactly one" unless matches.size == 1
26
+
27
+ matches.first[1]
28
+ end
29
+
30
+ # The layout's output with this content between its markers
31
+ def merge(layout_output, content)
32
+ content_of(layout_output)
33
+ layout_output.sub(MARKED) { "#{$1}#{content}#{$3}" }
34
+ end
35
+
36
+ # The children of the document's Content block, nil when it has none
37
+ def nodes(document)
38
+ content_node(document)&.fetch("children", [])
39
+ end
40
+
41
+ # The layout's document with these nodes in its Content block
42
+ def document_with(layout_document, nodes)
43
+ document = layout_document.deep_dup
44
+ content = content_node(document)
45
+ raise Error, "The layout has no Content block" unless content
46
+
47
+ content["children"] = nodes || []
48
+ document
49
+ end
50
+
51
+ def content_node(node)
52
+ return nil unless node.is_a?(Hash)
53
+ return node if node["type"] == "content"
54
+
55
+ Array(node["children"]).each do |child|
56
+ found = content_node(child)
57
+ return found if found
58
+ end
59
+ nil
60
+ end
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module TessEdit
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/tess_edit.rb CHANGED
@@ -3,6 +3,7 @@ require "stimulus-rails"
3
3
  require "mrml"
4
4
  require "tess_edit/version"
5
5
  require "tess_edit/engine"
6
+ require "tess_edit/content"
6
7
 
7
8
  module TessEdit
8
9
  Result = Data.define(:html, :errors)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tess_edit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom de Grunt
@@ -84,14 +84,15 @@ files:
84
84
  - app/javascript/tess_edit/blocks/button.js
85
85
  - app/javascript/tess_edit/blocks/column.js
86
86
  - app/javascript/tess_edit/blocks/condition.js
87
+ - app/javascript/tess_edit/blocks/content.js
87
88
  - app/javascript/tess_edit/blocks/divider.js
89
+ - app/javascript/tess_edit/blocks/email.js
88
90
  - app/javascript/tess_edit/blocks/fields.js
89
91
  - app/javascript/tess_edit/blocks/hero.js
90
92
  - app/javascript/tess_edit/blocks/image.js
91
93
  - app/javascript/tess_edit/blocks/loop.js
92
94
  - app/javascript/tess_edit/blocks/navbar.js
93
95
  - app/javascript/tess_edit/blocks/raw.js
94
- - app/javascript/tess_edit/blocks/root.js
95
96
  - app/javascript/tess_edit/blocks/section.js
96
97
  - app/javascript/tess_edit/blocks/social.js
97
98
  - app/javascript/tess_edit/blocks/spacer.js
@@ -110,6 +111,8 @@ files:
110
111
  - app/javascript/tess_edit/export/mjml.js
111
112
  - app/javascript/tess_edit/export/richtext.js
112
113
  - app/javascript/tess_edit/index.js
114
+ - app/javascript/tess_edit/kits/builtin.js
115
+ - app/javascript/tess_edit/kits/email.js
113
116
  - app/javascript/tess_edit/ui/blocks_panel.js
114
117
  - app/javascript/tess_edit/ui/canvas.js
115
118
  - app/javascript/tess_edit/ui/code_input.js
@@ -126,6 +129,7 @@ files:
126
129
  - config/routes.rb
127
130
  - lib/tasks/tess_edit_tasks.rake
128
131
  - lib/tess_edit.rb
132
+ - lib/tess_edit/content.rb
129
133
  - lib/tess_edit/engine.rb
130
134
  - lib/tess_edit/version.rb
131
135
  - vendor/javascript/tess_edit/tiptap.js