tess_edit 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +83 -6
- data/app/javascript/tess_edit/blocks/builtin.js +3 -2
- data/app/javascript/tess_edit/blocks/button.js +3 -1
- data/app/javascript/tess_edit/blocks/column.js +3 -1
- data/app/javascript/tess_edit/blocks/condition.js +3 -1
- data/app/javascript/tess_edit/blocks/content.js +36 -0
- data/app/javascript/tess_edit/blocks/divider.js +3 -1
- data/app/javascript/tess_edit/blocks/{root.js → email.js} +3 -3
- data/app/javascript/tess_edit/blocks/fields.js +4 -2
- data/app/javascript/tess_edit/blocks/image.js +3 -1
- data/app/javascript/tess_edit/blocks/loop.js +3 -1
- data/app/javascript/tess_edit/blocks/section.js +4 -2
- data/app/javascript/tess_edit/blocks/spacer.js +3 -1
- data/app/javascript/tess_edit/blocks/text.js +3 -1
- data/app/javascript/tess_edit/controllers/editor_controller.js +153 -56
- data/app/javascript/tess_edit/core/document.js +66 -16
- data/app/javascript/tess_edit/core/registry.js +119 -11
- data/app/javascript/tess_edit/index.js +4 -3
- data/app/javascript/tess_edit/kits/builtin.js +5 -0
- data/app/javascript/tess_edit/kits/email.js +28 -0
- data/app/javascript/tess_edit/ui/blocks_panel.js +54 -5
- data/app/javascript/tess_edit/ui/canvas.js +12 -9
- data/app/javascript/tess_edit/ui/icons.js +1 -0
- data/app/javascript/tess_edit/ui/inspector.js +4 -2
- data/app/javascript/tess_edit/ui/tree.js +2 -2
- data/lib/tess_edit/content.rb +62 -0
- data/lib/tess_edit/version.rb +1 -1
- data/lib/tess_edit.rb +1 -0
- metadata +6 -2
|
@@ -1,6 +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
6
|
// document JSON of the document to load (falls back to the json input, then a sample)
|
|
5
7
|
// sample load the sample document when there is nothing else (default true)
|
|
6
8
|
// panels "split" (Blocks and Layers left, Inspect right), "left" or "right" (all panels
|
|
@@ -8,8 +10,8 @@
|
|
|
8
10
|
// top bar is remembered per browser. Below NARROW_WIDTH the editor
|
|
9
11
|
// puts all panels on the left, whatever the choice, to leave room
|
|
10
12
|
// for the canvas.
|
|
11
|
-
// compileUrl the endpoint that compiles MJML to HTML (default
|
|
12
|
-
// the engine mounted at /tess_edit)
|
|
13
|
+
// compileUrl the endpoint that compiles the email kit's MJML to HTML (default
|
|
14
|
+
// /tess_edit/compile, the engine mounted at /tess_edit)
|
|
13
15
|
// mediaUrl the host's images, chosen and uploaded in Image blocks (see
|
|
14
16
|
// core/media.js); without it images only have a URL
|
|
15
17
|
// variablesUrl the variables a template can use, suggested while typing {{
|
|
@@ -22,20 +24,25 @@
|
|
|
22
24
|
// lock JSON: only the insides of some blocks and some root fields can
|
|
23
25
|
// change, e.g. {"except": ["content"], "fields": ["data.subject"]}
|
|
24
26
|
// (see core/lock.js)
|
|
27
|
+
// blocks JSON: the only block types that can be added, e.g. ["text", "image"];
|
|
28
|
+
// without it, all of the kit's
|
|
29
|
+
// options JSON: anything a host's blocks need from the page, as
|
|
30
|
+
// editor.options, e.g. the choices of a select field
|
|
31
|
+
// (options: (editor) => editor.options.parties)
|
|
25
32
|
// Targets (all optional, usually hidden inputs in a form):
|
|
26
|
-
// jsonInput, mjmlInput, htmlInput kept up to date with every change
|
|
33
|
+
// jsonInput, mjmlInput, htmlInput kept up to date with every change, with
|
|
34
|
+
// the kit's outputs (an <output>Input target for each)
|
|
27
35
|
// Events:
|
|
28
|
-
// tess-edit:change detail: { json,
|
|
29
|
-
//
|
|
36
|
+
// tess-edit:change detail: { json, ...the kit's outputs, errors, document }:
|
|
37
|
+
// for an email { json, mjml, html, errors, document }, with
|
|
38
|
+
// html empty when the MJML could not be compiled and errors saying why
|
|
30
39
|
|
|
31
40
|
import { Controller } from "@hotwired/stimulus"
|
|
32
|
-
import { accepted, ancestors, createDocument, createFromPalette, duplicate, find, getValue, insert, itemType, move, normalize, remove, setValue, walk } from "tess_edit/core/document"
|
|
41
|
+
import { accepted, ancestors, createDocument, createFromPalette, duplicate, find, findType, getValue, insert, itemType, misfit, move, normalize, remove, setValue, walk } from "tess_edit/core/document"
|
|
33
42
|
import { History } from "tess_edit/core/history"
|
|
34
43
|
import { canEditField, canFill, isFree, parseLock } from "tess_edit/core/lock"
|
|
35
|
-
import { getBlock, paletteItems } from "tess_edit/core/registry"
|
|
36
|
-
import {
|
|
37
|
-
import { toMjml } from "tess_edit/export/mjml"
|
|
38
|
-
import { COMPILE_URL, compile } from "tess_edit/export/compile"
|
|
44
|
+
import { DEFAULT_KIT, getBlock, getKit, paletteItems, paletteSources } from "tess_edit/core/registry"
|
|
45
|
+
import { COMPILE_URL } from "tess_edit/export/compile"
|
|
39
46
|
import { IMAGE_FIELD, fetchMedia, uploadMedia } from "tess_edit/core/media"
|
|
40
47
|
import { Translations, keysIn, suggestKey, toTranslation, translationTag } from "tess_edit/core/translations"
|
|
41
48
|
import { Variables, loopScope } from "tess_edit/core/variables"
|
|
@@ -59,18 +66,27 @@ const TRANSLATION_LOCALE_KEY = "tess_edit:translation_locale"
|
|
|
59
66
|
const NARROW_WIDTH = 1000
|
|
60
67
|
|
|
61
68
|
export default class extends Controller {
|
|
62
|
-
static values = { document: String, sample: { type: Boolean, default: true }, panels: { type: String, default: "split" }, compileUrl: { type: String, default: COMPILE_URL }, mediaUrl: String, variablesUrl: String, translationsUrl: String, lock: String }
|
|
69
|
+
static values = { kit: { type: String, default: DEFAULT_KIT }, document: String, sample: { type: Boolean, default: true }, panels: { type: String, default: "split" }, compileUrl: { type: String, default: COMPILE_URL }, mediaUrl: String, variablesUrl: String, translationsUrl: String, lock: String, blocks: String, options: String }
|
|
63
70
|
static targets = ["jsonInput", "mjmlInput", "htmlInput"]
|
|
64
71
|
|
|
65
72
|
connect() {
|
|
73
|
+
this.kit = getKit(this.kitValue)
|
|
74
|
+
if (!this.kit) {
|
|
75
|
+
console.error(`TessEdit: unknown kit ${this.kitValue}, editing an email instead`)
|
|
76
|
+
this.kit = getKit(DEFAULT_KIT)
|
|
77
|
+
}
|
|
66
78
|
this.lock = parseLock(this.lockValue)
|
|
79
|
+
this.allowedTypes = this.parseJson(this.blocksValue, "blocks")
|
|
80
|
+
this.options = this.parseJson(this.optionsValue, "options") || {}
|
|
81
|
+
// The host's tiles from palette sources (core/registry.js), by source key
|
|
82
|
+
this.sources = new Map()
|
|
67
83
|
this.history = new History()
|
|
68
84
|
this.doc = this.loadInitial()
|
|
69
85
|
this.selectedId = this.doc.id
|
|
70
86
|
this.editingId = null
|
|
71
87
|
this.drag = null
|
|
72
88
|
this.mode = "edit"
|
|
73
|
-
this.device =
|
|
89
|
+
this.device = this.kit.devices?.[0]?.key || null
|
|
74
90
|
this.leftTab = "blocks"
|
|
75
91
|
this.panels = [this.storedPanels(), this.panelsValue].find((value) => PANELS.includes(value)) || "split"
|
|
76
92
|
this.narrow = false
|
|
@@ -89,6 +105,7 @@ export default class extends Controller {
|
|
|
89
105
|
this.renderAll()
|
|
90
106
|
this.writeOutputs()
|
|
91
107
|
if (this.media.enabled) this.loadMedia()
|
|
108
|
+
this.loadSources()
|
|
92
109
|
if (this.translations.enabled) {
|
|
93
110
|
this.translations.subscribe(() => this.showTranslations())
|
|
94
111
|
this.loadTranslations()
|
|
@@ -163,6 +180,31 @@ export default class extends Controller {
|
|
|
163
180
|
return translationTag(key, translation.args)
|
|
164
181
|
}
|
|
165
182
|
|
|
183
|
+
parseJson(value, name) {
|
|
184
|
+
if (!value) return null
|
|
185
|
+
try {
|
|
186
|
+
return JSON.parse(value)
|
|
187
|
+
} catch (error) {
|
|
188
|
+
console.error(`TessEdit: the ${name} value is not JSON`, error)
|
|
189
|
+
return null
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// The palette sources' tiles; a source whose items are null has none here
|
|
194
|
+
async loadSources() {
|
|
195
|
+
await Promise.all(
|
|
196
|
+
paletteSources().map(async (source) => {
|
|
197
|
+
try {
|
|
198
|
+
const items = await source.items(this)
|
|
199
|
+
if (items) this.sources.set(source.key, { source, items, error: null })
|
|
200
|
+
} catch (error) {
|
|
201
|
+
this.sources.set(source.key, { source, items: [], error: error.message })
|
|
202
|
+
}
|
|
203
|
+
})
|
|
204
|
+
)
|
|
205
|
+
if (this.sources.size) renderBlocksPanel(this, this.blocksElement)
|
|
206
|
+
}
|
|
207
|
+
|
|
166
208
|
async loadMedia() {
|
|
167
209
|
try {
|
|
168
210
|
this.media.images = await fetchMedia(this.mediaUrlValue)
|
|
@@ -231,12 +273,12 @@ export default class extends Controller {
|
|
|
231
273
|
const source = this.documentValue || (this.hasJsonInputTarget && this.jsonInputTarget.value)
|
|
232
274
|
if (source && source.trim()) {
|
|
233
275
|
try {
|
|
234
|
-
return normalize(source)
|
|
276
|
+
return normalize(source, { kit: this.kit.key })
|
|
235
277
|
} catch (error) {
|
|
236
278
|
console.error("TessEdit: could not load the document", error)
|
|
237
279
|
}
|
|
238
280
|
}
|
|
239
|
-
return this.sampleValue ?
|
|
281
|
+
return this.sampleValue ? this.kit.starter() : createDocument(this.kit.key)
|
|
240
282
|
}
|
|
241
283
|
|
|
242
284
|
// --- public API -------------------------------------------------------------
|
|
@@ -248,15 +290,15 @@ export default class extends Controller {
|
|
|
248
290
|
setDocument(input) {
|
|
249
291
|
this.stopEditing()
|
|
250
292
|
this.history.record(JSON.stringify(this.doc))
|
|
251
|
-
this.doc = normalize(input)
|
|
293
|
+
this.doc = normalize(input, { kit: this.kit.key })
|
|
252
294
|
this.selectedId = this.doc.id
|
|
253
295
|
this.renderAll()
|
|
254
296
|
}
|
|
255
297
|
|
|
298
|
+
// The JSON and the kit's outputs, e.g. { json, mjml, html, errors } for an email
|
|
256
299
|
async export() {
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
return { json: JSON.stringify(this.doc), mjml, html: html || "", errors }
|
|
300
|
+
const { outputs, errors } = await this.kit.outputs(this.doc, { compileUrl: this.compileUrlValue })
|
|
301
|
+
return { json: JSON.stringify(this.doc), ...outputs, errors }
|
|
260
302
|
}
|
|
261
303
|
|
|
262
304
|
// --- locking ------------------------------------------------------------------
|
|
@@ -273,9 +315,29 @@ export default class extends Controller {
|
|
|
273
315
|
return canEditField(this.doc, this.lock, id, key)
|
|
274
316
|
}
|
|
275
317
|
|
|
276
|
-
// The block types that may be added to the node: none when it is locked
|
|
318
|
+
// The block types that may be added to the node: none when it is locked,
|
|
319
|
+
// only the allowed ones when the host limits them.
|
|
277
320
|
accepted(parentId) {
|
|
278
|
-
|
|
321
|
+
if (!this.canFill(parentId)) return []
|
|
322
|
+
return accepted(this.doc, parentId).filter((type) => this.allows(type))
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Whether a block of the type can go in the node; with `moving`, a block
|
|
326
|
+
// that is in the document already, which can always move where it fits
|
|
327
|
+
// A palette source's tile counts as moving: the host gave it, so the allowed
|
|
328
|
+
// blocks don't apply.
|
|
329
|
+
canInsert(parentId, type, { moving = false } = {}) {
|
|
330
|
+
if (!moving) return this.accepted(parentId).includes(type)
|
|
331
|
+
return this.canFill(parentId) && accepted(this.doc, parentId, { moving }).includes(type)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
allows(type) {
|
|
335
|
+
return !this.allowedTypes || this.allowedTypes.includes(type)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// The Blocks panel's tiles
|
|
339
|
+
paletteItems() {
|
|
340
|
+
return paletteItems(this.kit.key).filter((item) => this.allows(item.type))
|
|
279
341
|
}
|
|
280
342
|
|
|
281
343
|
// --- layout -------------------------------------------------------------------
|
|
@@ -288,7 +350,7 @@ export default class extends Controller {
|
|
|
288
350
|
this.sidePanel = h("aside")
|
|
289
351
|
this.inspectPanel = h("aside")
|
|
290
352
|
this.textToolbar = h("div", { class: "hidden absolute left-1/2 top-3 z-20 -translate-x-1/2 flex items-center gap-0.5 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 px-1.5 py-1 shadow-md" })
|
|
291
|
-
this.frame = h("iframe", { class: "block h-full w-full border-0 bg-white transition-[width] duration-200", title:
|
|
353
|
+
this.frame = h("iframe", { class: "block h-full w-full border-0 bg-white transition-[width] duration-200", title: `${this.kit.name} canvas` })
|
|
292
354
|
this.frameWrap = h("div", { class: "relative h-full mx-auto" }, this.frame)
|
|
293
355
|
this.topBar = h("div", { class: "flex items-center gap-1 h-12 px-3 border-b border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shrink-0" })
|
|
294
356
|
this.body = h("div", { class: "flex flex-1 min-h-0" })
|
|
@@ -428,9 +490,9 @@ export default class extends Controller {
|
|
|
428
490
|
|
|
429
491
|
// replaceChildren would turn a null into the text "null", so only pass real elements.
|
|
430
492
|
this.topBar.replaceChildren(...[
|
|
431
|
-
h("span", { class: "font-semibold text-gray-900 dark:text-gray-100 mr-3" },
|
|
432
|
-
toggle(this.mode, [["edit", "edit", "Edit"], ["preview", "eye",
|
|
433
|
-
toggle(this.device,
|
|
493
|
+
h("span", { class: "font-semibold text-gray-900 dark:text-gray-100 mr-3" }, this.kit.name),
|
|
494
|
+
toggle(this.mode, [["edit", "edit", "Edit"], ["preview", "eye", this.kit.previewTitle || "Preview"]], (mode) => this.setMode(mode)),
|
|
495
|
+
this.kit.devices?.length ? toggle(this.device, this.kit.devices.map((device) => [device.key, device.icon, device.title]), (device) => this.setDevice(device)) : null,
|
|
434
496
|
this.translations.enabled ? this.translationLocaleMenu() : null,
|
|
435
497
|
h("div", { class: "flex-1" }),
|
|
436
498
|
toggle(
|
|
@@ -500,51 +562,52 @@ export default class extends Controller {
|
|
|
500
562
|
|
|
501
563
|
setDevice(device) {
|
|
502
564
|
this.device = device
|
|
503
|
-
//
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
this.frameWrap.
|
|
507
|
-
this.
|
|
565
|
+
// A device without a width (desktop) fills the canvas area; one with a
|
|
566
|
+
// width (mobile, 375px) shows a full-height frame in the middle of it.
|
|
567
|
+
const width = this.kit.devices?.find((option) => option.key === device)?.width
|
|
568
|
+
this.frameWrap.style.width = width || ""
|
|
569
|
+
this.frameWrap.classList.toggle("shadow-sm", !!width)
|
|
570
|
+
this.main.classList.toggle("px-4", !!width)
|
|
508
571
|
this.renderTopBar()
|
|
509
572
|
setTimeout(() => this.canvas.refresh(), 250)
|
|
510
573
|
}
|
|
511
574
|
|
|
512
575
|
// --- rendering --------------------------------------------------------------
|
|
513
576
|
|
|
514
|
-
//
|
|
515
|
-
// older one, which
|
|
516
|
-
|
|
577
|
+
// Runs one of the kit's renderings, which may go to the server (the email's
|
|
578
|
+
// compiling). A newer run on the same channel aborts the older one, which
|
|
579
|
+
// then resolves to null.
|
|
580
|
+
async run(channel, render) {
|
|
517
581
|
this.requests[channel]?.abort()
|
|
518
582
|
const request = (this.requests[channel] = new AbortController())
|
|
519
|
-
const result = await
|
|
583
|
+
const result = await render({ compileUrl: this.compileUrlValue, signal: request.signal })
|
|
520
584
|
return request.signal.aborted ? null : result
|
|
521
585
|
}
|
|
522
586
|
|
|
523
587
|
async renderCanvas() {
|
|
524
588
|
if (this.editingId) return
|
|
525
|
-
const result = await this.
|
|
589
|
+
const result = await this.run("canvas", (options) => this.kit.canvas(this.doc, { ...options, editor: this.mode === "edit" }))
|
|
526
590
|
if (!result || this.editingId) return
|
|
527
591
|
this.showErrors(result.errors)
|
|
528
|
-
// Without HTML the canvas keeps the last
|
|
592
|
+
// Without HTML the canvas keeps the last document that rendered.
|
|
529
593
|
if (result.html !== null) this.canvas.write(result.html, this.mode)
|
|
530
594
|
}
|
|
531
595
|
|
|
532
596
|
showErrors(errors) {
|
|
533
|
-
this.errorBar.textContent = errors.length ?
|
|
597
|
+
this.errorBar.textContent = errors.length ? `${this.kit.errors}: ${errors.join("; ")}` : ""
|
|
534
598
|
this.errorBar.classList.toggle("hidden", !errors.length)
|
|
535
599
|
}
|
|
536
600
|
|
|
537
601
|
async writeOutputs() {
|
|
538
602
|
this.loadTranslations()
|
|
539
603
|
const json = JSON.stringify(this.doc)
|
|
540
|
-
const
|
|
541
|
-
const result = await this.compileMjml(mjml, "outputs")
|
|
604
|
+
const result = await this.run("outputs", (options) => this.kit.outputs(this.doc, options))
|
|
542
605
|
if (!result) return
|
|
543
|
-
const
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
this.dispatch("change", { detail: {
|
|
606
|
+
const outputs = { json, ...result.outputs }
|
|
607
|
+
for (const [name, value] of Object.entries(outputs)) {
|
|
608
|
+
if (this[`has${name[0].toUpperCase()}${name.slice(1)}InputTarget`]) this[`${name}InputTarget`].value = value
|
|
609
|
+
}
|
|
610
|
+
this.dispatch("change", { detail: { ...outputs, errors: result.errors, document: this.doc } })
|
|
548
611
|
}
|
|
549
612
|
|
|
550
613
|
// Applies a change to the document as one undo step.
|
|
@@ -669,7 +732,9 @@ export default class extends Controller {
|
|
|
669
732
|
|
|
670
733
|
drop(drag, parentId, index) {
|
|
671
734
|
if (!drag || !this.canFill(parentId) || (drag.kind === "move" && !this.isFree(drag.id))) return
|
|
672
|
-
if (drag.kind === "
|
|
735
|
+
if (drag.kind === "source") {
|
|
736
|
+
this.insertFromSource(drag.source, drag.item, { parentId, index })
|
|
737
|
+
} else if (drag.kind === "new") {
|
|
673
738
|
const node = createFromPalette(drag.key)
|
|
674
739
|
this.commit((doc) => insert(doc, parentId, index, node))
|
|
675
740
|
this.select(node.id)
|
|
@@ -692,21 +757,53 @@ export default class extends Controller {
|
|
|
692
757
|
// lock, at the end of the first unlocked block it fits in.
|
|
693
758
|
addFromPalette(key) {
|
|
694
759
|
const type = itemType(key)
|
|
760
|
+
const place = this.placeFor((parentId) => this.accepted(parentId).includes(type))
|
|
761
|
+
if (place) return this.insertAt(key, place.parentId, place.index)
|
|
762
|
+
const definition = getBlock(type)
|
|
763
|
+
if (definition?.unique && findType(this.doc, type)) return this.flash(`There is a ${definition.name} already; a document has one.`)
|
|
764
|
+
this.flash(`${definition?.name || key} has to go inside a column. Select one first, or drag it onto the canvas.`)
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
// Where a click on a tile adds a block, for blocks that `fits` a parent
|
|
768
|
+
placeFor(fits) {
|
|
695
769
|
const chain = [find(this.doc, this.selectedId)?.node || this.doc, ...ancestors(this.doc, this.selectedId)]
|
|
696
|
-
if (
|
|
770
|
+
if (fits(chain[0].id)) return { parentId: chain[0].id, index: chain[0].children.length }
|
|
697
771
|
for (let i = 0; i < chain.length - 1; i++) {
|
|
698
|
-
if (
|
|
772
|
+
if (fits(chain[i + 1].id)) return { parentId: chain[i + 1].id, index: chain[i + 1].children.indexOf(chain[i]) + 1 }
|
|
699
773
|
}
|
|
700
|
-
if (
|
|
774
|
+
if (fits(this.doc.id)) return { parentId: this.doc.id, index: this.doc.children.length }
|
|
701
775
|
if (this.lock) {
|
|
702
776
|
let target = null
|
|
703
777
|
walk(this.doc, (node) => {
|
|
704
778
|
if (target) return false
|
|
705
|
-
if (this.lock.except.includes(node.type) &&
|
|
779
|
+
if (this.lock.except.includes(node.type) && fits(node.id)) target = node
|
|
706
780
|
})
|
|
707
|
-
if (target) return
|
|
781
|
+
if (target) return { parentId: target.id, index: target.children.length }
|
|
708
782
|
}
|
|
709
|
-
|
|
783
|
+
return null
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
// Inserts what a palette source's tile creates (see core/registry.js) at a
|
|
787
|
+
// place, or where a click on a tile would add it
|
|
788
|
+
async insertFromSource(source, item, { parentId, index } = {}) {
|
|
789
|
+
let node
|
|
790
|
+
try {
|
|
791
|
+
node = await source.create(item, this)
|
|
792
|
+
} catch (error) {
|
|
793
|
+
this.flash(error.message)
|
|
794
|
+
return null
|
|
795
|
+
}
|
|
796
|
+
const name = item.label || getBlock(node.type)?.name || "This"
|
|
797
|
+
const fits = (id) => this.canFill(id) && !misfit(node, accepted(this.doc, id, { moving: true }))
|
|
798
|
+
const place = parentId ? { parentId, index } : this.placeFor(fits)
|
|
799
|
+
if (!place || !fits(place.parentId)) {
|
|
800
|
+
const type = place && misfit(node, accepted(this.doc, place.parentId, { moving: true }))
|
|
801
|
+
this.flash(type && type !== node.type ? `${name} can't go here: its ${getBlock(type)?.name || type} blocks don't fit.` : `${name} doesn't fit here. Select where it goes first, or drag it onto the canvas.`)
|
|
802
|
+
return null
|
|
803
|
+
}
|
|
804
|
+
this.commit((doc) => insert(doc, place.parentId, place.index, node))
|
|
805
|
+
this.select(node.id)
|
|
806
|
+
return node
|
|
710
807
|
}
|
|
711
808
|
|
|
712
809
|
openInsertMenu(parentId, index, rect) {
|
|
@@ -721,7 +818,7 @@ export default class extends Controller {
|
|
|
721
818
|
}
|
|
722
819
|
this.closeMenu()
|
|
723
820
|
const allowed = this.accepted(parentId)
|
|
724
|
-
const items = paletteItems().filter((item) => allowed.includes(item.type))
|
|
821
|
+
const items = this.paletteItems().filter((item) => allowed.includes(item.type))
|
|
725
822
|
if (!items.length) return
|
|
726
823
|
this.menu = h(
|
|
727
824
|
"div",
|
|
@@ -812,8 +909,8 @@ export default class extends Controller {
|
|
|
812
909
|
|
|
813
910
|
async openCodeDialog(tab = "json") {
|
|
814
911
|
this.stopEditing()
|
|
815
|
-
const { json,
|
|
816
|
-
const contents = { json: JSON.stringify(JSON.parse(json), null, 2),
|
|
912
|
+
const { json, errors, ...outputs } = await this.export()
|
|
913
|
+
const contents = { json: JSON.stringify(JSON.parse(json), null, 2), ...outputs }
|
|
817
914
|
const textarea = h("textarea", { class: "flex-1 w-full resize-none rounded-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-900 p-3 font-mono text-xs text-gray-800 dark:text-gray-200 outline-none focus:border-blue-500", spellcheck: false })
|
|
818
915
|
const status = h("span", { class: "text-xs text-gray-500 dark:text-gray-400" })
|
|
819
916
|
const tabs = h("div", { class: "flex gap-1" })
|
|
@@ -836,9 +933,9 @@ export default class extends Controller {
|
|
|
836
933
|
apply.classList.toggle("hidden", !loadable)
|
|
837
934
|
status.className = "text-xs text-gray-500 dark:text-gray-400"
|
|
838
935
|
status.textContent =
|
|
839
|
-
key === "json" ? (this.lock ? "The document. Parts of it are locked, so it can't be loaded here." : "The document. Edit and load it, or paste one in.") : key === "
|
|
936
|
+
key === "json" ? (this.lock ? "The document. Parts of it are locked, so it can't be loaded here." : "The document. Edit and load it, or paste one in.") : key === "html" && errors.length ? `${this.kit.errors}: ${errors.join("; ")}` : this.kit.notes?.[key] || ""
|
|
840
937
|
tabs.replaceChildren(
|
|
841
|
-
...
|
|
938
|
+
...Object.keys(contents).map((name) =>
|
|
842
939
|
h("button", { type: "button", class: `rounded-md px-3 py-1.5 text-xs font-medium ${name === tab ? "bg-gray-900 text-white dark:bg-gray-100 dark:text-gray-900" : "text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"}`, onclick: () => show(name) }, name.toUpperCase())
|
|
843
940
|
)
|
|
844
941
|
)
|
|
@@ -847,7 +944,7 @@ export default class extends Controller {
|
|
|
847
944
|
const close = () => dialog.remove()
|
|
848
945
|
const download = () => {
|
|
849
946
|
const blob = new Blob([textarea.value], { type: tab === "json" ? "application/json" : tab === "html" ? "text/html" : "text/plain" })
|
|
850
|
-
const link = h("a", { href: URL.createObjectURL(blob), download:
|
|
947
|
+
const link = h("a", { href: URL.createObjectURL(blob), download: `${this.kit.key}.${tab}` })
|
|
851
948
|
link.click()
|
|
852
949
|
URL.revokeObjectURL(link.href)
|
|
853
950
|
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
// The document is a plain JSON tree:
|
|
2
2
|
// { id, type, attributes: {}, data: {}, children: [] }
|
|
3
|
-
// `attributes`
|
|
4
|
-
//
|
|
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
|
|
5
|
+
// (rich text, Liquid expressions, link lists). A block copied from elsewhere,
|
|
6
|
+
// such as a host's reusable content, may also have `origin`: the id of the
|
|
7
|
+
// block it is a copy of (see renumber). All functions here are pure apart
|
|
5
8
|
// from the mutating helpers, which change the tree they are given in place.
|
|
6
9
|
|
|
7
|
-
import { getBlock, paletteItem, typesForRoles } from "tess_edit/core/registry"
|
|
10
|
+
import { DEFAULT_KIT, getBlock, getKit, kitOf, paletteItem, typesForRoles } from "tess_edit/core/registry"
|
|
8
11
|
|
|
9
12
|
export const VERSION = 1
|
|
10
13
|
|
|
@@ -30,8 +33,11 @@ export function createNode(type, overrides = {}) {
|
|
|
30
33
|
}
|
|
31
34
|
}
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
// An empty document of a kit (see core/registry.js), an email by default.
|
|
37
|
+
export function createDocument(kit = DEFAULT_KIT) {
|
|
38
|
+
const definition = getKit(kit)
|
|
39
|
+
if (!definition) throw new Error(`Unknown kit: ${kit}`)
|
|
40
|
+
return { version: VERSION, ...createNode(definition.root) }
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
// Calls fn(node, parent, index, depth) for every node, depth first.
|
|
@@ -79,12 +85,34 @@ export function acceptedRoles(doc, parentId) {
|
|
|
79
85
|
const accepts = getBlock(hit.node.type)?.accepts
|
|
80
86
|
if (accepts !== "inherit") return accepts || []
|
|
81
87
|
const owner = ancestors(doc, parentId).find((node) => getBlock(node.type)?.accepts !== "inherit")
|
|
82
|
-
return (owner ? getBlock(owner.type).accepts : getBlock(
|
|
88
|
+
return (owner ? getBlock(owner.type).accepts : getBlock(doc.type).accepts).filter((role) => role !== "column")
|
|
83
89
|
}
|
|
84
90
|
|
|
85
|
-
// The block types that may be placed directly inside
|
|
86
|
-
|
|
87
|
-
|
|
91
|
+
// The block types of the document's kit that may be placed directly inside
|
|
92
|
+
// the node with `parentId`. A block that is `unique`, such as the Content
|
|
93
|
+
// block, can't be added when the document has one, but it can be moved.
|
|
94
|
+
export function accepted(doc, parentId, { moving = false } = {}) {
|
|
95
|
+
const types = typesForRoles(acceptedRoles(doc, parentId), kitOf(doc)?.key)
|
|
96
|
+
return moving ? types : types.filter((type) => !getBlock(type).unique || !findType(doc, type))
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// The first node of a type, depth first
|
|
100
|
+
export function findType(doc, type) {
|
|
101
|
+
let result = null
|
|
102
|
+
walk(doc, (node) => {
|
|
103
|
+
if (result) return false
|
|
104
|
+
if (node.type === type) result = node
|
|
105
|
+
})
|
|
106
|
+
return result
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// The first type that keeps a node from going inside `parentId`, where the
|
|
110
|
+
// types in `allowed` can go, or null when it fits: the node's own, or for a
|
|
111
|
+
// container that takes what its parent takes ("inherit"), one of its children's.
|
|
112
|
+
export function misfit(node, allowed) {
|
|
113
|
+
if (!allowed.includes(node.type)) return node.type
|
|
114
|
+
if (getBlock(node.type)?.accepts !== "inherit") return null
|
|
115
|
+
return node.children.find((child) => !allowed.includes(child.type))?.type || null
|
|
88
116
|
}
|
|
89
117
|
|
|
90
118
|
export function canInsert(doc, parentId, type) {
|
|
@@ -97,10 +125,14 @@ export function isHorizontal(doc, parentId) {
|
|
|
97
125
|
return hit ? !!getBlock(hit.node.type)?.horizontal : false
|
|
98
126
|
}
|
|
99
127
|
|
|
100
|
-
// Gives a node and all of its descendants fresh ids, for duplicating and
|
|
101
|
-
|
|
128
|
+
// Gives a node and all of its descendants fresh ids, for duplicating and
|
|
129
|
+
// pasting. The copy is new content, so it has no origin; with `origin` each
|
|
130
|
+
// block's origin is the id of the block it is a copy of instead.
|
|
131
|
+
export function renumber(node, { origin = false } = {}) {
|
|
102
132
|
const copy = clone(node)
|
|
103
133
|
walk(copy, (n) => {
|
|
134
|
+
if (origin) n.origin = n.id
|
|
135
|
+
else delete n.origin
|
|
104
136
|
n.id = uid()
|
|
105
137
|
})
|
|
106
138
|
return copy
|
|
@@ -162,12 +194,15 @@ export function getValue(node, key) {
|
|
|
162
194
|
return node[scope]?.[name]
|
|
163
195
|
}
|
|
164
196
|
|
|
165
|
-
// Light validation of a document read from storage or an import.
|
|
166
|
-
|
|
197
|
+
// Light validation of a document read from storage or an import. With a
|
|
198
|
+
// `kit`, the document has to be one of that kit's.
|
|
199
|
+
export function normalize(input, { kit } = {}) {
|
|
167
200
|
const doc = typeof input === "string" ? JSON.parse(input) : clone(input)
|
|
168
|
-
// The root block was called "page"
|
|
169
|
-
if (doc?.type === "page") doc.type = "
|
|
170
|
-
|
|
201
|
+
// The email's root block was called "page", then "root"; such documents still load.
|
|
202
|
+
if (doc?.type === "page" || doc?.type === "root") doc.type = "email"
|
|
203
|
+
const own = kitOf(doc)
|
|
204
|
+
if (!own) throw new Error("Not a TessEdit document: the top node must be a root block")
|
|
205
|
+
if (kit && own.key !== kit) throw new Error(`This is a document for the ${own.name} editor, not the ${getKit(kit)?.name || kit} one`)
|
|
171
206
|
walk(doc, (node) => {
|
|
172
207
|
if (!getBlock(node.type)) throw new Error(`Unknown block type: ${node.type}`)
|
|
173
208
|
node.id ||= uid()
|
|
@@ -179,6 +214,21 @@ export function normalize(input) {
|
|
|
179
214
|
return doc
|
|
180
215
|
}
|
|
181
216
|
|
|
217
|
+
// The Content block of a layout (see blocks/content.js): where the documents
|
|
218
|
+
// using the layout put their own blocks.
|
|
219
|
+
export function contentNode(doc) {
|
|
220
|
+
return findType(doc, "content")
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// The layout with these blocks in its Content block, as a new document
|
|
224
|
+
export function withContent(layout, nodes) {
|
|
225
|
+
const doc = clone(layout)
|
|
226
|
+
const content = contentNode(doc)
|
|
227
|
+
if (!content) throw new Error("The layout has no Content block")
|
|
228
|
+
content.children = clone(nodes || [])
|
|
229
|
+
return doc
|
|
230
|
+
}
|
|
231
|
+
|
|
182
232
|
// The node (or small tree) a Blocks panel tile creates.
|
|
183
233
|
export function createFromPalette(key) {
|
|
184
234
|
const item = paletteItem(key)
|