tess_edit 0.6.3 → 0.6.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: c3d67adc7a3fb980851a6f7d15dbfb6c0be257627d7da6e376ef1eb026168923
4
- data.tar.gz: 48b26477825363cf6522ba0ebc03aa7ec8f33358416353df412bf763e584afcd
3
+ metadata.gz: 33f9692789fccf3b721207f4580b05f5239b28446db19337d55f9151159fbdb2
4
+ data.tar.gz: 0f96d538e9e5614fc185ea2fa7c43e046f412ce5b152e3559dad289e3a6047c6
5
5
  SHA512:
6
- metadata.gz: 27e7d60dbbfeef61adc64aebb42848c7d3ec43122f0c075a3bfd409c7d37c84a7bbd6482df95d3c0124b81fcbef21a1f0868291d3be7ff50f3913c7ea81cc001
7
- data.tar.gz: 869f784a685769ffa46bf7ccaccb7ed8f58ad21c0815550e0ee85a6d68b397af05c7b45e61768f221d1fe18ca3609edacddf3b76a0890e9dc7e654c239972986
6
+ metadata.gz: 14b571ccf3f5aa9bba36439c852c2a13ae14ef2243d3db11333c9c5e262fd06c33d0522d0b116daf4f8f76db3b715e109d4df773faa50f04668cc0580788b255
7
+ data.tar.gz: f6c17136fff60e6ff8474589d7cf5aaa555896302b5d1a6083005149087c2dc1eaf41009b3fa35ab8158c0d9bb7b1e298d429ee9ba947a02cdf42fe7ece083f4
@@ -326,6 +326,15 @@ export default class extends Controller {
326
326
  })
327
327
  }
328
328
 
329
+ // Takes the image out of a block's image field, as one undo step
330
+ clearImage(id, field = IMAGE_FIELD) {
331
+ if (!this.canEditField(id, field.key)) return
332
+ this.commit((doc) => {
333
+ setValue(doc, id, field.image, "")
334
+ setValue(doc, id, field.key, "")
335
+ })
336
+ }
337
+
329
338
  // An image file dropped on a block: shown at once, then uploaded
