tess_edit 0.5.18 → 0.6.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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +103 -68
  3. data/app/controllers/tess_edit/compilations_controller.rb +6 -3
  4. data/app/javascript/tess_edit/blocks/assign.js +2 -5
  5. data/app/javascript/tess_edit/blocks/builtin.js +6 -9
  6. data/app/javascript/tess_edit/blocks/button.js +1 -4
  7. data/app/javascript/tess_edit/blocks/column.js +1 -4
  8. data/app/javascript/tess_edit/blocks/condition.js +2 -5
  9. data/app/javascript/tess_edit/blocks/content.js +4 -11
  10. data/app/javascript/tess_edit/blocks/divider.js +1 -4
  11. data/app/javascript/tess_edit/blocks/image.js +1 -4
  12. data/app/javascript/tess_edit/blocks/loop.js +2 -5
  13. data/app/javascript/tess_edit/blocks/raw.js +1 -10
  14. data/app/javascript/tess_edit/blocks/section.js +1 -5
  15. data/app/javascript/tess_edit/blocks/spacer.js +1 -4
  16. data/app/javascript/tess_edit/blocks/table.js +8 -15
  17. data/app/javascript/tess_edit/blocks/text.js +1 -4
  18. data/app/javascript/tess_edit/controllers/editor_controller.js +49 -24
  19. data/app/javascript/tess_edit/core/document.js +8 -8
  20. data/app/javascript/tess_edit/core/media.js +1 -1
  21. data/app/javascript/tess_edit/core/registry.js +40 -26
  22. data/app/javascript/tess_edit/export/compile.js +15 -6
  23. data/app/javascript/tess_edit/export/richtext.js +1 -1
  24. data/app/javascript/tess_edit/export/table.js +9 -7
  25. data/app/javascript/tess_edit/index.js +2 -3
  26. data/app/javascript/tess_edit/ui/canvas.js +5 -5
  27. data/config/locales/en.yml +1 -39
  28. data/config/locales/nl.yml +1 -39
  29. data/lib/tess_edit/content.rb +5 -4
  30. data/lib/tess_edit/version.rb +1 -1
  31. data/lib/tess_edit.rb +34 -8
  32. metadata +1 -24
  33. data/app/javascript/tess_edit/blocks/email.js +0 -83
  34. data/app/javascript/tess_edit/blocks/hero.js +0 -60
  35. data/app/javascript/tess_edit/blocks/navbar.js +0 -49
  36. data/app/javascript/tess_edit/blocks/social.js +0 -39
  37. data/app/javascript/tess_edit/blocks/wrapper.js +0 -24
  38. data/app/javascript/tess_edit/core/starter.js +0 -89
  39. data/app/javascript/tess_edit/export/mjml.js +0 -117
  40. data/app/javascript/tess_edit/kits/builtin.js +0 -5
  41. data/app/javascript/tess_edit/kits/email.js +0 -28
@@ -27,7 +27,6 @@ export default {
27
27
  fields: [
28
28
  background,
29
29
  imageField("attributes.background-url", "Background image", "data.background"),
30
- { key: "attributes.full-width", label: "Full width", type: "toggle", on: "full-width", off: "", visible: (node, editor) => editor?.kit?.key === "email" },
31
30
  { key: "attributes.direction", label: "Column order", type: "segmented", options: [{ value: "ltr", label: "Left to right" }, { value: "rtl", label: "Right to left" }] },
32
31
  { key: "attributes.text-align", label: "Align", type: "segmented", options: ALIGN },
33
32
  padding
@@ -35,8 +34,5 @@ export default {
35
34
  },
36
35
  border
37
36
  ],
38
- palette: layouts.map(([key, label, icon, widths]) => ({ key, label, icon, category: "layout", create: ({ createNode }) => section(createNode, widths) })),
39
- render: {
40
- mjml: (node, { container }) => container("mj-section", node)
41
- }
37
+ palette: layouts.map(([key, label, icon, widths]) => ({ key, label, icon, category: "layout", create: ({ createNode }) => section(createNode, widths) }))
42
38
  }
@@ -5,8 +5,5 @@ export default {
5
5
  category: "content",
6
6
  roles: ["content"],
7
7
  defaults: () => ({ attributes: { height: "24px" } }),
8
- fields: [{ title: "Spacer", fields: [{ key: "attributes.height", label: "Height", type: "size" }, { key: "attributes.container-background-color", label: "Background", type: "color" }] }],
9
- render: {
10
- mjml: (node, { element }) => element("mj-spacer", node)
11
- }
8
+ fields: [{ title: "Spacer", fields: [{ key: "attributes.height", label: "Height", type: "size" }, { key: "attributes.container-background-color", label: "Background", type: "color" }] }]
12
9
  }
@@ -7,14 +7,14 @@
7
7
  // selected cell, whose Table group also adds and deletes rows and columns
8
8
  // (core/table.js, the "table" field in ui/inspector.js).
9
9
  //
10
- // In an email the table is an mj-table, which MJML copies as it is, so its
11
- // cells' blocks are HTML (export/table.js), not MJML. Another kit takes them
12
- // in with extendBlock and its own render hook, as a host's PDF kit does.
10
+ // A kit takes them in with extendBlock and its own render hooks. The table's
11
+ // HTML, with its rows and cells, is export/table.js's tableHtml, which an
12
+ // email's mj-table and a note's <table> both use; a row and a cell render
13
+ // nothing of their own there.
13
14
 
