tess_edit 0.1.1 → 0.2.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 +25 -1
- data/app/javascript/tess_edit/blocks/button.js +2 -2
- data/app/javascript/tess_edit/blocks/condition.js +1 -1
- data/app/javascript/tess_edit/blocks/fields.js +2 -1
- data/app/javascript/tess_edit/blocks/image.js +3 -3
- data/app/javascript/tess_edit/blocks/loop.js +1 -1
- data/app/javascript/tess_edit/blocks/root.js +2 -2
- data/app/javascript/tess_edit/controllers/editor_controller.js +11 -3
- data/app/javascript/tess_edit/core/media.js +1 -1
- data/app/javascript/tess_edit/core/variables.js +146 -0
- data/app/javascript/tess_edit/ui/inspector.js +28 -6
- data/app/javascript/tess_edit/ui/text_editing.js +60 -5
- data/app/javascript/tess_edit/ui/variable_menu.js +232 -0
- data/lib/tess_edit/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fb9045dd564b3a5af5755160f42b2cf660899f889374880040de6b3cf220833e
|
|
4
|
+
data.tar.gz: c56b77d27bb42274be6230b2f937e8c1d985de448d4cbe7707a273578f528b4e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3c12a4fa0b1b9d90157a18803f5a6f66d7a92d23b701e338ca836c32fccc2ef566a94824e9db74ad25f494117e4424ee17a62736239dcfbefcffb6c277d2826a
|
|
7
|
+
data.tar.gz: a402b856964cf728c68f13462641aefa2201bba09402af78b8541f95f37fda2f7957f7bc8773deea920fafa75b693910145c6145a1b3d35424790e7db66cf7fc
|
data/README.md
CHANGED
|
@@ -6,12 +6,16 @@ The interface follows familiar layout: blocks and a layers tree on the left, the
|
|
|
6
6
|
|
|
7
7
|

|
|
8
8
|
|
|
9
|
+
It completes Liquid tags and variables as you type, making it easier to work with dynamic content.
|
|
10
|
+

