tess_edit 0.5.4 → 0.5.5

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: 339b84b5435ae8bc50b728abaa20b03ee21815d12c6f1e35fe352e9284e180dc
4
- data.tar.gz: 691c2849891dbd2fa6853899956d22d394d170fe25d63f70faf798c5ef6aa503
3
+ metadata.gz: d9e9b7a64743faae5c3291e13d37a4ed05e76501a6133218aabb569681df4974
4
+ data.tar.gz: 322204628014614d615f0cad63d04acdd07d410b9ea02417287f47eaa4530919
5
5
  SHA512:
6
- metadata.gz: f9f5a4b747ae3bc8e16a2682498aa33fb414950a5d111f6d9b874c20d80fd6d5fe185e6f389d1a829e5276337c6e22fbb5f9158118f9485c6a9d39ab16a33a51
7
- data.tar.gz: c9c6a0a7c8203d2149aae95403496a0c8fe24763bccf9f29dc08f2ba619103c31f96b135d8837418289c61fa858c0b8030f89f28924640eadb5477e35a22435d
6
+ metadata.gz: e1163e2a6a1c06464db00540eb42b467db92d5640ae05bedf0906e2968ae9541b54439bd685084bacde1b9c4a97b4512652c4d71210e190ace96ded1628dd392
7
+ data.tar.gz: 7cbc3479b8009322508e19de37d1f2f959414d731feb9f1a87e059be3b95808b0d64b75d62ce1aee30e6a0636ab371297e7cd030383637d6b2fccfe45da801ac
data/README.md CHANGED
@@ -19,6 +19,7 @@ It completes Liquid tags and variables as you type, making it easier to work wit
19
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
+ - 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.
22
23
  - Undo/redo, duplicate, delete, keyboard shortcuts (⌘Z, ⇧⌘Z, ⌘D, Delete, Enter to edit text, Esc).
23
24
  - Desktop and mobile preview, and a preview of the HTML as sent.
24
25
  - Panels on both sides, or combined as tabs in one sidebar on the left or right to give the canvas more room.
@@ -103,6 +104,8 @@ Every change updates the inputs and dispatches `tess-edit:change` with `{ json,
103
104
 
104
105
  Render the HTML with Liquid when sending. A host app can also store only the JSON or MJML and compile on its own with `TessEdit.compile(mjml)`.
105
106
 
107
+ The fields are written a moment after each change. A form submitted before that, such as a Save clicked right after typing, waits for them and then goes on, also with Turbo.
108
+
106
109
  ### Compiling
107
110
 
108
111
  The editor turns the JSON into MJML in the browser and has the server compile the MJML to HTML, with [MRML](https://github.com/jdrouet/mrml) (the `mrml` gem, precompiled, no Node or Rust needed). The canvas, the preview and the `html` input all come from the same compiler the host app uses when it sends:
@@ -293,7 +296,7 @@ registerBlock({
293
296
  })
294
297
  ```
