tess_edit 0.5.3 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f82d8e8c56d5625f8f107f277eca46f3f925f1efd3a61c04869416f9cf22c1f0
4
- data.tar.gz: 2ae16142383df65bc903ed777e1a8e040d9a48985ec326a7bc6d63488659da20
3
+ metadata.gz: 339b84b5435ae8bc50b728abaa20b03ee21815d12c6f1e35fe352e9284e180dc
4
+ data.tar.gz: 691c2849891dbd2fa6853899956d22d394d170fe25d63f70faf798c5ef6aa503
5
5
  SHA512:
6
- metadata.gz: 02ad9420d971ac73f9d86f594ef543f55ca3778ed31626d27b781714c9ba00886db964b6d7a98ce220dabc995cec61100ca20d0fdaa0bb9aaa327bba103deb20
7
- data.tar.gz: 0c7596f3ffe417f49bee62cef20dde71c1cd9584c35df42c3dae41927442c1f209d6e3b6f4ae728b1f548b3cbff7aa46d96c043baac5f53547f470542120fc59
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,6 +319,24 @@ 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
 
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
+
307
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
@@ -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. A
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
- startEditing(id) {
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 key = hit && getBlock(hit.node.type)?.richText
717
- if (!key || this.mode !== "edit" || !this.isFree(id)) return
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
- this.textEditing.start(container, getValue(hit.node, key), (json) => {
726
- setValue(this.doc, id, key, json)
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
- }, { scope: loopScope(this.doc, id) })
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" && getBlock(find(this.doc, this.selectedId)?.node.type)?.richText) {
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
- definitions.set(type, { roles: [], kits: [DEFAULT_KIT], ...definition, palette })
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
- textContainer(id) {
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 ? "Editing text · Esc to finish" : free || !this.editor.lock ? label(hit.node) : `${label(hit.node)} · Layout` }))
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
- if (hit && hit.id === this.editor.selectedId && getBlock(find(this.editor.doc, hit.id).node.type)?.richText) {
346
- this.editor.startEditing(hit.id)
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
- if (hit && !hit.placeholder && getBlock(find(this.editor.doc, hit.id).node.type)?.richText) this.editor.startEditing(hit.id)
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) {
@@ -15,7 +15,7 @@
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"
@@ -197,6 +197,10 @@ export class Inspector {
197
197
  }
198
198
 
199
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
+
200
204
  switch (field.type) {
201
205
  case "textarea":
202
206
  return this.variables(h("textarea", { class: `${INPUT} min-h-32`, rows: 4, value, oninput: (event) => set(event.target.value) }), node, field)
@@ -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
@@ -1,3 +1,3 @@
1
1
  module TessEdit
2
- VERSION = "0.5.3"
2
+ VERSION = "0.5.4"
3
3
  end
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.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom de Grunt