14
15
  import { ALIGN, VERTICAL_ALIGN, background, padding } from "tess_edit/blocks/fields"
15
16
  import { plainText } from "tess_edit/blocks/text"
16
17
  import { newTable } from "tess_edit/core/table"
17
- import { tableHtml } from "tess_edit/export/table"
18
18
 
19
19
  const tableActions = { key: "data", label: "", type: "table" }
20
20
 
@@ -23,7 +23,7 @@ export const table = {
23
23
  name: "Table",
24
24
  icon: '<rect x="3" y="5" width="18" height="14" rx="1"/><path d="M3 10h18M3 15h18M9 10v9M15 10v9"/>',
25
25
  category: "layout",
26
- // Content, as Text is: in an email inside a column, as an mj-table must be
26
+ // Content, as Text is
27
27
  roles: ["content"],
28
28
  accepts: ["row"],
29
29
  defaults: () => ({ attributes: { padding: "10px 25px" }, data: { columns: [], borderColor: "#d1d5db", borderWidth: "1px", cellPadding: "6px 8px", headingBackground: "#f3f4f6" } }),
@@ -50,10 +50,7 @@ export const table = {
50
50
  ]
51
51
  }
52
52
  ],
53
- hint: "Rows of cells. Select a cell to add rows and columns. Put rows in a Loop to repeat them for each item.",
54
- render: {
55
- mjml: (node, ctx) => ctx.element("mj-table", node, tableHtml(node, ctx))
56
- }
53
+ hint: "Rows of cells. Select a cell to add rows and columns. Put rows in a Loop to repeat them for each item."
57
54
  }
58
55
 
59
56
  export const tableRow = {
@@ -76,9 +73,7 @@ export const tableRow = {
76
73
  ]
77
74
  }
78
75
  ],
79
- label: (node) => (node.data.heading ? "Heading row" : "Row"),
80
- // In its table's (export/table.js)
81
- render: { mjml: () => "" }
76
+ label: (node) => (node.data.heading ? "Heading row" : "Row")
82
77
  }
83
78
 
84
79
  export const tableCell = {
@@ -106,7 +101,5 @@ export const tableCell = {
106
101
  label(node) {
107
102
  const text = (node.children || []).map((child) => plainText(child.data?.content)).join(" ").trim()
108
103
  return text ? `Cell · ${text.slice(0, 32)}` : "Cell"
109
- },
110
- // In its table's (export/table.js)
111
- render: { mjml: () => "" }
104
+ }
112
105
  }
@@ -34,8 +34,5 @@ export default {
34
34
  category: "content",
35
35
  create: ({ createNode }) => createNode("text", { attributes: { "font-size": "24px", "font-weight": "700", "line-height": "1.3" }, data: { content: doc("Heading") } })
36
36
  }
37
- ],
38
- render: {
39
- mjml: (node, { element, richText }) => element("mj-text", node, richText(node.data.content))
40
- }
37
+ ]
41
38
  }
@@ -1,8 +1,8 @@
1
1
  // <div data-controller="tess-edit"> … </div>
2
2
  //
3
3
  // Values:
4
- // kit what the editor makes: "email" (the default), or a kit the host
5
- // registered (see core/registry.js)
4
+ // kit what the editor makes: the key of a kit the host registered
5
+ // (see core/registry.js); may be left out when there is one
6
6
  // document JSON of the document to load (falls back to the json input, then a sample)
7
7
  // sample load the sample document when there is nothing else (default true)
8
8
  // frame a border with rounded corners around the editor (default true); a
@@ -15,8 +15,9 @@
15
15
  // below the canvas, shown and hidden from the top bar and opened
16
16
  // by a tap on a block. A kit without blocks (blocks: false) has
17
17
  // Inspect on the right only, or in the sheet.
18
- // compileUrl the endpoint that compiles the email kit's MJML to HTML (default
19
- // /tess_edit/compile, the engine mounted at /tess_edit)
18
+ // compileUrl the engine's compile endpoint, for a kit that compiles on the
19
+ // server (default /tess_edit/compile, the engine mounted at
20
+ // /tess_edit, see export/compile.js)
20
21
  // mediaUrl the host's images, chosen and uploaded in Image blocks (see
21
22
  // core/media.js); without it images only have a URL
22
23
  // variablesUrl the variables a template can use, suggested while typing {{
@@ -44,23 +45,24 @@
44
45
  // editor.options, e.g. the choices of a select field
45
46
  // (options: (editor) => editor.options.parties)
46
47
  // Targets (all optional, usually hidden inputs in a form):
47
- // jsonInput, mjmlInput, htmlInput kept up to date with every change, with
48
- // the kit's outputs (an <output>Input target for each)
48
+ // jsonInput, htmlInput, … kept up to date with every change: the JSON and
49
+ // the kit's outputs, an <output>Input target for each
50
+ // (e.g. mjmlInput for an email's mjml)
49
51
  // slot an element of the host's, named by data-tess-edit-slot,
50
52
  // that a "slot" field shows in Inspect (see ui/inspector.js)
51
53
  // translations a <script type="application/json"> with the editor's words
52
54
  // in the page's language (see core/i18n.js)
