tess_edit 0.5.4 → 0.5.6

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: f40808bae8dcda2f666c84361d3827261aaec15ae2260df2dede99b15d6783b2
4
+ data.tar.gz: 2f68441c824237fc96a258305d5583f473ad4f8fd4630d4a6d9b2cb65a946b7f
5
5
  SHA512:
6
- metadata.gz: f9f5a4b747ae3bc8e16a2682498aa33fb414950a5d111f6d9b874c20d80fd6d5fe185e6f389d1a829e5276337c6e22fbb5f9158118f9485c6a9d39ab16a33a51
7
- data.tar.gz: c9c6a0a7c8203d2149aae95403496a0c8fe24763bccf9f29dc08f2ba619103c31f96b135d8837418289c61fa858c0b8030f89f28924640eadb5477e35a22435d
6
+ metadata.gz: 2dbde1a8dde3fbad145f33c77547547ed7cd9973557f8c54941ac9d26b78511f93ee283e722c866e1d4d893e93c4eddf3e4b8163c119be6acea14f5b86669136
7
+ data.tar.gz: 46eb21506a8e333555a6bd5c7454c1cae49286de3f8eaa0ad2292a8ae6b8a1922e28bfef7d8698f40e17da900b4d853af392ef730d3846c73a9af0fab087daba
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
 
@@ -11,8 +11,10 @@
11
11
  // as tabs on one side). The default; a choice made in the editor's
12
12
  // top bar is remembered per browser. Below NARROW_WIDTH the editor
13
13
  // puts all panels on the left, whatever the choice, to leave room
14
- // for the canvas. A kit without blocks (blocks: false) has Inspect
15
- // on the right only.
14
+ // for the canvas, and below COMPACT_WIDTH (a phone) in a sheet
15
+ // below the canvas, shown and hidden from the top bar and opened
16
+ // by a tap on a block. A kit without blocks (blocks: false) has
17
+ // Inspect on the right only, or in the sheet.
16
18
  // compileUrl the endpoint that compiles the email kit's MJML to HTML (default
17
19
  // /tess_edit/compile, the engine mounted at /tess_edit)
18
20
  // mediaUrl the host's images, chosen and uploaded in Image blocks (see
@@ -71,6 +73,8 @@ const PANELS_KEY = "tess_edit:panels"
71
73
  const TRANSLATION_LOCALE_KEY = "tess_edit:translation_locale"
72
74
  // Two sidebars (256px + 320px) leave too little canvas below this editor width.
73
75
  const NARROW_WIDTH = 1000
76
+ // Even one sidebar (320px) leaves too little canvas below this editor width.
77
+ const COMPACT_WIDTH = 640
74
78
  const ARROWS = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }
75
79
 
76
80
  // A new block where it was let go in a free layout