|
|
11
|
+
|
|
9
12
|
## Features
|
|
10
13
|
|
|
11
14
|
- Blocks: Section (1-4 columns), Column, Wrapper, Hero, Text, Heading, Image, Button, Divider, Spacer, Social, Navbar and raw HTML.
|
|
12
15
|
- Liquid blocks: **Loop** (`{% for line in order.lines %}`) and **Condition** (`{% if order.total > 100 %}`) wrap other blocks.
|
|
13
16
|
- Drag from the Blocks panel onto the canvas or into the Layers tree; move blocks on the canvas (drag handle) or in the tree; plus buttons and empty-column placeholders open an "Add block" menu.
|
|
14
17
|
- 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.
|
|
18
|
+
- 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.
|
|
15
19
|
- Undo/redo, duplicate, delete, keyboard shortcuts (⌘Z, ⇧⌘Z, ⌘D, Delete, Enter to edit text, Esc).
|
|
16
20
|
- Desktop and mobile preview, and a preview of the HTML as sent.
|
|
17
21
|
- Panels on both sides, or combined as tabs in one sidebar on the left or right to give the canvas more room.
|
|
@@ -146,6 +150,24 @@ A block stores the image's `url` in the attribute (`src`, `background-url`) and
|
|
|
146
150
|
|
|
147
151
|
A block of your own gets the same with an image field, `imageField("attributes.background-url", "Background image", "data.background")`, imported from `tess_edit`.
|
|
148
152
|
|
|
153
|
+
### Variables
|
|
154
|
+
|
|
155
|
+
To suggest the variables a template can use, give the editor an endpoint with `data-tess-edit-variables-url-value`. Typing `{{` in text on the canvas, or in a setting such as a link, subject or image address, then shows them, one level at a time: `order.` shows the order's variables, `order.customer.` the customer's. Arrow keys and Enter or Tab choose, Escape closes. The text toolbar, and settings that usually hold Liquid, have a `{ }` button to browse them instead. A Loop's collection and a Condition are suggested without the braces.
|
|
156
|
+
|
|
157
|
+
The endpoint answers `GET` with the variables at `path` (the roots without one), as JSON:
|
|
158
|
+
|
|
159
|
+
```json
|
|
160
|
+
{"path": "order", "variables": [
|
|
161
|
+
{"name": "customer", "label": "Ordered by", "type": "object", "collection": false, "children": true},
|
|
162
|
+
{"name": "order_lines", "label": "Order lines", "type": "object", "collection": true, "children": true},
|
|
163
|
+
{"name": "total", "label": "Total", "type": "money", "hint": "Including VAT", "collection": false, "children": false}
|
|
164
|
+
]}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
`children` says a variable has variables of its own, at the path with its name added; a `collection` is a list of them, and its path goes into its elements (`order.order_lines.product`). The editor offers a collection's `first`, `last` and `size`, and inside a Loop block (`{% for line in order.order_lines %}`) its variable, `line`, asking for `order.order_lines.…` for it. An unknown path answers 404. [Liquidum](https://github.com/entdec/liquidum)'s `/liquidum/variables` answers this way from your drops; the dummy app's `VariablesController` is a fixed example (`/?variables=1`).
|
|
168
|
+
|
|
169
|
+
A field of your own gets the `{ }` button with `liquid: true`, or `liquid: "expression"` (a condition) or `liquid: "collection"` (what a loop goes over) for Liquid without braces.
|
|
170
|
+
|
|
149
171
|
### Blocks and custom blocks
|
|
150
172
|
|
|
151
173
|
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:
|
|
@@ -186,7 +208,9 @@ cd test/dummy && bin/dev
|
|
|
186
208
|
|
|
187
209
|
`/?lock=1` opens a locked layout (`test/dummy/config/demo_layout.json`) with only its Content block, subject and preview text open.
|
|
188
210
|
|
|
189
|
-
`/?media=1` gives the editor the demo's image endpoint (`MediaController`), so images can be chosen from a list, uploaded, or dragged onto the canvas from the desktop; uploads go to `test/dummy/tmp/media`. Without it, images are URLs only.
|
|
211
|
+
`/?media=1` gives the editor the demo's image endpoint (`MediaController`), so images can be chosen from a list, uploaded, or dragged onto the canvas from the desktop; uploads go to `test/dummy/tmp/media`. Without it, images are URLs only.
|
|
212
|
+
|
|
213
|
+
`/?variables=1` gives the editor the demo's variables (`VariablesController`), an order's, to try the suggestions. The options combine, e.g. `/?lock=1&media=1&variables=1`.
|
|
190
214
|
|
|
191
215
|
Tests:
|
|
192
216
|
|
|
@@ -14,8 +14,8 @@ export default {
|
|
|
14
14
|
{
|
|
15
15
|
title: "Button",
|
|
16
16
|
fields: [
|
|
17
|
-
{ key: "data.content", label: "Label", type: "text" },
|
|
18
|
-
{ key: "attributes.href", label: "Link", type: "text" },
|
|
17
|
+
{ key: "data.content", label: "Label", type: "text", liquid: true },
|
|
18
|
+
{ key: "attributes.href", label: "Link", type: "text", liquid: true },
|
|
19
19
|
{ key: "attributes.target", label: "Open in", type: "segmented", options: [{ value: "_blank", label: "New tab" }, { value: "_self", label: "Same tab" }] },
|
|
20
20
|
{ key: "attributes.background-color", label: "Button color", type: "color" },
|
|
21
21
|
{ key: "attributes.width", label: "Width", type: "size", placeholder: "Auto" },
|
|
@@ -6,7 +6,7 @@ export default {
|
|
|
6
6
|
roles: ["body", "content"],
|
|
7
7
|
accepts: "inherit",
|
|
8
8
|
defaults: () => ({ data: { expression: "customer.vip" } }),
|
|
9
|
-
fields: [{ title: "Condition", fields: [{ key: "data.expression", label: "Show when", type: "text", hint: "A Liquid condition, e.g. order.total > 100." }] }],
|
|
9
|
+
fields: [{ title: "Condition", fields: [{ key: "data.expression", label: "Show when", type: "text", liquid: "expression", hint: "A Liquid condition, e.g. order.total > 100." }] }],
|
|
10
10
|
hint: "Shows its blocks only when the condition is true: {% if expression %}.",
|
|
11
11
|
label: (node) => `If ${node.data.expression || "…"}`,
|
|
12
12
|
render: {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// Field definitions shared by the built-in blocks, and usable by custom ones.
|
|
2
2
|
// A field: { key: "attributes.padding" | "data.subject", label, type, ... }.
|
|
3
3
|
// Types: text, textarea, code, size, color, select, segmented, toggle,
|
|
4
|
-
// spacing and list; see ui/inspector.js.
|
|
4
|
+
// spacing and list; see ui/inspector.js. `liquid` gives a field a button to
|
|
5
|
+
// insert the host's variables (see Inspector#variables).
|
|
5
6
|
|
|
6
7
|
export const ALIGN = [
|
|
7
8
|
{ value: "left", label: "Left" },
|
|
@@ -14,9 +14,9 @@ export default {
|
|
|
14
14
|
{
|
|
15
15
|
title: "Image",
|
|
16
16
|
fields: [
|
|
17
|
-
{ ...IMAGE_FIELD, label: "Image", type: "image" },
|
|
18
|
-
{ key: "attributes.alt", label: "Alt text", type: "text" },
|
|
19
|
-
{ key: "attributes.href", label: "Link", type: "text" },
|
|
17
|
+
{ ...IMAGE_FIELD, label: "Image", type: "image", liquid: true },
|
|
18
|
+
{ key: "attributes.alt", label: "Alt text", type: "text", liquid: true },
|
|
19
|
+
{ key: "attributes.href", label: "Link", type: "text", liquid: true },
|
|
20
20
|
{ key: "attributes.width", label: "Width", type: "size", placeholder: "Full" },
|
|
21
21
|
{ key: "attributes.height", label: "Height", type: "size", placeholder: "Auto" },
|
|
22
22
|
align,
|
|
@@ -11,7 +11,7 @@ export default {
|
|
|
11
11
|
title: "Loop",
|
|
12
12
|
fields: [
|
|
13
13
|
{ key: "data.variable", label: "Variable", type: "text", hint: "The name each item gets, e.g. line." },
|
|
14
|
-
{ key: "data.collection", label: "Collection", type: "text", hint: "What to loop over, e.g. invoice.lines." }
|
|
14
|
+
{ key: "data.collection", label: "Collection", type: "text", liquid: "collection", hint: "What to loop over, e.g. invoice.lines." }
|
|
15
15
|
]
|
|
16
16
|
}
|
|
17
17
|
],
|
|
@@ -24,8 +24,8 @@ export default {
|
|
|
24
24
|
{
|
|
25
25
|
title: "Email",
|
|
26
26
|
fields: [
|
|
27
|
-
{ key: "data.subject", label: "Subject", type: "text" },
|
|
28
|
-
{ key: "data.preheader", label: "Preview text", type: "text", hint: "Shown after the subject in most inboxes." }
|
|
27
|
+
{ key: "data.subject", label: "Subject", type: "text", liquid: true },
|
|
28
|
+
{ key: "data.preheader", label: "Preview text", type: "text", liquid: true, hint: "Shown after the subject in most inboxes." }
|
|
29
29
|
]
|
|
30
30
|
},
|
|
31
31
|
{
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
// the engine mounted at /tess_edit)
|
|
13
13
|
// mediaUrl the host's images, chosen and uploaded in Image blocks (see
|
|
14
14
|
// core/media.js); without it images only have a URL
|
|
15
|
+
// variablesUrl the variables a template can use, suggested while typing {{
|
|
16
|
+
// and browsed with { } buttons (see core/variables.js), e.g.
|
|
17
|
+
// Liquidum's /liquidum/variables?roots=…
|
|
15
18
|
// lock JSON: only the insides of some blocks and some root fields can
|
|
16
19
|
// change, e.g. {"except": ["content"], "fields": ["data.subject"]}
|
|
17
20
|
// (see core/lock.js)
|
|
@@ -30,10 +33,12 @@ import { starterDocument } from "tess_edit/core/starter"
|
|
|
30
33
|
import { toMjml } from "tess_edit/export/mjml"
|
|
31
34
|
import { COMPILE_URL, compile } from "tess_edit/export/compile"
|
|
32
35
|
import { IMAGE_FIELD, fetchMedia, uploadMedia } from "tess_edit/core/media"
|
|
36
|
+
import { Variables, loopScope } from "tess_edit/core/variables"
|
|
33
37
|
import { Canvas } from "tess_edit/ui/canvas"
|
|
34
38
|
import { Tree } from "tess_edit/ui/tree"
|
|
35
39
|
import { Inspector } from "tess_edit/ui/inspector"
|
|
36
40
|
import { TextEditing } from "tess_edit/ui/text_editing"
|
|
41
|
+
import { VariableMenu } from "tess_edit/ui/variable_menu"
|
|
37
42
|
import { renderBlocksPanel } from "tess_edit/ui/blocks_panel"
|
|
38
43
|
import { debounce, h } from "tess_edit/ui/dom"
|
|
39
44
|
import { icon } from "tess_edit/ui/icons"
|
|
@@ -46,7 +51,7 @@ const PANELS_KEY = "tess_edit:panels"
|
|
|
46
51
|
const NARROW_WIDTH = 1000
|
|
47
52
|
|
|
48
53
|
export default class extends Controller {
|
|
49
|
-
static values = { document: String, sample: { type: Boolean, default: true }, panels: { type: String, default: "split" }, compileUrl: { type: String, default: COMPILE_URL }, mediaUrl: String, lock: String }
|
|
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 }
|
|
50
55
|
static targets = ["jsonInput", "mjmlInput", "htmlInput"]
|
|
51
56
|
|
|
52
57
|
connect() {
|
|
@@ -63,6 +68,8 @@ export default class extends Controller {
|
|
|
63
68
|
this.narrow = false
|
|
64
69
|
this.requests = {}
|
|
65
70
|
this.media = { enabled: Boolean(this.mediaUrlValue), images: [], uploading: false, error: null }
|
|
71
|
+
this.variables = new Variables(this.variablesUrlValue)
|
|
72
|
+
this.variableMenu = this.variables.enabled ? new VariableMenu(this) : null
|
|
66
73
|
|
|
67
74
|
this.scheduleCanvas = debounce(() => this.renderCanvas(), 60)
|
|
68
75
|
this.scheduleOutputs = debounce(() => this.writeOutputs(), 300)
|
|
@@ -127,6 +134,7 @@ export default class extends Controller {
|
|
|
127
134
|
this.scheduleOutputs.cancel()
|
|
128
135
|
Object.values(this.requests).forEach((request) => request.abort())
|
|
129
136
|
this.textEditing?.stop()
|
|
137
|
+
this.variableMenu?.close()
|
|
130
138
|
this.closeMenu()
|
|
131
139
|
this.resizeObserver?.disconnect()
|
|
132
140
|
document.removeEventListener("mousedown", this.outsideClick)
|
|
@@ -216,7 +224,7 @@ export default class extends Controller {
|
|
|
216
224
|
this.canvas = new Canvas(this, this.frame)
|
|
217
225
|
this.tree = new Tree(this, this.treeElement)
|
|
218
226
|
this.inspector = new Inspector(this, this.inspectorElement)
|
|
219
|
-
this.textEditing = new TextEditing(this.textToolbar)
|
|
227
|
+
this.textEditing = new TextEditing(this.textToolbar, { menu: this.variableMenu })
|
|
220
228
|
renderBlocksPanel(this, this.blocksElement)
|
|
221
229
|
this.observeWidth()
|
|
222
230
|
|
|
@@ -500,7 +508,7 @@ export default class extends Controller {
|
|
|
500
508
|
this.tree.render()
|
|
501
509
|
this.canvas.refresh()
|
|
502
510
|
this.scheduleOutputs()
|
|
503
|
-
})
|
|
511
|
+
}, { scope: loopScope(this.doc, id) })
|
|
504
512
|
this.canvas.refresh()
|
|
505
513
|
}
|
|
506
514
|
|
|
@@ -38,7 +38,7 @@ export const IMAGE_FIELD = { key: "attributes.src", image: "data.image", source:
|
|
|
38
38
|
// An image field for an image attribute, e.g. a background:
|
|
39
39
|
// imageField("attributes.background-url", "Background image", "data.background")
|
|
40
40
|
export function imageField(key, label, image) {
|
|
41
|
-
return { key, label, type: "image", image, source: `${image}Source
|
|
41
|
+
return { key, label, type: "image", image, source: `${image}Source`, liquid: true }
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
// The image fields of a block definition
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// The variables a template can use, from the host app. The editor's
|
|
2
|
+
// variablesUrl answers one level at a time:
|
|
3
|
+
//
|
|
4
|
+
// GET ?path=order.customer → { path, variables: [{ name, label, type, hint, collection, children }] }
|
|
5
|
+
//
|
|
6
|
+
// with the roots (order, …) for an empty path. `children` says a variable has
|
|
7
|
+
// variables of its own; a `collection` is a list of them. The path goes into
|
|
8
|
+
// a collection's elements: order.order_lines.product. Liquidum's
|
|
9
|
+
// VariablesController answers this way.
|
|
10
|
+
//
|
|
11
|
+
// In Liquid a collection's elements are reached through a Loop block's
|
|
12
|
+
// variable ({% for line in order.order_lines %} → line.product) or with
|
|
13
|
+
// first and last (order.order_lines.first.product), so those are resolved to
|
|
14
|
+
// such a path before asking.
|
|
15
|
+
|
|
16
|
+
import { ancestors } from "tess_edit/core/document"
|
|
17
|
+
|
|
18
|
+
// The members Liquid gives every collection
|
|
19
|
+
export const COLLECTION_MEMBERS = [
|
|
20
|
+
{ name: "first", label: "First", type: "object", children: true },
|
|
21
|
+
{ name: "last", label: "Last", type: "object", children: true },
|
|
22
|
+
{ name: "size", label: "Number of items", type: "integer", children: false }
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
const ELEMENT = new Set(["first", "last"])
|
|
26
|
+
|
|
27
|
+
export class Variables {
|
|
28
|
+
constructor(url, { fetch: fetcher = (...args) => fetch(...args) } = {}) {
|
|
29
|
+
this.url = url
|
|
30
|
+
this.fetch = fetcher
|
|
31
|
+
this.levels = new Map()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get enabled() {
|
|
35
|
+
return Boolean(this.url)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// The host's variables at a path (as the endpoint takes it), cached
|
|
39
|
+
at(path) {
|
|
40
|
+
if (!this.levels.has(path)) {
|
|
41
|
+
const request = this.request(path)
|
|
42
|
+
request.catch(() => this.levels.delete(path))
|
|
43
|
+
this.levels.set(path, request)
|
|
44
|
+
}
|
|
45
|
+
return this.levels.get(path)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async request(path) {
|
|
49
|
+
const url = new URL(this.url, window.location.href)
|
|
50
|
+
if (path) url.searchParams.set("path", path)
|
|
51
|
+
const response = await this.fetch(url, { headers: { Accept: "application/json" }, credentials: "same-origin" })
|
|
52
|
+
if (response.status === 404) return []
|
|
53
|
+
if (!response.ok) throw new Error(`Loading the variables failed (${response.status} ${response.statusText})`)
|
|
54
|
+
return (await response.json()).variables || []
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The variables at a path as written in the template, e.g. "line.product"
|
|
58
|
+
// in a loop over order.order_lines, or "order.order_lines" (a collection:
|
|
59
|
+
// first, last and size). scope is from loopScope.
|
|
60
|
+
async options(path, scope = []) {
|
|
61
|
+
const names = path ? path.split(".") : []
|
|
62
|
+
if (!names.length) return [...scope.map(loopVariable), ...(await this.at(""))]
|
|
63
|
+
|
|
64
|
+
const variable = await this.variable(names, scope)
|
|
65
|
+
if (!variable?.children) return []
|
|
66
|
+
if (variable.collection) return COLLECTION_MEMBERS
|
|
67
|
+
return this.at(resolve(names, scope))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// What the last name of a path is: a variable with children, a collection, …
|
|
71
|
+
async variable(names, scope) {
|
|
72
|
+
const last = names[names.length - 1]
|
|
73
|
+
const parent = names.slice(0, -1)
|
|
74
|
+
if (!parent.length) {
|
|
75
|
+
const own = scope.find((entry) => entry.variable === last)
|
|
76
|
+
if (own) return loopVariable(own)
|
|
77
|
+
} else if (ELEMENT.has(last)) {
|
|
78
|
+
const collection = await this.variable(parent, scope)
|
|
79
|
+
return collection?.collection ? { name: last, children: true } : null
|
|
80
|
+
}
|
|
81
|
+
const siblings = parent.length ? await this.options(parent.join("."), scope) : await this.options("", scope)
|
|
82
|
+
return siblings.find((variable) => variable.name === last)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function loopVariable({ variable, collection }) {
|
|
87
|
+
return { name: variable, label: `Each of ${collection}`, type: "object", children: true, loop: true }
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// The path the host knows a template's path by: loop variables become their
|
|
91
|
+
// collection, and first and last go into the collection's elements.
|
|
92
|
+
export function resolve(names, scope = []) {
|
|
93
|
+
let resolved = [...names]
|
|
94
|
+
// Inner loops first: their collection can start with an outer loop's variable
|
|
95
|
+
for (let i = 0; i < scope.length; i++) {
|
|
96
|
+
for (const { variable, collection } of scope) {
|
|
97
|
+
if (resolved[0] === variable) resolved = [...collection.split("."), ...resolved.slice(1)]
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return resolved.filter((name) => !ELEMENT.has(name) && !/^\d+$/.test(name)).join(".")
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The loop variables a block can use: its Loop blocks, the innermost first.
|
|
104
|
+
export function loopScope(doc, id) {
|
|
105
|
+
return ancestors(doc, id)
|
|
106
|
+
.filter((node) => node.type === "loop")
|
|
107
|
+
.map((node) => ({ variable: node.data.variable || "item", collection: node.data.collection || "items" }))
|
|
108
|
+
.filter(({ variable, collection }) => /^\w+$/.test(variable) && collection)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The variable path being written at the caret, if any:
|
|
112
|
+
// "Hello {{ order.cus" → { start: 9, path: "order.cus", tag: "{{" }
|
|
113
|
+
// In "output" mode only inside {{ … }} or {% … %} (tag says which); in
|
|
114
|
+
// "expression" mode (a Loop's collection, a Condition) anywhere, with no tag.
|
|
115
|
+
// Not after a filter's | or in a quoted string.
|
|
116
|
+
export function liquidToken(before, mode = "output") {
|
|
117
|
+
let expression = before
|
|
118
|
+
let tag = null
|
|
119
|
+
if (mode === "output") {
|
|
120
|
+
const output = before.lastIndexOf("{{")
|
|
121
|
+
const open = Math.max(output, before.lastIndexOf("{%"))
|
|
122
|
+
if (open < 0 || Math.max(before.lastIndexOf("}}"), before.lastIndexOf("%}")) > open) return null
|
|
123
|
+
tag = open === output ? "{{" : "{%"
|
|
124
|
+
expression = before.slice(open + 2)
|
|
125
|
+
}
|
|
126
|
+
if ((expression.match(/["']/g) || []).length % 2) return null
|
|
127
|
+
|
|
128
|
+
const match = expression.match(/(?:^|[^\w.])([A-Za-z_][\w]*(?:\.[\w]*)*)?$/)
|
|
129
|
+
if (!match) return null
|
|
130
|
+
const path = match[1] || ""
|
|
131
|
+
const start = before.length - path.length
|
|
132
|
+
if (/\|\s*$/.test(before.slice(0, start))) return null
|
|
133
|
+
return { start, path, tag }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// The text that replaces a token's last name for a chosen variable: its name,
|
|
137
|
+
// then a dot to go on with its variables, or the end of a {{ tag. `before` is
|
|
138
|
+
// the text before the token, `after` the text after the caret. For a Loop's
|
|
139
|
+
// collection (`collection: true`) a collection is where the path ends.
|
|
140
|
+
export function completion(variable, { tag = null, before = "", after = "", collection = false } = {}) {
|
|
141
|
+
// "{{order" reads better as "{{ order"
|
|
142
|
+
const space = /\{[{%]-?$/.test(before) ? " " : ""
|
|
143
|
+
if (variable.children && !(collection && variable.collection)) return { text: `${space}${variable.name}.`, more: true }
|
|
144
|
+
const close = tag === "{{" && !/^\s*-?\}\}/.test(after) && !/^\s*\|/.test(after)
|
|
145
|
+
return { text: `${space}${variable.name}${close ? " }}" : ""}`, more: false }
|
|
146
|
+
}
|
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
import { imageSource } from "tess_edit/core/media"
|
|
5
5
|
import { getBlock, label } from "tess_edit/core/registry"
|
|
6
6
|
import { find, getValue } from "tess_edit/core/document"
|
|
7
|
+
import { loopScope } from "tess_edit/core/variables"
|
|
7
8
|
import { codeInput } from "tess_edit/ui/code_input"
|
|
8
9
|
import { h } from "tess_edit/ui/dom"
|
|
9
10
|
import { icon } from "tess_edit/ui/icons"
|
|
11
|
+
import { inputAdapter, suggestIn } from "tess_edit/ui/variable_menu"
|
|
10
12
|
|
|
11
13
|
const INPUT = "w-full min-w-0 rounded-md border border-gray-300 bg-white px-2 py-1 text-[13px] text-gray-900 shadow-xs outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
|
|
12
14
|
|
|
@@ -36,6 +38,7 @@ export class Inspector {
|
|
|
36
38
|
this.editor = editor
|
|
37
39
|
this.element = element
|
|
38
40
|
this.openGroups = new Map()
|
|
41
|
+
this.adapters = new Map()
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
render() {
|
|
@@ -43,6 +46,8 @@ export class Inspector {
|
|
|
43
46
|
const hit = find(doc, selectedId) || { node: doc }
|
|
44
47
|
const node = hit.node
|
|
45
48
|
const definition = getBlock(node.type)
|
|
49
|
+
this.editor.variableMenu?.close()
|
|
50
|
+
this.adapters = new Map()
|
|
46
51
|
|
|
47
52
|
const actions =
|
|
48
53
|
!this.editor.isFree(node.id)
|
|
@@ -102,12 +107,16 @@ export class Inspector {
|
|
|
102
107
|
const expand = field.type === "code" && editable
|
|
103
108
|
? h("button", { type: "button", title: `Edit ${field.label} in a larger window`, class: "absolute right-0 top-0 rounded px-1 text-[11px] font-medium text-blue-600 hover:bg-blue-50", onclick: () => this.expand(node, field) }, "Expand")
|
|
104
109
|
: null
|
|
110
|
+
const adapter = editable && field.liquid && this.adapters.get(field.key)
|
|
111
|
+
const variables = adapter
|
|
112
|
+
? h("button", { type: "button", title: "Insert a variable", class: "absolute right-0 top-0 rounded px-1 font-mono text-[11px] font-medium text-blue-600 hover:bg-blue-50", onclick: (event) => this.editor.variableMenu.browse(adapter, event.currentTarget) }, "{ }")
|
|
113
|
+
: null
|
|
105
114
|
// A div, not a label: a label would forward clicks to the first button or color picker inside it.
|
|
106
115
|
return h(
|
|
107
116
|
"div",
|
|
108
117
|
{ class: "relative block" },
|
|
109
118
|
h("span", { class: "mb-1 block text-xs font-medium text-gray-700" }, field.label),
|
|
110
|
-
expand,
|
|
119
|
+
expand || variables,
|
|
111
120
|
control,
|
|
112
121
|
field.hint ? h("span", { class: "mt-1 block text-[11px] leading-snug text-gray-500" }, field.hint) : null
|
|
113
122
|
)
|
|
@@ -116,7 +125,7 @@ export class Inspector {
|
|
|
116
125
|
control(node, field, value, set) {
|
|
117
126
|
switch (field.type) {
|
|
118
127
|
case "textarea":
|
|
119
|
-
return h("textarea", { class: `${INPUT} min-h-32`, rows: 4, value, oninput: (event) => set(event.target.value) })
|
|
128
|
+
return this.variables(h("textarea", { class: `${INPUT} min-h-32`, rows: 4, value, oninput: (event) => set(event.target.value) }), node, field)
|
|
120
129
|
|
|
121
130
|
case "code":
|
|
122
131
|
return codeInput({ value, language: field.language, oninput: set, class: "h-60" })
|
|
@@ -190,14 +199,27 @@ export class Inspector {
|
|
|
190
199
|
return this.image(node, field, value, set)
|
|
191
200
|
|
|
192
201
|
default:
|
|
193
|
-
return h("input", { type: "text", class: INPUT, value, placeholder: field.placeholder || "", oninput: (event) => set(event.target.value) })
|
|
202
|
+
return this.variables(h("input", { type: "text", class: INPUT, value, placeholder: field.placeholder || "", oninput: (event) => set(event.target.value) }), node, field)
|
|
194
203
|
}
|
|
195
204
|
}
|
|
196
205
|
|
|
206
|
+
// Suggests the host's Liquid variables (editor.variables) while typing {{ in
|
|
207
|
+
// a text field. A field's `liquid` also gives it a { } button to browse
|
|
208
|
+
// them: true for Liquid in text, "expression" for a condition, "collection"
|
|
209
|
+
// for what a Loop goes over.
|
|
210
|
+
variables(input, node, field) {
|
|
211
|
+
if (!this.editor.variables?.enabled) return input
|
|
212
|
+
const mode = field.liquid === "expression" || field.liquid === "collection" ? field.liquid : "output"
|
|
213
|
+
const adapter = inputAdapter(input, { mode, scope: loopScope(this.editor.doc, node.id) })
|
|
214
|
+
suggestIn(this.editor.variableMenu, input, adapter)
|
|
215
|
+
this.adapters.set(field.key, adapter)
|
|
216
|
+
return input
|
|
217
|
+
}
|
|
218
|
+
|
|
197
219
|
// An image URL, or with the host's images (editor.media, core/media.js) a
|
|
198
220
|
// choice between one of those, a new upload, and a URL
|
|
199
221
|
image(node, field, value, set) {
|
|
200
|
-
const url = h("input", { type: "text", class: INPUT, value, placeholder: field.placeholder || "https://…", oninput: (event) => set(event.target.value) })
|
|
222
|
+
const url = this.variables(h("input", { type: "text", class: INPUT, value, placeholder: field.placeholder || "https://…", oninput: (event) => set(event.target.value) }), node, field)
|
|
201
223
|
if (!this.editor.media.enabled) return url
|
|
202
224
|
|
|
203
225
|
const source = imageSource(node, this.editor, field)
|
|
@@ -342,7 +364,7 @@ export class Inspector {
|
|
|
342
364
|
{ class: INPUT, onchange: (event) => update(items.map((it, i) => (i === index ? { ...it, [part.name]: event.target.value } : it))) },
|
|
343
365
|
part.options.map((option) => h("option", { value: option, selected: option === item[part.name] }, option))
|
|
344
366
|
)
|
|
345
|
-
: h("input", {
|
|
367
|
+
: this.variables(h("input", {
|
|
346
368
|
type: "text",
|
|
347
369
|
class: INPUT,
|
|
348
370
|
placeholder: part.label,
|
|
@@ -351,7 +373,7 @@ export class Inspector {
|
|
|
351
373
|
items = items.map((it, i) => (i === index ? { ...it, [part.name]: event.target.value } : it))
|
|
352
374
|
set(items)
|
|
353
375
|
}
|
|
354
|
-
})
|
|
376
|
+
}), node, { key: `${field.key}.${index}.${part.name}` })
|
|
355
377
|
),
|
|
356
378
|
h(
|
|
357
379
|
"div",
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Inline rich-text editing with Tiptap, mounted inside the canvas iframe on the
|
|
2
|
-
// text block's own element, plus the formatting toolbar above the canvas.
|
|
2
|
+
// text block's own element, plus the formatting toolbar above the canvas. With
|
|
3
|
+
// the host's variables (`menu`, a ui/variable_menu.js VariableMenu) typing {{
|
|
4
|
+
// suggests them, and the toolbar's { } button browses them.
|
|
3
5
|
|
|
4
6
|
import { Color, Editor, StarterKit, TextAlign, TextStyle } from "tess_edit/vendor/tiptap"
|
|
5
7
|
import { h } from "tess_edit/ui/dom"
|
|
@@ -9,8 +11,9 @@ const BUTTON = "w-7 h-7 flex items-center justify-center rounded-md text-gray-60
|
|
|
9
11
|
const ACTIVE = "bg-blue-50 text-blue-700"
|
|
10
12
|
|
|
11
13
|
export class TextEditing {
|
|
12
|
-
constructor(toolbar) {
|
|
14
|
+
constructor(toolbar, { menu = null } = {}) {
|
|
13
15
|
this.toolbar = toolbar
|
|
16
|
+
this.menu = menu
|
|
14
17
|
this.tiptap = null
|
|
15
18
|
}
|
|
16
19
|
|
|
@@ -18,7 +21,8 @@ export class TextEditing {
|
|
|
18
21
|
return !!this.tiptap
|
|
19
22
|
}
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
// scope: the loop variables of the block (core/variables.js loopScope)
|
|
25
|
+
start(container, content, onUpdate, { scope = [] } = {}) {
|
|
22
26
|
this.stop()
|
|
23
27
|
container.replaceChildren()
|
|
24
28
|
this.tiptap = new Editor({
|
|
@@ -37,9 +41,16 @@ export class TextEditing {
|
|
|
37
41
|
TextAlign.configure({ types: ["heading", "paragraph"] })
|
|
38
42
|
],
|
|
39
43
|
autofocus: "end",
|
|
40
|
-
|
|
44
|
+
editorProps: { handleKeyDown: (view, event) => this.menu?.key(event) || false },
|
|
45
|
+
onUpdate: ({ editor, transaction }) => {
|
|
46
|
+
onUpdate(editor.getJSON())
|
|
47
|
+
if (this.menu && !transaction.getMeta("variable")) this.menu.suggest(this.adapter)
|
|
48
|
+
},
|
|
49
|
+
onSelectionUpdate: () => this.menu?.open && !this.menu.state?.browse && this.menu.suggest(this.adapter),
|
|
50
|
+
onBlur: () => this.menu?.open && !this.menu.state?.browse && this.menu.close(),
|
|
41
51
|
onTransaction: () => this.renderToolbar()
|
|
42
52
|
})
|
|
53
|
+
this.adapter = this.menu && tiptapAdapter(this.tiptap, scope)
|
|
43
54
|
this.renderToolbar()
|
|
44
55
|
return this.tiptap
|
|
45
56
|
}
|
|
@@ -47,6 +58,7 @@ export class TextEditing {
|
|
|
47
58
|
stop() {
|
|
48
59
|
if (!this.tiptap) return null
|
|
49
60
|
const json = this.tiptap.getJSON()
|
|
61
|
+
this.menu?.close()
|
|
50
62
|
this.tiptap.destroy()
|
|
51
63
|
this.tiptap = null
|
|
52
64
|
this.toolbar.replaceChildren()
|
|
@@ -117,8 +129,51 @@ export class TextEditing {
|
|
|
117
129
|
button("list-bullet", "Bulleted list", editor.isActive("bulletList"), () => chain().toggleBulletList().run()),
|
|
118
130
|
button("list-ordered", "Numbered list", editor.isActive("orderedList"), () => chain().toggleOrderedList().run()),
|
|
119
131
|
separator(),
|
|
120
|
-
button("clear", "Clear formatting", false, () => chain().unsetAllMarks().clearNodes().run())
|
|
132
|
+
button("clear", "Clear formatting", false, () => chain().unsetAllMarks().clearNodes().run()),
|
|
133
|
+
this.menu
|
|
134
|
+
? h("button", {
|
|
135
|
+
type: "button",
|
|
136
|
+
title: "Insert a variable",
|
|
137
|
+
class: `${BUTTON} w-auto px-1.5 font-mono text-xs`,
|
|
138
|
+
onmousedown: (event) => event.preventDefault(),
|
|
139
|
+
onclick: (event) => this.menu.browse(this.adapter, event.currentTarget)
|
|
140
|
+
}, "{ }")
|
|
141
|
+
: null
|
|
121
142
|
].filter(Boolean))
|
|
122
143
|
this.toolbar.classList.remove("hidden")
|
|
123
144
|
}
|
|
124
145
|
}
|
|
146
|
+
|
|
147
|
+
// A variable menu adapter for Tiptap (see ui/variable_menu.js): offsets are in
|
|
148
|
+
// the paragraph with the caret, in which every character and inline node
|
|
149
|
+
// takes one position.
|
|
150
|
+
function tiptapAdapter(editor, scope) {
|
|
151
|
+
const text = ($pos, from, to) => $pos.parent.textBetween(from, to, undefined, "\ufffc")
|
|
152
|
+
const dispatch = (from, to, value) => {
|
|
153
|
+
editor.view.dispatch(editor.state.tr.insertText(value, from, to).setMeta("variable", true))
|
|
154
|
+
editor.view.focus()
|
|
155
|
+
}
|
|
156
|
+
return {
|
|
157
|
+
mode: "output",
|
|
158
|
+
scope,
|
|
159
|
+
before: () => text(editor.state.selection.$from, 0, editor.state.selection.$from.parentOffset),
|
|
160
|
+
after: () => {
|
|
161
|
+
const $to = editor.state.selection.$to
|
|
162
|
+
return text($to, $to.parentOffset, $to.parent.content.size)
|
|
163
|
+
},
|
|
164
|
+
replace(start, end, value) {
|
|
165
|
+
const base = editor.state.selection.$from.start()
|
|
166
|
+
dispatch(base + start, base + end, value)
|
|
167
|
+
},
|
|
168
|
+
insert(value) {
|
|
169
|
+
const { from, to } = editor.state.selection
|
|
170
|
+
dispatch(from, to, value)
|
|
171
|
+
},
|
|
172
|
+
// The caret's place in the iframe, in the page's viewport
|
|
173
|
+
rect() {
|
|
174
|
+
const caret = editor.view.coordsAtPos(editor.state.selection.from)
|
|
175
|
+
const frame = editor.view.dom.ownerDocument.defaultView.frameElement?.getBoundingClientRect() || { left: 0, top: 0 }
|
|
176
|
+
return { left: caret.left + frame.left, right: caret.right + frame.left, top: caret.top + frame.top, bottom: caret.bottom + frame.top }
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// The variable picker: suggestions while typing Liquid ({{ order.… ), and a
|
|
2
|
+
// menu to browse the variables from a field's { } button or the text
|
|
3
|
+
// toolbar. It works on an "adapter" for the place being typed in:
|
|
4
|
+
//
|
|
5
|
+
// before() the text before the caret (in the line or paragraph)
|
|
6
|
+
// after() the text after it
|
|
7
|
+
// replace(start, end, text) replaces part of before() + after(), by offsets in it
|
|
8
|
+
// rect() where to show the menu, in the page's viewport
|
|
9
|
+
// mode "output" (Liquid in text: {{ … }}), "expression" (a
|
|
10
|
+
// Condition) or "collection" (a Loop's collection)
|
|
11
|
+
// scope the loop variables there (core/variables.js loopScope)
|
|
12
|
+
//
|
|
13
|
+
// See inputAdapter below and TextEditing for Tiptap's.
|
|
14
|
+
|
|
15
|
+
import { completion, liquidToken } from "tess_edit/core/variables"
|
|
16
|
+
import { h } from "tess_edit/ui/dom"
|
|
17
|
+
|
|
18
|
+
const ITEM = "flex w-full items-center gap-2 px-2.5 py-1.5 text-left text-[13px]"
|
|
19
|
+
|
|
20
|
+
export class VariableMenu {
|
|
21
|
+
constructor(editor) {
|
|
22
|
+
this.editor = editor
|
|
23
|
+
this.element = null
|
|
24
|
+
this.request = 0
|
|
25
|
+
this.onOutside = (event) => {
|
|
26
|
+
if (this.element && !this.element.contains(event.target) && event.target !== this.anchor) this.close()
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get open() {
|
|
31
|
+
return Boolean(this.element)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Shows the variables for the path being typed, or closes when there's none.
|
|
35
|
+
suggest(adapter) {
|
|
36
|
+
const suggesting = this.suggesting(adapter)
|
|
37
|
+
this.pending = suggesting
|
|
38
|
+
suggesting.finally(() => this.pending === suggesting && (this.pending = null))
|
|
39
|
+
return suggesting
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async suggesting(adapter) {
|
|
43
|
+
const before = adapter.before()
|
|
44
|
+
const token = liquidToken(before, adapter.mode === "output" ? "output" : "expression")
|
|
45
|
+
if (!token) return this.close()
|
|
46
|
+
|
|
47
|
+
const dot = token.path.lastIndexOf(".")
|
|
48
|
+
const parent = dot < 0 ? "" : token.path.slice(0, dot)
|
|
49
|
+
const partial = token.path.slice(dot + 1).toLowerCase()
|
|
50
|
+
const start = token.start + dot + 1
|
|
51
|
+
const request = ++this.request
|
|
52
|
+
let items
|
|
53
|
+
try {
|
|
54
|
+
items = await this.editor.variables.options(parent, adapter.scope)
|
|
55
|
+
} catch (error) {
|
|
56
|
+
items = []
|
|
57
|
+
}
|
|
58
|
+
if (request !== this.request) return
|
|
59
|
+
// Names that start with what's typed first, then labels that have it
|
|
60
|
+
const named = items.filter((item) => item.name.toLowerCase().startsWith(partial))
|
|
61
|
+
items = [...named, ...items.filter((item) => !named.includes(item) && item.label?.toLowerCase().includes(partial))]
|
|
62
|
+
if (!items.length || (items.length === 1 && items[0].name.toLowerCase() === partial && !items[0].children)) return this.close()
|
|
63
|
+
|
|
64
|
+
this.show({ adapter, browse: false, items, index: 0, choose: (item) => this.complete(adapter, token, start, item) })
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async complete(adapter, token, start, item) {
|
|
68
|
+
const before = adapter.before()
|
|
69
|
+
const { text, more } = completion(item, { tag: token.tag, before: before.slice(0, start), after: adapter.after(), collection: adapter.mode === "collection" })
|
|
70
|
+
adapter.replace(start, before.length, text)
|
|
71
|
+
more ? await this.suggest(adapter) : this.close()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// A menu to browse the variables from `anchor` (a button), inserting the
|
|
75
|
+
// chosen one at the caret.
|
|
76
|
+
async browse(adapter, anchor, path = "") {
|
|
77
|
+
this.anchor = anchor
|
|
78
|
+
const request = ++this.request
|
|
79
|
+
let items
|
|
80
|
+
let error = null
|
|
81
|
+
try {
|
|
82
|
+
items = await this.editor.variables.options(path, adapter.scope)
|
|
83
|
+
} catch (failure) {
|
|
84
|
+
items = []
|
|
85
|
+
error = failure.message
|
|
86
|
+
}
|
|
87
|
+
if (request !== this.request) return
|
|
88
|
+
this.show({
|
|
89
|
+
adapter,
|
|
90
|
+
browse: true,
|
|
91
|
+
path,
|
|
92
|
+
items,
|
|
93
|
+
error,
|
|
94
|
+
index: -1,
|
|
95
|
+
choose: (item) => {
|
|
96
|
+
const full = path ? `${path}.${item.name}` : item.name
|
|
97
|
+
const drill = item.children && !(adapter.mode === "collection" && item.collection)
|
|
98
|
+
if (drill) return this.browse(adapter, anchor, full)
|
|
99
|
+
this.close()
|
|
100
|
+
adapter.insert(adapter.mode === "output" ? `{{ ${full} }}` : full)
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Arrow keys, Enter and Tab choose, Escape closes. True when it took the key.
|
|
106
|
+
key(event) {
|
|
107
|
+
if (this.state?.browse) return false
|
|
108
|
+
if ((event.key === "Enter" || (event.key === "Tab" && this.open)) && this.pending) {
|
|
109
|
+
// The menu doesn't show yet what the last keys lead to: choose from that
|
|
110
|
+
// once it's there. Enter doesn't submit a form in the meantime.
|
|
111
|
+
this.pending.then(() => this.state && !this.state.browse && this.state.choose(this.state.items[this.state.index]))
|
|
112
|
+
event.preventDefault()
|
|
113
|
+
event.stopPropagation()
|
|
114
|
+
return true
|
|
115
|
+
}
|
|
116
|
+
if (!this.open) return false
|
|
117
|
+
const { items } = this.state
|
|
118
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
119
|
+
this.state.index = (this.state.index + (event.key === "ArrowDown" ? 1 : items.length - 1)) % items.length
|
|
120
|
+
this.render()
|
|
121
|
+
} else if (event.key === "Enter" || event.key === "Tab") {
|
|
122
|
+
this.state.choose(items[this.state.index])
|
|
123
|
+
} else if (event.key === "Escape") {
|
|
124
|
+
this.close()
|
|
125
|
+
} else {
|
|
126
|
+
return false
|
|
127
|
+
}
|
|
128
|
+
event.preventDefault()
|
|
129
|
+
event.stopPropagation()
|
|
130
|
+
return true
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
show(state) {
|
|
134
|
+
this.state = state
|
|
135
|
+
if (!this.element) {
|
|
136
|
+
this.element = h("div", {
|
|
137
|
+
class: "absolute z-50 w-72 max-h-72 overflow-y-auto rounded-md border border-gray-200 bg-white py-1 shadow-lg",
|
|
138
|
+
role: "listbox",
|
|
139
|
+
"aria-label": "Variables",
|
|
140
|
+
// Keep the focus (and caret) in the field being typed in
|
|
141
|
+
onmousedown: (event) => event.preventDefault(),
|
|
142
|
+
onkeydown: (event) => event.key === "Escape" && this.close()
|
|
143
|
+
})
|
|
144
|
+
this.editor.root.append(this.element)
|
|
145
|
+
document.addEventListener("mousedown", this.onOutside, true)
|
|
146
|
+
}
|
|
147
|
+
this.render()
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
render() {
|
|
151
|
+
const { adapter, browse, path, items, index, error, choose } = this.state
|
|
152
|
+
const header = browse
|
|
153
|
+
? h(
|
|
154
|
+
"div",
|
|
155
|
+
{ class: "flex items-center gap-1 border-b border-gray-100 px-2.5 pb-1.5 pt-0.5 text-xs text-gray-500" },
|
|
156
|
+
path
|
|
157
|
+
? h("button", { type: "button", class: "rounded px-1 text-blue-600 hover:bg-blue-50", onclick: () => this.browse(adapter, this.anchor, path.split(".").slice(0, -1).join(".")) }, "‹ Back")
|
|
158
|
+
: null,
|
|
159
|
+
h("span", { class: "truncate font-mono" }, path || "Variables")
|
|
160
|
+
)
|
|
161
|
+
: null
|
|
162
|
+
const rows = items.map((item, i) =>
|
|
163
|
+
h(
|
|
164
|
+
"button",
|
|
165
|
+
{
|
|
166
|
+
type: "button",
|
|
167
|
+
role: "option",
|
|
168
|
+
"aria-selected": i === index ? "true" : "false",
|
|
169
|
+
class: `${ITEM} ${i === index ? "bg-blue-50 text-blue-800" : "text-gray-800 hover:bg-gray-50"}`,
|
|
170
|
+
title: item.hint || "",
|
|
171
|
+
onclick: () => choose(item)
|
|
172
|
+
},
|
|
173
|
+
h("span", { class: "min-w-0 flex-1 truncate" }, item.label || item.name),
|
|
174
|
+
h("span", { class: "shrink-0 font-mono text-[11px] text-gray-500" }, item.name),
|
|
175
|
+
item.children && !(adapter.mode === "collection" && item.collection) ? h("span", { class: "shrink-0 text-gray-400" }, "›") : null
|
|
176
|
+
)
|
|
177
|
+
)
|
|
178
|
+
const empty = !items.length ? h("p", { class: "px-2.5 py-1.5 text-xs text-gray-500" }, error || "No variables here") : null
|
|
179
|
+
this.element.replaceChildren(...[header, ...rows, empty].filter(Boolean))
|
|
180
|
+
this.place(browse ? this.anchor.getBoundingClientRect() : adapter.rect())
|
|
181
|
+
this.element.querySelector("[aria-selected='true']")?.scrollIntoView({ block: "nearest" })
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Below the rect, or above it when there's no room, inside the editor.
|
|
185
|
+
place(rect) {
|
|
186
|
+
const root = this.editor.root.getBoundingClientRect()
|
|
187
|
+
const height = this.element.offsetHeight
|
|
188
|
+
const below = rect.bottom - root.top + 4
|
|
189
|
+
const top = below + height > root.height && rect.top - root.top - height - 4 > 0 ? rect.top - root.top - height - 4 : below
|
|
190
|
+
const left = Math.max(4, Math.min(rect.left - root.left, root.width - this.element.offsetWidth - 4))
|
|
191
|
+
Object.assign(this.element.style, { top: `${top}px`, left: `${left}px` })
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
close() {
|
|
195
|
+
this.request++
|
|
196
|
+
this.element?.remove()
|
|
197
|
+
this.element = null
|
|
198
|
+
this.state = null
|
|
199
|
+
this.anchor = null
|
|
200
|
+
document.removeEventListener("mousedown", this.onOutside, true)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// An adapter for an input or textarea; replacing text fires its input event,
|
|
205
|
+
// so the field saves it as if typed.
|
|
206
|
+
export function inputAdapter(input, { mode = "output", scope = [] } = {}) {
|
|
207
|
+
const fire = () => input.dispatchEvent(new Event("input", { bubbles: true }))
|
|
208
|
+
return {
|
|
209
|
+
mode,
|
|
210
|
+
scope,
|
|
211
|
+
before: () => input.value.slice(0, input.selectionStart ?? input.value.length),
|
|
212
|
+
after: () => input.value.slice(input.selectionEnd ?? input.value.length),
|
|
213
|
+
replace(start, end, text) {
|
|
214
|
+
input.setRangeText(text, start, end, "end")
|
|
215
|
+
fire()
|
|
216
|
+
},
|
|
217
|
+
insert(text) {
|
|
218
|
+
input.focus()
|
|
219
|
+
const start = input.selectionStart ?? input.value.length
|
|
220
|
+
input.setRangeText(text, start, input.selectionEnd ?? start, "end")
|
|
221
|
+
fire()
|
|
222
|
+
},
|
|
223
|
+
rect: () => input.getBoundingClientRect()
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Suggestions while typing in an input or textarea
|
|
228
|
+
export function suggestIn(menu, input, adapter) {
|
|
229
|
+
input.addEventListener("input", (event) => event.isTrusted && menu.suggest(adapter))
|
|
230
|
+
input.addEventListener("keydown", (event) => menu.key(event))
|
|
231
|
+
input.addEventListener("blur", () => !menu.state?.browse && menu.close())
|
|
232
|
+
}
|
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.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tom de Grunt
|
|
@@ -104,6 +104,7 @@ files:
|
|
|
104
104
|
- app/javascript/tess_edit/core/media.js
|
|
105
105
|
- app/javascript/tess_edit/core/registry.js
|
|
106
106
|
- app/javascript/tess_edit/core/starter.js
|
|
107
|
+
- app/javascript/tess_edit/core/variables.js
|
|
107
108
|
- app/javascript/tess_edit/export/compile.js
|
|
108
109
|
- app/javascript/tess_edit/export/mjml.js
|
|
109
110
|
- app/javascript/tess_edit/export/richtext.js
|
|
@@ -116,6 +117,7 @@ files:
|
|
|
116
117
|
- app/javascript/tess_edit/ui/inspector.js
|
|
117
118
|
- app/javascript/tess_edit/ui/text_editing.js
|
|
118
119
|
- app/javascript/tess_edit/ui/tree.js
|
|
120
|
+
- app/javascript/tess_edit/ui/variable_menu.js
|
|
119
121
|
- config/importmap.rb
|
|
120
122
|
- config/routes.rb
|
|
121
123
|
- lib/tasks/tess_edit_tasks.rake
|