53
55
  // Events:
54
- // tess-edit:change detail: { json, ...the kit's outputs, errors, document }:
55
- // for an email { json, mjml, html, errors, document }, with
56
- // html empty when the MJML could not be compiled and errors saying why
56
+ // tess-edit:change detail: { json, ...the kit's outputs, errors, document },
57
+ // e.g. { json, html, errors, document }, with errors saying
58
+ // why an output could not be made
57
59
 
58
60
  import { Controller } from "@hotwired/stimulus"
59
61
  import { accepted, ancestors, createDocument, createFromPalette, duplicate, find, findType, getValue, insert, itemType, misfit, move, normalize, remove, setValue, walk } from "tess_edit/core/document"
60
62
  import { History } from "tess_edit/core/history"
61
63
  import { setTranslations, t } from "tess_edit/core/i18n"
62
64
  import { canEditField, canFill, isFree, parseLock } from "tess_edit/core/lock"
63
- import { DEFAULT_KIT, getBlock, getKit, inlinePart, inlineParts, paletteItems, paletteSources } from "tess_edit/core/registry"
65
+ import { getBlock, getKit, kits, inlinePart, inlineParts, paletteItems, paletteSources } from "tess_edit/core/registry"
64
66
  import { COMPILE_URL } from "tess_edit/export/compile"
65
67
  import { IMAGE_FIELD, fetchMedia, uploadMedia } from "tess_edit/core/media"
66
68
  import { Translations, keysIn, suggestKey, toTranslation, translationTag } from "tess_edit/core/translations"
@@ -82,7 +84,7 @@ import { debounce, h } from "tess_edit/ui/dom"
82
84
  import { icon } from "tess_edit/ui/icons"
83
85
 
84
86
  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"
85
- const TAB = "px-3 h-12 text-[13px] font-medium border-b-2 -mb-px"
87
+ const TAB = "shrink-0 whitespace-nowrap px-3 h-12 text-[13px] font-medium border-b-2 -mb-px"
86
88
  const PANELS = ["left", "split", "right"]
87
89
  const PANELS_KEY = "tess_edit:panels"
88
90
  const TRANSLATION_LOCALE_KEY = "tess_edit:translation_locale"
@@ -99,15 +101,17 @@ function placed(node, at) {
99
101
  }
100
102
 
101
103
  export default class extends Controller {
102
- static values = { kit: { type: String, default: DEFAULT_KIT }, document: String, sample: { type: Boolean, default: true }, frame: { type: Boolean, default: true }, panels: { type: String, default: "split" }, compileUrl: { type: String, default: COMPILE_URL }, mediaUrl: String, variablesUrl: String, filtersUrl: String, tagsUrl: String, translationsUrl: String, values: String, lock: String, blocks: String, options: String }
103
- static targets = ["jsonInput", "mjmlInput", "htmlInput", "slot", "translations"]
104
+ static values = { kit: String, document: String, sample: { type: Boolean, default: true }, frame: { type: Boolean, default: true }, panels: { type: String, default: "split" }, compileUrl: { type: String, default: COMPILE_URL }, mediaUrl: String, variablesUrl: String, filtersUrl: String, tagsUrl: String, translationsUrl: String, values: String, lock: String, blocks: String, options: String }
105
+ static targets = ["jsonInput", "slot", "translations"]
104
106
 
105
107
  connect() {
106
108
  if (this.hasTranslationsTarget) setTranslations(this.parseJson(this.translationsTarget.textContent, "translations"))
107
- this.kit = getKit(this.kitValue)
109
+ // Without a kit, the one the host registered
110
+ this.kit = this.kitValue ? getKit(this.kitValue) : kits().length === 1 ? kits()[0] : null
108
111
  if (!this.kit) {
109
- console.error(`TessEdit: unknown kit ${this.kitValue}, editing an email instead`)
110
- this.kit = getKit(DEFAULT_KIT)
112
+ const problem = this.kitValue ? `there is no kit ${this.kitValue}` : kits().length ? "say which kit with data-tess-edit-kit-value" : "no kit is registered"
113
+ this.element.textContent = `TessEdit: ${problem} (see registerKit)`
114
+ throw new Error(`TessEdit: ${problem}`)
111
115
  }
112
116
  this.lock = parseLock(this.lockValue)
113
117
  this.allowedTypes = this.parseJson(this.blocksValue, "blocks")
@@ -361,7 +365,7 @@ export default class extends Controller {
361
365
  console.error("TessEdit: could not load the document", error)
362
366
  }
363
367
  }
364
- return this.sampleValue ? this.kit.starter() : createDocument(this.kit.key)
368
+ return this.sampleValue && this.kit.starter ? this.kit.starter() : createDocument(this.kit.key)
365
369
  }
366
370
 
367
371
  // --- public API -------------------------------------------------------------
@@ -383,7 +387,7 @@ export default class extends Controller {
383
387
  this.renderAll()
384
388
  }
385
389
 