@@ -105,6 +109,8 @@ export default class extends Controller {
105
109
  this.leftTab = "blocks"
106
110
  this.panels = [this.storedPanels(), this.panelsValue].find((value) => PANELS.includes(value)) || "split"
107
111
  this.narrow = false
112
+ this.compact = false
113
+ this.sheetOpen = false
108
114
  this.requests = {}
109
115
  this.media = { enabled: Boolean(this.mediaUrlValue), images: [], uploading: false, error: null }
110
116
  this.variables = new Variables(this.variablesUrlValue)
@@ -119,6 +125,10 @@ export default class extends Controller {
119
125
  this.build()
120
126
  this.renderAll()
121
127
  this.writeOutputs()
128
+ // The form it's in waits for the outputs still to be written (beforeSubmit)
129
+ this.form = this.element.closest("form")
130
+ this.beforeSubmit = this.beforeSubmit.bind(this)
131
+ this.form?.addEventListener("submit", this.beforeSubmit, true)
122
132
  if (this.media.enabled) this.loadMedia()
123
133
  this.loadSources()
124
134
  if (this.translations.enabled) {
@@ -287,6 +297,7 @@ export default class extends Controller {
287
297
  }
288
298
 
289
299
  disconnect() {
300
+ this.form?.removeEventListener("submit", this.beforeSubmit, true)
290
301
  this.scheduleCanvas.cancel()
291
302
  this.scheduleOutputs.cancel()
292
303
  Object.values(this.requests).forEach((request) => request.abort())
@@ -382,6 +393,7 @@ export default class extends Controller {
382
393
  this.inspectorElement = h("div", { class: "flex flex-1 flex-col min-h-0", dataset: { tessEditPanel: "inspect" } })
383
394
  this.sidePanel = h("aside")
384
395
  this.inspectPanel = h("aside")
396
+ this.sheetClose = h("button", { type: "button", title: "Hide panels", class: `${TOOL} ml-auto self-center`, html: icon("close"), onclick: () => this.setSheetOpen(false) })
385
397
  this.textToolbar = h("div", { class: "hidden absolute left-1/2 top-3 z-20 -translate-x-1/2 flex items-center gap-0.5 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 px-1.5 py-1 shadow-md" })
386
398
  this.frame = h("iframe", { class: "block h-full w-full border-0 bg-white transition-[width] duration-200", title: `${this.kit.name} canvas` })
387
399
  this.frameWrap = h("div", { class: "relative h-full mx-auto" }, this.frame)
@@ -395,7 +407,7 @@ export default class extends Controller {
395
407
  this.body
396
408
  )
397
409
  this.errorBar = h("div", { class: "hidden absolute inset-x-3 bottom-3 z-20 rounded-md border border-red-200 dark:border-red-800 bg-red-50 dark:bg-red-900/40 px-3 py-2 text-xs text-red-700 dark:text-red-300 shadow-sm", role: "alert" })
398
- this.main = h("main", { class: "relative flex-1 min-w-0 bg-gray-100 dark:bg-gray-900", ondragover: (event) => this.drag && event.preventDefault() }, this.textToolbar, this.frameWrap, this.errorBar)
410
+ this.main = h("main", { class: "relative flex-1 min-w-0 min-h-0 bg-gray-100 dark:bg-gray-900", ondragover: (event) => this.drag && event.preventDefault() }, this.textToolbar, this.frameWrap, this.errorBar)
399
411
  this.arrangePanels()
400
412
  this.element.append(this.root)
401
413
 
@@ -425,20 +437,30 @@ export default class extends Controller {
425
437
  }
426
438
 
427
439
  // Split: Blocks and Layers on the left, Inspect on the right. Left or right:
428
- // one sidebar on that side with Blocks, Layers and Inspect as tabs.
440
+ // one sidebar on that side with Blocks, Layers and Inspect as tabs. Sheet:
441
+ // those tabs in the lower half, below the canvas, while it is open.
429
442
  arrangePanels() {
430
443
  const panelClass = (border, width) => `${width} shrink-0 flex flex-col ${border} border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 min-h-0`
444
+ const sheetClass = `${panelClass("border-t", "h-1/2")}${this.sheetOpen ? "" : " hidden"}`
431
445
  // Only the sidebars move: moving <main> would reload the canvas iframe and blank it.
432
446
  if (!this.main.isConnected) this.body.append(this.main)
447
+ this.body.classList.toggle("flex-col", this.compact)
433
448
  this.inspector?.freezeSlots()
434
449
  if (!this.hasBlocks) {
435
- this.inspectPanel.className = panelClass("border-l", "w-80")
436
- if (this.inspectorElement.parentNode !== this.inspectPanel) this.inspectPanel.replaceChildren(this.inspectorElement)
450
+ this.inspectPanel.className = this.compact ? sheetClass : panelClass("border-l", "w-80")
451
+ const children = this.compact ? [this.sheetHeader("Inspect"), this.inspectorElement] : [this.inspectorElement]
452
+ const current = [...this.inspectPanel.children]
453
+ if (current.length !== children.length || children.some((child, index) => current[index] !== child)) this.inspectPanel.replaceChildren(...children)
437
454
  if (this.main.nextSibling !== this.inspectPanel) this.main.after(this.inspectPanel)
438
455
  return
439
456
  }
440
457
  this.sidePanel.replaceChildren(this.leftTabs, this.blocksElement, this.treeElement)
441
- if (this.layout === "split") {
458
+ if (this.layout === "sheet") {
459
+ this.sidePanel.className = sheetClass
460
+ this.sidePanel.append(this.inspectorElement)
461
+ this.inspectPanel.remove()
462
+ this.main.after(this.sidePanel)
463
+ } else if (this.layout === "split") {
442
464
  this.sidePanel.className = panelClass("border-r", "w-64")
443
465
  this.inspectPanel.className = panelClass("border-l", "w-80")
444
466
  this.inspectPanel.replaceChildren(this.inspectorElement)
@@ -452,6 +474,22 @@ export default class extends Controller {
452
474
  }
453
475
  }
454
476
 
477
+ // The sheet's title bar for a kit without blocks, which has no tabs
478
+ sheetHeader(title) {
479
+ this.sheetTitle ||= h("div", { class: "flex items-center h-12 px-3 border-b border-gray-200 dark:border-gray-700 shrink-0 text-[13px] font-medium" })
480
+ this.sheetTitle.replaceChildren(title, this.sheetClose)
481
+ return this.sheetTitle
482
+ }
483
+
484
+ // Shows or hides the panels' sheet on a compact editor
485
+ setSheetOpen(open) {
486
+ if (!this.compact || open === this.sheetOpen) return
487
+ this.sheetOpen = open
488
+ ;(this.hasBlocks ? this.sidePanel : this.inspectPanel).classList.toggle("hidden", !open)
489
+ this.renderTopBar()
490
+ this.canvas.refresh()
491
+ }
492
+
455
493
  setPanels(panels) {
456
494
  if (!PANELS.includes(panels)) return
457
495
  this.panels = panels
@@ -468,13 +506,17 @@ export default class extends Controller {
468
506
  this.renderLeftTabs()
469
507
  }
470
508
 
471
- // Combines the panels while the editor is narrower than NARROW_WIDTH.
509
+ // Combines the panels while the editor is narrower than NARROW_WIDTH, and
510
+ // puts them in a sheet while it is narrower than COMPACT_WIDTH.
472
511
  observeWidth() {
473
512
  if (typeof ResizeObserver === "undefined") return
474
513
  this.resizeObserver = new ResizeObserver(([entry]) => {
475
514
  const narrow = entry.contentRect.width < NARROW_WIDTH
476
- if (narrow === this.narrow) return
515
+ const compact = entry.contentRect.width < COMPACT_WIDTH
516
+ if (narrow === this.narrow && compact === this.compact) return
477
517
  this.narrow = narrow
518
+ if (compact !== this.compact) this.sheetOpen = false
519
+ this.compact = compact
478
520
  this.applyPanels()
479
521
  this.canvas.refresh()
480
522
  })
@@ -485,6 +527,7 @@ export default class extends Controller {
485
527
  // split, or the kit has no blocks
486
528
  get layout() {
487
529
  if (!this.hasBlocks) return "split"
530
+ if (this.compact) return "sheet"
488
531
  return this.narrow && this.panels === "split" ? "left" : this.panels
489
532
  }
490
533
 
@@ -534,15 +577,18 @@ export default class extends Controller {
534
577
  )
535
578
  )
536
579
 
580
+ // A compact editor's top bar scrolls sideways rather than overflow
581
+ this.topBar.classList.toggle("overflow-x-auto", this.compact)
537
582
  // replaceChildren would turn a null into the text "null", so only pass real elements.
538
583
  this.topBar.replaceChildren(...[
539
- h("span", { class: "font-semibold text-gray-900 dark:text-gray-100 mr-3" }, this.kit.name),
584
+ this.compact ? null : h("span", { class: "font-semibold text-gray-900 dark:text-gray-100 mr-3" }, this.kit.name),
540
585
  toggle(this.mode, [["edit", "edit", "Edit"], ["preview", "eye", this.kit.previewTitle || "Preview"]], (mode) => this.setMode(mode)),
541
586
  this.kit.devices?.length ? toggle(this.device, this.kit.devices.map((device) => [device.key, device.icon, device.title]), (device) => this.setDevice(device)) : null,
542
587
  this.translations.enabled ? this.translationLocaleMenu() : null,
543
588
  h("div", { class: "flex-1" }),
544
- // Without blocks there are no panels to arrange
545
- this.hasBlocks && toggle(
589
+ // A compact editor shows its panels or not; one without blocks has none to arrange
590
+ this.compact && h("button", { type: "button", title: this.sheetOpen ? "Hide panels" : "Show panels", "aria-pressed": String(this.sheetOpen), class: `${TOOL} ${this.sheetOpen ? "bg-gray-100 dark:bg-gray-700" : ""}`, html: icon("panels-bottom"), onclick: () => this.setSheetOpen(!this.sheetOpen) }),
591
+ this.hasBlocks && !this.compact && toggle(
546
592
  this.layout,
547
593
  [
548
594
  ["left", "panels-left", "All panels on the left"],
@@ -551,7 +597,7 @@ export default class extends Controller {
551
597
  ],
552
598
  (panels) => this.setPanels(panels)
553
599
  ),
554
- this.hasBlocks && h("span", { class: "mx-1 h-5 w-px bg-gray-200 dark:bg-gray-700" }),
600
+ (this.hasBlocks || this.compact) && h("span", { class: "mx-1 h-5 w-px bg-gray-200 dark:bg-gray-700" }),
555
601
  h("button", { type: "button", title: "Undo (⌘Z)", class: TOOL, disabled: !this.history.canUndo, html: icon("undo"), onclick: () => this.undo() }),
556
602
  h("button", { type: "button", title: "Redo (⇧⌘Z)", class: TOOL, disabled: !this.history.canRedo, html: icon("redo"), onclick: () => this.redo() }),
557
603
  h(
@@ -589,6 +635,7 @@ export default class extends Controller {
589
635
  // replaceChildren would turn a null into the text "null", so only pass real tabs.
590
636
  const tabs = [tab("blocks", "Blocks"), tab("layers", "Layers")]
591
637
  if (this.combined) tabs.push(tab("inspect", "Inspect"))
638
+ if (this.compact && this.hasBlocks) tabs.push(this.sheetClose)
592
639
  this.leftTabs.replaceChildren(...tabs)
593
640
  this.blocksElement.classList.toggle("hidden", this.leftTab !== "blocks")
594
641
  this.treeElement.classList.toggle("hidden", this.leftTab !== "layers")
@@ -645,7 +692,34 @@ export default class extends Controller {
645
692
  this.errorBar.classList.toggle("hidden", !errors.length)
646
693
  }
647
694
 
648
- async writeOutputs() {
695
+ // A submit while the outputs are still to be written, or being written,
696
+ // waits for them and then goes on, so the form sends the document as it
697
+ // is, also right after a change
698
+ beforeSubmit(event) {
699
+ if (this.resubmitting || (!this.scheduleOutputs.pending() && !this.writing)) return
700
+ event.preventDefault()
701
+ event.stopImmediatePropagation()
702
+ const submitter = event.submitter
703
+ // Submitted again in a task of its own: during this submit event's, the
704
+ // browser ignores it
705
+ Promise.resolve(this.scheduleOutputs.flush() || this.writing).then(() => setTimeout(() => {
706
+ this.resubmitting = true
707
+ try {
708
+ this.form.requestSubmit(submitter?.form === this.form ? submitter : undefined)
709
+ } finally {
710
+ this.resubmitting = false
711
+ }
712
+ }))
713
+ }
714
+
715
+ writeOutputs() {
716
+ const writing = (this.writing = this.write().finally(() => {
717
+ if (this.writing === writing) this.writing = null
718
+ }))
719
+ return writing
720
+ }
721
+
722
+ async write() {
649
723
  this.loadTranslations()
650
724
  const json = JSON.stringify(this.doc)
651
725
  const result = await this.run("outputs", (options) => this.kit.outputs(this.doc, options))
@@ -672,9 +746,11 @@ export default class extends Controller {
672
746
 
673
747
  // --- selection and editing ---------------------------------------------------
674
748
 
675
- // `reveal` (a click on the canvas) shows the Inspect tab when the panels are combined.
749
+ // `reveal` (a click on the canvas) shows the Inspect tab when the panels are
750
+ // combined, and opens their sheet on a compact editor.
676
751
  select(id, { reveal = false } = {}) {
677
752
  if (reveal && this.combined && this.leftTab !== "inspect") this.setLeftTab("inspect")
753
+ if (reveal) this.setSheetOpen(true)
678
754
  if (this.editingId && this.editingId !== id) this.stopEditing()
679
755
  if (!find(this.doc, id)) id = this.doc.id
680
756
  if (this.selectedId === id) return
@@ -773,6 +849,8 @@ export default class extends Controller {
773
849
  if (drag.kind === "move" && !this.isFree(drag.id)) return
774
850
  this.closeMenu()
775
851
  if (this.editingId) this.stopEditing()
852
+ // Out of the way of the canvas it goes onto
853
+ if (drag.kind !== "move") this.setSheetOpen(false)
776
854
  this.drag = drag
777
855
  this.canvas.setHover(null)
778
856
  }
@@ -830,14 +908,15 @@ export default class extends Controller {
830
908
 
831
909
  // Clicking a tile: add inside the selection when it fits, otherwise after the
832
910
  // 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.
911
+ // lock, at the end of the first unlocked block it fits in, or without one,
912
+ // at the end of the last block it fits in (a form's last page, say).
834
913
  addFromPalette(key) {
835
914
  const type = itemType(key)
836
915
  const place = this.placeFor((parentId) => this.accepted(parentId).includes(type))
837
916
  if (place) return this.insertAt(key, place.parentId, place.index)
838
917
  const definition = getBlock(type)
839
918
  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.`)
919
+ this.flash(`${definition?.name || key} doesn't fit anywhere yet. Add a block it goes in first, or drag it onto the canvas.`)
841
920
  }
842
921
 
843
922
  // Where a click on a tile adds a block, for blocks that `fits` a parent
@@ -855,8 +934,13 @@ export default class extends Controller {
855
934
  if (this.lock.except.includes(node.type) && fits(node.id)) target = node
856
935
  })
857
936
  if (target) return { parentId: target.id, index: target.children.length }
937
+ return null
858
938
  }
859
- return null
939
+ let last = null
940
+ walk(this.doc, (node) => {
941
+ if (fits(node.id)) last = node
942
+ })
943
+ return last ? { parentId: last.id, index: last.children.length } : null
860
944
  }
861
945
 
862
946
  // 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
 
@@ -26,6 +26,7 @@ const paths = {
26
26
  "cols-21": '<rect x="3" y="6" width="10.5" height="12" rx="1.5"/><rect x="15.5" y="6" width="5.5" height="12" rx="1.5"/>',
27
27
  "panels-left": '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M10 4v16"/><path d="M5.5 8h2M5.5 11h2" opacity=".6"/>',
28
28
  "panels-split": '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M8 4v16M16 4v16"/>',
29
+ "panels-bottom": '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M3 13h18"/><path d="M7 16.5h4" opacity=".6"/>',
29
30
  "panels-right": '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M14 4v16"/><path d="M16.5 8h2M16.5 11h2" opacity=".6"/>',
30
31
  lock: '<rect x="5" y="11" width="14" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/>',
31
32
  plus: '<path d="M12 5v14M5 12h14"/>',
@@ -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.6"
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.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom de Grunt