330
339
  async dropImage(id, file, field = IMAGE_FIELD) {
331
340
  this.select(id)
@@ -1417,6 +1426,9 @@ export default class extends Controller {
1417
1426
  onKeydown(event) {
1418
1427
  const target = event.target
1419
1428
  const typing = target.closest?.("input, textarea, select, [contenteditable='true']")
1429
+ // A text field undoes its own typing; a select, a checkbox or a button has
1430
+ // no undo of its own, so ⌘Z there is the editor's
1431
+ const ownUndo = target.closest?.("textarea, [contenteditable='true'], input:not([type=checkbox], [type=radio], [type=file], [type=button], [type=submit], [type=range], [type=color])")
1420
1432
  const mod = event.metaKey || event.ctrlKey
1421
1433
 
1422
1434
  if (this.menu && event.key === "Escape") {
@@ -1444,6 +1456,11 @@ export default class extends Controller {
1444
1456
  event.key === "0" ? this.setZoom(1) : this.zoomBy(event.key === "-" ? -1 : 1)
1445
1457
  return
1446
1458
  }
1459
+ if (mod && !event.altKey && !ownUndo && ["z", "y"].includes(event.key.toLowerCase())) {
1460
+ event.preventDefault()
1461
+ event.shiftKey || event.key.toLowerCase() === "y" ? this.redo() : this.undo()
1462
+ return
1463
+ }
1447
1464
  if (typing) return
1448
1465
  // Space held: the canvas is dragged around (Pan, ui/canvas.js)
1449
1466
  if (event.code === "Space" && !mod && !event.altKey) {
@@ -1454,12 +1471,6 @@ export default class extends Controller {
1454
1471
  if (event.shiftKey && !mod && !event.altKey && event.code === "Digit1") {
1455
1472
  event.preventDefault()
1456
1473
  this.zoomToFit()
1457
- } else if (mod && event.key.toLowerCase() === "z") {
1458
- event.preventDefault()
1459
- event.shiftKey ? this.redo() : this.undo()
1460
- } else if (mod && event.key.toLowerCase() === "y") {
1461
- event.preventDefault()
1462
- this.redo()
1463
1474
  } else if (mod && event.key.toLowerCase() === "d") {
1464
1475
  event.preventDefault()
1465
1476
  if (this.selectedId !== this.doc.id) this.duplicate(this.selectedId)
@@ -0,0 +1,31 @@
1
+ // A color field's value as a color and its opacity, for its swatch and its
2
+ // Opacity slider: #rgb, #rrggbb, #rrggbbaa, rgb(), rgba() and transparent.
3
+ // Anything else (a color's name, Liquid) isn't read: null.
4
+ export function parseColor(value) {
5
+ const text = String(value ?? "").trim().toLowerCase()
6
+ if (text === "transparent") return { hex: "#ffffff", alpha: 0 }
7
+
8
+ let match = text.match(/^#([0-9a-f]{3})$/)
9
+ if (match) return { hex: `#${[...match[1]].map((digit) => digit + digit).join("")}`, alpha: 1 }
10
+
11
+ match = text.match(/^#([0-9a-f]{6})([0-9a-f]{2})?$/)
12
+ if (match) return { hex: `#${match[1]}`, alpha: match[2] ? round(parseInt(match[2], 16) / 255) : 1 }
13
+
14
+ match = text.match(/^rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*([\d.]+)(%?)\s*)?\)$/)
15
+ if (!match) return null
16
+ const channels = match.slice(1, 4).map(Number)
17
+ const alpha = match[4] === undefined ? 1 : Number(match[4]) / (match[5] ? 100 : 1)
18
+ if (channels.some((channel) => channel > 255) || !(alpha >= 0 && alpha <= 1)) return null
19
+ return { hex: `#${channels.map((channel) => channel.toString(16).padStart(2, "0")).join("")}`, alpha: round(alpha) }
20
+ }
21
+
22
+ // Its hex when it's opaque, else rgba(), which email clients take best
23
+ export function formatColor(hex, alpha) {
24
+ if (alpha >= 1) return hex
25
+ const [red, green, blue] = [1, 3, 5].map((index) => parseInt(hex.slice(index, index + 2), 16))
26
+ return `rgba(${red}, ${green}, ${blue}, ${round(alpha)})`
27
+ }
28
+
29
+ function round(alpha) {
30
+ return Math.round(alpha * 100) / 100
31
+ }
@@ -14,6 +14,7 @@
14
14
  // host's Stimulus controllers in it keep their state: it and its group stay
15
15
  // in Inspect, hidden while another block is selected (see place in ui/dom.js).
16
16
 
17
+ import { formatColor, parseColor } from "tess_edit/core/color"
17
18
  import { t } from "tess_edit/core/i18n"
18
19
  import { imageSource } from "tess_edit/core/media"
19
20
  import { fieldType, getBlock, label } from "tess_edit/core/registry"
@@ -78,6 +79,9 @@ export class Inspector {
78
79
  }
79
80
 
80
81
  render() {
82
+ // A control that's drawn again loses focus: the editor keeps it, so its
83
+ // keys (⌘Z) still work
84
+ const focused = this.element?.contains(document.activeElement)
81
85
  const { doc, selectedId } = this.editor
82
86
  const hit = find(doc, selectedId) || { node: doc }
83
87
  const node = hit.node
@@ -119,6 +123,7 @@ export class Inspector {
119
123
 
120
124
  place(this.body, [locked, hint, ...groups].filter(Boolean), new Set([...this.keptGroups.values()].map((kept) => kept.details)))
121
125
  this.showLiquidProblems()
126
+ if (focused && !this.element.contains(document.activeElement)) this.editor.root?.focus({ preventScroll: true })
122
127
  }
123
128
 
124
129
  // Under a field with Liquid, what's wrong with it (core/liquid_check.js),
@@ -187,9 +192,11 @@ export class Inspector {
187
192
  const set = (next, options = {}) => this.editor.setValue(node.id, field.key, next, options)
188
193
  // A fieldset disables every input and button of a locked field at once.
189
194
  const editable = this.editor.canEditField(node.id, field.key)
190
- const control = editable
191
- ? this.control(node, field, value, set)
192
- : h("fieldset", { disabled: true, class: "opacity-60" }, this.control(node, field, value, set))
195
+ // A toggle is its checkbox next to its label, which clicks it: checked is on
196
+ const own = field.type === "toggle"
197
+ ? h("label", { class: "flex cursor-pointer items-center gap-2 text-xs font-medium text-gray-700 dark:text-gray-300" }, this.control(node, field, value, set), h("span", {}, t(field.label)))
198
+ : this.control(node, field, value, set)
199
+ const control = editable ? own : h("fieldset", { disabled: true, class: "opacity-60" }, own)
193
200
  const expand = field.type === "code" && editable
194
201
  ? h("button", { type: "button", title: t("Edit %{label} in a larger window", { label: t(field.label) }), class: "rounded px-1 text-[11px] font-medium text-blue-600 dark:text-blue-400 hover:bg-blue-50 dark:hover:bg-blue-900/40", onclick: () => this.expand(node, field) }, t("Expand"))
195
202
  : null
@@ -216,7 +223,7 @@ export class Inspector {
216
223
  return h(
217
224
  "div",
218
225
  { class: "relative block", oninput: typeof field.hint === "function" ? showHint : null },
219
- h("span", { class: "mb-1 block text-xs font-medium text-gray-700 dark:text-gray-300" }, t(field.label)),
226
+ field.type === "toggle" ? null : h("span", { class: "mb-1 block text-xs font-medium text-gray-700 dark:text-gray-300" }, t(field.label)),
220
227
  tools,
221
228
  control,
222
229
  input ? this.translationHint(input) : null,
@@ -291,23 +298,14 @@ export class Inspector {
291
298
  )
292
299
  )
293
300
 
294
- case "toggle": {
295
- const state = h("span", { class: "text-xs text-gray-600 dark:text-gray-300" }, t(value === field.on ? "On" : "Off"))
296
- return h(
297
- "span",
298
- { class: "flex items-center gap-2" },
299
- h("input", {
300
- type: "checkbox",
301
- class: "h-4 w-4 rounded border-gray-300 dark:border-gray-600 dark:bg-gray-700 text-blue-600 dark:text-blue-400",
302
- checked: value === field.on,
303
- onchange: (event) => {
304
- set(event.target.checked ? field.on : field.off)
305
- state.textContent = t(event.target.checked ? "On" : "Off")
306
- }
307
- }),
308
- state
309
- )
310
- }
301
+ // Only its checkbox: field() puts it next to its label
302
+ case "toggle":
303
+ return h("input", {
304
+ type: "checkbox",
305
+ class: "h-4 w-4 shrink-0 rounded border-gray-300 dark:border-gray-600 dark:bg-gray-700 text-blue-600 dark:text-blue-400",
306
+ checked: value === field.on,
307
+ onchange: (event) => set(event.target.checked ? field.on : field.off)
308
+ })
311
309
 
312
310
  case "spacing":
313
311
  return this.spacing(value, set)
@@ -461,12 +459,13 @@ export class Inspector {
461
459
  "select",
462
460
  {
463
461
  class: `${INPUT} min-w-0 flex-1 pr-7`,
462
+ // The empty choice takes the image out
464
463
  onchange: (event) => {
465
464
  const image = images.find((option) => option.id === event.target.value)
466
- if (image) this.editor.chooseImage(node.id, image, field)
465
+ image ? this.editor.chooseImage(node.id, image, field) : this.editor.clearImage(node.id, field)
467
466
  }
468
467
  },
469
- h("option", { value: "" }, t(images.length ? "Choose an image" : "No images yet")),
468
+ h("option", { value: "" }, t(value ? "No image" : images.length ? "Choose an image" : "No images yet")),
470
469
  images.map((image) => h("option", { value: image.id, selected: image.id === value }, image.name))
471
470
  )
472
471
  return h(
@@ -528,23 +527,50 @@ export class Inspector {
528
527
  return new FontPicker(this.editor, { options, value, input: INPUT, choose: set }).button
529
528
  }
530
529
 
530
+ // A color: its swatch, its value as text (any CSS color, or Liquid), and
531
+ // its opacity, 0% transparent (core/color.js). A value the slider can't
532
+ // read, such as a color's name or Liquid, leaves it off.
531
533
  color(value, set) {
534
+ let color = parseColor(value)
535
+ const change = (next) => {
536
+ text.value = next
537
+ set(next)
538
+ show()
539
+ }
532
540
  const text = h("input", {
533
541
  type: "text", class: `${INPUT} font-mono text-xs`, value, placeholder: t("Default"), oninput: (event) => {
534
542
  set(event.target.value)
535
- if (/^#[0-9a-f]{6}$/i.test(event.target.value)) swatch.value = event.target.value
543
+ color = parseColor(event.target.value)
544
+ if (color) swatch.value = color.hex
545
+ show(event.target.value)
536
546
  }
537
547
  })
538
548
  const swatch = h("input", {
539
549
  type: "color",
540
550
  class: "h-7 w-8 shrink-0 cursor-pointer rounded border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 p-0.5",
541
- value: /^#[0-9a-f]{6}$/i.test(value) ? value : "#ffffff",
551
+ value: color?.hex || "#ffffff",
542
552
  oninput: (event) => {
543
- text.value = event.target.value
544
- set(event.target.value)
553
+ color = { hex: event.target.value, alpha: color?.alpha ?? 1 }
554
+ change(formatColor(color.hex, color.alpha))
555
+ }
556
+ })
557
+ const percent = h("span", { class: "w-9 shrink-0 text-right text-[11px] tabular-nums text-gray-500 dark:text-gray-400" })
558
+ const opacity = h("input", {
559
+ type: "range", min: "0", max: "100", step: "1",
560
+ class: "h-1.5 min-w-0 flex-1 cursor-pointer accent-blue-600 disabled:cursor-default disabled:opacity-40",
561
+ title: t("Opacity"), "aria-label": t("Opacity"),
562
+ oninput: (event) => {
563
+ color = { hex: color?.hex || swatch.value, alpha: Number(event.target.value) / 100 }
564
+ change(formatColor(color.hex, color.alpha))
545
565
  }
546
566
  })
547
- return h("span", { class: "flex items-center gap-1.5" }, swatch, text)
567
+ const show = (current = text.value) => {
568
+ opacity.disabled = !color && current.trim() !== ""
569
+ opacity.value = String(Math.round((color?.alpha ?? 1) * 100))
570
+ percent.textContent = `${opacity.value}%`
571
+ }
572
+ show(value || "")
573
+ return h("span", { class: "block space-y-1.5" }, h("span", { class: "flex items-center gap-1.5" }, swatch, text), h("span", { class: "flex items-center gap-2", title: t("Opacity") }, opacity, percent))
548
574
  }
549
575
 
550
576
  spacing(value, set) {
@@ -191,6 +191,7 @@ en:
191
191
  new_tab: "New tab"
192
192
  no_argument_for_names_in_the_t_filter: "No argument for %{names} in the t filter"
193
193
  no_fonts_with_that_name: "No fonts with that name"
194
+ no_image: "No image"
194
195
  no_images_yet: "No images yet"
195
196
  no_languages_yet: "No languages yet"
196
197
  no_variables_here: "No variables here"
@@ -199,6 +200,7 @@ en:
199
200
  numbered_list: "Numbered list"
200
201
  off_word: "Off"
201
202
  on_word: "On"
203
+ opacity: "Opacity"
202
204
  open_in: "Open in"
203
205
  open_source: "Open %{source}"
204
206
  open_where_it_s_kept: "Open where it's kept"
@@ -191,6 +191,7 @@ nl:
191
191
  new_tab: "Nieuw tabblad"
192
192
  no_argument_for_names_in_the_t_filter: "Geen argument voor %{names} in het t-filter"
193
193
  no_fonts_with_that_name: "Geen lettertypen met die naam"
194
+ no_image: "Geen afbeelding"
194
195
  no_images_yet: "Nog geen afbeeldingen"
195
196
  no_languages_yet: "Nog geen talen"
196
197
  no_variables_here: "Hier zijn geen variabelen"
@@ -199,6 +200,7 @@ nl:
199
200
  numbered_list: "Genummerde lijst"
200
201
  off_word: "Uit"
201
202
  on_word: "Aan"
203
+ opacity: "Dekking"
202
204
  open_in: "Openen in"
203
205
  open_source: "%{source} openen"
204
206
  open_where_it_s_kept: "Openen waar het staat"
@@ -1,3 +1,3 @@
1
1
  module TessEdit
2
- VERSION = "0.6.3"
2
+ VERSION = "0.6.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.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom de Grunt
@@ -83,6 +83,7 @@ files:
83
83
  - app/javascript/tess_edit/blocks/table.js
84
84
  - app/javascript/tess_edit/blocks/text.js
85
85
  - app/javascript/tess_edit/controllers/editor_controller.js
86
+ - app/javascript/tess_edit/core/color.js
86
87
  - app/javascript/tess_edit/core/document.js
87
88
  - app/javascript/tess_edit/core/history.js
88
89
  - app/javascript/tess_edit/core/i18n.js