295
298
 
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`).
299
+ 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. A key with a `*` stands for that part of each item of a list: `{ key: "data.options.*.label", plain: true }` makes every option's label editable, each marked with its own key (`data-tess-edit-field="data.options.0.label"`). The demo's Note kit has a Card block with both (`test/dummy/app/javascript/kits/note.js`).
297
300
 
298
301
  #### Your own form fields in Inspect
299
302
 
@@ -119,6 +119,10 @@ export default class extends Controller {
119
119
  this.build()
120
120
  this.renderAll()
121
121
  this.writeOutputs()
122
+ // The form it's in waits for the outputs still to be written (beforeSubmit)
123
+ this.form = this.element.closest("form")
124
+ this.beforeSubmit = this.beforeSubmit.bind(this)
125
+ this.form?.addEventListener("submit", this.beforeSubmit, true)
122
126
  if (this.media.enabled) this.loadMedia()
123
127
  this.loadSources()
124
128
  if (this.translations.enabled) {
@@ -287,6 +291,7 @@ export default class extends Controller {
287
291
  }
288
292
 
289
293
  disconnect() {
294
+ this.form?.removeEventListener("submit", this.beforeSubmit, true)
290
295
  this.scheduleCanvas.cancel()
291
296
  this.scheduleOutputs.cancel()
292
297
  Object.values(this.requests).forEach((request) => request.abort())
@@ -645,7 +650,34 @@ export default class extends Controller {
645
650
  this.errorBar.classList.toggle("hidden", !errors.length)
646
651
  }
647
652
 
648
- async writeOutputs() {
653
+ // A submit while the outputs are still to be written, or being written,
654
+ // waits for them and then goes on, so the form sends the document as it
655
+ // is, also right after a change
656
+ beforeSubmit(event) {
657
+ if (this.resubmitting || (!this.scheduleOutputs.pending() && !this.writing)) return
658
+ event.preventDefault()
659
+ event.stopImmediatePropagation()
660
+ const submitter = event.submitter
661
+ // Submitted again in a task of its own: during this submit event's, the
662
+ // browser ignores it
663
+ Promise.resolve(this.scheduleOutputs.flush() || this.writing).then(() => setTimeout(() => {
664
+ this.resubmitting = true
665
+ try {
666
+ this.form.requestSubmit(submitter?.form === this.form ? submitter : undefined)
667
+ } finally {
668
+ this.resubmitting = false
669
+ }
670
+ }))
671
+ }
672
+
673
+ writeOutputs() {
674
+ const writing = (this.writing = this.write().finally(() => {
675
+ if (this.writing === writing) this.writing = null
676
+ }))
677
+ return writing
678
+ }
679
+
680
+ async write() {
649
681
  this.loadTranslations()
650
682
  const json = JSON.stringify(this.doc)
651
683
  const result = await this.run("outputs", (options) => this.kit.outputs(this.doc, options))
@@ -830,14 +862,15 @@ export default class extends Controller {
830
862
 
831
863
  // Clicking a tile: add inside the selection when it fits, otherwise after the
832
864
  // nearest ancestor that fits, otherwise at the end of the root, or with a
833
- // lock, at the end of the first unlocked block it fits in.
865
+ // lock, at the end of the first unlocked block it fits in, or without one,
866
+ // at the end of the last block it fits in (a form's last page, say).
834
867
  addFromPalette(key) {
835
868
  const type = itemType(key)
836
869
  const place = this.placeFor((parentId) => this.accepted(parentId).includes(type))
837
870
  if (place) return this.insertAt(key, place.parentId, place.index)
838
871
  const definition = getBlock(type)
839
872
  if (definition?.unique && findType(this.doc, type)) return this.flash(`There is a ${definition.name} already; a document has one.`)
840
- this.flash(`${definition?.name || key} has to go inside a column. Select one first, or drag it onto the canvas.`)
873
+ this.flash(`${definition?.name || key} doesn't fit anywhere yet. Add a block it goes in first, or drag it onto the canvas.`)
841
874
  }
842
875
 
843
876
  // Where a click on a tile adds a block, for blocks that `fits` a parent
@@ -855,8 +888,13 @@ export default class extends Controller {
855
888
  if (this.lock.except.includes(node.type) && fits(node.id)) target = node
856
889
  })
857
890
  if (target) return { parentId: target.id, index: target.children.length }
891
+ return null
858
892
  }
859
- return null
893
+ let last = null
894
+ walk(this.doc, (node) => {
895
+ if (fits(node.id)) last = node
896
+ })
897
+ return last ? { parentId: last.id, index: last.children.length } : null
860
898
  }
861
899
 
862
900
  // Inserts what a palette source's tile creates (see core/registry.js) at a
@@ -175,14 +175,20 @@ export function duplicate(doc, id) {
175
175
  return copy
176
176
  }
177
177
 
178
- // Sets a value on a node. `key` is "attributes.padding" or "data.subject".
178
+ // Sets a value on a node. `key` is "attributes.padding" or "data.subject",
179
+ // or deeper into a list, "data.options.0.label" (an inline part of each item,
180
+ // see `inline` in core/registry.js).
179
181
  // An empty string removes an attribute, so MJML falls back to its default.
