tess_edit 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +30 -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 +39 -31
- 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/blocks_panel.js +3 -3
- data/app/javascript/tess_edit/ui/code_input.js +8 -8
- data/app/javascript/tess_edit/ui/inspector.js +58 -36
- data/app/javascript/tess_edit/ui/text_editing.js +65 -10
- data/app/javascript/tess_edit/ui/tree.js +6 -6
- data/app/javascript/tess_edit/ui/variable_menu.js +232 -0
- data/lib/tess_edit/version.rb +1 -1
- metadata +3 -1
|
@@ -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 dark:border-gray-700 bg-white dark:bg-gray-800 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 dark:border-gray-700 px-2.5 pb-1.5 pt-0.5 text-xs text-gray-500 dark:text-gray-400" },
|
|
156
|
+
path
|
|
157
|
+
? h("button", { type: "button", class: "rounded px-1 text-blue-600 dark:text-blue-400 hover:bg-blue-50 dark:hover:bg-blue-900/40", 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 dark:bg-blue-900/40 text-blue-800 dark:text-blue-200" : "text-gray-800 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-700"}`,
|
|
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 dark:text-gray-400" }, item.name),
|
|
175
|
+
item.children && !(adapter.mode === "collection" && item.collection) ? h("span", { class: "shrink-0 text-gray-400 dark:text-gray-500" }, "›") : null
|
|
176
|
+
)
|
|
177
|
+
)
|
|
178
|
+
const empty = !items.length ? h("p", { class: "px-2.5 py-1.5 text-xs text-gray-500 dark:text-gray-400" }, 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.1
|
|
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
|