tess_edit 0.5.2 → 0.5.4
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 +35 -2
- data/app/javascript/tess_edit/blocks/fields.js +2 -1
- data/app/javascript/tess_edit/controllers/editor_controller.js +17 -10
- data/app/javascript/tess_edit/core/registry.js +46 -2
- data/app/javascript/tess_edit/index.js +1 -1
- data/app/javascript/tess_edit/ui/canvas.js +27 -6
- data/app/javascript/tess_edit/ui/font_picker.js +141 -0
- data/app/javascript/tess_edit/ui/inspector.js +9 -16
- data/app/javascript/tess_edit/ui/text_editing.js +46 -1
- 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: 339b84b5435ae8bc50b728abaa20b03ee21815d12c6f1e35fe352e9284e180dc
|
|
4
|
+
data.tar.gz: 691c2849891dbd2fa6853899956d22d394d170fe25d63f70faf798c5ef6aa503
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f9f5a4b747ae3bc8e16a2682498aa33fb414950a5d111f6d9b874c20d80fd6d5fe185e6f389d1a829e5276337c6e22fbb5f9158118f9485c6a9d39ab16a33a51
|
|
7
|
+
data.tar.gz: c9c6a0a7c8203d2149aae95403496a0c8fe24763bccf9f29dc08f2ba619103c31f96b135d8837418289c61fa858c0b8030f89f28924640eadb5477e35a22435d
|
data/README.md
CHANGED
|
@@ -16,7 +16,7 @@ It completes Liquid tags and variables as you type, making it easier to work wit
|
|
|
16
16
|
- Blocks: Section (1-4 columns), Column, Wrapper, Hero, Text, Heading, Image, Button, Divider, Spacer, Social, Navbar and raw HTML.
|
|
17
17
|
- Liquid blocks: **Loop** (`{% for line in order.lines %}`) and **Condition** (`{% if order.total > 100 %}`) wrap other blocks.
|
|
18
18
|
- 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.
|
|
19
|
-
- Rich text editing on the canvas with [Tiptap](https://tiptap.dev): bold, italic, underline, strikethrough, colour, links (Liquid allowed in the URL), alignment, lists and headings.
|
|
19
|
+
- Rich text editing on the canvas with [Tiptap](https://tiptap.dev): bold, italic, underline, strikethrough, colour, links (Liquid allowed in the URL), alignment, lists and headings. A block can have several texts edited on the canvas, rich or a plain line, such as a question's label and its description.
|
|
20
20
|
- Your app's Liquid variables suggested while typing `{{`, in text and in settings, and browsable from `{ }` buttons, including those of the Loop blocks around a block.
|
|
21
21
|
- Your app's translations for Liquid's `t` filter (`{{ '.greeting' | t }}`) shown as their text in a language of your choice, with every language's text, missing ones marked, editable when pointed at, and selected text made translatable.
|
|
22
22
|
- Undo/redo, duplicate, delete, keyboard shortcuts (⌘Z, ⇧⌘Z, ⌘D, Delete, Enter to edit text, Esc).
|
|
@@ -280,6 +280,21 @@ Roles say where a block may go: `body` (in the email or a wrapper, like sections
|
|
|
280
280
|
|
|
281
281
|
`extendBlock(type, { fields })` adds fields to a block that is registered: a group with the title of one of its groups adds to that one, any other goes last, or before the group its `before` names.
|
|
282
282
|
|
|
283
|
+
#### Texts edited on the canvas
|
|
284
|
+
|
|
285
|
+
A block's `richText: "data.content"` is a Tiptap value edited on the block's own element, by clicking a selected block or double-clicking it. A block with more texts, or with other content around its text, declares its parts with `inline` instead, and marks the element of each with `data-tess-edit-field` in the editor:
|
|
286
|
+
|
|
287
|
+
```js
|
|
288
|
+
registerBlock({
|
|
289
|
+
type: "question",
|
|
290
|
+
inline: [{ key: "data.label", plain: true }, { key: "data.description" }],
|
|
291
|
+
render: { form: (node, { editor }) => `<label${editor ? ' data-tess-edit-field="data.label"' : ""}>${node.data.label}</label> <input> …` },
|
|
292
|
+
…
|
|
293
|
+
})
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
The text editor is mounted on that element only, so the rest of the block, such as an input, stays as it is. A `plain` part is one line of text, stored as a string: no toolbar or marks, and Enter or leaving it finishes. The others are rich text as above, finished with Esc. A part can also be a field in Inspect, so both ways work. The demo's Note kit has a Card block with both (`test/dummy/app/javascript/kits/note.js`).
|
|
297
|
+
|
|
283
298
|
#### Your own form fields in Inspect
|
|
284
299
|
|
|
285
300
|
A `slot` field shows an element of your page in Inspect, for settings that aren't part of the document, such as a template's trigger in your own form fields. Render the element inside the editor, hidden, as a `slot` target named by `data-tess-edit-slot`:
|
|
@@ -304,7 +319,25 @@ extendBlock("email", {
|
|
|
304
319
|
|
|
305
320
|
With a `summary` the field shows collapsed to it, with a button to edit it; without one the element always shows. Its values are your form fields, submitted with the form; without the element the field doesn't show. The element moves into Inspect once and stays there, hidden while another block is selected, so your Stimulus controllers in it keep their state. Combining or splitting the panels moves it again: its controllers reconnect then, with the values typed and chosen written into the fields' attributes first. The demo has one at `/?slots=1` (`test/dummy/app/javascript/slots.js`).
|
|
306
321
|
|
|
307
|
-
|
|
322
|
+
#### Field types of your own
|
|
323
|
+
|
|
324
|
+
For a field TessEdit's types can't make, such as one whose parts depend on each other, register a type of your own and use it in a block's `fields`:
|
|
325
|
+
|
|
326
|
+
```js
|
|
327
|
+
import { registerFieldType } from "tess_edit"
|
|
328
|
+
|
|
329
|
+
registerFieldType("rules", (node, field, value, set, editor) => {
|
|
330
|
+
const element = document.createElement("div")
|
|
331
|
+
// … inputs that call set(next) with the field's new value
|
|
332
|
+
return element
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
registerBlock({ …, fields: [{ title: "Show when", fields: [{ key: "data.rules", label: "Rules", type: "rules" }] }] })
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
The element goes under the field's label in Inspect. `value` is the field's value (`undefined` when it has none) and `set(next)` stores a new one as an undo step. Inspect doesn't render the field again after a `set`, so the element keeps its focus and updates itself; it renders it again when another block is selected or the document changes otherwise. A locked field's element is disabled through a fieldset. TessEdit's own types can't be replaced. The demo's Card block has one, `stars`.
|
|
339
|
+
|
|
340
|
+
A select field's `options` can be a function of the editor, `options: (editor) => [...]`, for choices from your app. Give the editor those with `data-tess-edit-options-value` (JSON), which blocks read as `editor.options`. Font family fields are a select of `editor.options.fonts`: CSS font families as text, `"Maytra"` or `"'EB Garamond', serif"`, or `{ value, label }`; without them, common web fonts (`fields.FONTS`). A font a design has that isn't among them stays selected. The field lists each font in itself, with a search box, so your page needs the fonts too, such as `@font-face` rules: a browser loads a font only once it's used, and the list shows fonts as they scroll into view.
|
|
308
341
|
|
|
309
342
|
## Kits
|
|
310
343
|
|
|
@@ -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, font, segmented, toggle,
|
|
4
|
-
// spacing, list and slot (an element of the host's); see ui/inspector.js
|
|
4
|
+
// spacing, list and slot (an element of the host's); see ui/inspector.js,
|
|
5
|
+
// and the host's own, registerFieldType (core/registry.js). A
|
|
5
6
|
// font field is a select of the host's fonts, the editor's options.fonts, or
|
|
6
7
|
// FONTS without them. A select's options can be a
|
|
7
8
|
// function of the editor, options(editor), for choices the host gives. A
|
|
@@ -47,7 +47,7 @@ import { Controller } from "@hotwired/stimulus"
|
|
|
47
47
|
import { accepted, ancestors, createDocument, createFromPalette, duplicate, find, findType, getValue, insert, itemType, misfit, move, normalize, remove, setValue, walk } from "tess_edit/core/document"
|
|
48
48
|
import { History } from "tess_edit/core/history"
|
|
49
49
|
import { canEditField, canFill, isFree, parseLock } from "tess_edit/core/lock"
|
|
50
|
-
import { DEFAULT_KIT, getBlock, getKit, paletteItems, paletteSources } from "tess_edit/core/registry"
|
|
50
|
+
import { DEFAULT_KIT, getBlock, getKit, inlinePart, inlineParts, paletteItems, paletteSources } from "tess_edit/core/registry"
|
|
51
51
|
import { COMPILE_URL } from "tess_edit/export/compile"
|
|
52
52
|
import { IMAGE_FIELD, fetchMedia, uploadMedia } from "tess_edit/core/media"
|
|
53
53
|
import { Translations, keysIn, suggestKey, toTranslation, translationTag } from "tess_edit/core/translations"
|
|
@@ -98,6 +98,7 @@ export default class extends Controller {
|
|
|
98
98
|
this.doc = this.loadInitial()
|
|
99
99
|
this.selectedId = this.doc.id
|
|
100
100
|
this.editingId = null
|
|
101
|
+
this.editingPart = null
|
|
101
102
|
this.drag = null
|
|
102
103
|
this.mode = "edit"
|
|
103
104
|
this.device = this.kit.devices?.[0]?.key || null
|
|
@@ -711,23 +712,28 @@ export default class extends Controller {
|
|
|
711
712
|
this.select(siblings[Math.min(hit.index, siblings.length - 1)]?.id || hit.parent.id)
|
|
712
713
|
}
|
|
713
714
|
|
|
714
|
-
|
|
715
|
+
// Edits an inline part of a block on the canvas: the one with this key,
|
|
716
|
+
// or its first (see `inline` in core/registry.js)
|
|
717
|
+
startEditing(id, key = null) {
|
|
715
718
|
const hit = find(this.doc, id)
|
|
716
|
-
const
|
|
717
|
-
if (!
|
|
718
|
-
if (this.editingId === id) return
|
|
719
|
+
const part = hit && (key ? inlinePart(hit.node.type, key) : inlineParts(hit.node.type)[0])
|
|
720
|
+
if (!part || this.mode !== "edit" || !this.canEditField(id, part.key)) return
|
|
721
|
+
if (this.editingId === id && this.editingPart?.key === part.key) return
|
|
719
722
|
this.stopEditing()
|
|
720
723
|
this.select(id)
|
|
721
|
-
const container = this.canvas.textContainer(id)
|
|
724
|
+
const container = this.canvas.textContainer(id, part.key)
|
|
722
725
|
if (!container) return
|
|
723
726
|
this.editingId = id
|
|
727
|
+
this.editingPart = part
|
|
724
728
|
this.editSnapshot = JSON.stringify(this.doc)
|
|
725
|
-
|
|
726
|
-
setValue(this.doc, id, key,
|
|
729
|
+
const update = (value) => {
|
|
730
|
+
setValue(this.doc, id, part.key, value)
|
|
727
731
|
this.tree.render()
|
|
728
732
|
this.canvas.refresh()
|
|
729
733
|
this.scheduleOutputs()
|
|
730
|
-
}
|
|
734
|
+
}
|
|
735
|
+
if (part.plain) this.textEditing.startPlain(container, getValue(hit.node, part.key), update, () => this.stopEditing())
|
|
736
|
+
else this.textEditing.start(container, getValue(hit.node, part.key), update, { scope: loopScope(this.doc, id) })
|
|
731
737
|
this.canvas.refresh()
|
|
732
738
|
}
|
|
733
739
|
|
|
@@ -735,6 +741,7 @@ export default class extends Controller {
|
|
|
735
741
|
if (!this.editingId) return
|
|
736
742
|
this.textEditing.stop()
|
|
737
743
|
this.editingId = null
|
|
744
|
+
this.editingPart = null
|
|
738
745
|
if (this.editSnapshot !== JSON.stringify(this.doc)) this.history.record(this.editSnapshot)
|
|
739
746
|
this.editSnapshot = null
|
|
740
747
|
this.renderTopBar()
|
|
@@ -965,7 +972,7 @@ export default class extends Controller {
|
|
|
965
972
|
event.preventDefault()
|
|
966
973
|
this.remove(this.selectedId)
|
|
967
974
|
}
|
|
968
|
-
} else if (event.key === "Enter" &&
|
|
975
|
+
} else if (event.key === "Enter" && inlineParts(find(this.doc, this.selectedId)?.node.type).length) {
|
|
969
976
|
event.preventDefault()
|
|
970
977
|
this.startEditing(this.selectedId)
|
|
971
978
|
} else if (event.key === "Escape") {
|
|
@@ -15,7 +15,13 @@
|
|
|
15
15
|
// // a "code" field takes language: "html" (default) or "css"
|
|
16
16
|
// hint: "Shown at the top of Inspect.",
|
|
17
17
|
// label: (node) => `Product · ${node.data.sku}`, // optional, for Layers and the canvas
|
|
18
|
-
// richText: "data.content", // optional: a Tiptap JSON value edited on the canvas by double-click
|
|
18
|
+
// richText: "data.content", // optional: a Tiptap JSON value edited on the canvas by double-click,
|
|
19
|
+
// // on the block's own element; the shorthand of one inline part
|
|
20
|
+
// inline: [{ key: "data.label", plain: true }, { key: "data.description" }],
|
|
21
|
+
// // optional: the parts edited on the canvas, each on the element
|
|
22
|
+
// // its render marks with data-tess-edit-field="<key>" in the
|
|
23
|
+
// // editor, so the rest of the block stays as it is. A plain part
|
|
24
|
+
// // is one line of text, a string; the others are Tiptap JSON
|
|
19
25
|
// palette: [{ key, label, icon, category, create: ({ createNode }) => node }], // optional
|
|
20
26
|
// kits: ["email"], // the kits it is part of, default ["email"]
|
|
21
27
|
// unique: true, // optional: at most one per document, like Content
|
|
@@ -81,6 +87,7 @@ const definitions = new Map()
|
|
|
81
87
|
const categoryList = new Map()
|
|
82
88
|
const kitList = new Map()
|
|
83
89
|
const sourceList = new Map()
|
|
90
|
+
const fieldTypeList = new Map()
|
|
84
91
|
|
|
85
92
|
export const DEFAULT_KIT = "email"
|
|
86
93
|
|
|
@@ -156,10 +163,47 @@ export function registerBlock(definition) {
|
|
|
156
163
|
...item,
|
|
157
164
|
type
|
|
158
165
|
}))
|
|
159
|
-
|
|
166
|
+
const inline = definition.inline ?? (definition.richText ? [{ key: definition.richText }] : [])
|
|
167
|
+
if (inline.some((part) => !part?.key)) throw new Error(`Block ${type} needs a key for each inline part`)
|
|
168
|
+
definitions.set(type, { roles: [], kits: [DEFAULT_KIT], ...definition, palette, inline })
|
|
160
169
|
return definitions.get(type)
|
|
161
170
|
}
|
|
162
171
|
|
|
172
|
+
// The parts of a block edited on the canvas (see `inline`)
|
|
173
|
+
export function inlineParts(type) {
|
|
174
|
+
return getBlock(type)?.inline || []
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function inlinePart(type, key) {
|
|
178
|
+
return inlineParts(type).find((part) => part.key === key)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// The field types of ui/inspector.js
|
|
182
|
+
export const FIELD_TYPES = ["text", "textarea", "code", "size", "color", "select", "font", "segmented", "toggle", "spacing", "list", "image", "slot"]
|
|
183
|
+
|
|
184
|
+
// A field type of the host's, for what TessEdit's own can't do, such as
|
|
185
|
+
// parts that depend on each other:
|
|
186
|
+
//
|
|
187
|
+
// registerFieldType("rules", (node, field, value, set, editor) => element)
|
|
188
|
+
//
|
|
189
|
+
// It renders the control under the field's label in Inspect: value is the
|
|
190
|
+
// field's value (undefined when it has none), set(next) stores a new one as
|
|
191
|
+
// an undo step. Inspect doesn't render the field again after a set, so the
|
|
192
|
+
// element keeps its focus and updates itself; it renders all fields again
|
|
193
|
+
// when another block is selected or the document changes otherwise. A
|
|
194
|
+
// locked field's element is disabled through a fieldset around it.
|
|
195
|
+
// Registering a type again replaces it; TessEdit's own can't be.
|
|
196
|
+
export function registerFieldType(type, render) {
|
|
197
|
+
if (!type || typeof type !== "string") throw new Error("A field type needs a name")
|
|
198
|
+
if (FIELD_TYPES.includes(type)) throw new Error(`Field type ${type} is TessEdit's own`)
|
|
199
|
+
if (typeof render !== "function") throw new Error(`Field type ${type} needs render(node, field, value, set, editor)`)
|
|
200
|
+
fieldTypeList.set(type, render)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function fieldType(type) {
|
|
204
|
+
return fieldTypeList.get(type)
|
|
205
|
+
}
|
|
206
|
+
|
|
163
207
|
// Adds kits, render hooks and field groups to a registered block. A group
|
|
164
208
|
// with the title of one it has adds its fields to that one; a new group goes
|
|
165
209
|
// last, or before the group its `before` names.
|
|
@@ -12,7 +12,7 @@ export { compile } from "tess_edit/export/compile"
|
|
|
12
12
|
export { escapeAttribute, escapeText, richTextToHtml } from "tess_edit/export/richtext"
|
|
13
13
|
export { contentNode, createDocument, createNode, findType, normalize, renumber, withContent } from "tess_edit/core/document"
|
|
14
14
|
export { CONTENT_END, CONTENT_START } from "tess_edit/blocks/content"
|
|
15
|
-
export { registerBlock, registerCategory, registerKit, registerPaletteSource, extendBlock, getBlock, getKit, kitOf, kits, blockTypes, paletteItems, paletteSources } from "tess_edit/core/registry"
|
|
15
|
+
export { registerBlock, registerCategory, registerFieldType, registerKit, registerPaletteSource, extendBlock, getBlock, getKit, kitOf, kits, blockTypes, paletteItems, paletteSources } from "tess_edit/core/registry"
|
|
16
16
|
export * as fields from "tess_edit/blocks/fields"
|
|
17
17
|
export { imageField } from "tess_edit/core/media"
|
|
18
18
|
export { starterDocument } from "tess_edit/core/starter"
|
|
@@ -22,6 +22,7 @@ body{min-height:100vh;margin:0}
|
|
|
22
22
|
.tess-label{position:absolute;bottom:100%;left:0;background:#2563eb;color:#fff;padding:3px 6px;border-radius:4px 4px 0 0;white-space:nowrap}
|
|
23
23
|
.tess-hover .tess-label{background:#60a5fa}
|
|
24
24
|
.tess-editing .tess-label{background:#f59e0b}
|
|
25
|
+
.tess-plain{outline:none;cursor:text}
|
|
25
26
|
.tess-inside .tess-label,.tess-inside .tess-tools{bottom:auto;top:0;border-radius:0 0 4px 4px}
|
|
26
27
|
.tess-tools{position:absolute;bottom:100%;right:0;display:flex;gap:1px;pointer-events:auto;background:#2563eb;border-radius:4px 4px 0 0;padding:1px}
|
|
27
28
|
.tess-tools [data-tool]{cursor:pointer;color:#fff;width:22px;height:20px;display:flex;align-items:center;justify-content:center;border-radius:3px;background:none;border:0;padding:0}
|
|
@@ -218,11 +219,29 @@ export class Canvas {
|
|
|
218
219
|
return { left, top, right, bottom, width: right - left, height: bottom - top }
|
|
219
220
|
}
|
|
220
221
|
|
|
221
|
-
|
|
222
|
+
// Where an inline part is edited: the element its render marks with
|
|
223
|
+
// data-tess-edit-field, or for the richText shorthand the block's own
|
|
224
|
+
textContainer(id, key) {
|
|
222
225
|
const cell = this.doc?.querySelector(`.tess-${id}`)
|
|
226
|
+
const part = this.partElement(cell, key)
|
|
227
|
+
if (part) return part
|
|
228
|
+
if (key !== getBlock(find(this.editor.doc, id)?.node.type)?.richText) return null
|
|
223
229
|
return cell?.querySelector(":scope > div") || cell
|
|
224
230
|
}
|
|
225
231
|
|
|
232
|
+
partElement(cell, key) {
|
|
233
|
+
return [...(cell?.querySelectorAll("[data-tess-edit-field]") || [])].find((element) => element.dataset.tessEditField === key)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// The inline part of a block at a clicked element: a marked one, or the
|
|
237
|
+
// richText shorthand's anywhere in the block
|
|
238
|
+
partAt(target, id) {
|
|
239
|
+
const definition = getBlock(find(this.editor.doc, id)?.node.type)
|
|
240
|
+
const marked = target.closest?.("[data-tess-edit-field]")
|
|
241
|
+
if (marked && this.hit(marked)?.id === id && definition?.inline.some((part) => part.key === marked.dataset.tessEditField)) return marked.dataset.tessEditField
|
|
242
|
+
return definition?.richText || null
|
|
243
|
+
}
|
|
244
|
+
|
|
226
245
|
// --- overlay ---------------------------------------------------------------
|
|
227
246
|
|
|
228
247
|
box(rect, className) {
|
|
@@ -258,7 +277,7 @@ export class Canvas {
|
|
|
258
277
|
if (!rect) return
|
|
259
278
|
const box = this.box(rect, editingId === selectedId ? "tess-selected tess-editing" : "tess-selected")
|
|
260
279
|
const free = this.editor.isFree(selectedId)
|
|
261
|
-
box.append(Object.assign(this.doc.createElement("div"), { className: "tess-label", textContent: editingId === selectedId ?
|
|
280
|
+
box.append(Object.assign(this.doc.createElement("div"), { className: "tess-label", textContent: editingId === selectedId ? `Editing text · ${this.editor.editingPart?.plain ? "Enter" : "Esc"} to finish` : free || !this.editor.lock ? label(hit.node) : `${label(hit.node)} · Layout` }))
|
|
262
281
|
|
|
263
282
|
const handle = this.free.handle(selectedId)
|
|
264
283
|
if (handle) box.append(handle)
|
|
@@ -330,7 +349,7 @@ export class Canvas {
|
|
|
330
349
|
}
|
|
331
350
|
|
|
332
351
|
onClick(event) {
|
|
333
|
-
if (this.editor.editingId && event.target.closest?.(".ProseMirror")) return
|
|
352
|
+
if (this.editor.editingId && event.target.closest?.(".ProseMirror, .tess-plain")) return
|
|
334
353
|
if (event.target.closest?.("#tess-overlay")) return
|
|
335
354
|
event.preventDefault()
|
|
336
355
|
if (this.openLink(event)) return
|
|
@@ -342,8 +361,9 @@ export class Canvas {
|
|
|
342
361
|
this.editor.openInsertMenu(hit.id, 0, rect)
|
|
343
362
|
return
|
|
344
363
|
}
|
|
345
|
-
|
|
346
|
-
|
|
364
|
+
const part = hit && hit.id === this.editor.selectedId && this.partAt(event.target, hit.id)
|
|
365
|
+
if (part) {
|
|
366
|
+
this.editor.startEditing(hit.id, part)
|
|
347
367
|
return
|
|
348
368
|
}
|
|
349
369
|
this.editor.select(hit ? hit.id : this.editor.doc.id, { reveal: true })
|
|
@@ -360,7 +380,8 @@ export class Canvas {
|
|
|
360
380
|
|
|
361
381
|
onDoubleClick(event) {
|
|
362
382
|
const hit = this.hit(event.target)
|
|
363
|
-
|
|
383
|
+
const part = hit && !hit.placeholder && this.partAt(event.target, hit.id)
|
|
384
|
+
if (part) this.editor.startEditing(hit.id, part)
|
|
364
385
|
}
|
|
365
386
|
|
|
366
387
|
onDragOver(event) {
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// A font field's control: a button with the font in it, and a list to choose
|
|
2
|
+
// from, each font shown in itself, with a search box for long lists. A
|
|
3
|
+
// native select can't show its options in their own fonts (macOS draws it
|
|
4
|
+
// in the select's font). Fonts are shown as they scroll into view, so a long
|
|
5
|
+
// list only loads those seen; the host's page has to have the fonts (e.g. as
|
|
6
|
+
// @font-face rules, which load when used).
|
|
7
|
+
|
|
8
|
+
import { h } from "tess_edit/ui/dom"
|
|
9
|
+
|
|
10
|
+
const ITEM = "flex w-full items-center gap-2 px-2.5 py-1.5 text-left text-[15px]"
|
|
11
|
+
|
|
12
|
+
export class FontPicker {
|
|
13
|
+
// options: [{ value, label }], the first one none (the placeholder);
|
|
14
|
+
// choose(value) when one is chosen
|
|
15
|
+
constructor(editor, { options, value, input, choose }) {
|
|
16
|
+
this.editor = editor
|
|
17
|
+
this.options = options
|
|
18
|
+
this.value = value || ""
|
|
19
|
+
this.choose = choose
|
|
20
|
+
this.button = h("button", { type: "button", class: `${input} flex items-center justify-between gap-2 text-left`, "aria-haspopup": "listbox", onclick: () => (this.element ? this.close() : this.open()) })
|
|
21
|
+
this.onOutside = (event) => {
|
|
22
|
+
if (this.element && !this.element.contains(event.target) && !this.button.contains(event.target)) this.close()
|
|
23
|
+
}
|
|
24
|
+
this.label()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// The chosen font's name, in it
|
|
28
|
+
label() {
|
|
29
|
+
const option = this.options.find((option) => option.value === this.value) || this.options[0]
|
|
30
|
+
this.button.replaceChildren(
|
|
31
|
+
h("span", { class: "min-w-0 flex-1 truncate", style: fontStyle(option.value) }, option.label),
|
|
32
|
+
h("span", { class: "shrink-0 text-gray-400 dark:text-gray-500", "aria-hidden": "true" }, "▾")
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
open() {
|
|
37
|
+
this.query = ""
|
|
38
|
+
this.search = h("input", {
|
|
39
|
+
type: "search",
|
|
40
|
+
placeholder: "Search fonts",
|
|
41
|
+
class: "w-full rounded-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 px-2 py-1 text-[13px] text-gray-900 dark:text-gray-100 outline-none focus:border-blue-500",
|
|
42
|
+
oninput: (event) => {
|
|
43
|
+
this.query = event.target.value
|
|
44
|
+
this.index = 0
|
|
45
|
+
this.render()
|
|
46
|
+
},
|
|
47
|
+
onkeydown: (event) => this.key(event)
|
|
48
|
+
})
|
|
49
|
+
this.list = h("div", { class: "max-h-72 overflow-y-auto py-1", role: "listbox", "aria-label": "Fonts" })
|
|
50
|
+
this.element = h("div", { class: "absolute z-50 w-72 rounded-md border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-lg" }, h("div", { class: "p-1.5 border-b border-gray-100 dark:border-gray-700" }, this.search), this.list)
|
|
51
|
+
this.editor.root.append(this.element)
|
|
52
|
+
this.observer = new IntersectionObserver((entries) => entries.forEach((entry) => entry.isIntersecting && show(entry.target)), { root: this.list })
|
|
53
|
+
document.addEventListener("mousedown", this.onOutside, true)
|
|
54
|
+
this.index = Math.max(0, this.matches().findIndex((option) => option.value === this.value))
|
|
55
|
+
this.render()
|
|
56
|
+
this.place()
|
|
57
|
+
this.search.focus()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
matches() {
|
|
61
|
+
const query = this.query.trim().toLowerCase()
|
|
62
|
+
return query ? this.options.filter((option, i) => i > 0 && option.label.toLowerCase().includes(query)) : this.options
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
render() {
|
|
66
|
+
const matches = this.matches()
|
|
67
|
+
this.observer.disconnect()
|
|
68
|
+
const rows = matches.map((option, i) => {
|
|
69
|
+
const chosen = option.value === this.value
|
|
70
|
+
const row = h(
|
|
71
|
+
"button",
|
|
72
|
+
{
|
|
73
|
+
type: "button",
|
|
74
|
+
role: "option",
|
|
75
|
+
"aria-selected": chosen ? "true" : "false",
|
|
76
|
+
"data-font": option.value,
|
|
77
|
+
class: `${ITEM} ${i === this.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"}`,
|
|
78
|
+
onclick: () => this.pick(option)
|
|
79
|
+
},
|
|
80
|
+
h("span", { class: "w-3 shrink-0 text-[11px]" }, chosen ? "✓" : ""),
|
|
81
|
+
h("span", { class: "min-w-0 flex-1 truncate" }, option.label)
|
|
82
|
+
)
|
|
83
|
+
this.observer.observe(row)
|
|
84
|
+
return row
|
|
85
|
+
})
|
|
86
|
+
const empty = matches.length ? null : h("p", { class: "px-2.5 py-1.5 text-xs text-gray-500 dark:text-gray-400" }, "No fonts with that name")
|
|
87
|
+
this.list.replaceChildren(...rows, ...(empty ? [empty] : []))
|
|
88
|
+
rows[this.index]?.scrollIntoView({ block: "nearest" })
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
key(event) {
|
|
92
|
+
const matches = this.matches()
|
|
93
|
+
if ((event.key === "ArrowDown" || event.key === "ArrowUp") && matches.length) {
|
|
94
|
+
this.index = (this.index + (event.key === "ArrowDown" ? 1 : matches.length - 1)) % matches.length
|
|
95
|
+
this.render()
|
|
96
|
+
} else if (event.key === "Enter") {
|
|
97
|
+
if (matches[this.index]) this.pick(matches[this.index])
|
|
98
|
+
} else if (event.key === "Escape") {
|
|
99
|
+
this.close()
|
|
100
|
+
this.button.focus()
|
|
101
|
+
} else {
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
event.preventDefault()
|
|
105
|
+
event.stopPropagation()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
pick(option) {
|
|
109
|
+
this.value = option.value
|
|
110
|
+
this.label()
|
|
111
|
+
this.close()
|
|
112
|
+
this.choose(option.value)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Below the button, or above it when there's no room, inside the editor
|
|
116
|
+
place() {
|
|
117
|
+
const root = this.editor.root.getBoundingClientRect()
|
|
118
|
+
const rect = this.button.getBoundingClientRect()
|
|
119
|
+
const height = this.element.offsetHeight
|
|
120
|
+
const below = rect.bottom - root.top + 4
|
|
121
|
+
const top = below + height > root.height && rect.top - root.top - height - 4 > 0 ? rect.top - root.top - height - 4 : below
|
|
122
|
+
const left = Math.max(4, Math.min(rect.left - root.left, root.width - this.element.offsetWidth - 4))
|
|
123
|
+
Object.assign(this.element.style, { top: `${top}px`, left: `${left}px` })
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
close() {
|
|
127
|
+
this.observer?.disconnect()
|
|
128
|
+
this.element?.remove()
|
|
129
|
+
this.element = null
|
|
130
|
+
document.removeEventListener("mousedown", this.onOutside, true)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// A row's font, once it shows
|
|
135
|
+
function show(row) {
|
|
136
|
+
if (row.dataset.font) row.style.fontFamily = row.dataset.font
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function fontStyle(value) {
|
|
140
|
+
return value ? `font-family: ${value}` : ""
|
|
141
|
+
}
|
|
@@ -15,12 +15,13 @@
|
|
|
15
15
|
// in Inspect, hidden while another block is selected (see place in ui/dom.js).
|
|
16
16
|
|
|
17
17
|
import { imageSource } from "tess_edit/core/media"
|
|
18
|
-
import { getBlock, label } from "tess_edit/core/registry"
|
|
18
|
+
import { fieldType, getBlock, label } from "tess_edit/core/registry"
|
|
19
19
|
import { fontOptions } from "tess_edit/blocks/fields"
|
|
20
20
|
import { find, getValue } from "tess_edit/core/document"
|
|
21
21
|
import { translationCalls } from "tess_edit/core/translations"
|
|
22
22
|
import { loopScope } from "tess_edit/core/variables"
|
|
23
23
|
import { codeInput } from "tess_edit/ui/code_input"
|
|
24
|
+
import { FontPicker } from "tess_edit/ui/font_picker"
|
|
24
25
|
import { freeze, h, place } from "tess_edit/ui/dom"
|
|
25
26
|
import { icon } from "tess_edit/ui/icons"
|
|
26
27
|
import { inputAdapter, suggestIn } from "tess_edit/ui/variable_menu"
|
|
@@ -196,6 +197,10 @@ export class Inspector {
|
|
|
196
197
|
}
|
|
197
198
|
|
|
198
199
|
control(node, field, value, set) {
|
|
200
|
+
// A host's own (registerFieldType in core/registry.js)
|
|
201
|
+
const custom = fieldType(field.type)
|
|
202
|
+
if (custom) return custom(node, field, getValue(node, field.key), set, this.editor)
|
|
203
|
+
|
|
199
204
|
switch (field.type) {
|
|
200
205
|
case "textarea":
|
|
201
206
|
return this.variables(h("textarea", { class: `${INPUT} min-h-32`, rows: 4, value, oninput: (event) => set(event.target.value) }), node, field)
|
|
@@ -480,22 +485,10 @@ export class Inspector {
|
|
|
480
485
|
input.textarea.focus()
|
|
481
486
|
}
|
|
482
487
|
|
|
483
|
-
// The host's fonts (options.fonts) or common ones, each shown in itself
|
|
488
|
+
// The host's fonts (options.fonts) or common ones, each shown in itself (ui/font_picker.js)
|
|
484
489
|
font(field, value, set) {
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
{
|
|
488
|
-
class: `${INPUT} pr-7`,
|
|
489
|
-
style: value ? `font-family: ${value}` : "",
|
|
490
|
-
onchange: (event) => {
|
|
491
|
-
event.target.style.fontFamily = event.target.value
|
|
492
|
-
set(event.target.value)
|
|
493
|
-
}
|
|
494
|
-
},
|
|
495
|
-
fontOptions(this.editor.options?.fonts, value, field.placeholder).map((option) =>
|
|
496
|
-
h("option", { value: option.value, selected: option.value === (value || ""), style: option.value ? `font-family: ${option.value}` : "" }, option.label)
|
|
497
|
-
)
|
|
498
|
-
)
|
|
490
|
+
const options = fontOptions(this.editor.options?.fonts, value, field.placeholder)
|
|
491
|
+
return new FontPicker(this.editor, { options, value, input: INPUT, choose: set }).button
|
|
499
492
|
}
|
|
500
493
|
|
|
501
494
|
color(value, set) {
|
|
@@ -5,6 +5,10 @@
|
|
|
5
5
|
// translations (`editor.translations`, core/translations.js) a t filter shows
|
|
6
6
|
// its text in the editor's language until the caret goes into it, and the
|
|
7
7
|
// toolbar turns selected text into a translation.
|
|
8
|
+
//
|
|
9
|
+
// A plain part (a block's inline part with plain: true, see core/registry.js)
|
|
10
|
+
// is one line of text, edited on its element itself, with no toolbar or
|
|
11
|
+
// marks: Enter or leaving it finishes (finish).
|
|
8
12
|
|
|
9
13
|
import { Color, Decoration, DecorationSet, Editor, Extension, Plugin, PluginKey, StarterKit, TextAlign, TextStyle } from "tess_edit/vendor/tiptap"
|
|
10
14
|
import { missingArguments, translationCalls } from "tess_edit/core/translations"
|
|
@@ -23,6 +27,7 @@ export class TextEditing {
|
|
|
23
27
|
this.menu = menu
|
|
24
28
|
this.editor = editor
|
|
25
29
|
this.tiptap = null
|
|
30
|
+
this.plain = null
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
get translating() {
|
|
@@ -30,7 +35,37 @@ export class TextEditing {
|
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
get active() {
|
|
33
|
-
return !!this.tiptap
|
|
38
|
+
return !!this.tiptap || !!this.plain
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// One line of text on the element itself: onUpdate gets it as it's typed,
|
|
42
|
+
// finish when Enter is pressed or the element left
|
|
43
|
+
startPlain(element, value, onUpdate, finish) {
|
|
44
|
+
this.stop()
|
|
45
|
+
const text = () => element.textContent.replace(/\s*\n\s*/g, " ")
|
|
46
|
+
const listeners = {
|
|
47
|
+
input: () => onUpdate(text()),
|
|
48
|
+
keydown: (event) => {
|
|
49
|
+
if (event.key !== "Enter") return
|
|
50
|
+
// Not to the editor, which would start editing again
|
|
51
|
+
event.preventDefault()
|
|
52
|
+
event.stopPropagation()
|
|
53
|
+
finish()
|
|
54
|
+
},
|
|
55
|
+
blur: () => finish()
|
|
56
|
+
}
|
|
57
|
+
element.textContent = value ?? ""
|
|
58
|
+
element.contentEditable = "plaintext-only"
|
|
59
|
+
element.classList.add("tess-plain")
|
|
60
|
+
for (const [name, listener] of Object.entries(listeners)) element.addEventListener(name, listener)
|
|
61
|
+
this.plain = { element, listeners, text }
|
|
62
|
+
element.focus()
|
|
63
|
+
const range = element.ownerDocument.createRange()
|
|
64
|
+
range.selectNodeContents(element)
|
|
65
|
+
range.collapse(false)
|
|
66
|
+
element.ownerDocument.getSelection().removeAllRanges()
|
|
67
|
+
element.ownerDocument.getSelection().addRange(range)
|
|
68
|
+
return element
|
|
34
69
|
}
|
|
35
70
|
|
|
36
71
|
// scope: the loop variables of the block (core/variables.js loopScope)
|
|
@@ -106,6 +141,7 @@ export class TextEditing {
|
|
|
106
141
|
}
|
|
107
142
|
|
|
108
143
|
stop() {
|
|
144
|
+
if (this.plain) return this.stopPlain()
|
|
109
145
|
if (!this.tiptap) return null
|
|
110
146
|
const json = this.tiptap.getJSON()
|
|
111
147
|
this.menu?.close()
|
|
@@ -116,6 +152,15 @@ export class TextEditing {
|
|
|
116
152
|
return json
|
|
117
153
|
}
|
|
118
154
|
|
|
155
|
+
stopPlain() {
|
|
156
|
+
const { element, listeners, text } = this.plain
|
|
157
|
+
this.plain = null
|
|
158
|
+
for (const [name, listener] of Object.entries(listeners)) element.removeEventListener(name, listener)
|
|
159
|
+
element.removeAttribute("contenteditable")
|
|
160
|
+
element.classList.remove("tess-plain")
|
|
161
|
+
return text()
|
|
162
|
+
}
|
|
163
|
+
|
|
119
164
|
renderToolbar() {
|
|
120
165
|
const editor = this.tiptap
|
|
121
166
|
if (!editor) return
|
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.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tom de Grunt
|
|
@@ -118,6 +118,7 @@ files:
|
|
|
118
118
|
- app/javascript/tess_edit/ui/code_input.js
|
|
119
119
|
- app/javascript/tess_edit/ui/dialog.js
|
|
120
120
|
- app/javascript/tess_edit/ui/dom.js
|
|
121
|
+
- app/javascript/tess_edit/ui/font_picker.js
|
|
121
122
|
- app/javascript/tess_edit/ui/free_layout.js
|
|
122
123
|
- app/javascript/tess_edit/ui/icons.js
|
|
123
124
|
- app/javascript/tess_edit/ui/inspector.js
|