tess_edit 0.5.7 → 0.5.9
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 +15 -1
- data/app/javascript/tess_edit/controllers/editor_controller.js +25 -1
- data/app/javascript/tess_edit/core/values.js +83 -0
- data/app/javascript/tess_edit/ui/canvas.js +40 -8
- data/app/javascript/tess_edit/ui/text_editing.js +65 -2
- data/config/locales/en.yml +4 -0
- data/config/locales/nl.yml +4 -0
- data/lib/tess_edit/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 94b3e253b2d2bafa86ed6cf13e79149557163935761a6dc094e022149e9a3f92
|
|
4
|
+
data.tar.gz: 2294e04e5844d24e8db4644ee09d6920039f49478a2493ce0cb3613054385b2f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1bea8580aacae0b75388206bac1e907a72d72531bd1933f9b997d15f6c5064339206d3dcb86878390bb1c3c7bf9ba6c9dcdde9540b897d44758565b04a907a22
|
|
7
|
+
data.tar.gz: 564132d1c020d4cd5f6ef3ffbfbf589df954c639f52eafb05e8ef9b3c30d98b6b53dff59c7fde2a89ee9bdc39dad28528850f2690baab58156f9c527c2ae7c82
|
data/README.md
CHANGED
|
@@ -19,6 +19,7 @@ It completes Liquid tags and variables as you type, making it easier to work wit
|
|
|
19
19
|
- Rich text editing on the canvas with [Tiptap](https://tiptap.dev): bold, italic, underline, strikethrough, colour, links (Liquid allowed in the URL), alignment, lists and headings. A block can have several texts edited on the canvas, rich or a plain line, such as a question's label and its description.
|
|
20
20
|
- Your app's Liquid variables suggested while typing `{{`, in text and in settings, and browsable from `{ }` buttons, including those of the Loop blocks around a block.
|
|
21
21
|
- Your app's translations for Liquid's `t` filter (`{{ '.greeting' | t }}`) shown as their text in a language of your choice, with every language's text, missing ones marked, editable when pointed at, and selected text made translatable.
|
|
22
|
+
- Your app's values for the Liquid in a document (`{{ customer.first_name }}` as "Ada"), shown in its place until the caret goes into it.
|
|
22
23
|
- Clicking a tile in the Blocks panel adds the block in or after the selection, or else at the end of the last block it fits in.
|
|
23
24
|
- Undo/redo, duplicate, delete, keyboard shortcuts (⌘Z, ⇧⌘Z, ⌘D, Delete, Enter to edit text, Esc).
|
|
24
25
|
- Desktop and mobile preview, and a preview of the HTML as sent.
|
|
@@ -279,6 +280,19 @@ The endpoint answers, as JSON:
|
|
|
279
280
|
|
|
280
281
|
Where the texts are kept, and what a key starting with a dot is relative to, is up to your app. The dummy app's `TranslationsController` keeps them in memory (`/?translations=1`).
|
|
281
282
|
|
|
283
|
+
### Values
|
|
284
|
+
|
|
285
|
+
When the editor is for a record, such as an order's confirmation or a contract's text, your app can show what its Liquid comes to: give the values by expression, as it's written between the braces, filters and all, with its whitespace as one space:
|
|
286
|
+
|
|
287
|
+
```erb
|
|
288
|
+
<div data-controller="tess-edit" data-tess-edit-values-value='<%= {"customer.first_name" => "Ada", "order.total | money" => "€ 12,50"}.to_json %>'>
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
- The canvas shows each output tag with a value as that value, marked, with its Liquid when pointed at. One without a value, or with an empty one, shows its Liquid; `t` filters are translations.
|
|
292
|
+
- In text being edited on the canvas a value shows until the caret goes into its Liquid; a click on it does that.
|
|
293
|
+
|
|
294
|
+
A kit whose canvas your app renders can give them with the canvas, `{ html, errors, values }`, as they depend on the record as it's being edited, and `editor.setValues(values)` (on the element's `tessEdit`) sets them from anywhere; each replaces them all. `core/values.js` finds the expressions in a document (`expressionsIn`), to ask for theirs. The demo has some at `/?values=1`.
|
|
295
|
+
|
|
282
296
|
### Blocks and custom blocks
|
|
283
297
|
|
|
284
298
|
Every block is a plugin: one module in `app/javascript/tess_edit/blocks/` with its defaults, its Inspect fields, its Blocks panel tiles and a `render.mjml` hook. The built-in blocks are registered the same way a host app registers its own, so register yours before the editor connects:
|
|
@@ -415,7 +429,7 @@ cd test/dummy && bin/dev
|
|
|
415
429
|
|
|
416
430
|
`/?blocks=text,image` only offers those blocks.
|
|
417
431
|
|
|
418
|
-
`/?translations=1` gives the editor the demo's translations (`TranslationsController`, in memory, in English, Dutch and German). The options combine, e.g. `/?lock=1&media=1&variables=1&translations=1`.
|
|
432
|
+
`/?values=1` gives it values for the sample's customer and order. `/?translations=1` gives the editor the demo's translations (`TranslationsController`, in memory, in English, Dutch and German). The options combine, e.g. `/?lock=1&media=1&variables=1&translations=1`.
|
|
419
433
|
|
|
420
434
|
Tests:
|
|
421
435
|
|
|
@@ -32,6 +32,10 @@
|
|
|
32
32
|
// (see core/lock.js)
|
|
33
33
|
// blocks JSON: the only block types that can be added, e.g. ["text", "image"];
|
|
34
34
|
// without it, all of the kit's
|
|
35
|
+
// values JSON: the host's values for the document's Liquid, by expression
|
|
36
|
+
// ({"customer.first_name": "Ada"}), shown in place of it until the
|
|
37
|
+
// caret goes into it (see core/values.js); also from the kit's
|
|
38
|
+
// canvas result, or set with setValues
|
|
35
39
|
// options JSON: anything a host's blocks need from the page, as
|
|
36
40
|
// editor.options, e.g. the choices of a select field
|
|
37
41
|
// (options: (editor) => editor.options.parties)
|
|
@@ -56,6 +60,7 @@ import { DEFAULT_KIT, getBlock, getKit, inlinePart, inlineParts, paletteItems, p
|
|
|
56
60
|
import { COMPILE_URL } from "tess_edit/export/compile"
|
|
57
61
|
import { IMAGE_FIELD, fetchMedia, uploadMedia } from "tess_edit/core/media"
|
|
58
62
|
import { Translations, keysIn, suggestKey, toTranslation, translationTag } from "tess_edit/core/translations"
|
|
63
|
+
import { Values } from "tess_edit/core/values"
|
|
59
64
|
import { Variables, loopScope } from "tess_edit/core/variables"
|
|
60
65
|
import { Canvas } from "tess_edit/ui/canvas"
|
|
61
66
|
import { freeChild, number } from "tess_edit/ui/free_layout"
|
|
@@ -87,7 +92,7 @@ function placed(node, at) {
|
|
|
87
92
|
}
|
|
88
93
|
|
|
89
94
|
export default class extends Controller {
|
|
90
|
-
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, translationsUrl: String, lock: String, blocks: String, options: String }
|
|
95
|
+
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, translationsUrl: String, values: String, lock: String, blocks: String, options: String }
|
|
91
96
|
static targets = ["jsonInput", "mjmlInput", "htmlInput", "slot", "translations"]
|
|
92
97
|
|
|
93
98
|
connect() {
|
|
@@ -120,6 +125,8 @@ export default class extends Controller {
|
|
|
120
125
|
this.variables = new Variables(this.variablesUrlValue)
|
|
121
126
|
this.translations = new Translations(this.translationsUrlValue)
|
|
122
127
|
this.translationPopover = this.translations.enabled ? new TranslationPopover(this) : null
|
|
128
|
+
this.values = new Values()
|
|
129
|
+
this.values.set(this.parseJson(this.valuesValue, "values"))
|
|
123
130
|
this.translationLocaleChoice = this.stored(TRANSLATION_LOCALE_KEY)
|
|
124
131
|
this.variableMenu = this.variables.enabled || this.translations.enabled ? new VariableMenu(this) : null
|
|
125
132
|
|
|
@@ -139,9 +146,19 @@ export default class extends Controller {
|
|
|
139
146
|
this.translations.subscribe(() => this.showTranslations())
|
|
140
147
|
this.loadTranslations()
|
|
141
148
|
}
|
|
149
|
+
// Values show on the canvas and in text being edited
|
|
150
|
+
this.values.subscribe(() => {
|
|
151
|
+
this.canvas.annotate()
|
|
152
|
+
this.textEditing.refreshValues()
|
|
153
|
+
})
|
|
142
154
|
this.element.tessEdit = this
|
|
143
155
|
}
|
|
144
156
|
|
|
157
|
+
// Other values from the host, once it's connected
|
|
158
|
+
valuesValueChanged() {
|
|
159
|
+
if (this.values) this.values.set(this.parseJson(this.valuesValue, "values"))
|
|
160
|
+
}
|
|
161
|
+
|
|
145
162
|
// Points the editor at other variables (variablesUrl), once it's connected
|
|
146
163
|
variablesUrlValueChanged() {
|
|
147
164
|
if (!this.variables) return
|
|
@@ -335,6 +352,11 @@ export default class extends Controller {
|
|
|
335
352
|
return JSON.parse(JSON.stringify(this.doc))
|
|
336
353
|
}
|
|
337
354
|
|
|
355
|
+
// The host's values for the document's Liquid, by expression (core/values.js)
|
|
356
|
+
setValues(values) {
|
|
357
|
+
this.values.set(values)
|
|
358
|
+
}
|
|
359
|
+
|
|
338
360
|
setDocument(input) {
|
|
339
361
|
this.stopEditing()
|
|
340
362
|
this.history.record(JSON.stringify(this.doc))
|
|
@@ -686,6 +708,8 @@ export default class extends Controller {
|
|
|
686
708
|
if (this.editingId) return
|
|
687
709
|
const result = await this.run("canvas", (options) => this.kit.canvas(this.doc, { ...options, editor: this.mode === "edit" }))
|
|
688
710
|
if (!result || this.editingId) return
|
|
711
|
+
// A canvas the host renders can give the values it has
|
|
712
|
+
if (result.values) this.values.set(result.values)
|
|
689
713
|
this.showErrors(result.errors)
|
|
690
714
|
// Without HTML the canvas keeps the last document that rendered.
|
|
691
715
|
if (result.html !== null) this.canvas.write(result.html, this.mode)
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Values from the host app for the Liquid in a document, such as a
|
|
2
|
+
// contract's for {{ contract.account.name }} when the editor is a record's:
|
|
3
|
+
// the canvas and text being edited show an expression's value in place of
|
|
4
|
+
// its Liquid, and the Liquid again when the caret goes into it.
|
|
5
|
+
//
|
|
6
|
+
// The host gives them by the expression as it's written in the tag, without
|
|
7
|
+
// the braces, filters and all:
|
|
8
|
+
//
|
|
9
|
+
// { "contract.account.name": "sv COFA", "contract.starts_on | date: '%-d %B %Y'": "1 maart 2025" }
|
|
10
|
+
//
|
|
11
|
+
// with the kit's canvas result ({ html, errors, values }), for a canvas the
|
|
12
|
+
// host renders, or with the editor's setValues(values). An expression without
|
|
13
|
+
// one keeps its Liquid; t filters are translations (core/translations.js).
|
|
14
|
+
|
|
15
|
+
import { translationCalls } from "tess_edit/core/translations"
|
|
16
|
+
|
|
17
|
+
// An output tag, {{ … }}, with its expression
|
|
18
|
+
const OUTPUT = /\{\{-?\s*((?:(?!\}\}).)+?)\s*-?\}\}/g
|
|
19
|
+
|
|
20
|
+
// The expression of a tag as the host gets it: its whitespace as one space
|
|
21
|
+
export function normalizeExpression(expression) {
|
|
22
|
+
return String(expression ?? "").trim().replace(/\s+/g, " ")
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// The output tags in a text that aren't t filters: [{ start, end, expression, text }]
|
|
26
|
+
export function valueCalls(text) {
|
|
27
|
+
if (typeof text !== "string" || !text.includes("{{")) return []
|
|
28
|
+
const translations = translationCalls(text)
|
|
29
|
+
return Array.from(text.matchAll(OUTPUT), (match) => ({
|
|
30
|
+
start: match.index,
|
|
31
|
+
end: match.index + match[0].length,
|
|
32
|
+
expression: normalizeExpression(match[1]),
|
|
33
|
+
text: match[0]
|
|
34
|
+
})).filter((call) => !translations.some((translation) => translation.start === call.start))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Every expression in a document's texts and settings
|
|
38
|
+
export function expressionsIn(value, expressions = new Set()) {
|
|
39
|
+
if (typeof value === "string") {
|
|
40
|
+
for (const call of valueCalls(value)) expressions.add(call.expression)
|
|
41
|
+
} else if (Array.isArray(value)) {
|
|
42
|
+
value.forEach((item) => expressionsIn(item, expressions))
|
|
43
|
+
} else if (value && typeof value === "object") {
|
|
44
|
+
Object.values(value).forEach((item) => expressionsIn(item, expressions))
|
|
45
|
+
}
|
|
46
|
+
return expressions
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// The values the editor has from the host app, by expression
|
|
50
|
+
export class Values {
|
|
51
|
+
constructor() {
|
|
52
|
+
this.entries = new Map()
|
|
53
|
+
this.listeners = new Set()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get enabled() {
|
|
57
|
+
return this.entries.size > 0
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Calls fn whenever they change
|
|
61
|
+
subscribe(fn) {
|
|
62
|
+
this.listeners.add(fn)
|
|
63
|
+
return () => this.listeners.delete(fn)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Replaces them all; false when nothing changed. Empty values are none.
|
|
67
|
+
set(values) {
|
|
68
|
+
const entries = new Map()
|
|
69
|
+
for (const [expression, value] of Object.entries(values || {})) {
|
|
70
|
+
if (value === null || value === undefined || value === "") continue
|
|
71
|
+
entries.set(normalizeExpression(expression), String(value))
|
|
72
|
+
}
|
|
73
|
+
if (entries.size === this.entries.size && [...entries].every(([key, value]) => this.entries.get(key) === value)) return false
|
|
74
|
+
this.entries = entries
|
|
75
|
+
this.listeners.forEach((fn) => fn())
|
|
76
|
+
return true
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Its text, or null when there's none
|
|
80
|
+
value(expression) {
|
|
81
|
+
return this.entries.get(normalizeExpression(expression)) ?? null
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
// hover and selection outlines, block tools, plus buttons and drop indicators.
|
|
3
3
|
// Elements are mapped back to blocks through the "tess-<id>" classes that the
|
|
4
4
|
// editor-mode MJML puts on every element. With the host's translations, t
|
|
5
|
-
// filters show their text in the editor's language
|
|
5
|
+
// filters show their text in the editor's language, and with its values
|
|
6
|
+
// other Liquid output shows its value (see annotate).
|
|
6
7
|
|
|
7
8
|
import { ancestors, find, isDescendant, isHorizontal } from "tess_edit/core/document"
|
|
8
9
|
import { t } from "tess_edit/core/i18n"
|
|
9
10
|
import { imageFields } from "tess_edit/core/media"
|
|
10
11
|
import { getBlock, inlinePart, label } from "tess_edit/core/registry"
|
|
11
12
|
import { missingArguments, textParts, translationCalls } from "tess_edit/core/translations"
|
|
13
|
+
import { valueCalls } from "tess_edit/core/values"
|
|
12
14
|
import { FREE_CSS, FreeLayout } from "tess_edit/ui/free_layout"
|
|
13
15
|
import { icon } from "tess_edit/ui/icons"
|
|
14
16
|
|
|
@@ -46,6 +48,9 @@ a{cursor:default}
|
|
|
46
48
|
.tess-t-arg{border-radius:3px;padding:0 2px;background:rgba(37,99,235,.18);font-size:.9em}
|
|
47
49
|
.tess-t-missing .tess-t-arg{background:rgba(220,38,38,.18)}
|
|
48
50
|
.tess-t-hidden{display:none}
|
|
51
|
+
.tess-v-filled,.tess-v-raw{border-radius:3px;background:rgba(5,150,105,.08);box-shadow:inset 0 -1px 0 rgba(5,150,105,.55)}
|
|
52
|
+
.ProseMirror .tess-v-filled{cursor:text}
|
|
53
|
+
.tess-v-hidden{display:none}
|
|
49
54
|
`
|
|
50
55
|
|
|
51
56
|
// A file dragged in from outside the browser (block drags set editor.drag)
|
|
@@ -129,30 +134,42 @@ export class Canvas {
|
|
|
129
134
|
doc.addEventListener("mouseout", (event) => event.target.closest?.("[data-tess-key]") && this.editor.translationPopover?.scheduleHide())
|
|
130
135
|
}
|
|
131
136
|
|
|
132
|
-
// --- translations
|
|
137
|
+
// --- translations and values -----------------------------------------------
|
|
133
138
|
|
|
134
139
|
// Wraps every t filter in the email's text in a span that shows its text
|
|
135
|
-
// (showTranslations),
|
|
140
|
+
// (showTranslations), and with the host's values every other output tag in
|
|
141
|
+
// one that shows its value (showValues), keeping the Liquid for when there's
|
|
142
|
+
// none. Again when the values change: what's wrapped stays as it is.
|
|
136
143
|
annotate() {
|
|
137
|
-
|
|
144
|
+
const translating = Boolean(this.editor.translations?.enabled)
|
|
145
|
+
const valuing = Boolean(this.editor.values?.enabled)
|
|
146
|
+
if (!this.doc?.body) return
|
|
147
|
+
// Without values any more, what shows one shows its Liquid again
|
|
148
|
+
if (!translating && !valuing) return this.showValues()
|
|
138
149
|
const doc = this.doc
|
|
139
150
|
const walker = doc.createTreeWalker(doc.body, 4 /* NodeFilter.SHOW_TEXT */)
|
|
140
151
|
const nodes = []
|
|
141
152
|
while (walker.nextNode()) {
|
|
142
153
|
const node = walker.currentNode
|
|
143
|
-
if (node.nodeValue.includes("{{") && !node.parentElement?.closest("#tess-overlay, script, style, title")) nodes.push(node)
|
|
154
|
+
if (node.nodeValue.includes("{{") && !node.parentElement?.closest("#tess-overlay, script, style, title, .tess-t, .tess-v, .ProseMirror")) nodes.push(node)
|
|
144
155
|
}
|
|
145
156
|
for (const node of nodes) {
|
|
146
157
|
const text = node.nodeValue
|
|
147
|
-
const calls = translationCalls(text)
|
|
158
|
+
const calls = [...(translating ? translationCalls(text) : []), ...(valuing ? valueCalls(text).map((call) => ({ ...call, value: true })) : [])].sort((a, b) => a.start - b.start)
|
|
148
159
|
if (!calls.length) continue
|
|
149
160
|
const fragment = doc.createDocumentFragment()
|
|
150
161
|
let at = 0
|
|
151
162
|
for (const call of calls) {
|
|
152
163
|
fragment.append(text.slice(at, call.start))
|
|
153
164
|
const span = doc.createElement("span")
|
|
154
|
-
|
|
155
|
-
|
|
165
|
+
if (call.value) {
|
|
166
|
+
span.className = "tess-v"
|
|
167
|
+
Object.assign(span.dataset, { tessExpression: call.expression, tessCall: call.text })
|
|
168
|
+
span.textContent = call.text
|
|
169
|
+
} else {
|
|
170
|
+
span.className = "tess-t"
|
|
171
|
+
Object.assign(span.dataset, { tessKey: call.key, tessCall: call.text, tessArgs: call.args.join(",") }, call.locale ? { tessLocale: call.locale } : {})
|
|
172
|
+
}
|
|
156
173
|
fragment.append(span)
|
|
157
174
|
at = call.end
|
|
158
175
|
}
|
|
@@ -160,6 +177,21 @@ export class Canvas {
|
|
|
160
177
|
node.replaceWith(fragment)
|
|
161
178
|
}
|
|
162
179
|
this.showTranslations()
|
|
180
|
+
this.showValues()
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Each output tag's value, its Liquid when pointed at, or the Liquid when
|
|
184
|
+
// there's none
|
|
185
|
+
showValues() {
|
|
186
|
+
const values = this.editor.values
|
|
187
|
+
if (!values || !this.doc?.body) return
|
|
188
|
+
for (const span of this.doc.querySelectorAll(".tess-v[data-tess-call]")) {
|
|
189
|
+
const value = values.value(span.dataset.tessExpression)
|
|
190
|
+
span.textContent = value ?? span.dataset.tessCall
|
|
191
|
+
span.title = value === null ? "" : span.dataset.tessCall
|
|
192
|
+
span.classList.toggle("tess-v-filled", value !== null)
|
|
193
|
+
}
|
|
194
|
+
this.refresh()
|
|
163
195
|
}
|
|
164
196
|
|
|
165
197
|
// The text in the editor's language (or the call's own), or the Liquid when
|
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
// suggests them, and the toolbar's { } button browses them. With the host's
|
|
5
5
|
// translations (`editor.translations`, core/translations.js) a t filter shows
|
|
6
6
|
// its text in the editor's language until the caret goes into it, and the
|
|
7
|
-
// toolbar turns selected text into a translation.
|
|
7
|
+
// toolbar turns selected text into a translation. With the host's values
|
|
8
|
+
// (`editor.values`, core/values.js) other Liquid output shows its value the
|
|
9
|
+
// same way.
|
|
8
10
|
//
|
|
9
11
|
// A plain part (a block's inline part with plain: true, see core/registry.js)
|
|
10
12
|
// is one line of text, edited on its element itself, with no toolbar or
|
|
@@ -13,6 +15,7 @@
|
|
|
13
15
|
import { Color, Decoration, DecorationSet, Editor, Extension, Plugin, PluginKey, StarterKit, TextAlign, TextStyle } from "tess_edit/vendor/tiptap"
|
|
14
16
|
import { t } from "tess_edit/core/i18n"
|
|
15
17
|
import { missingArguments, translationCalls } from "tess_edit/core/translations"
|
|
18
|
+
import { valueCalls } from "tess_edit/core/values"
|
|
16
19
|
import { showText } from "tess_edit/ui/canvas"
|
|
17
20
|
import { askText } from "tess_edit/ui/dialog"
|
|
18
21
|
import { h } from "tess_edit/ui/dom"
|
|
@@ -21,6 +24,7 @@ import { icon } from "tess_edit/ui/icons"
|
|
|
21
24
|
const BUTTON = "w-7 h-7 flex items-center justify-center rounded-md text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 hover:text-gray-900 dark:hover:text-gray-100"
|
|
22
25
|
const ACTIVE = "bg-blue-50 dark:bg-blue-900/40 text-blue-700 dark:text-blue-300"
|
|
23
26
|
const TRANSLATIONS = new PluginKey("tessTranslations")
|
|
27
|
+
const VALUES = new PluginKey("tessValues")
|
|
24
28
|
|
|
25
29
|
export class TextEditing {
|
|
26
30
|
constructor(toolbar, { menu = null, editor = null } = {}) {
|
|
@@ -93,7 +97,8 @@ export class TextEditing {
|
|
|
93
97
|
TextStyle,
|
|
94
98
|
Color,
|
|
95
99
|
TextAlign.configure({ types: ["heading", "paragraph"] }),
|
|
96
|
-
...(this.translating ? [translationsExtension(this)] : [])
|
|
100
|
+
...(this.translating ? [translationsExtension(this)] : []),
|
|
101
|
+
...(this.editor?.values ? [valuesExtension(this)] : [])
|
|
97
102
|
],
|
|
98
103
|
autofocus: "end",
|
|
99
104
|
editorProps: { handleKeyDown: (view, event) => this.menu?.key(event) || false },
|
|
@@ -115,6 +120,11 @@ export class TextEditing {
|
|
|
115
120
|
if (this.tiptap && this.translating) this.tiptap.view.dispatch(this.tiptap.state.tr.setMeta(TRANSLATIONS, true))
|
|
116
121
|
}
|
|
117
122
|
|
|
123
|
+
// Again, when the values change
|
|
124
|
+
refreshValues() {
|
|
125
|
+
if (this.tiptap && this.editor?.values) this.tiptap.view.dispatch(this.tiptap.state.tr.setMeta(VALUES, true))
|
|
126
|
+
}
|
|
127
|
+
|
|
118
128
|
// Keeps the selected text as a translation (see the editor's
|
|
119
129
|
// makeTranslatable) and puts its t filter in its place
|
|
120
130
|
async makeTranslatable() {
|
|
@@ -330,3 +340,56 @@ function translationChip(view, key, value, unfilled, edit) {
|
|
|
330
340
|
})
|
|
331
341
|
return chip
|
|
332
342
|
}
|
|
343
|
+
|
|
344
|
+
// Shows each output tag's value in place of its Liquid, unless the caret or
|
|
345
|
+
// selection is inside it or it has none: then its Liquid shows, marked when
|
|
346
|
+
// it has one. A click on the value puts the caret in the Liquid.
|
|
347
|
+
function valuesExtension(editing) {
|
|
348
|
+
return Extension.create({
|
|
349
|
+
name: "tessValues",
|
|
350
|
+
addProseMirrorPlugins() {
|
|
351
|
+
return [new Plugin({ key: VALUES, props: { decorations: (state) => valueDecorations(state, editing) } })]
|
|
352
|
+
}
|
|
353
|
+
})
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function valueDecorations(state, editing) {
|
|
357
|
+
const values = editing.editor.values
|
|
358
|
+
if (!values?.enabled) return DecorationSet.empty
|
|
359
|
+
const { from, to } = state.selection
|
|
360
|
+
const decorations = []
|
|
361
|
+
state.doc.descendants((node, pos) => {
|
|
362
|
+
if (!node.isTextblock) return true
|
|
363
|
+
// Every character and inline node takes one position
|
|
364
|
+
const text = node.textBetween(0, node.content.size, undefined, "\ufffc")
|
|
365
|
+
for (const call of valueCalls(text)) {
|
|
366
|
+
const value = values.value(call.expression)
|
|
367
|
+
if (value === null) continue
|
|
368
|
+
const start = pos + 1 + call.start
|
|
369
|
+
const end = pos + 1 + call.end
|
|
370
|
+
if (from < end && to > start) {
|
|
371
|
+
decorations.push(Decoration.inline(start, end, { class: "tess-v-raw" }))
|
|
372
|
+
} else {
|
|
373
|
+
decorations.push(Decoration.inline(start, end, { class: "tess-v-hidden" }))
|
|
374
|
+
// In the expression, after its first word
|
|
375
|
+
const caret = start + call.text.indexOf(call.expression) + Math.max(1, call.expression.search(/[\s|]|$/))
|
|
376
|
+
decorations.push(Decoration.widget(start, (view) => valueChip(view, call, value, () => editing.tiptap?.chain().focus().setTextSelection(caret).run()), { side: -1, key: `v:${call.expression}:${value}`, ignoreSelection: true }))
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return false
|
|
380
|
+
})
|
|
381
|
+
return DecorationSet.create(state.doc, decorations)
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function valueChip(view, call, value, edit) {
|
|
385
|
+
const chip = view.dom.ownerDocument.createElement("span")
|
|
386
|
+
chip.className = "tess-v tess-v-filled"
|
|
387
|
+
chip.contentEditable = "false"
|
|
388
|
+
chip.title = call.text
|
|
389
|
+
chip.textContent = value
|
|
390
|
+
chip.addEventListener("mousedown", (event) => {
|
|
391
|
+
event.preventDefault()
|
|
392
|
+
edit()
|
|
393
|
+
})
|
|
394
|
+
return chip
|
|
395
|
+
}
|
data/config/locales/en.yml
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
en:
|
|
5
5
|
tess_edit:
|
|
6
6
|
editor:
|
|
7
|
+
1_column: "1 column"
|
|
8
|
+
2_columns: "2 columns"
|
|
9
|
+
3_columns: "3 columns"
|
|
10
|
+
4_columns: "4 columns"
|
|
7
11
|
a_key_is_made_of_letters_digits_and_separated_by_dots_e_g_or: "A key is made of letters, digits, _ and -, separated by dots, e.g. .order.thanks"
|
|
8
12
|
a_key_starting_with_a_dot_belongs_to_this_template: "A key starting with a dot belongs to this template."
|
|
9
13
|
a_liquid_condition_e_g_order_total_100: "A Liquid condition, e.g. order.total > 100."
|
data/config/locales/nl.yml
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
nl:
|
|
5
5
|
tess_edit:
|
|
6
6
|
editor:
|
|
7
|
+
1_column: "1 kolom"
|
|
8
|
+
2_columns: "2 kolommen"
|
|
9
|
+
3_columns: "3 kolommen"
|
|
10
|
+
4_columns: "4 kolommen"
|
|
7
11
|
a_key_is_made_of_letters_digits_and_separated_by_dots_e_g_or: "Een sleutel bestaat uit letters, cijfers, _ en -, gescheiden door punten, bijv. .order.thanks"
|
|
8
12
|
a_key_starting_with_a_dot_belongs_to_this_template: "Een sleutel die met een punt begint, hoort bij deze template."
|
|
9
13
|
a_liquid_condition_e_g_order_total_100: "Een Liquid-voorwaarde, bijv. order.total > 100."
|
data/lib/tess_edit/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: tess_edit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.5.
|
|
4
|
+
version: 0.5.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tom de Grunt
|
|
@@ -108,6 +108,7 @@ files:
|
|
|
108
108
|
- app/javascript/tess_edit/core/registry.js
|
|
109
109
|
- app/javascript/tess_edit/core/starter.js
|
|
110
110
|
- app/javascript/tess_edit/core/translations.js
|
|
111
|
+
- app/javascript/tess_edit/core/values.js
|
|
111
112
|
- app/javascript/tess_edit/core/variables.js
|
|
112
113
|
- app/javascript/tess_edit/export/compile.js
|
|
113
114
|
- app/javascript/tess_edit/export/mjml.js
|