tess_edit 0.5.2 → 0.5.3

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: 56237e4ae78a71ad87e395e50e51b32b7d5b45c36643aea13bf39e55fb731dcf
4
- data.tar.gz: 269a64d4e87297bbd25f3827b3c762b50e29e78d8fdfff86c11a6f950a0eb309
3
+ metadata.gz: f82d8e8c56d5625f8f107f277eca46f3f925f1efd3a61c04869416f9cf22c1f0
4
+ data.tar.gz: 2ae16142383df65bc903ed777e1a8e040d9a48985ec326a7bc6d63488659da20
5
5
  SHA512:
6
- metadata.gz: 3063da8d408f7337c482b9e6f0f0bd0a6f5efa04519683a38a96d1e1a2cb4945900210c022d350f946652ffffc33d88b8debe0faeb0676a47a0694e9dbe179a8
7
- data.tar.gz: 28393958e0d69c307e87b9b1ca9053d75ecc30e200cbe93c2272a4d4ecfdfffd06ced51350598ff89ac2a38d9127f9bf1e7e7fad34cd457db27cfbbb7119e295
6
+ metadata.gz: 02ad9420d971ac73f9d86f594ef543f55ca3778ed31626d27b781714c9ba00886db964b6d7a98ce220dabc995cec61100ca20d0fdaa0bb9aaa327bba103deb20
7
+ data.tar.gz: 0c7596f3ffe417f49bee62cef20dde71c1cd9584c35df42c3dae41927442c1f209d6e3b6f4ae728b1f548b3cbff7aa46d96c043baac5f53547f470542120fc59
data/README.md CHANGED
@@ -304,7 +304,7 @@ extendBlock("email", {
304
304
 
305
305
  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
306
 
307
- 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.
307
+ 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
308
 
309
309
  ## Kits
310
310
 
@@ -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
+ }
@@ -21,6 +21,7 @@ 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"
@@ -480,22 +481,10 @@ export class Inspector {
480
481
  input.textarea.focus()
481
482
  }
482
483
 
483
- // The host's fonts (options.fonts) or common ones, each shown in itself
484
+ // The host's fonts (options.fonts) or common ones, each shown in itself (ui/font_picker.js)
484
485
  font(field, value, set) {
485
- return h(
486
- "select",
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
- )
486
+ const options = fontOptions(this.editor.options?.fonts, value, field.placeholder)
487
+ return new FontPicker(this.editor, { options, value, input: INPUT, choose: set }).button
499
488
  }
500
489
 
501
490
  color(value, set) {
@@ -1,3 +1,3 @@
1
1
  module TessEdit
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
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.2
4
+ version: 0.5.3
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