386
- // The JSON and the kit's outputs, e.g. { json, mjml, html, errors } for an email
390
+ // The JSON and the kit's outputs, e.g. { json, html, errors }
387
391
  async export() {
388
392
  const { outputs, errors } = await this.kit.outputs(this.doc, { compileUrl: this.compileUrlValue })
389
393
  return { json: JSON.stringify(this.doc), ...outputs, errors }
@@ -433,7 +437,9 @@ export default class extends Controller {
433
437
  build() {
434
438
  this.blocksElement = h("div", { class: "flex-1 overflow-y-auto pb-4" })
435
439
  this.treeElement = h("div", { class: "flex-1 overflow-y-auto" })
436
- this.leftTabs = h("div", { class: "flex border-b border-gray-200 dark:border-gray-700 px-2 shrink-0" })
440
+ this.leftTabs = h("div", { class: "flex border-b border-gray-200 dark:border-gray-700 px-2 shrink-0 overflow-x-auto" })
441
+ // The kit's own panels (registerKit's panels), a tab each after Layers
442
+ this.kitPanels = (this.hasBlocks ? this.kit.panels || [] : []).map((panel) => ({ ...panel, element: h("div", { class: "hidden flex-1 overflow-y-auto min-h-0", dataset: { tessEditPanel: panel.key } }) }))
437
443
  this.inspectorElement = h("div", { class: "flex flex-1 flex-col min-h-0", dataset: { tessEditPanel: "inspect" } })
438
444
  this.sidePanel = h("aside")
439
445
  this.inspectPanel = h("aside")
@@ -460,6 +466,7 @@ export default class extends Controller {
460
466
  this.inspector = new Inspector(this, this.inspectorElement)
461
467
  this.textEditing = new TextEditing(this.textToolbar, { menu: this.variableMenu, editor: this })
462
468
  renderBlocksPanel(this, this.blocksElement)
469
+ for (const panel of this.kitPanels) panel.render?.(panel.element, this)
463
470
  this.observeWidth()
464
471
 
465
472
  // The insert menu closes on a mousedown outside it (here or in the canvas,
@@ -498,7 +505,7 @@ export default class extends Controller {
498
505
  if (this.main.nextSibling !== this.inspectPanel) this.main.after(this.inspectPanel)
499
506
  return
500
507
  }
501
- this.sidePanel.replaceChildren(this.leftTabs, this.blocksElement, this.treeElement)
508
+ this.sidePanel.replaceChildren(this.leftTabs, this.blocksElement, this.treeElement, ...this.kitPanels.map((panel) => panel.element))
502
509
  if (this.layout === "sheet") {
503
510
  this.sidePanel.className = sheetClass
504
511
  this.sidePanel.append(this.inspectorElement)
@@ -680,16 +687,26 @@ export default class extends Controller {
680
687
  // replaceChildren would turn a null into the text "null", so only pass real tabs.
681
688
  const tabs = [tab("blocks", t("Blocks")), tab("layers", t("Layers"))]
682
689
  if (this.combined) tabs.push(tab("inspect", t("Inspect")))
690
+ for (const panel of this.kitPanels) tabs.push(tab(panel.key, t(panel.label)))
683
691
  if (this.compact && this.hasBlocks) tabs.push(this.sheetClose)
684
692
  this.leftTabs.replaceChildren(...tabs)
685
693
  this.blocksElement.classList.toggle("hidden", this.leftTab !== "blocks")
686
694
  this.treeElement.classList.toggle("hidden", this.leftTab !== "layers")
687
695
  this.inspectorElement.classList.toggle("hidden", this.combined && this.leftTab !== "inspect")
696
+ for (const panel of this.kitPanels) panel.element.classList.toggle("hidden", this.leftTab !== panel.key)
697
+ }
698
+
699
+ // The kit's panel that is open, if one is
700
+ get panel() {
701
+ return this.kitPanels.some((panel) => panel.key === this.leftTab) ? this.leftTab : null
688
702
  }
689
703
 
690
704
  setLeftTab(tab) {
705
+ const panel = this.panel
691
706
  this.leftTab = tab
692
707
  this.renderLeftTabs()
708
+ // The canvas is told which of the kit's panels is open, so it draws again when that changes
709
+ if (panel !== this.panel) this.renderCanvas()
693
710
  }
694
711
 
695
712
  setMode(mode) {
@@ -713,8 +730,8 @@ export default class extends Controller {
713
730
 
714
731
  // --- rendering --------------------------------------------------------------
715
732
 
716
- // Runs one of the kit's renderings, which may go to the server (the email's
717
- // compiling). A newer run on the same channel aborts the older one, which
733
+ // Runs one of the kit's renderings, which may go to the server (a kit that
734
+ // compiles there). A newer run on the same channel aborts the older one, which
718
735
  // then resolves to null.
719
736
  async run(channel, render) {
720
737
  this.requests[channel]?.abort()
@@ -725,7 +742,7 @@ export default class extends Controller {
725
742
 
726
743
  async renderCanvas() {
727
744
  if (this.editingId) return
728
- const result = await this.run("canvas", (options) => this.kit.canvas(this.doc, { ...options, editor: this.mode === "edit" }))
745
+ const result = await this.run("canvas", (options) => this.kit.canvas(this.doc, { ...options, editor: this.mode === "edit", panel: this.panel }))
729
746
  if (!result || this.editingId) return
730
747
  // A canvas the host renders can give the values it has
731
748
  if (result.values) this.values.set(result.values)
@@ -759,6 +776,13 @@ export default class extends Controller {
759
776
  }))
760
777
  }
761
778
 
779
+ // The input of an output (jsonInput, htmlInput, …): a target named after
780
+ // it, which isn't declared, as a kit has outputs of its own
781
+ outputInput(name) {
782
+ const target = `${name}Input`
783
+ return [...this.element.querySelectorAll(`[data-${this.identifier}-target]`)].find((element) => element.getAttribute(`data-${this.identifier}-target`).split(/\s+/).includes(target))
784
+ }
785
+
762
786
  writeOutputs() {
763
787
  const writing = (this.writing = this.write().finally(() => {
764
788
  if (this.writing === writing) this.writing = null
@@ -773,7 +797,8 @@ export default class extends Controller {
773
797
  if (!result) return
774
798
  const outputs = { json, ...result.outputs }
775
799
  for (const [name, value] of Object.entries(outputs)) {
776
- if (this[`has${name[0].toUpperCase()}${name.slice(1)}InputTarget`]) this[`${name}InputTarget`].value = value
800
+ const input = this.outputInput(name)
801
+ if (input) input.value = value
777
802
  }
778
803
  this.dispatch("change", { detail: { ...outputs, errors: result.errors, document: this.doc } })
779
804
  }
@@ -1,13 +1,13 @@
1
1
  // The document is a plain JSON tree:
2
2
  // { id, type, attributes: {}, data: {}, children: [] }
3
- // `attributes` hold a block's styling, named after MJML's attributes (a kit
4
- // with other output maps them onto its own), `data` holds everything else
3
+ // `attributes` hold a block's styling, named after CSS properties and
4
+ // MJML's attributes (each kit maps them onto its own output), `data` holds everything else
5
5
  // (rich text, Liquid expressions, link lists). A block copied from elsewhere,
6
6
  // such as a host's reusable content, may also have `origin`: the id of the
7
7
  // block it is a copy of (see renumber). All functions here are pure apart
8
8
  // from the mutating helpers, which change the tree they are given in place.
9
9
 
10
- import { DEFAULT_KIT, getBlock, getKit, kitOf, paletteItem, typesForRoles } from "tess_edit/core/registry"
10
+ import { getBlock, getKit, kitOf, paletteItem, typesForRoles } from "tess_edit/core/registry"
11
11
 
12
12
  export const VERSION = 1
13
13
 
@@ -33,8 +33,8 @@ export function createNode(type, overrides = {}) {
33
33
  }
34
34
  }
35
35
 
36
- // An empty document of a kit (see core/registry.js), an email by default.
37
- export function createDocument(kit = DEFAULT_KIT) {
36
+ // An empty document of a kit (see core/registry.js)
37
+ export function createDocument(kit) {
38
38
  const definition = getKit(kit)
39
39
  if (!definition) throw new Error(`Unknown kit: ${kit}`)
40
40
  return { version: VERSION, ...createNode(definition.root) }
@@ -178,7 +178,7 @@ export function duplicate(doc, id) {
178
178
  // Sets a value on a node. `key` is "attributes.padding" or "data.subject",
179
179
  // or deeper into a list, "data.options.0.label" (an inline part of each item,
180
180
  // see `inline` in core/registry.js).
181
- // An empty string removes an attribute, so MJML falls back to its default.
181
+ // An empty string removes an attribute, so the kit's output falls back to its default.
182
182
  export function setValue(doc, id, key, value) {
183
183
  const hit = find(doc, id)
184
184
  if (!hit) return
@@ -203,10 +203,10 @@ export function getValue(node, key) {
203
203
  // `kit`, the document has to be one of that kit's.
204
204
  export function normalize(input, { kit } = {}) {
205
205
  const doc = typeof input === "string" ? JSON.parse(input) : clone(input)
206
- // The email's root block was called "page", then "root"; such documents still load.
207
- if (doc?.type === "page" || doc?.type === "root") doc.type = "email"
208
206
  const own = kitOf(doc)
209
207
  if (!own) throw new Error("Not a TessEdit document: the top node must be a root block")
208
+ // A root type the kit had before (its aliases) loads as its root
209
+ doc.type = own.root
210
210
  if (kit && own.key !== kit) throw new Error(`This is a document for the ${own.name} editor, not the ${getKit(kit)?.name || kit} one`)
211
211
  walk(doc, (node) => {
212
212
  if (!getBlock(node.type)) throw new Error(`Unknown block type: ${node.type}`)
@@ -3,7 +3,7 @@
3
3
  // GET → { images: [{ id, url, name }] }
4
4
  // POST multipart/form-data, "file" → { id, url, name }, or { errors: [...] }
5
5
  //
6
- // `id` is the host's own reference to the image, `url` is where the email
6
+ // `id` is the host's own reference to the image, `url` is where the document
7
7
  // loads it from. An image field (type "image") stores both: the URL in its
8
8
  // key, the id and where the image comes from in its `image` and `source` keys.
9
9
 
@@ -23,8 +23,8 @@
23
23
  // // editor, so the rest of the block stays as it is. A plain part
24
24
  // // is one line of text, a string; the others are Tiptap JSON
25
25
  // palette: [{ key, label, icon, category, create: ({ createNode }) => node }], // optional
26
- // kits: ["email"], // the kits it is part of, default ["email"]; [] for none
27
- // // yet: a kit takes it in with extendBlock, and its render hook
26
+ // kits: ["note"], // the kits it is part of, default none: a kit takes it
27
+ // // in with extendBlock, and its render hook
28
28
  // unique: true, // optional: at most one per document, like Content
29
29
  // layout: "free", // optional, on a container: its children are placed by
30
30
  // // data.x and data.y, dragged on the canvas (ui/free_layout.js)
@@ -33,13 +33,14 @@
33
33
  // resizeChildren: { width: "data.width" },
34
34
  // // optional, on a free container: the same for all of its children,
35
35
  // // or a function of the child; unit: "mm" keeps sizes as "52mm"
36
- // render: { mjml: (node, ctx) => "<mj-text …>" } // one per format of its kits:
37
- // // mjml (export/mjml.js), html (export/html.js)
36
+ // render: { note: (node, ctx) => "<div …>" } // one per kit it is part of, named after
37
+ // // the kit's key (or what its kit's renderer calls)
38
38
  // })
39
39
  //
40
- // Roles: "body" (inside the page or a wrapper: sections, heroes), "wrapper",
41
- // "column" (inside a section), "content" (inside a column or hero), and
42
- // "row" (inside a table) and "cell" (inside a row), see blocks/table.js. A
40
+ // Roles: "body" (inside a kit's root, or an email's wrapper: sections),
41
+ // "column" (inside a section), "content" (inside a column, or wherever a
42
+ // kit's own containers take it), and "row" (inside a table) and "cell"
43
+ // (inside a row), see blocks/table.js. A
43
44
  // container accepts the roles in its `accepts`; Loop and Condition use
44
45
  // "inherit" and accept what their nearest other container accepts.
45
46
  //
@@ -54,27 +55,41 @@
54
55
  // key: "note", // data-tess-edit-kit-value; blocks name it in `kits`
55
56
  // name: "Note", // shown in the top bar
56
57
  // root: "note", // the type of its root block, unique to the kit
57
- // starter: () => doc, // the sample document
58
+ // aliases: ["page"], // optional: root types its documents had before,
59
+ // // loaded as the kit's root
60
+ // starter: () => doc, // optional: the sample document
58
61
  // devices: [{ key, icon, title, width }], // optional: canvas widths in the top bar
59
62
  // previewTitle: "…", // the Preview button's title
60
- // canvas: async (doc, { editor, compileUrl, signal, options, element }) => ({ html, errors }),
63
+ // canvas: async (doc, { editor, compileUrl, signal, options, element, panel }) => ({ html, errors }),
61
64
  // // the canvas's HTML, with the tess-<id> and tessph-<id>
62
65
  // // classes when editor is true; null when aborted.
63
66
  // // options are the editor's (data-tess-edit-options-value),
64
67
  // // element its element, for a kit that reads its page;
65
68
  // // a link with data-tess-open opens in a new tab
69
+ // // panel is the key of the kit's panel that is open, or null
66
70
  // outputs: async (doc, { compileUrl, signal, options, element }) => ({ outputs: { html: "…" }, errors }),
67
71
  // // what the editor stores besides the JSON (the inputs,
68
72
  // // the change event and the Code dialog); null when aborted
73
+ // palette: ["text", "heading"], // optional: the order of the Blocks panel's tiles,
74
+ // // by key; tiles it leaves out follow in registration order
69
75
  // blocks: false, // optional: a kit of its root alone, such as a text
70
76
  // // message, has no Blocks and Layers panels
77
+ // panels: [{ key: "test", label: "Test", render: (element, editor) => {} }],
78
+ // // optional: panels of the kit's own, a tab each after
79
+ // // Layers (and Inspect, when it is a tab). render fills its
80
+ // // element once, when the editor is built; editor is the
81
+ // // editor (its doc, select, renderCanvas, and its element,
82
+ // // which sends tess-edit:change). The canvas is told which
83
+ // // one is open (panel) and drawn again when that changes;
84
+ // // a panel that changes what it shows calls
85
+ // // editor.renderCanvas(). label is translated (t)
71
86
  // notes: { html: "…" }, // optional: what each output is, in the Code dialog
72
- // errors: "The email could not be compiled" // shown before the reasons
87
+ // errors: "The note could not be rendered" // shown before the reasons
73
88
  // })
74
89
  //
75
- // The built-in kit is "email" (MJML, compiled to HTML on the server, see
76
- // kits/email.js). A host adds its own kit with its root block and blocks,
77
- // and brings TessEdit's blocks into it with extendBlock:
90
+ // TessEdit has no kit of its own: a host (or a gem, such as Nuntius with its
91
+ // email) registers one with its root block and blocks, and brings TessEdit's
92
+ // blocks into it with extendBlock:
78
93
  //
79
94
  // extendBlock("text", { kits: ["note"], render: { note: (node, ctx) => "<div>…</div>" } })
80
95
  //
@@ -84,7 +99,6 @@
84
99
 
85
100
  import { builtinBlocks, builtinCategories } from "tess_edit/blocks/builtin"
86
101
  import { t } from "tess_edit/core/i18n"
87
- import { builtinKits } from "tess_edit/kits/builtin"
88
102
 
89
103
  const definitions = new Map()
90
104
  const categoryList = new Map()
@@ -92,8 +106,6 @@ const kitList = new Map()
92
106
  const sourceList = new Map()
93
107
  const fieldTypeList = new Map()
94
108
 
95
- export const DEFAULT_KIT = "email"
96
-
97
109
  // What the editor says when a kit's canvas can't be rendered, unless the kit says it itself (errors)
98
110
  export const KIT_ERRORS = "The %{name} could not be rendered"
99
111
 
@@ -102,11 +114,11 @@ export function registerKit(kit) {
102
114
  if (!key || !name) throw new Error("A kit needs a key and a name")
103
115
  if (!root) throw new Error(`Kit ${key} needs a root block type`)
104
116
  if (typeof canvas !== "function" || typeof outputs !== "function") throw new Error(`Kit ${key} needs canvas(doc, options) and outputs(doc, options)`)
105
- kitList.set(key, { devices: null, notes: {}, errors: KIT_ERRORS, ...kit })
117
+ kitList.set(key, { devices: null, notes: {}, errors: KIT_ERRORS, aliases: [], panels: [], ...kit })
106
118
  return kitList.get(key)
107
119
  }
108
120
 
109
- export function getKit(key = DEFAULT_KIT) {
121
+ export function getKit(key) {
110
122
  return kitList.get(key)
111
123
  }
112
124
 
@@ -116,7 +128,7 @@ export function kits() {
116
128
 
117
129
  // The kit a document belongs to, from its root block
118
130
  export function kitOf(doc) {
119
- return kits().find((kit) => kit.root === doc?.type)
131
+ return kits().find((kit) => kit.root === doc?.type || kit.aliases.includes(doc?.type))
120
132
  }
121
133
 
122
134
  function inKit(definition, kit) {
@@ -161,8 +173,8 @@ export function registerBlock(definition) {
161
173
  if (!name) throw new Error(`Block ${type} needs a name`)
162
174
  // A block of no kit yet (kits: []), such as a Table, gets its render hook
163
175
  // from the kit that takes it in (extendBlock)
164
- const ownKits = definition.kits ?? [DEFAULT_KIT]
165
- if (ownKits.length && !Object.values(render || {}).some((hook) => typeof hook === "function")) throw new Error(`Block ${type} needs render.mjml(node, ctx) or render.html(node, ctx)`)
176
+ const ownKits = definition.kits ?? []
177
+ if (ownKits.length && !Object.values(render || {}).some((hook) => typeof hook === "function")) throw new Error(`Block ${type} needs a render hook for its kits, e.g. render.${ownKits[0]}(node, ctx)`)
166
178
  if (!kits().some((kit) => kit.root === type) && !(definition.roles || []).length) throw new Error(`Block ${type} needs roles, e.g. ["content"]`)
167
179
 
168
180
  const palette = (definition.palette ?? (definition.category ? [{ key: type, label: name, icon: definition.icon, category: definition.category }] : [])).map((item) => ({
@@ -174,7 +186,7 @@ export function registerBlock(definition) {
174
186
  }))
175
187
  const inline = definition.inline ?? (definition.richText ? [{ key: definition.richText }] : [])
176
188
  if (inline.some((part) => !part?.key)) throw new Error(`Block ${type} needs a key for each inline part`)
177
- definitions.set(type, { roles: [], kits: [DEFAULT_KIT], ...definition, palette, inline })
189
+ definitions.set(type, { roles: [], kits: [], render: {}, ...definition, palette, inline })
178
190
  return definitions.get(type)
179
191
  }
180
192
 
@@ -267,10 +279,13 @@ export function categories() {
267
279
  return [...categoryList.values()].sort((a, b) => a.order - b.order)
268
280
  }
269
281
 
270
- // Tiles for the Blocks panel, in registration order, of a kit's blocks when
271
- // one is given.
282
+ // Tiles for the Blocks panel, of a kit's blocks when one is given: in the
283
+ // order of the kit's `palette`, the others after them in registration order.
272
284
  export function paletteItems(kit) {
273
- return [...definitions.values()].filter((definition) => inKit(definition, kit)).flatMap((definition) => definition.palette)
285
+ const items = [...definitions.values()].filter((definition) => inKit(definition, kit)).flatMap((definition) => definition.palette)
286
+ const order = kitList.get(kit)?.palette || []
287
+ const rank = (item) => (order.includes(item.key) ? order.indexOf(item.key) : order.length)
288
+ return items.map((item, index) => ({ item, index })).sort((a, b) => rank(a.item) - rank(b.item) || a.index - b.index).map(({ item }) => item)
274
289
  }
275
290
 
276
291
  export function paletteItem(key) {
@@ -282,6 +297,5 @@ export function label(node) {
282
297
  return definition?.label?.(node) || t(definition?.name) || node.type
283
298
  }
284
299
 
285
- builtinKits.forEach(registerKit)
286
300
  builtinCategories.forEach(registerCategory)
287
301
  builtinBlocks.forEach(registerBlock)
@@ -1,20 +1,29 @@
1
- // MJML → HTML on the server. The editor posts the MJML to the engine's compile
2
- // endpoint, which compiles it with MRML (TessEdit.compile in Ruby), so the
3
- // canvas shows the same HTML the host app sends.
1
+ // Compiling on the server, for a kit whose output the browser can't make
2
+ // itself, such as an email's MJML that becomes HTML. The host (or a gem)
3
+ // registers a compiler by name in Ruby (TessEdit.register_compiler), and the
4
+ // kit posts its source to the engine's compile endpoint, POST /tess_edit/compile
5
+ // with { compiler, source }:
4
6
  //
5
- // Resolves to { html, errors }: html is null when the MJML could not be
7
+ // compile(mjml, { compiler: "mjml", url: compileUrl, signal })
8
+ //
9
+ // so the canvas shows the same HTML the host makes when it sends or saves.
10
+ //
11
+ // Resolves to { html, errors }: html is null when the source could not be
6
12
  // compiled or the request failed, with the reasons in errors. Resolves to null
7
13
  // when the request was aborted through options.signal.
8
14
 
15
+ // The engine mounted at /tess_edit; the editor's compileUrl value says
16
+ // otherwise, and a kit gets it as compileUrl
9
17
  export const COMPILE_URL = "/tess_edit/compile"
10
18
 
11
- export async function compile(mjml, { url = COMPILE_URL, signal } = {}) {
19
+ export async function compile(source, { compiler, url = COMPILE_URL, signal } = {}) {
20
+ if (!compiler) throw new Error("compile needs the name of a compiler")
12
21
  const token = document.querySelector("meta[name='csrf-token']")?.content
13
22
  try {
14
23
  const response = await fetch(url, {
15
24
  method: "POST",
16
25
  headers: { "Content-Type": "application/json", Accept: "application/json", ...(token ? { "X-CSRF-Token": token } : {}) },
17
- body: JSON.stringify({ mjml }),
26
+ body: JSON.stringify({ compiler, source }),
18
27
  credentials: "same-origin",
19
28
  signal
20
29
  })
@@ -1,4 +1,4 @@
1
- // Turns Tiptap/ProseMirror JSON into email-friendly HTML. Liquid ({{ … }} and
1
+ // Turns Tiptap/ProseMirror JSON into HTML with inline styles, as emails need. Liquid ({{ … }} and
2
2
  // {% … %}) is copied verbatim so quotes and comparison operators keep working.
3
3
 
4
4
  const LIQUID = /(\{\{[\s\S]*?\}\}|\{%[\s\S]*?%\})/g
@@ -1,13 +1,15 @@
1
- // A table (blocks/table.js) as HTML, for a kit whose tables are HTML: the
2
- // email's mj-table, which MJML copies as it is, so a cell's blocks are HTML
3
- // too, not MJML. Its styles are inline, as mail clients need: borders,
4
- // padding and heading rows on each cell.
1
+ // A table (blocks/table.js) as HTML, for a kit whose tables are HTML, such
2
+ // as an email's mj-table, which MJML copies as it is, so a cell's blocks are
3
+ // HTML too. Its styles are inline, as mail clients need: borders, padding
4
+ // and heading rows on each cell.
5
5
  //
6
- // tableHtml(node, { editor, richText, escapeText, escapeAttribute })
6
+ // tableHtml(node, { editor, richText, escapeText, escapeAttribute, cellBlock })
7
7
  // // → "<colgroup>…</colgroup><thead>…</thead><tbody>…</tbody>"
8
8
  //
9
9
  // A cell's blocks are rendered here (cellBlock): Text, Image, Button,
10
- // Divider, Spacer, Loop, Condition, Assign and tables; others show nothing.
10
+ // Divider, Spacer, Loop, Condition, Assign and tables. The kit's own blocks
11
+ // go through its cellBlock(node, ctx), if it gives one; without it they show
12
+ // nothing.
11
13
  // With editor, every element has its tess-<id> class, an empty container a
12
14
  // placeholder that fits there, and Liquid tags are left out, so a Loop's
13
15
  // rows show once.
@@ -157,6 +159,6 @@ export function cellBlock(node, ctx) {
157
159
  return ctx.editor ? children() : `${open}${children()}${close}`
158
160
  }
159
161
  default:
160
- return ""
162
+ return ctx.cellBlock?.(node, { ...ctx, style, classes: (block) => classes(block, ctx) }) || ""
161
163
  }
162
164
  }
@@ -7,15 +7,14 @@
7
7
  import EditorController from "tess_edit/controllers/editor_controller"
8
8
 
9
9
  export { EditorController }
10
- export { toMjml } from "tess_edit/export/mjml"
11
- export { compile } from "tess_edit/export/compile"
10
+ export { COMPILE_URL, compile } from "tess_edit/export/compile"
12
11
  export { escapeAttribute, escapeText, richTextToHtml } from "tess_edit/export/richtext"
12
+ export { tableHtml } from "tess_edit/export/table"
13
13
  export { contentNode, createDocument, createNode, findType, normalize, renumber, withContent } from "tess_edit/core/document"
14
14
  export { CONTENT_END, CONTENT_START } from "tess_edit/blocks/content"
15
15
  export { registerBlock, registerCategory, registerFieldType, registerKit, registerPaletteSource, extendBlock, getBlock, getKit, kitOf, kits, blockTypes, paletteItems, paletteSources } from "tess_edit/core/registry"
16
16
  export * as fields from "tess_edit/blocks/fields"
17
17
  export { imageField } from "tess_edit/core/media"
18
- export { starterDocument } from "tess_edit/core/starter"
19
18
  export { cellOf, tableOf } from "tess_edit/core/table"
20
19
 
21
20
  export function registerTessEdit(application, identifier = "tess-edit") {