@pseudolux-ui/media-library 0.1.0 → 0.2.0

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.
@@ -0,0 +1,361 @@
1
+ <view>
2
+ <div class="sentence">
3
+ <p class="sent">
4
+ <button class="tk" type="button" $class="{ 'is-set': state.set.shape, 'is-open': state.pop === 'shape' }" $aria-expanded="state.pop === 'shape'" @click="toggle('shape')">{{ state.part.shape }}</button>
5
+ pictures of
6
+ <button class="tk" type="button" $class="{ 'is-set': state.set.about, 'is-open': state.pop === 'about' }" $aria-expanded="state.pop === 'about'" @click="toggle('about')">{{ state.part.about }}</button>
7
+ in
8
+ <button class="tk" type="button" $class="{ 'is-set': state.set.group, 'is-open': state.pop === 'group' }" $aria-expanded="state.pop === 'group'" $disabled="!state.hasGroups" @click="toggle('group')">{{ state.part.group }}</button>
9
+ that
10
+ <button class="tk" type="button" $class="{ 'is-set': state.set.condition, 'is-open': state.pop === 'condition' }" $aria-expanded="state.pop === 'condition'" @click="toggle('condition')">{{ state.part.condition }}</button>,
11
+ added
12
+ <button class="tk" type="button" $class="{ 'is-set': state.set.when, 'is-open': state.pop === 'when' }" $aria-expanded="state.pop === 'when'" @click="toggle('when')">{{ state.part.when }}</button>.
13
+ </p>
14
+
15
+ <div class="pop" role="group" $aria-label="state.popLabel" $show="state.pop && state.pop !== 'about'" @keydown="onPopKey">
16
+ <b>{{ state.popLabel }}</b>
17
+ <button class="opt" type="button" $for="one in state.options" $key="one.key" $aria-pressed="one.on" @click="choose(one.value)">{{ one.label }}</button>
18
+ <button class="px-btn px-btn-ghost px-btn-sm done" type="button" @click="close">Done</button>
19
+ </div>
20
+
21
+ <form class="pop" role="group" aria-label="Pictures of" $show="state.pop === 'about'" @submit="submitAbout" @keydown="onPopKey">
22
+ <b>Pictures of</b>
23
+ <input class="px-input about" type="text" aria-label="Pictures of" $placeholder="state.aboutHint" $model="state.aboutText" $ref="about" @input="onAbout" />
24
+ <button class="px-btn px-btn-ghost px-btn-sm" type="button" @click="clearAbout">Anything</button>
25
+ <button class="px-btn px-btn-ghost px-btn-sm done" type="submit">Done</button>
26
+ </form>
27
+
28
+ <div class="px-row acts" $show="state.isSet || state.saved.length">
29
+ <button class="px-btn px-btn-ghost px-btn-sm" type="button" $show="state.isSet" @click="clear">Clear</button>
30
+ <button class="px-btn px-btn-secondary px-btn-sm" type="button" $show="state.isSet && !state.isSaved" @click="save">Save this sentence</button>
31
+ <span class="px-chip saved" $for="one in state.saved" $key="one.key" $class="{ 'is-on': one.on }">
32
+ <button class="saved-go" type="button" $title="one.said" $aria-pressed="one.on" @click="use(one)">{{ one.name }}</button>
33
+ <button type="button" $aria-label="'Forget the saved sentence ' + one.name" @click="forget(one)"><svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" aria-hidden="true"><path d="M18 6 6 18M6 6l12 12"></path></svg></button>
34
+ </span>
35
+ </div>
36
+
37
+ <span class="sr-only" role="status">{{ state.said }}</span>
38
+ </div>
39
+ </view>
40
+
41
+ <logic lang="ts">
42
+ import { reactive, onMounted, shared, defineRefs } from '@lightspeed-js/core'
43
+ import { SENTENCE, CAPABILITIES } from '../config.ts'
44
+ import { BLANK, SHAPES, CONDITIONS, WHENS, isSet, sameSentence, said, words } from '../sentence.ts'
45
+
46
+ /**
47
+ * The sentence the pictures are read through, each part a button that opens
48
+ * its choices underneath. The same idea as the "When" sentence on an event:
49
+ * the page says what it is showing in words, and the words are the controls.
50
+ */
51
+ const refs = defineRefs()
52
+
53
+ const SAVED_KEY = 'pseudolux:sentences'
54
+
55
+ const state = reactive({
56
+ part: words(BLANK),
57
+ set: { shape: false, about: false, group: false, condition: false, when: false },
58
+ isSet: false,
59
+ isSaved: false,
60
+ pop: '',
61
+ popLabel: '',
62
+ options: [],
63
+ hasGroups: false,
64
+ aboutText: '',
65
+ aboutHint: 'Words in the name, alt text or labels',
66
+ saved: [],
67
+ said: '',
68
+ })
69
+
70
+ const sentence = shared(SENTENCE)
71
+ const capabilities = shared(CAPABILITIES)
72
+
73
+ let groups = []
74
+ let usage = false
75
+ let saved = []
76
+
77
+ const current = () => ({ ...BLANK, ...(sentence.value || {}) })
78
+
79
+ const POP_LABELS = { shape: 'Shape', group: 'In', condition: 'That', when: 'Added' }
80
+
81
+ function optionsFor(part) {
82
+ const now = current()
83
+ if (part === 'shape') return SHAPES.map(([value, label]) => ({ value, label }))
84
+ if (part === 'when') return WHENS.map(([value, label]) => ({ value, label }))
85
+ if (part === 'group') return [{ value: '', label: 'any group' }].concat(groups.map((one) => ({ value: one, label: one })))
86
+ // Whether a picture is used anywhere is only a choice where the listing
87
+ // can answer it.
88
+ return CONDITIONS.filter(([value]) => usage || (value !== 'unused' && value !== 'used') || now.condition === value).map(([value, label]) => ({ value, label }))
89
+ }
90
+
91
+ function draw() {
92
+ const now = current()
93
+ state.part = words(now)
94
+ state.set = {
95
+ shape: Boolean(now.shape),
96
+ about: Boolean(now.about),
97
+ group: Boolean(now.group),
98
+ condition: Boolean(now.condition),
99
+ when: Boolean(now.when),
100
+ }
101
+ state.isSet = isSet(now)
102
+ state.said = state.isSet ? 'Showing ' + said(now) : ''
103
+ if (state.pop && state.pop !== 'about') {
104
+ state.popLabel = POP_LABELS[state.pop]
105
+ state.options = optionsFor(state.pop).map((one) => ({ ...one, key: state.pop + ':' + one.value, on: now[state.pop] === one.value }))
106
+ }
107
+ if (state.pop !== 'about') state.aboutText = now.about
108
+ state.isSaved = saved.some((one) => sameSentence({ ...BLANK, ...one.sentence }, now))
109
+ state.saved = saved.map((one, index) => ({
110
+ key: index + ':' + one.name,
111
+ index,
112
+ name: one.name,
113
+ said: said({ ...BLANK, ...one.sentence }),
114
+ on: sameSentence({ ...BLANK, ...one.sentence }, now),
115
+ }))
116
+ }
117
+
118
+ function toggle(part) {
119
+ state.pop = state.pop === part ? '' : part
120
+ draw()
121
+ if (state.pop === 'about') requestAnimationFrame(() => refs.about && refs.about.focus())
122
+ }
123
+
124
+ function close() {
125
+ state.pop = ''
126
+ }
127
+
128
+ function onPopKey(event) {
129
+ if (event.key !== 'Escape') return
130
+ event.preventDefault()
131
+ close()
132
+ }
133
+
134
+ function choose(value) {
135
+ sentence.value = { ...current(), [state.pop]: value }
136
+ }
137
+
138
+ let typing = null
139
+ function onAbout() {
140
+ if (typing) clearTimeout(typing)
141
+ typing = setTimeout(() => {
142
+ typing = null
143
+ sentence.value = { ...current(), about: state.aboutText.trim().toLowerCase() }
144
+ }, 250)
145
+ }
146
+
147
+ function submitAbout(event) {
148
+ event.preventDefault()
149
+ if (typing) clearTimeout(typing)
150
+ typing = null
151
+ sentence.value = { ...current(), about: state.aboutText.trim().toLowerCase() }
152
+ close()
153
+ }
154
+
155
+ function clearAbout() {
156
+ state.aboutText = ''
157
+ sentence.value = { ...current(), about: '' }
158
+ }
159
+
160
+ function clear() {
161
+ state.pop = ''
162
+ sentence.value = { ...BLANK }
163
+ }
164
+
165
+ // --- sentences kept for next time, in this browser -------------------------------
166
+
167
+ function nameOf(now) {
168
+ const part = words(now)
169
+ return [
170
+ now.shape ? part.shape : '',
171
+ now.about ? part.about : '',
172
+ now.group ? 'in ' + part.group : '',
173
+ now.condition ? 'that ' + part.condition : '',
174
+ now.when ? part.when : '',
175
+ ].filter(Boolean).join(' ')
176
+ }
177
+
178
+ function keep() {
179
+ try {
180
+ localStorage.setItem(SAVED_KEY, JSON.stringify(saved))
181
+ } catch {
182
+ // Kept for this visit only, then.
183
+ }
184
+ }
185
+
186
+ function save() {
187
+ const now = current()
188
+ if (!isSet(now) || state.isSaved) return
189
+ saved = saved.concat([{ name: nameOf(now), sentence: now }])
190
+ keep()
191
+ draw()
192
+ }
193
+
194
+ function use(one) {
195
+ const kept = saved[one.index]
196
+ if (!kept) return
197
+ state.pop = ''
198
+ sentence.value = one.on ? { ...BLANK } : { ...BLANK, ...kept.sentence }
199
+ }
200
+
201
+ function forget(one) {
202
+ saved = saved.filter((_, index) => index !== one.index)
203
+ keep()
204
+ draw()
205
+ }
206
+
207
+ sentence.subscribe(draw)
208
+
209
+ capabilities.subscribe((value) => {
210
+ if (!value) return
211
+ groups = value.groups || []
212
+ usage = Boolean(value.usageFilter)
213
+ state.hasGroups = groups.length > 0
214
+ state.aboutHint = value.text ? 'Words in the name, alt text or labels' : 'Words in the name'
215
+ draw()
216
+ })
217
+
218
+ onMounted(() => {
219
+ try {
220
+ const kept = JSON.parse(localStorage.getItem(SAVED_KEY) || '[]')
221
+ if (Array.isArray(kept)) saved = kept.filter((one) => one && typeof one.name === 'string' && one.sentence)
222
+ } catch {
223
+ // None kept.
224
+ }
225
+ draw()
226
+ })
227
+ </logic>
228
+
229
+ <style>
230
+ .sentence {
231
+ display: flex;
232
+ flex-direction: column;
233
+ gap: 14px;
234
+ }
235
+
236
+ .sent {
237
+ font-family: var(--px-font-sentence);
238
+ font-size: clamp(20px, 1.4vw + 10px, 28px);
239
+ line-height: 1.6;
240
+ letter-spacing: -.01em;
241
+ max-width: 1100px;
242
+ }
243
+
244
+ .tk {
245
+ font: inherit;
246
+ border: 0;
247
+ background: transparent;
248
+ color: var(--px-text);
249
+ padding: 0 4px;
250
+ margin: 0 1px;
251
+ border-radius: var(--px-r-m);
252
+ border-bottom: 2px dashed var(--px-border-strong);
253
+ cursor: pointer;
254
+ }
255
+
256
+ .tk:hover {
257
+ background: var(--px-surface-2);
258
+ }
259
+
260
+ .tk:disabled {
261
+ border-bottom-color: transparent;
262
+ cursor: default;
263
+ }
264
+
265
+ .tk:disabled:hover {
266
+ background: transparent;
267
+ }
268
+
269
+ .tk.is-set {
270
+ color: var(--px-accent-text);
271
+ background: var(--px-accent-soft);
272
+ border-bottom: 2px solid var(--px-accent);
273
+ }
274
+
275
+ .tk.is-open {
276
+ box-shadow: 0 0 0 3px var(--px-focus);
277
+ }
278
+
279
+ .pop {
280
+ display: flex;
281
+ flex-wrap: wrap;
282
+ gap: 8px;
283
+ align-items: center;
284
+ padding: 12px 14px;
285
+ border: 1px solid var(--px-border);
286
+ border-radius: var(--px-r-l);
287
+ background: var(--px-surface);
288
+ box-shadow: var(--px-shadow);
289
+ max-width: 1100px;
290
+ }
291
+
292
+ .pop b {
293
+ font-size: var(--px-fs-s);
294
+ margin-right: 6px;
295
+ }
296
+
297
+ .opt {
298
+ min-height: 36px;
299
+ padding: 0 14px;
300
+ border-radius: 999px;
301
+ border: 1px solid var(--px-border);
302
+ background: var(--px-surface-2);
303
+ font: inherit;
304
+ font-size: 14px;
305
+ color: var(--px-text);
306
+ cursor: pointer;
307
+ }
308
+
309
+ .opt[aria-pressed="true"] {
310
+ background: var(--px-text);
311
+ color: var(--px-bg);
312
+ border-color: var(--px-text);
313
+ }
314
+
315
+ .done {
316
+ margin-left: auto;
317
+ }
318
+
319
+ .about {
320
+ flex: 1;
321
+ min-width: 200px;
322
+ max-width: 420px;
323
+ }
324
+
325
+ .acts {
326
+ gap: 8px;
327
+ flex-wrap: wrap;
328
+ }
329
+
330
+ .saved {
331
+ padding-right: 4px;
332
+ }
333
+
334
+ .saved.is-on {
335
+ background: var(--px-accent-soft);
336
+ color: var(--px-accent-text);
337
+ border-color: var(--px-accent);
338
+ }
339
+
340
+ .saved .saved-go {
341
+ width: auto;
342
+ height: auto;
343
+ border-radius: 0;
344
+ display: inline;
345
+ font: inherit;
346
+ color: inherit;
347
+ background: transparent;
348
+ border: 0;
349
+ padding: 0;
350
+ cursor: pointer;
351
+ }
352
+
353
+ .sr-only {
354
+ position: absolute;
355
+ width: 1px;
356
+ height: 1px;
357
+ overflow: hidden;
358
+ clip-path: inset(50%);
359
+ white-space: nowrap;
360
+ }
361
+ </style>