tess_edit 0.2.2 → 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 +114 -9
- 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 +267 -60
- 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/core/translations.js +229 -0
- 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 +96 -12
- data/app/javascript/tess_edit/ui/dialog.js +103 -0
- data/app/javascript/tess_edit/ui/icons.js +5 -1
- data/app/javascript/tess_edit/ui/inspector.js +76 -7
- data/app/javascript/tess_edit/ui/text_editing.js +112 -11
- data/app/javascript/tess_edit/ui/translate_dialog.js +30 -0
- data/app/javascript/tess_edit/ui/translation_popover.js +165 -0
- data/app/javascript/tess_edit/ui/tree.js +2 -2
- data/app/javascript/tess_edit/ui/variable_menu.js +29 -1
- data/lib/tess_edit/content.rb +62 -0
- data/lib/tess_edit/version.rb +1 -1
- data/lib/tess_edit.rb +1 -0
- data/vendor/javascript/tess_edit/tiptap.js +13 -13
- metadata +10 -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,37 +10,49 @@
|
|
|
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 {{
|
|
16
18
|
// and browsed with { } buttons (see core/variables.js), e.g.
|
|
17
19
|
// Liquidum's /liquidum/variables?roots=…
|
|
20
|
+
// translationsUrl the host's translations for Liquid's t filter ({{ '.greeting' | t }}),
|
|
21
|
+
// shown in a language chosen in the top bar, listed and edited
|
|
22
|
+
// while pointed at, and made from selected text (see
|
|
23
|
+
// core/translations.js)
|
|
18
24
|
// lock JSON: only the insides of some blocks and some root fields can
|
|
19
25
|
// change, e.g. {"except": ["content"], "fields": ["data.subject"]}
|
|
20
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)
|
|
21
32
|
// Targets (all optional, usually hidden inputs in a form):
|
|
22
|
-
// 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)
|
|
23
35
|
// Events:
|
|
24
|
-
// tess-edit:change detail: { json,
|
|
25
|
-
//
|
|
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
|
|
26
39
|
|
|
27
40
|
import { Controller } from "@hotwired/stimulus"
|
|
28
|
-
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"
|
|
29
42
|
import { History } from "tess_edit/core/history"
|
|
30
43
|
import { canEditField, canFill, isFree, parseLock } from "tess_edit/core/lock"
|
|
31
|
-
import { getBlock, paletteItems } from "tess_edit/core/registry"
|
|
32
|
-
import {
|
|
33
|
-
import { toMjml } from "tess_edit/export/mjml"
|
|
34
|
-
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"
|
|
35
46
|
import { IMAGE_FIELD, fetchMedia, uploadMedia } from "tess_edit/core/media"
|
|
47
|
+
import { Translations, keysIn, suggestKey, toTranslation, translationTag } from "tess_edit/core/translations"
|
|
36
48
|
import { Variables, loopScope } from "tess_edit/core/variables"
|
|
37
49
|
import { Canvas } from "tess_edit/ui/canvas"
|
|
38
50
|
import { Tree } from "tess_edit/ui/tree"
|
|
39
51
|
import { Inspector } from "tess_edit/ui/inspector"
|
|
40
52
|
import { TextEditing } from "tess_edit/ui/text_editing"
|
|
41
53
|
import { VariableMenu } from "tess_edit/ui/variable_menu"
|
|
54
|
+
import { TranslationPopover } from "tess_edit/ui/translation_popover"
|
|
55
|
+
import { askTranslationKey } from "tess_edit/ui/translate_dialog"
|
|
42
56
|
import { renderBlocksPanel } from "tess_edit/ui/blocks_panel"
|
|
43
57
|
import { debounce, h } from "tess_edit/ui/dom"
|
|
44
58
|
import { icon } from "tess_edit/ui/icons"
|
|
@@ -47,29 +61,42 @@ const TOOL = "w-8 h-8 flex items-center justify-center rounded-md text-gray-600
|
|
|
47
61
|
const TAB = "px-3 h-12 text-[13px] font-medium border-b-2 -mb-px"
|
|
48
62
|
const PANELS = ["left", "split", "right"]
|
|
49
63
|
const PANELS_KEY = "tess_edit:panels"
|
|
64
|
+
const TRANSLATION_LOCALE_KEY = "tess_edit:translation_locale"
|
|
50
65
|
// Two sidebars (256px + 320px) leave too little canvas below this editor width.
|
|
51
66
|
const NARROW_WIDTH = 1000
|
|
52
67
|
|
|
53
68
|
export default class extends Controller {
|
|
54
|
-
static values = { document: String, sample: { type: Boolean, default: true }, panels: { type: String, default: "split" }, compileUrl: { type: String, default: COMPILE_URL }, mediaUrl: String, variablesUrl: String, lock: String }
|
|
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 }
|
|
55
70
|
static targets = ["jsonInput", "mjmlInput", "htmlInput"]
|
|
56
71
|
|
|
57
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
|
+
}
|
|
58
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()
|
|
59
83
|
this.history = new History()
|
|
60
84
|
this.doc = this.loadInitial()
|
|
61
85
|
this.selectedId = this.doc.id
|
|
62
86
|
this.editingId = null
|
|
63
87
|
this.drag = null
|
|
64
88
|
this.mode = "edit"
|
|
65
|
-
this.device =
|
|
89
|
+
this.device = this.kit.devices?.[0]?.key || null
|
|
66
90
|
this.leftTab = "blocks"
|
|
67
91
|
this.panels = [this.storedPanels(), this.panelsValue].find((value) => PANELS.includes(value)) || "split"
|
|
68
92
|
this.narrow = false
|
|
69
93
|
this.requests = {}
|
|
70
94
|
this.media = { enabled: Boolean(this.mediaUrlValue), images: [], uploading: false, error: null }
|
|
71
95
|
this.variables = new Variables(this.variablesUrlValue)
|
|
72
|
-
this.
|
|
96
|
+
this.translations = new Translations(this.translationsUrlValue)
|
|
97
|
+
this.translationPopover = this.translations.enabled ? new TranslationPopover(this) : null
|
|
98
|
+
this.translationLocaleChoice = this.stored(TRANSLATION_LOCALE_KEY)
|
|
99
|
+
this.variableMenu = this.variables.enabled || this.translations.enabled ? new VariableMenu(this) : null
|
|
73
100
|
|
|
74
101
|
this.scheduleCanvas = debounce(() => this.renderCanvas(), 60)
|
|
75
102
|
this.scheduleOutputs = debounce(() => this.writeOutputs(), 300)
|
|
@@ -78,9 +105,106 @@ export default class extends Controller {
|
|
|
78
105
|
this.renderAll()
|
|
79
106
|
this.writeOutputs()
|
|
80
107
|
if (this.media.enabled) this.loadMedia()
|
|
108
|
+
this.loadSources()
|
|
109
|
+
if (this.translations.enabled) {
|
|
110
|
+
this.translations.subscribe(() => this.showTranslations())
|
|
111
|
+
this.loadTranslations()
|
|
112
|
+
}
|
|
81
113
|
this.element.tessEdit = this
|
|
82
114
|
}
|
|
83
115
|
|
|
116
|
+
// --- translations -------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
// The language translations show in: the one chosen in the top bar, else
|
|
119
|
+
// the host's first; null shows their Liquid ("Keys")
|
|
120
|
+
get translationLocale() {
|
|
121
|
+
const { locales } = this.translations
|
|
122
|
+
if (this.translationLocaleChoice === "") return null
|
|
123
|
+
return locales.includes(this.translationLocaleChoice) ? this.translationLocaleChoice : locales[0] || null
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
setTranslationLocale(locale) {
|
|
127
|
+
this.translationLocaleChoice = locale
|
|
128
|
+
try {
|
|
129
|
+
localStorage.setItem(TRANSLATION_LOCALE_KEY, locale)
|
|
130
|
+
} catch {}
|
|
131
|
+
this.showTranslations()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// The keys in the document the editor doesn't have yet
|
|
135
|
+
loadTranslations() {
|
|
136
|
+
if (this.translations.enabled) this.translations.load(keysIn(this.doc))
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
showTranslations() {
|
|
140
|
+
this.renderTopBar()
|
|
141
|
+
this.canvas.showTranslations()
|
|
142
|
+
this.textEditing.refreshTranslations()
|
|
143
|
+
this.inspector.refreshTranslations()
|
|
144
|
+
this.translationPopover?.refresh()
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Keeps a text as a translation in the editor's language (the host's first
|
|
148
|
+
// in "Keys"), under a key the user confirms, and gives its t filter; null
|
|
149
|
+
// when it isn't. Its Liquid variables become placeholders, given as the
|
|
150
|
+
// filter's arguments: "Hi {{ user.name }}" is kept as "Hi %{name}", in
|
|
151
|
+
// {{ '.hi' | t: name: user.name }}.
|
|
152
|
+
async makeTranslatable(text) {
|
|
153
|
+
const translations = this.translations
|
|
154
|
+
const locale = this.translationLocale || translations.locales[0]
|
|
155
|
+
if (!locale) {
|
|
156
|
+
this.flash(translations.error || "There are no languages to translate into yet.")
|
|
157
|
+
return null
|
|
158
|
+
}
|
|
159
|
+
const translation = toTranslation(text)
|
|
160
|
+
if (translation.error) {
|
|
161
|
+
this.flash(translation.error)
|
|
162
|
+
return null
|
|
163
|
+
}
|
|
164
|
+
const key = await askTranslationKey(this.root, {
|
|
165
|
+
text: translation.text,
|
|
166
|
+
locale,
|
|
167
|
+
suggested: suggestKey(text),
|
|
168
|
+
existing: async (key) => {
|
|
169
|
+
await translations.load([key])
|
|
170
|
+
return translations.value(key, locale)
|
|
171
|
+
}
|
|
172
|
+
})
|
|
173
|
+
if (!key) return null
|
|
174
|
+
try {
|
|
175
|
+
await translations.save(key, locale, translation.text)
|
|
176
|
+
} catch (error) {
|
|
177
|
+
this.flash(error.message)
|
|
178
|
+
return null
|
|
179
|
+
}
|
|
180
|
+
return translationTag(key, translation.args)
|
|
181
|
+
}
|
|
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
|
+
|
|
84
208
|
async loadMedia() {
|
|
85
209
|
try {
|
|
86
210
|
this.media.images = await fetchMedia(this.mediaUrlValue)
|
|
@@ -135,6 +259,7 @@ export default class extends Controller {
|
|
|
135
259
|
Object.values(this.requests).forEach((request) => request.abort())
|
|
136
260
|
this.textEditing?.stop()
|
|
137
261
|
this.variableMenu?.close()
|
|
262
|
+
this.translationPopover?.hide({ force: true })
|
|
138
263
|
this.closeMenu()
|
|
139
264
|
this.resizeObserver?.disconnect()
|
|
140
265
|
document.removeEventListener("mousedown", this.outsideClick)
|
|
@@ -148,12 +273,12 @@ export default class extends Controller {
|
|
|
148
273
|
const source = this.documentValue || (this.hasJsonInputTarget && this.jsonInputTarget.value)
|
|
149
274
|
if (source && source.trim()) {
|
|
150
275
|
try {
|
|
151
|
-
return normalize(source)
|
|
276
|
+
return normalize(source, { kit: this.kit.key })
|
|
152
277
|
} catch (error) {
|
|
153
278
|
console.error("TessEdit: could not load the document", error)
|
|
154
279
|
}
|
|
155
280
|
}
|
|
156
|
-
return this.sampleValue ?
|
|
281
|
+
return this.sampleValue ? this.kit.starter() : createDocument(this.kit.key)
|
|
157
282
|
}
|
|
158
283
|
|
|
159
284
|
// --- public API -------------------------------------------------------------
|
|
@@ -165,15 +290,15 @@ export default class extends Controller {
|
|
|
165
290
|
setDocument(input) {
|
|
166
291
|
this.stopEditing()
|
|
167
292
|
this.history.record(JSON.stringify(this.doc))
|
|
168
|
-
this.doc = normalize(input)
|
|
293
|
+
this.doc = normalize(input, { kit: this.kit.key })
|
|
169
294
|
this.selectedId = this.doc.id
|
|
170
295
|
this.renderAll()
|
|
171
296
|
}
|
|
172
297
|
|
|
298
|
+
// The JSON and the kit's outputs, e.g. { json, mjml, html, errors } for an email
|
|
173
299
|
async export() {
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
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 }
|
|
177
302
|
}
|
|
178
303
|
|
|
179
304
|
// --- locking ------------------------------------------------------------------
|
|
@@ -190,9 +315,29 @@ export default class extends Controller {
|
|
|
190
315
|
return canEditField(this.doc, this.lock, id, key)
|
|
191
316
|
}
|
|
192
317
|
|
|
193
|
-
// 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.
|
|
194
320
|
accepted(parentId) {
|
|
195
|
-
|
|
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))
|
|
196
341
|
}
|
|
197
342
|
|
|
198
343
|
// --- layout -------------------------------------------------------------------
|
|
@@ -205,7 +350,7 @@ export default class extends Controller {
|
|
|
205
350
|
this.sidePanel = h("aside")
|
|
206
351
|
this.inspectPanel = h("aside")
|
|
207
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" })
|
|
208
|
-
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` })
|
|
209
354
|
this.frameWrap = h("div", { class: "relative h-full mx-auto" }, this.frame)
|
|
210
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" })
|
|
211
356
|
this.body = h("div", { class: "flex flex-1 min-h-0" })
|
|
@@ -224,7 +369,7 @@ export default class extends Controller {
|
|
|
224
369
|
this.canvas = new Canvas(this, this.frame)
|
|
225
370
|
this.tree = new Tree(this, this.treeElement)
|
|
226
371
|
this.inspector = new Inspector(this, this.inspectorElement)
|
|
227
|
-
this.textEditing = new TextEditing(this.textToolbar, { menu: this.variableMenu })
|
|
372
|
+
this.textEditing = new TextEditing(this.textToolbar, { menu: this.variableMenu, editor: this })
|
|
228
373
|
renderBlocksPanel(this, this.blocksElement)
|
|
229
374
|
this.observeWidth()
|
|
230
375
|
|
|
@@ -302,8 +447,12 @@ export default class extends Controller {
|
|
|
302
447
|
}
|
|
303
448
|
|
|
304
449
|
storedPanels() {
|
|
450
|
+
return this.stored(PANELS_KEY)
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
stored(key) {
|
|
305
454
|
try {
|
|
306
|
-
return localStorage.getItem(
|
|
455
|
+
return localStorage.getItem(key)
|
|
307
456
|
} catch {
|
|
308
457
|
return null
|
|
309
458
|
}
|
|
@@ -339,10 +488,12 @@ export default class extends Controller {
|
|
|
339
488
|
)
|
|
340
489
|
)
|
|
341
490
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
toggle(this.
|
|
491
|
+
// replaceChildren would turn a null into the text "null", so only pass real elements.
|
|
492
|
+
this.topBar.replaceChildren(...[
|
|
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,
|
|
496
|
+
this.translations.enabled ? this.translationLocaleMenu() : null,
|
|
346
497
|
h("div", { class: "flex-1" }),
|
|
347
498
|
toggle(
|
|
348
499
|
this.layout,
|
|
@@ -362,6 +513,26 @@ export default class extends Controller {
|
|
|
362
513
|
h("span", { html: icon("export") }),
|
|
363
514
|
"Code"
|
|
364
515
|
)
|
|
516
|
+
].filter(Boolean))
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// Which language translations show in on the canvas
|
|
520
|
+
translationLocaleMenu() {
|
|
521
|
+
const current = this.translationLocale || ""
|
|
522
|
+
return h(
|
|
523
|
+
"label",
|
|
524
|
+
{ class: "ml-1 flex items-center gap-1 text-gray-500 dark:text-gray-400", title: "Show translations in" },
|
|
525
|
+
h("span", { html: icon("globe") }),
|
|
526
|
+
h(
|
|
527
|
+
"select",
|
|
528
|
+
{
|
|
529
|
+
class: "h-7 rounded-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 py-0 pl-1.5 pr-7 text-xs text-gray-800 dark:text-gray-200",
|
|
530
|
+
"aria-label": "Show translations in",
|
|
531
|
+
onchange: (event) => this.setTranslationLocale(event.target.value)
|
|
532
|
+
},
|
|
533
|
+
this.translations.locales.map((locale) => h("option", { value: locale, selected: locale === current }, locale.toUpperCase())),
|
|
534
|
+
h("option", { value: "", selected: current === "" }, "Keys")
|
|
535
|
+
)
|
|
365
536
|
)
|
|
366
537
|
}
|
|
367
538
|
|
|
@@ -391,50 +562,52 @@ export default class extends Controller {
|
|
|
391
562
|
|
|
392
563
|
setDevice(device) {
|
|
393
564
|
this.device = device
|
|
394
|
-
//
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
this.frameWrap.
|
|
398
|
-
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)
|
|
399
571
|
this.renderTopBar()
|
|
400
572
|
setTimeout(() => this.canvas.refresh(), 250)
|
|
401
573
|
}
|
|
402
574
|
|
|
403
575
|
// --- rendering --------------------------------------------------------------
|
|
404
576
|
|
|
405
|
-
//
|
|
406
|
-
// older one, which
|
|
407
|
-
|
|
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) {
|
|
408
581
|
this.requests[channel]?.abort()
|
|
409
582
|
const request = (this.requests[channel] = new AbortController())
|
|
410
|
-
const result = await
|
|
583
|
+
const result = await render({ compileUrl: this.compileUrlValue, signal: request.signal })
|
|
411
584
|
return request.signal.aborted ? null : result
|
|
412
585
|
}
|
|
413
586
|
|
|
414
587
|
async renderCanvas() {
|
|
415
588
|
if (this.editingId) return
|
|
416
|
-
const result = await this.
|
|
589
|
+
const result = await this.run("canvas", (options) => this.kit.canvas(this.doc, { ...options, editor: this.mode === "edit" }))
|
|
417
590
|
if (!result || this.editingId) return
|
|
418
591
|
this.showErrors(result.errors)
|
|
419
|
-
// Without HTML the canvas keeps the last
|
|
592
|
+
// Without HTML the canvas keeps the last document that rendered.
|
|
420
593
|
if (result.html !== null) this.canvas.write(result.html, this.mode)
|
|
421
594
|
}
|
|
422
595
|
|
|
423
596
|
showErrors(errors) {
|
|
424
|
-
this.errorBar.textContent = errors.length ?
|
|
597
|
+
this.errorBar.textContent = errors.length ? `${this.kit.errors}: ${errors.join("; ")}` : ""
|
|
425
598
|
this.errorBar.classList.toggle("hidden", !errors.length)
|
|
426
599
|
}
|
|
427
600
|
|
|
428
601
|
async writeOutputs() {
|
|
602
|
+
this.loadTranslations()
|
|
429
603
|
const json = JSON.stringify(this.doc)
|
|
430
|
-
const
|
|
431
|
-
const result = await this.compileMjml(mjml, "outputs")
|
|
604
|
+
const result = await this.run("outputs", (options) => this.kit.outputs(this.doc, options))
|
|
432
605
|
if (!result) return
|
|
433
|
-
const
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
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 } })
|
|
438
611
|
}
|
|
439
612
|
|
|
440
613
|
// Applies a change to the document as one undo step.
|
|
@@ -559,7 +732,9 @@ export default class extends Controller {
|
|
|
559
732
|
|
|
560
733
|
drop(drag, parentId, index) {
|
|
561
734
|
if (!drag || !this.canFill(parentId) || (drag.kind === "move" && !this.isFree(drag.id))) return
|
|
562
|
-
if (drag.kind === "
|
|
735
|
+
if (drag.kind === "source") {
|
|
736
|
+
this.insertFromSource(drag.source, drag.item, { parentId, index })
|
|
737
|
+
} else if (drag.kind === "new") {
|
|
563
738
|
const node = createFromPalette(drag.key)
|
|
564
739
|
this.commit((doc) => insert(doc, parentId, index, node))
|
|
565
740
|
this.select(node.id)
|
|
@@ -582,21 +757,53 @@ export default class extends Controller {
|
|
|
582
757
|
// lock, at the end of the first unlocked block it fits in.
|
|
583
758
|
addFromPalette(key) {
|
|
584
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) {
|
|
585
769
|
const chain = [find(this.doc, this.selectedId)?.node || this.doc, ...ancestors(this.doc, this.selectedId)]
|
|
586
|
-
if (
|
|
770
|
+
if (fits(chain[0].id)) return { parentId: chain[0].id, index: chain[0].children.length }
|
|
587
771
|
for (let i = 0; i < chain.length - 1; i++) {
|
|
588
|
-
if (
|
|
772
|
+
if (fits(chain[i + 1].id)) return { parentId: chain[i + 1].id, index: chain[i + 1].children.indexOf(chain[i]) + 1 }
|
|
589
773
|
}
|
|
590
|
-
if (
|
|
774
|
+
if (fits(this.doc.id)) return { parentId: this.doc.id, index: this.doc.children.length }
|
|
591
775
|
if (this.lock) {
|
|
592
776
|
let target = null
|
|
593
777
|
walk(this.doc, (node) => {
|
|
594
778
|
if (target) return false
|
|
595
|
-
if (this.lock.except.includes(node.type) &&
|
|
779
|
+
if (this.lock.except.includes(node.type) && fits(node.id)) target = node
|
|
596
780
|
})
|
|
597
|
-
if (target) return
|
|
781
|
+
if (target) return { parentId: target.id, index: target.children.length }
|
|
782
|
+
}
|
|
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
|
|
598
795
|
}
|
|
599
|
-
|
|
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
|
|
600
807
|
}
|
|
601
808
|
|
|
602
809
|
openInsertMenu(parentId, index, rect) {
|
|
@@ -611,7 +818,7 @@ export default class extends Controller {
|
|
|
611
818
|
}
|
|
612
819
|
this.closeMenu()
|
|
613
820
|
const allowed = this.accepted(parentId)
|
|
614
|
-
const items = paletteItems().filter((item) => allowed.includes(item.type))
|
|
821
|
+
const items = this.paletteItems().filter((item) => allowed.includes(item.type))
|
|
615
822
|
if (!items.length) return
|
|
616
823
|
this.menu = h(
|
|
617
824
|
"div",
|
|
@@ -702,8 +909,8 @@ export default class extends Controller {
|
|
|
702
909
|
|
|
703
910
|
async openCodeDialog(tab = "json") {
|
|
704
911
|
this.stopEditing()
|
|
705
|
-
const { json,
|
|
706
|
-
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 }
|
|
707
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 })
|
|
708
915
|
const status = h("span", { class: "text-xs text-gray-500 dark:text-gray-400" })
|
|
709
916
|
const tabs = h("div", { class: "flex gap-1" })
|
|
@@ -726,9 +933,9 @@ export default class extends Controller {
|
|
|
726
933
|
apply.classList.toggle("hidden", !loadable)
|
|
727
934
|
status.className = "text-xs text-gray-500 dark:text-gray-400"
|
|
728
935
|
status.textContent =
|
|
729
|
-
key === "json" ? (this.lock ? "The document. Parts of it are locked, so it can't be loaded here." : "The document. Edit and load it, or paste one in.") : key === "
|
|
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] || ""
|
|
730
937
|
tabs.replaceChildren(
|
|
731
|
-
...
|
|
938
|
+
...Object.keys(contents).map((name) =>
|
|
732
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())
|
|
733
940
|
)
|
|
734
941
|
)
|
|
@@ -737,7 +944,7 @@ export default class extends Controller {
|
|
|
737
944
|
const close = () => dialog.remove()
|
|
738
945
|
const download = () => {
|
|
739
946
|
const blob = new Blob([textarea.value], { type: tab === "json" ? "application/json" : tab === "html" ? "text/html" : "text/plain" })
|
|
740
|
-
const link = h("a", { href: URL.createObjectURL(blob), download:
|
|
947
|
+
const link = h("a", { href: URL.createObjectURL(blob), download: `${this.kit.key}.${tab}` })
|
|
741
948
|
link.click()
|
|
742
949
|
URL.revokeObjectURL(link.href)
|
|
743
950
|
}
|