180
182
  export function setValue(doc, id, key, value) {
181
183
  const hit = find(doc, id)
182
184
  if (!hit) return
183
- const [scope, name] = key.split(".")
185
+ const [scope, name, ...rest] = key.split(".")
184
186
  hit.node[scope] ||= {}
185
- if (scope === "attributes" && (value === "" || value === null || value === undefined)) {
187
+ if (rest.length) {
188
+ let target = hit.node[scope][name]
189
+ for (const step of rest.slice(0, -1)) target = target?.[step]
190
+ if (target && typeof target === "object") target[rest.at(-1)] = value
191
+ } else if (scope === "attributes" && (value === "" || value === null || value === undefined)) {
186
192
  delete hit.node[scope][name]
187
193
  } else {
188
194
  hit.node[scope][name] = value
@@ -190,8 +196,7 @@ export function setValue(doc, id, key, value) {
190
196
  }
191
197
 
192
198
  export function getValue(node, key) {
193
- const [scope, name] = key.split(".")
194
- return node[scope]?.[name]
199
+ return key.split(".").reduce((value, step) => value?.[step], node)
195
200
  }
196
201
 
197
202
  // Light validation of a document read from storage or an import. With a
@@ -174,8 +174,15 @@ export function inlineParts(type) {
174
174
  return getBlock(type)?.inline || []
175
175
  }
176
176
 
177
+ // The part with this key; a part whose key has a * stands for that part of
178
+ // each item of a list, e.g. "data.options.*.label" for "data.options.2.label"
177
179
  export function inlinePart(type, key) {
178
- return inlineParts(type).find((part) => part.key === key)
180
+ const part = inlineParts(type).find((candidate) => candidate.key === key || (candidate.key.includes("*") && keyPattern(candidate.key).test(key || "")))
181
+ return part && { ...part, key }
182
+ }
183
+
184
+ function keyPattern(key) {
185
+ return new RegExp(`^${key.split("*").map((piece) => piece.replace(/[.]/g, "\\.")).join("\\d+")}$`)
179
186
  }
180
187
 
181
188
  // The field types of ui/inspector.js
@@ -6,7 +6,7 @@
6
6
 
7
7
  import { ancestors, find, isDescendant, isHorizontal } from "tess_edit/core/document"
8
8
  import { imageFields } from "tess_edit/core/media"
9
- import { getBlock, label } from "tess_edit/core/registry"
9
+ import { getBlock, inlinePart, label } from "tess_edit/core/registry"
10
10
  import { missingArguments, textParts, translationCalls } from "tess_edit/core/translations"
11
11
  import { FREE_CSS, FreeLayout } from "tess_edit/ui/free_layout"
12
12
  import { icon } from "tess_edit/ui/icons"
@@ -23,6 +23,7 @@ body{min-height:100vh;margin:0}
23
23
  .tess-hover .tess-label{background:#60a5fa}
24
24
  .tess-editing .tess-label{background:#f59e0b}
25
25
  .tess-plain{outline:none;cursor:text}
26
+ .tess-plain:empty{display:inline-block;min-width:1px}
26
27
  .tess-inside .tess-label,.tess-inside .tess-tools{bottom:auto;top:0;border-radius:0 0 4px 4px}
27
28
  .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}
28
29
  .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}
@@ -238,7 +239,7 @@ export class Canvas {
238
239
  partAt(target, id) {
239
240
  const definition = getBlock(find(this.editor.doc, id)?.node.type)
240
241
  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
+ if (marked && this.hit(marked)?.id === id && definition && inlinePart(definition.type, marked.dataset.tessEditField)) return marked.dataset.tessEditField
242
243
  return definition?.richText || null
243
244
  }
244
245
 
@@ -24,13 +24,26 @@ export function h(tag, props = {}, ...children) {
24
24
  return element
25
25
  }
26
26
 
27
+ // pending() says whether a call is waiting; flush() makes it now
27
28
  export function debounce(fn, wait) {
28
- let timer
29
+ let timer = null
30
+ let waiting = null
29
31
  const debounced = (...args) => {
30
32
  clearTimeout(timer)
31
- timer = setTimeout(() => fn(...args), wait)
33
+ waiting = args
34
+ timer = setTimeout(debounced.flush, wait)
35
+ }
36
+ debounced.cancel = () => {
37
+ clearTimeout(timer)
38
+ timer = waiting = null
39
+ }
40
+ debounced.pending = () => waiting !== null
41
+ debounced.flush = () => {
42
+ if (!waiting) return undefined
43
+ const args = waiting
44
+ debounced.cancel()
45
+ return fn(...args)
32
46
  }
33
- debounced.cancel = () => clearTimeout(timer)
34
47
  return debounced
35
48
  }
36
49
 
@@ -59,12 +59,18 @@ export class TextEditing {
59
59
  element.classList.add("tess-plain")
60
60
  for (const [name, listener] of Object.entries(listeners)) element.addEventListener(name, listener)
61
61
  this.plain = { element, listeners, text }
62
- element.focus()
62
+ // Where it is: Safari scrolls to the caret, which in an empty inline
63
+ // element it puts at the top of the page (hence also .tess-plain:empty's
64
+ // box, in canvas.js)
65
+ const win = element.ownerDocument.defaultView
66
+ const { scrollX, scrollY } = win
67
+ element.focus({ preventScroll: true })
63
68
  const range = element.ownerDocument.createRange()
64
69
  range.selectNodeContents(element)
65
70
  range.collapse(false)
66
71
  element.ownerDocument.getSelection().removeAllRanges()
67
72
  element.ownerDocument.getSelection().addRange(range)
73
+ if (win.scrollX !== scrollX || win.scrollY !== scrollY) win.scrollTo(scrollX, scrollY)
68
74
  return element
69
75
  }
70
76
 
@@ -1,3 +1,3 @@
1
1
  module TessEdit
2
- VERSION = "0.5.4"
2
+ VERSION = "0.5.5"
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.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom de Grunt