tess_edit 0.5.11 → 0.5.12
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 +13 -1
- data/app/javascript/tess_edit/controllers/editor_controller.js +8 -5
- data/app/javascript/tess_edit/core/liquid.js +183 -0
- data/app/javascript/tess_edit/core/variables.js +78 -5
- data/app/javascript/tess_edit/ui/inspector.js +2 -2
- data/app/javascript/tess_edit/ui/text_editing.js +1 -1
- data/app/javascript/tess_edit/ui/variable_menu.js +108 -17
- data/config/locales/en.yml +1 -0
- data/config/locales/nl.yml +1 -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: 6fbe80bedc7281410bc4d08388da4901f5371c3282edc67ef41c8fe1efd4f858
|
|
4
|
+
data.tar.gz: 461eed20d4d3a0e860bdcf750162c08b0f31335e35e8d95b7d8554b656d69f6b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e355ee3b554123245f5a8cf76ece3a6514e1376b8bc67c9d686143fcc929b2d19e7b8e9b85d98800cda6d321e5f33ed035ee894519122a328b5836fecc9456c7
|
|
7
|
+
data.tar.gz: 0fbda871f654a364738555b7655166966f79d85dc0e88ed474cd3a49b459a72f2f4425f93346a4cb6ca0e4e6c4ea6d5f4cd598f91a53b908bd8b791eb29c5407
|
data/README.md
CHANGED
|
@@ -18,7 +18,7 @@ It completes Liquid tags and variables as you type, making it easier to work wit
|
|
|
18
18
|
- **Tables**: Table, Row and Cell blocks, a cell holding any blocks, with Loops and Conditions (and Assigns) between the rows. On the canvas a cell's menu adds and deletes rows and columns and puts its row in a Loop or Condition, a column's border is dragged to set its width, Tab goes from cell to cell (adding a row after the last), and a bracket by a Loop's or Condition's rows shows what they are.
|
|
19
19
|
- 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.
|
|
20
20
|
- 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.
|
|
21
|
-
- 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
|
+
- 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 and the Assigns before it; after a `|` your app's filters, with a where's property and the values it can have, and after `==` the values of what's compared.
|
|
22
22
|
- 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.
|
|
23
23
|
- 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.
|
|
24
24
|
- 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.
|
|
@@ -253,6 +253,18 @@ The endpoint answers `GET` with the variables at `path` (the roots without one),
|
|
|
253
253
|
|
|
254
254
|
`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`).
|
|
255
255
|
|
|
256
|
+
An Assign block before a block (in its container, or in one around it), and `{% assign %}` or an open `{% for %}` earlier in the same text, are variables there too. An Assign's variable is what its expression comes to: `{% assign lines = order.order_lines | where: "kind", "product" %}` is still order lines, through filters that keep a collection (`where`, `sort`, `reverse`, …), and `| first` makes it one of them, so `{% for line in lines %}` completes `line.` as a line.
|
|
257
|
+
|
|
258
|
+
A variable with `values` (`"values": [{"value": "shipping", "label": "Shipping"}]`) can only be those: after `line.kind == ` the editor suggests them, quoted.
|
|
259
|
+
|
|
260
|
+
With `data-tess-edit-filters-url-value` the editor also completes filters: after a `|` the filters, each with its label, and choosing one writes `| where: `, going on with its arguments: a property of the collection's items (`"kind"`) and the values it can have (`"shipping"`). The endpoint answers `GET` with:
|
|
261
|
+
|
|
262
|
+
```json
|
|
263
|
+
{"filters": [{"name": "where", "label": "Those whose property has the value", "arguments": [{"name": "property", "kind": "property"}, {"name": "value", "kind": "value"}], "keeps": "collection"}]}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
An argument's `kind` is `property`, `value` (one the property before it can have) or anything else, where the editor suggests variables. `keeps` says what a filter on a collection leaves: the same items (`"collection"`) or one of them (`"item"`); without the endpoint Liquid's own are known. Liquidum's `/liquidum/filters` answers this way, with your app's filters as well; the dummy app's `VariablesController#filters` is an example.
|
|
267
|
+
|
|
256
268
|
The endpoint can change while the editor is open, say when the record a template is for changes: set `data-tess-edit-variables-url-value` again and the editor asks the new one.
|
|
257
269
|
|
|
258
270
|
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.
|
|
@@ -23,6 +23,8 @@
|
|
|
23
23
|
// and browsed with { } buttons (see core/variables.js), e.g.
|
|
24
24
|
// Liquidum's /liquidum/variables?roots=…; a host can change it,
|
|
25
25
|
// e.g. when the class a template is for changes
|
|
26
|
+
// filtersUrl the filters a template can use, suggested after a | with their
|
|
27
|
+
// arguments (see core/variables.js), e.g. Liquidum's /liquidum/filters
|
|
26
28
|
// translationsUrl the host's translations for Liquid's t filter ({{ '.greeting' | t }}),
|
|
27
29
|
// shown in a language chosen in the top bar, listed and edited
|
|
28
30
|
// while pointed at, and made from selected text (see
|
|
@@ -61,7 +63,8 @@ import { COMPILE_URL } from "tess_edit/export/compile"
|
|
|
61
63
|
import { IMAGE_FIELD, fetchMedia, uploadMedia } from "tess_edit/core/media"
|
|
62
64
|
import { Translations, keysIn, suggestKey, toTranslation, translationTag } from "tess_edit/core/translations"
|
|
63
65
|
import { Values } from "tess_edit/core/values"
|
|
64
|
-
import { Variables
|
|
66
|
+
import { Variables } from "tess_edit/core/variables"
|
|
67
|
+
import { liquidScope } from "tess_edit/core/liquid"
|
|
65
68
|
import { addColumn, addRow, cellOf, columnCount, deleteColumn, deleteRow, nextCell, position, rows, wrapRow } from "tess_edit/core/table"
|
|
66
69
|
import { Canvas } from "tess_edit/ui/canvas"
|
|
67
70
|
import { freeChild, number } from "tess_edit/ui/free_layout"
|
|
@@ -93,7 +96,7 @@ function placed(node, at) {
|
|
|
93
96
|
}
|
|
94
97
|
|
|
95
98
|
export default class extends Controller {
|
|
96
|
-
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 }
|
|
99
|
+
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, filtersUrl: String, translationsUrl: String, values: String, lock: String, blocks: String, options: String }
|
|
97
100
|
static targets = ["jsonInput", "mjmlInput", "htmlInput", "slot", "translations"]
|
|
98
101
|
|
|
99
102
|
connect() {
|
|
@@ -123,7 +126,7 @@ export default class extends Controller {
|
|
|
123
126
|
this.sheetOpen = false
|
|
124
127
|
this.requests = {}
|
|
125
128
|
this.media = { enabled: Boolean(this.mediaUrlValue), images: [], uploading: false, error: null }
|
|
126
|
-
this.variables = new Variables(this.variablesUrlValue)
|
|
129
|
+
this.variables = new Variables(this.variablesUrlValue, { filtersUrl: this.filtersUrlValue })
|
|
127
130
|
this.translations = new Translations(this.translationsUrlValue)
|
|
128
131
|
this.translationPopover = this.translations.enabled ? new TranslationPopover(this) : null
|
|
129
132
|
this.values = new Values()
|
|
@@ -163,7 +166,7 @@ export default class extends Controller {
|
|
|
163
166
|
// Points the editor at other variables (variablesUrl), once it's connected
|
|
164
167
|
variablesUrlValueChanged() {
|
|
165
168
|
if (!this.variables) return
|
|
166
|
-
this.variables = new Variables(this.variablesUrlValue)
|
|
169
|
+
this.variables = new Variables(this.variablesUrlValue, { filtersUrl: this.filtersUrlValue })
|
|
167
170
|
if (!this.variableMenu && this.variables.enabled) {
|
|
168
171
|
this.variableMenu = new VariableMenu(this)
|
|
169
172
|
this.textEditing.menu = this.variableMenu
|
|
@@ -838,7 +841,7 @@ export default class extends Controller {
|
|
|
838
841
|
this.scheduleOutputs()
|
|
839
842
|
}
|
|
840
843
|
if (part.plain) this.textEditing.startPlain(container, getValue(hit.node, part.key), update, () => this.stopEditing())
|
|
841
|
-
else this.textEditing.start(container, getValue(hit.node, part.key), update, { scope:
|
|
844
|
+
else this.textEditing.start(container, getValue(hit.node, part.key), update, { scope: liquidScope(this.doc, id) })
|
|
842
845
|
this.canvas.refresh()
|
|
843
846
|
}
|
|
844
847
|
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// What's being written in Liquid at the caret, and what a block can use there.
|
|
2
|
+
//
|
|
3
|
+
// liquidContext reads the text before the caret and says what goes there:
|
|
4
|
+
//
|
|
5
|
+
// "{{ order.cus" → { kind: "variable", path: "order.cus" }
|
|
6
|
+
// "{{ order.placed_at | da" → { kind: "filter", partial: "da", input: "order.placed_at" }
|
|
7
|
+
// "{{ lines | where: \"pa" → { kind: "argument", filter: "where", index: 0, partial: "pa", quote: "\"" }
|
|
8
|
+
// "{{ lines | where: \"party\", " → { kind: "argument", filter: "where", index: 1, partial: "", args: ["party"] }
|
|
9
|
+
// "{% if line.party == \"sp" → { kind: "value", of: "line.party", partial: "sp", quote: "\"" }
|
|
10
|
+
//
|
|
11
|
+
// each with `start`, where what it replaces starts in the text, and `tag`
|
|
12
|
+
// ("{{", "{%" or null). In "output" mode (Liquid in text) only inside {{ … }}
|
|
13
|
+
// or {% … %}; in "expression" mode (a Loop's collection, a Condition, an
|
|
14
|
+
// Assign's value) anywhere. The editor's menu (ui/variable_menu.js) asks the
|
|
15
|
+
// host for what fits: its variables, its filters and their arguments, the
|
|
16
|
+
// values a variable can have (core/variables.js).
|
|
17
|
+
//
|
|
18
|
+
// liquidScope says which variables a block has besides the host's: its Loop
|
|
19
|
+
// blocks' and the Assigns before it, nearest first. An Assign's variable is
|
|
20
|
+
// what its expression comes to; through filters that keep a collection what
|
|
21
|
+
// it is (where, sort, …) it's still that collection (core/variables.js).
|
|
22
|
+
|
|
23
|
+
import { find } from "tess_edit/core/document"
|
|
24
|
+
|
|
25
|
+
const PATH = /[A-Za-z_][\w]*(?:\.[\w]*)*/
|
|
26
|
+
const NAME = /^\w+$/
|
|
27
|
+
|
|
28
|
+
// The parts of a Liquid expression outside quotes, split at `separator`
|
|
29
|
+
// (| between filters, , between arguments)
|
|
30
|
+
export function splitOutside(text, separator) {
|
|
31
|
+
const parts = []
|
|
32
|
+
let quote = null
|
|
33
|
+
let current = ""
|
|
34
|
+
for (const char of text) {
|
|
35
|
+
if (quote) {
|
|
36
|
+
if (char === quote) quote = null
|
|
37
|
+
} else if (char === '"' || char === "'") {
|
|
38
|
+
quote = char
|
|
39
|
+
} else if (char === separator) {
|
|
40
|
+
parts.push(current)
|
|
41
|
+
current = ""
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
current += char
|
|
45
|
+
}
|
|
46
|
+
parts.push(current)
|
|
47
|
+
return parts
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The quote a text ends inside, and where it opened, if it does
|
|
51
|
+
function openQuote(text) {
|
|
52
|
+
let quote = null
|
|
53
|
+
let at = -1
|
|
54
|
+
for (let i = 0; i < text.length; i++) {
|
|
55
|
+
const char = text[i]
|
|
56
|
+
if (quote) {
|
|
57
|
+
if (char === quote) quote = null
|
|
58
|
+
} else if (char === '"' || char === "'") {
|
|
59
|
+
quote = char
|
|
60
|
+
at = i
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return quote ? { quote, at } : null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// A quoted argument's text, or the argument as written
|
|
67
|
+
export function unquote(text) {
|
|
68
|
+
const trimmed = text.trim()
|
|
69
|
+
const match = trimmed.match(/^(["'])(.*)\1$/)
|
|
70
|
+
return match ? match[2] : trimmed
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function liquidContext(before, mode = "output") {
|
|
74
|
+
let expression = before
|
|
75
|
+
let tag = null
|
|
76
|
+
if (mode === "output") {
|
|
77
|
+
const output = before.lastIndexOf("{{")
|
|
78
|
+
const open = Math.max(output, before.lastIndexOf("{%"))
|
|
79
|
+
if (open < 0 || Math.max(before.lastIndexOf("}}"), before.lastIndexOf("%}")) > open) return null
|
|
80
|
+
tag = open === output ? "{{" : "{%"
|
|
81
|
+
expression = before.slice(open + 2).replace(/^-/, "")
|
|
82
|
+
}
|
|
83
|
+
const offset = before.length - expression.length
|
|
84
|
+
const quoted = openQuote(expression)
|
|
85
|
+
const head = quoted ? expression.slice(0, quoted.at) : expression
|
|
86
|
+
|
|
87
|
+
// In a filter's arguments: | name: a, b, …
|
|
88
|
+
const filters = splitOutside(head, "|")
|
|
89
|
+
if (filters.length > 1) {
|
|
90
|
+
const current = filters.at(-1)
|
|
91
|
+
const input = filters.slice(0, -1).join("|").trim()
|
|
92
|
+
const named = current.match(/^\s*(\w*)$/)
|
|
93
|
+
if (named && !quoted) return { kind: "filter", start: before.length - named[1].length, partial: named[1], input, tag }
|
|
94
|
+
|
|
95
|
+
const call = current.match(/^\s*(\w+)\s*:([\s\S]*)$/)
|
|
96
|
+
if (call) {
|
|
97
|
+
const args = splitOutside(call[2], ",")
|
|
98
|
+
const written = args.slice(0, -1).map(unquote)
|
|
99
|
+
if (quoted) {
|
|
100
|
+
const partial = expression.slice(quoted.at + 1)
|
|
101
|
+
return { kind: "argument", filter: call[1], index: args.length - 1, args: written, start: offset + quoted.at, partial, quote: quoted.quote, input, tag }
|
|
102
|
+
}
|
|
103
|
+
const last = args.at(-1)
|
|
104
|
+
const path = last.match(new RegExp(`^\\s*(${PATH.source})?$`))
|
|
105
|
+
if (path) {
|
|
106
|
+
const partial = path[1] || ""
|
|
107
|
+
return { kind: "argument", filter: call[1], index: args.length - 1, args: written, start: before.length - partial.length, partial, quote: null, input, tag }
|
|
108
|
+
}
|
|
109
|
+
return null
|
|
110
|
+
}
|
|
111
|
+
if (quoted) return null
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// A value compared with a variable: line.party == "sp
|
|
115
|
+
const compared = head.match(new RegExp(`(${PATH.source})\\s*(?:==|!=)\\s*$`))
|
|
116
|
+
if (compared && (quoted || /(?:==|!=)\s*$/.test(expression))) {
|
|
117
|
+
const partial = quoted ? expression.slice(quoted.at + 1) : ""
|
|
118
|
+
return { kind: "value", of: compared[1], start: quoted ? offset + quoted.at : before.length, partial, quote: quoted?.quote || null, tag }
|
|
119
|
+
}
|
|
120
|
+
if (quoted) return null
|
|
121
|
+
|
|
122
|
+
const match = expression.match(new RegExp(`(?:^|[^\\w.])(${PATH.source})?$`))
|
|
123
|
+
if (!match) return null
|
|
124
|
+
const path = match[1] || ""
|
|
125
|
+
return { kind: "variable", start: before.length - path.length, path, tag }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// The variable path an expression starts from, and the filters after it:
|
|
129
|
+
// 'contract.lines | where: "party", "account" | sort' → { path: "contract.lines", filters: ["where", "sort"] }
|
|
130
|
+
// In a tag, the path is the last one before the first |: "assign x = order.lines"
|
|
131
|
+
export function expressionParts(expression) {
|
|
132
|
+
const [first, ...rest] = splitOutside(expression, "|")
|
|
133
|
+
const paths = first.replace(/(["'])[\s\S]*?\1/g, " ").match(new RegExp(PATH.source, "g")) || []
|
|
134
|
+
const path = paths.filter((candidate) => !["assign", "for", "in", "if", "unless", "elsif", "and", "or", "contains"].includes(candidate)).at(-1)
|
|
135
|
+
if (!path) return null
|
|
136
|
+
return { path, filters: rest.map((filter) => filter.trim().match(/^\w+/)?.[0]).filter(Boolean) }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// The variables a block can use: its Loops' and the Assigns before it (in
|
|
140
|
+
// its container, or in one around it), nearest first
|
|
141
|
+
// [{ variable, collection }] a Loop's: each of a collection
|
|
142
|
+
// [{ variable, expression }] an Assign's: what its expression comes to
|
|
143
|
+
export function liquidScope(doc, id) {
|
|
144
|
+
const scope = []
|
|
145
|
+
let hit = find(doc, id)
|
|
146
|
+
while (hit?.parent) {
|
|
147
|
+
for (const sibling of hit.parent.children.slice(0, hit.index).reverse()) {
|
|
148
|
+
if (sibling.type !== "assign") continue
|
|
149
|
+
const { variable, expression } = sibling.data || {}
|
|
150
|
+
if (NAME.test(variable || "") && expression) scope.push({ variable, expression })
|
|
151
|
+
}
|
|
152
|
+
if (hit.parent.type === "loop") {
|
|
153
|
+
const variable = hit.parent.data.variable || "item"
|
|
154
|
+
const collection = hit.parent.data.collection || "items"
|
|
155
|
+
if (NAME.test(variable)) scope.push({ variable, collection })
|
|
156
|
+
}
|
|
157
|
+
hit = find(doc, hit.parent.id)
|
|
158
|
+
}
|
|
159
|
+
return scope
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// The assigns ({% assign x = … %}) and loops ({% for x in … %}) earlier in a
|
|
163
|
+
// text, before the caret, nearest first, as liquidScope gives them
|
|
164
|
+
export function textScope(before) {
|
|
165
|
+
const scope = []
|
|
166
|
+
for (const match of before.matchAll(/\{%-?\s*(assign|for)\s+(\w+)\s*(=|in)\s*([\s\S]*?)\s*-?%\}/g)) {
|
|
167
|
+
if (match[1] === "assign" && match[3] === "=") scope.unshift({ variable: match[2], expression: match[4] })
|
|
168
|
+
if (match[1] === "for" && match[3] === "in") {
|
|
169
|
+
// Only while the loop is open
|
|
170
|
+
const after = before.slice(match.index + match[0].length)
|
|
171
|
+
if (!/\{%-?\s*endfor\b/.test(after)) scope.unshift({ variable: match[2], collection: match[4].split(/\s/)[0] })
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return scope
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// What replaces a quoted argument or value, in the quote it was started
|
|
178
|
+
// with, and how much of the text after the caret it takes: its closing quote,
|
|
179
|
+
// when that's there already
|
|
180
|
+
export function quoted(value, { quote = null, after = "" } = {}) {
|
|
181
|
+
const mark = quote || '"'
|
|
182
|
+
return { text: `${mark}${value}${mark}`, through: after.startsWith(mark) ? 1 : 0 }
|
|
183
|
+
}
|
|
@@ -11,10 +11,28 @@
|
|
|
11
11
|
// In Liquid a collection's elements are reached through a Loop block's
|
|
12
12
|
// variable ({% for line in order.order_lines %} → line.product) or with
|
|
13
13
|
// first and last (order.order_lines.first.product), so those are resolved to
|
|
14
|
-
// such a path before asking.
|
|
14
|
+
// such a path before asking. An Assign's variable is what its expression
|
|
15
|
+
// comes to: through filters that keep a collection what it is (the host's
|
|
16
|
+
// filtersUrl says which, `keeps`), `lines = order.order_lines | where: …` is
|
|
17
|
+
// those lines still.
|
|
18
|
+
//
|
|
19
|
+
// The filters, for what follows a |, come from the editor's filtersUrl:
|
|
20
|
+
//
|
|
21
|
+
// GET → { filters: [{ name, label, arguments: [{ name, kind }], keeps }] }
|
|
22
|
+
//
|
|
23
|
+
// as Liquidum's FiltersController answers. A variable with `values`
|
|
24
|
+
// ([{ value, label }]) has those only, for a comparison or a where filter.
|
|
15
25
|
|
|
16
26
|
import { ancestors } from "tess_edit/core/document"
|
|
17
27
|
import { t } from "tess_edit/core/i18n"
|
|
28
|
+
import { expressionParts } from "tess_edit/core/liquid"
|
|
29
|
+
|
|
30
|
+
// What filters on a collection leave, without the host's: the same items, or one
|
|
31
|
+
export const KEEPS = {
|
|
32
|
+
where: "collection", where_exp: "collection", reject: "collection", sort: "collection", sort_natural: "collection",
|
|
33
|
+
reverse: "collection", uniq: "collection", compact: "collection", concat: "collection",
|
|
34
|
+
first: "item", last: "item", find: "item"
|
|
35
|
+
}
|
|
18
36
|
|
|
19
37
|
// The members Liquid gives every collection
|
|
20
38
|
export const COLLECTION_MEMBERS = [
|
|
@@ -26,12 +44,50 @@ export const COLLECTION_MEMBERS = [
|
|
|
26
44
|
const ELEMENT = new Set(["first", "last"])
|
|
27
45
|
|
|
28
46
|
export class Variables {
|
|
29
|
-
constructor(url, { fetch: fetcher = (...args) => fetch(...args) } = {}) {
|
|
47
|
+
constructor(url, { fetch: fetcher = (...args) => fetch(...args), filtersUrl = null } = {}) {
|
|
30
48
|
this.url = url
|
|
49
|
+
this.filtersUrl = filtersUrl
|
|
31
50
|
this.fetch = fetcher
|
|
32
51
|
this.levels = new Map()
|
|
33
52
|
}
|
|
34
53
|
|
|
54
|
+
// The host's filters, by name, cached; none without its filtersUrl
|
|
55
|
+
filters() {
|
|
56
|
+
if (!this.filtersUrl) return Promise.resolve(new Map())
|
|
57
|
+
this.filterList ||= this.get(this.filtersUrl, {}).then((body) => new Map((body.filters || []).map((filter) => [filter.name, filter])))
|
|
58
|
+
this.filterList.catch(() => (this.filterList = null))
|
|
59
|
+
return this.filterList
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async get(path, params) {
|
|
63
|
+
const url = new URL(path, window.location.href)
|
|
64
|
+
for (const [key, value] of Object.entries(params)) url.searchParams.set(key, value)
|
|
65
|
+
const response = await this.fetch(url, { headers: { Accept: "application/json" }, credentials: "same-origin" })
|
|
66
|
+
if (!response.ok) throw new Error(t("Loading the variables failed (%{status})", { status: `${response.status} ${response.statusText}` }))
|
|
67
|
+
return response.json()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// A scope from core/liquid.js with its Assigns made paths: a Loop's
|
|
71
|
+
// variable and an Assign of one item ({ variable, collection }) are each of
|
|
72
|
+
// a collection; an Assign of a collection or a value ({ variable, path })
|
|
73
|
+
// stands for its path. One that can't be followed isn't there.
|
|
74
|
+
async resolveScope(scope = []) {
|
|
75
|
+
if (!scope.some((entry) => entry.expression)) return scope
|
|
76
|
+
const filters = await this.filters().catch(() => new Map())
|
|
77
|
+
return scope.flatMap((entry) => {
|
|
78
|
+
if (!entry.expression) return [entry]
|
|
79
|
+
const parts = expressionParts(entry.expression)
|
|
80
|
+
if (!parts) return []
|
|
81
|
+
let item = false
|
|
82
|
+
for (const name of parts.filters) {
|
|
83
|
+
const keeps = filters.get(name)?.keeps ?? KEEPS[name]
|
|
84
|
+
if (keeps === "item" && !item) item = true
|
|
85
|
+
else if (keeps !== "collection") return []
|
|
86
|
+
}
|
|
87
|
+
return [item ? { variable: entry.variable, collection: parts.path } : { variable: entry.variable, path: parts.path }]
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
35
91
|
get enabled() {
|
|
36
92
|
return Boolean(this.url)
|
|
37
93
|
}
|
|
@@ -59,8 +115,18 @@ export class Variables {
|
|
|
59
115
|
// in a loop over order.order_lines, or "order.order_lines" (a collection:
|
|
60
116
|
// first, last and size). scope is from loopScope.
|
|
61
117
|
async options(path, scope = []) {
|
|
118
|
+
scope = await this.resolveScope(scope)
|
|
62
119
|
const names = path ? path.split(".") : []
|
|
63
|
-
if (!names.length)
|
|
120
|
+
if (!names.length) {
|
|
121
|
+
const own = []
|
|
122
|
+
for (const [index, entry] of scope.entries()) {
|
|
123
|
+
if (own.some((variable) => variable.name === entry.variable)) continue
|
|
124
|
+
// What it stands for, of the scope around it
|
|
125
|
+
const target = entry.path && (await this.variable(entry.path.split("."), scope.slice(index + 1)))
|
|
126
|
+
own.push(entry.path ? { ...(target || { children: false }), name: entry.variable, label: t("Assigned %{path}", { path: entry.path }), assign: true } : loopVariable(entry))
|
|
127
|
+
}
|
|
128
|
+
return [...own, ...(await this.at(""))]
|
|
129
|
+
}
|
|
64
130
|
|
|
65
131
|
const variable = await this.variable(names, scope)
|
|
66
132
|
if (!variable?.children) return []
|
|
@@ -68,12 +134,19 @@ export class Variables {
|
|
|
68
134
|
return this.at(resolve(names, scope))
|
|
69
135
|
}
|
|
70
136
|
|
|
137
|
+
// The variable a path as written comes to, with a scope from core/liquid.js
|
|
138
|
+
async lookup(path, scope = []) {
|
|
139
|
+
if (!path) return null
|
|
140
|
+
return this.variable(path.split("."), await this.resolveScope(scope))
|
|
141
|
+
}
|
|
142
|
+
|
|
71
143
|
// What the last name of a path is: a variable with children, a collection, …
|
|
72
144
|
async variable(names, scope) {
|
|
73
145
|
const last = names[names.length - 1]
|
|
74
146
|
const parent = names.slice(0, -1)
|
|
75
147
|
if (!parent.length) {
|
|
76
148
|
const own = scope.find((entry) => entry.variable === last)
|
|
149
|
+
if (own?.path) return { ...(await this.variable(own.path.split("."), scope.slice(scope.indexOf(own) + 1))), name: last }
|
|
77
150
|
if (own) return loopVariable(own)
|
|
78
151
|
} else if (ELEMENT.has(last)) {
|
|
79
152
|
const collection = await this.variable(parent, scope)
|
|
@@ -94,8 +167,8 @@ export function resolve(names, scope = []) {
|
|
|
94
167
|
let resolved = [...names]
|
|
95
168
|
// Inner loops first: their collection can start with an outer loop's variable
|
|
96
169
|
for (let i = 0; i < scope.length; i++) {
|
|
97
|
-
for (const { variable, collection } of scope) {
|
|
98
|
-
if (resolved[0] === variable) resolved = [...collection.split("."), ...resolved.slice(1)]
|
|
170
|
+
for (const { variable, collection, path } of scope) {
|
|
171
|
+
if (resolved[0] === variable && (collection || path)) resolved = [...(collection || path).split("."), ...resolved.slice(1)]
|
|
99
172
|
}
|
|
100
173
|
}
|
|
101
174
|
return resolved.filter((name) => !ELEMENT.has(name) && !/^\d+$/.test(name)).join(".")
|
|
@@ -20,7 +20,7 @@ import { fieldType, getBlock, label } from "tess_edit/core/registry"
|
|
|
20
20
|
import { ALIGN, fontOptions } from "tess_edit/blocks/fields"
|
|
21
21
|
import { find, getValue } from "tess_edit/core/document"
|
|
22
22
|
import { translationCalls } from "tess_edit/core/translations"
|
|
23
|
-
import {
|
|
23
|
+
import { liquidScope } from "tess_edit/core/liquid"
|
|
24
24
|
import { CELL, ROW, TABLE, addColumn, addRow, cellOf, columnCount, deleteColumn, deleteRow, position, rows, setColumn } from "tess_edit/core/table"
|
|
25
25
|
import { codeInput } from "tess_edit/ui/code_input"
|
|
26
26
|
import { FontPicker } from "tess_edit/ui/font_picker"
|
|
@@ -348,7 +348,7 @@ export class Inspector {
|
|
|
348
348
|
this.inputs.set(field.key, input)
|
|
349
349
|
if (!this.editor.variableMenu) return input
|
|
350
350
|
const mode = field.liquid === "expression" || field.liquid === "collection" ? field.liquid : "output"
|
|
351
|
-
const adapter = inputAdapter(input, { mode, scope:
|
|
351
|
+
const adapter = inputAdapter(input, { mode, scope: liquidScope(this.editor.doc, node.id) })
|
|
352
352
|
suggestIn(this.editor.variableMenu, input, adapter)
|
|
353
353
|
this.adapters.set(field.key, adapter)
|
|
354
354
|
return input
|
|
@@ -79,7 +79,7 @@ export class TextEditing {
|
|
|
79
79
|
return element
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
// scope: the
|
|
82
|
+
// scope: the Loops' and Assigns' variables of the block (core/liquid.js liquidScope)
|
|
83
83
|
start(container, content, onUpdate, { scope = [] } = {}) {
|
|
84
84
|
this.stop()
|
|
85
85
|
container.replaceChildren()
|
|
@@ -11,10 +11,15 @@
|
|
|
11
11
|
// scope the loop variables there (core/variables.js loopScope)
|
|
12
12
|
//
|
|
13
13
|
// See inputAdapter below and TextEditing for Tiptap's. With the host's
|
|
14
|
-
// translations it also suggests their keys while typing {{ '.
|
|
14
|
+
// translations it also suggests their keys while typing {{ '. What it
|
|
15
|
+
// suggests depends on what's being written (core/liquid.js liquidContext): a
|
|
16
|
+
// variable; after a |, the host's filters; a filter's argument, a property of
|
|
17
|
+
// the collection's items ("party") or a value it can have ("sponsor"); and
|
|
18
|
+
// after == or !=, the values of the variable compared.
|
|
15
19
|
|
|
20
|
+
import { expressionParts, liquidContext, quoted, textScope } from "tess_edit/core/liquid"
|
|
16
21
|
import { keyCompletion, keyToken } from "tess_edit/core/translations"
|
|
17
|
-
import { completion
|
|
22
|
+
import { completion } from "tess_edit/core/variables"
|
|
18
23
|
import { t } from "tess_edit/core/i18n"
|
|
19
24
|
import { h } from "tess_edit/ui/dom"
|
|
20
25
|
|
|
@@ -47,27 +52,98 @@ export class VariableMenu {
|
|
|
47
52
|
const key = this.editor.translations?.enabled && adapter.mode === "output" && keyToken(before)
|
|
48
53
|
if (key) return this.suggestKeys(adapter, key)
|
|
49
54
|
if (!this.editor.variables?.enabled) return this.close()
|
|
50
|
-
const
|
|
51
|
-
if (!
|
|
52
|
-
|
|
53
|
-
const dot = token.path.lastIndexOf(".")
|
|
54
|
-
const parent = dot < 0 ? "" : token.path.slice(0, dot)
|
|
55
|
-
const partial = token.path.slice(dot + 1).toLowerCase()
|
|
56
|
-
const start = token.start + dot + 1
|
|
55
|
+
const context = liquidContext(before, adapter.mode === "output" ? "output" : "expression")
|
|
56
|
+
if (!context) return this.close()
|
|
57
|
+
const scope = [...textScope(before), ...(adapter.scope || [])]
|
|
57
58
|
const request = ++this.request
|
|
58
|
-
let
|
|
59
|
+
let found
|
|
59
60
|
try {
|
|
60
|
-
|
|
61
|
+
found = await this.options(context, scope)
|
|
61
62
|
} catch (error) {
|
|
62
|
-
|
|
63
|
+
found = null
|
|
63
64
|
}
|
|
64
65
|
if (request !== this.request) return
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
if (!found?.items.length) return this.close()
|
|
67
|
+
this.show({ adapter, browse: false, items: found.items, index: 0, choose: found.choose })
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// What fits a context, those starting with what's typed first, then those
|
|
71
|
+
// whose label has it: { items, choose }
|
|
72
|
+
async options(context, scope) {
|
|
73
|
+
const variables = this.editor.variables
|
|
74
|
+
if (context.kind === "variable") return this.variableOptions(context, context.path, scope)
|
|
75
|
+
if (context.kind === "filter") {
|
|
76
|
+
const filters = [...(await variables.filters()).values()]
|
|
77
|
+
const items = ranked(filters.map((filter) => ({ name: filter.name, label: filter.label, hint: signature(filter), filter })), context.partial)
|
|
78
|
+
if (items.length === 1 && items[0].name === context.partial && !items[0].filter.arguments?.length) return null
|
|
79
|
+
return {
|
|
80
|
+
items,
|
|
81
|
+
choose: async (item) => {
|
|
82
|
+
const adapter = this.state.adapter
|
|
83
|
+
const more = item.filter.arguments?.length > 0
|
|
84
|
+
adapter.replace(context.start, adapter.before().length, more ? `${item.name}: ` : item.name)
|
|
85
|
+
more ? await this.suggest(adapter) : this.close()
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (context.kind === "argument") {
|
|
90
|
+
const filter = (await variables.filters()).get(context.filter)
|
|
91
|
+
const kind = filter?.arguments?.[context.index]?.kind
|
|
92
|
+
const more = filter?.arguments?.length > context.index + 1
|
|
93
|
+
if (kind === "property" || kind === "value") {
|
|
94
|
+
const elements = await this.elements(context.input, scope)
|
|
95
|
+
if (!elements) return null
|
|
96
|
+
const choices = kind === "property"
|
|
97
|
+
? elements.map((variable) => ({ value: variable.name, label: variable.label }))
|
|
98
|
+
: elements.find((variable) => variable.name === context.args[context.index - 1])?.values || []
|
|
99
|
+
return this.quotedOptions(context, choices, more)
|
|
100
|
+
}
|
|
101
|
+
return context.quote ? null : this.variableOptions(context, context.partial, scope)
|
|
102
|
+
}
|
|
103
|
+
if (context.kind === "value") {
|
|
104
|
+
const variable = await variables.lookup(context.of, scope)
|
|
105
|
+
return this.quotedOptions(context, variable?.values || [], false)
|
|
106
|
+
}
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
69
109
|
|
|
70
|
-
|
|
110
|
+
// The variables for a path being typed, from `context.start`
|
|
111
|
+
async variableOptions(context, path, scope) {
|
|
112
|
+
const dot = path.lastIndexOf(".")
|
|
113
|
+
const parent = dot < 0 ? "" : path.slice(0, dot)
|
|
114
|
+
const partial = path.slice(dot + 1)
|
|
115
|
+
const start = context.start + dot + 1
|
|
116
|
+
const items = ranked(await this.editor.variables.options(parent, scope), partial)
|
|
117
|
+
if (items.length === 1 && items[0].name.toLowerCase() === partial.toLowerCase() && !items[0].children) return null
|
|
118
|
+
return { items, choose: (item) => this.complete(this.state.adapter, context, start, item) }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// The variables of a collection's items, for a filter on it: in
|
|
122
|
+
// `lines | where: "`, those of each line
|
|
123
|
+
async elements(input, scope) {
|
|
124
|
+
const parts = expressionParts(input)
|
|
125
|
+
if (!parts) return null
|
|
126
|
+
const [entry] = await this.editor.variables.resolveScope([{ variable: "\u0000", expression: input }])
|
|
127
|
+
if (!entry?.path) return null
|
|
128
|
+
const collection = await this.editor.variables.lookup(entry.path, scope)
|
|
129
|
+
if (!collection?.collection) return null
|
|
130
|
+
return this.editor.variables.options(`${entry.path}.first`, scope)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Values in quotes ({ value, label }), then a comma for the next argument
|
|
134
|
+
quotedOptions(context, choices, more) {
|
|
135
|
+
const items = ranked(choices.map((choice) => ({ name: `"${choice.value}"`, value: choice.value, label: choice.label })), context.partial, (item) => item.value)
|
|
136
|
+
if (!items.length || (items.length === 1 && items[0].value === context.partial)) return null
|
|
137
|
+
return {
|
|
138
|
+
items,
|
|
139
|
+
choose: async (item) => {
|
|
140
|
+
const adapter = this.state.adapter
|
|
141
|
+
const before = adapter.before()
|
|
142
|
+
const { text, through } = quoted(item.value, { quote: context.quote, after: adapter.after() })
|
|
143
|
+
adapter.replace(context.start, before.length + through, more ? `${text}, ` : text)
|
|
144
|
+
more ? await this.suggest(adapter) : this.close()
|
|
145
|
+
}
|
|
146
|
+
}
|
|
71
147
|
}
|
|
72
148
|
|
|
73
149
|
// The translation keys with what's typed, those that start with it first,
|
|
@@ -94,6 +170,7 @@ export class VariableMenu {
|
|
|
94
170
|
}
|
|
95
171
|
|
|
96
172
|
async complete(adapter, token, start, item) {
|
|
173
|
+
if (!adapter) return
|
|
97
174
|
const before = adapter.before()
|
|
98
175
|
const { text, more } = completion(item, { tag: token.tag, before: before.slice(0, start), after: adapter.after(), collection: adapter.mode === "collection" })
|
|
99
176
|
adapter.replace(start, before.length, text)
|
|
@@ -259,3 +336,17 @@ export function suggestIn(menu, input, adapter) {
|
|
|
259
336
|
input.addEventListener("keydown", (event) => menu.key(event))
|
|
260
337
|
input.addEventListener("blur", () => !menu.state?.browse && menu.close())
|
|
261
338
|
}
|
|
339
|
+
|
|
340
|
+
// Those whose name (or `name(item)`) starts with what's typed, then those
|
|
341
|
+
// whose label has it
|
|
342
|
+
function ranked(items, partial, name = (item) => item.name) {
|
|
343
|
+
const typed = (partial || "").toLowerCase()
|
|
344
|
+
const named = items.filter((item) => name(item).toLowerCase().startsWith(typed))
|
|
345
|
+
return [...named, ...items.filter((item) => !named.includes(item) && item.label?.toLowerCase().includes(typed))]
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// A filter as it's written: date: format
|
|
349
|
+
function signature(filter) {
|
|
350
|
+
const args = (filter.arguments || []).map((argument) => argument.name)
|
|
351
|
+
return args.length ? `${filter.name}: ${args.join(", ")}` : filter.name
|
|
352
|
+
}
|
data/config/locales/en.yml
CHANGED
data/config/locales/nl.yml
CHANGED
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.12
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tom de Grunt
|
|
@@ -105,6 +105,7 @@ files:
|
|
|
105
105
|
- app/javascript/tess_edit/core/document.js
|
|
106
106
|
- app/javascript/tess_edit/core/history.js
|
|
107
107
|
- app/javascript/tess_edit/core/i18n.js
|
|
108
|
+
- app/javascript/tess_edit/core/liquid.js
|
|
108
109
|
- app/javascript/tess_edit/core/lock.js
|
|
109
110
|
- app/javascript/tess_edit/core/media.js
|
|
110
111
|
- app/javascript/tess_edit/core/registry.js
|