@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,362 @@
1
+ <view>
2
+ <div class="mx-app" $class="{ 'is-picking': state.picking }">
3
+ <div class="top">
4
+ <b class="title">{{ state.binOpen ? 'Bin' : 'Media' }}</b>
5
+
6
+ <div class="px-seg tabs" role="tablist" aria-label="View" $show="!state.binOpen">
7
+ <button type="button" role="tab" $for="one in state.tabs" $key="one.key" $aria-selected="one.key === state.tab" @click="setTab(one.key)">{{ one.label }}</button>
8
+ </div>
9
+
10
+ <label class="cmd" $show="!state.binOpen">
11
+ <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><circle cx="11" cy="11" r="7"></circle><path d="m20 20-3.5-3.5"></path></svg>
12
+ <input type="search" aria-label="Find pictures" $placeholder="state.searchHint" $model="state.typed" $ref="search" @input="onType" />
13
+ <span class="px-kbd hintk" title="Find or do anything">⌘K</span>
14
+ </label>
15
+
16
+ <div class="ctl">
17
+ <button class="icon-btn" type="button" $show="state.onWall" $aria-pressed="state.names" title="Show file names" @click="toggleNames">Aa Names</button>
18
+ <div class="px-seg" role="radiogroup" aria-label="Picture size" $show="state.onWall">
19
+ <button type="button" role="radio" $for="one in state.sizes" $key="one.key" $aria-checked="one.key === state.size" $aria-label="one.label" @click="setSize(one.key)">{{ one.key }}</button>
20
+ </div>
21
+ <button class="icon-btn" type="button" $show="state.canBin && state.ownBin" $aria-pressed="state.binOpen" @click="toggleBin">{{ state.binOpen ? 'Back to pictures' : 'Bin' }}</button>
22
+ <button class="px-btn px-btn-primary px-btn-sm" type="button" $show="state.canUpload && !state.binOpen" @click="askUpload">{{ state.uploadLabel }}</button>
23
+ </div>
24
+ </div>
25
+
26
+ <div class="wrap" $show="state.tab === 'desk' && !state.binOpen">
27
+ <LibraryDesk />
28
+ </div>
29
+ <div class="wrap" $show="state.tab === 'groups' && !state.binOpen">
30
+ <LibraryGroups />
31
+ </div>
32
+ <div class="wrap" $show="state.onWall">
33
+ <GroupCover $show="!state.binOpen" />
34
+ <SentenceBar $show="!state.binOpen" />
35
+ <PictureWall $mode="state.binOpen ? 'bin' : 'library'" $pick="state.picking" />
36
+ </div>
37
+
38
+ <PictureEditor />
39
+ <DescribeRun />
40
+ <UploadBatch />
41
+ <LinkDialog />
42
+ <CommandBar $if="!state.picking" />
43
+ </div>
44
+ </view>
45
+
46
+ <logic lang="ts">
47
+ import { reactive, onMounted, onUnmounted, shared, defineRefs, defineProps } from '@lightspeed-js/core'
48
+ import { canWrite, getLibrary, letterKeys, TAB, SENTENCE, WALL, CAPABILITIES, SELECTED, UPLOAD, OWN_BIN } from '../config.ts'
49
+ import { BLANK, isSet, parse } from '../sentence.ts'
50
+
51
+ /**
52
+ * The library as a place: one bar that finds, sizes and switches, and the
53
+ * pictures under it. The same frame is the hosted app's page and a CMS's
54
+ * Media page.
55
+ *
56
+ * `mode="bin"` is the bin as a page of its own, and `binButton="false"` is
57
+ * for the library page of a host that has one, so the bin has one way in.
58
+ * `mode="pick"` is the library inside a picker: no desk and no bin, and the
59
+ * picture looked at is the one chosen (the `CHOSEN` cell).
60
+ */
61
+ const props = defineProps({
62
+ mode: { default: 'library' },
63
+ binButton: { default: true },
64
+ })
65
+
66
+ const refs = defineRefs()
67
+
68
+ const WALL_KEY = 'pseudolux:wall'
69
+ const TAB_KEY = 'pseudolux:tab'
70
+
71
+ const state = reactive({
72
+ typed: '',
73
+ searchHint: 'Find pictures',
74
+ names: false,
75
+ size: 'M',
76
+ sizes: [
77
+ { key: 'S', label: 'Small pictures' },
78
+ { key: 'M', label: 'Medium pictures' },
79
+ { key: 'L', label: 'Large pictures' },
80
+ ],
81
+ canBin: false,
82
+ picking: props.mode === 'pick',
83
+ ownBin: props.mode !== 'bin' && props.mode !== 'pick' && props.binButton !== false && props.binButton !== 'false',
84
+ canUpload: false,
85
+ uploadLabel: 'Upload',
86
+ binOpen: props.mode === 'bin',
87
+ tab: 'all',
88
+ onWall: true,
89
+ tabs: [
90
+ ...(props.mode === 'pick' ? [] : [{ key: 'desk', label: 'Desk' }]),
91
+ { key: 'all', label: 'All pictures' },
92
+ { key: 'groups', label: 'Groups' },
93
+ ],
94
+ })
95
+
96
+ const tab = shared(TAB)
97
+ const sentence = shared(SENTENCE)
98
+ const wall = shared(WALL)
99
+ const capabilities = shared(CAPABILITIES)
100
+ const selected = shared(SELECTED)
101
+ const upload = shared(UPLOAD)
102
+ const ownBin = shared(OWN_BIN)
103
+
104
+ let groups = []
105
+ let writeGroups = []
106
+
107
+ // Names the group when there is no doubt where the files will go.
108
+ function nameUpload() {
109
+ const browsing = (sentence.value && sentence.value.group) || ''
110
+ const into = writeGroups.length === 1 ? writeGroups[0] : writeGroups.includes(browsing) ? browsing : ''
111
+ state.uploadLabel = into ? 'Upload to ' + into : 'Upload'
112
+ }
113
+
114
+ function askUpload() {
115
+ upload.value = (upload.value || 0) + 1
116
+ }
117
+
118
+ capabilities.subscribe((value) => {
119
+ if (!value) return
120
+ groups = value.groups || []
121
+ state.canBin = Boolean(value.remove) && canWrite()
122
+ state.canUpload = Boolean(value.upload) && canWrite() && typeof getLibrary().upload === 'function'
123
+ writeGroups = value.writeGroups && value.writeGroups.length ? value.writeGroups : value.writeGroup ? [value.writeGroup] : []
124
+ nameUpload()
125
+ const example = groups.length ? 'portraits in ' + groups[0] + ' that need words' : 'portraits that need words'
126
+ state.searchHint = 'Find pictures, or say it: ' + example
127
+ })
128
+
129
+ // Typing is not a question per keystroke: each one is a listing, and the
130
+ // answers to the prefixes of a word are all thrown away.
131
+ let typing = null
132
+ function onType() {
133
+ if (typing) clearTimeout(typing)
134
+ typing = setTimeout(() => {
135
+ sentence.value = parse(state.typed, { groups })
136
+ if (state.tab !== 'all') tab.value = 'all'
137
+ }, 250)
138
+ }
139
+
140
+ // Cleared from elsewhere, the box should not go on saying what was typed.
141
+ sentence.subscribe((value) => {
142
+ nameUpload()
143
+ if (!isSet(value || BLANK) && state.typed && !typing) state.typed = ''
144
+ })
145
+
146
+ function publishWall() {
147
+ wall.value = { size: state.size, names: state.names }
148
+ try {
149
+ localStorage.setItem(WALL_KEY, JSON.stringify(wall.value))
150
+ } catch {
151
+ // Remembering is a convenience; the wall is still drawn as asked.
152
+ }
153
+ }
154
+
155
+ function toggleNames() {
156
+ state.names = !state.names
157
+ publishWall()
158
+ }
159
+
160
+ function setSize(size) {
161
+ state.size = size
162
+ publishWall()
163
+ }
164
+
165
+ function toggleBin() {
166
+ state.binOpen = !state.binOpen
167
+ tab.value = state.binOpen ? 'bin' : state.tab
168
+ selected.value = null
169
+ }
170
+
171
+ function setTab(key) {
172
+ tab.value = key
173
+ }
174
+
175
+ // The desk and the groups page change the tab too, by opening a group.
176
+ tab.subscribe((value) => {
177
+ if (value === 'bin' || !value) {
178
+ state.onWall = true
179
+ if (value === 'bin' && state.ownBin) state.binOpen = true
180
+ return
181
+ }
182
+ state.binOpen = false
183
+ state.tab = value
184
+ state.onWall = value === 'all'
185
+ if (state.picking) return
186
+ try {
187
+ localStorage.setItem(TAB_KEY, value)
188
+ } catch {
189
+ // As above.
190
+ }
191
+ })
192
+
193
+ // "G" then a letter goes to a view. The G is only remembered for a moment.
194
+ let wentAt = 0
195
+ const PLACES = { d: 'desk', a: 'all', g: 'groups' }
196
+
197
+ function onKey(event) {
198
+ const target = event.target
199
+ const typingSomewhere = target && (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.tagName === 'SELECT' || target.isContentEditable)
200
+ if (typingSomewhere || event.metaKey || event.ctrlKey || event.altKey) return
201
+ // Something is open over the library: its keys are its own.
202
+ if (document.querySelector('[aria-modal="true"]') && [...document.querySelectorAll('[aria-modal="true"]')].some((one) => one.offsetParent !== null)) return
203
+ if (event.key === '/' && refs.search && !state.binOpen) {
204
+ event.preventDefault()
205
+ if (state.tab !== 'all') tab.value = 'all'
206
+ refs.search.focus()
207
+ return
208
+ }
209
+ if (!letterKeys() || state.binOpen) return
210
+ const key = event.key.toLowerCase()
211
+ if (wentAt && Date.now() - wentAt < 1500 && PLACES[key]) {
212
+ event.preventDefault()
213
+ wentAt = 0
214
+ tab.value = PLACES[key]
215
+ return
216
+ }
217
+ wentAt = key === 'g' ? Date.now() : 0
218
+ }
219
+
220
+ onMounted(() => {
221
+ try {
222
+ const saved = JSON.parse(localStorage.getItem(WALL_KEY) || '{}')
223
+ if (saved.size === 'S' || saved.size === 'M' || saved.size === 'L') state.size = saved.size
224
+ state.names = Boolean(saved.names)
225
+ } catch {
226
+ // As above.
227
+ }
228
+ wall.value = { size: state.size, names: state.names }
229
+ // Somebody who looks after a group starts at their desk; the last view
230
+ // used is remembered after that.
231
+ let start = canWrite() && !state.picking ? 'desk' : 'all'
232
+ try {
233
+ const saved = state.picking ? '' : localStorage.getItem(TAB_KEY)
234
+ if (saved === 'desk' || saved === 'all' || saved === 'groups') start = saved
235
+ } catch {
236
+ // As above.
237
+ }
238
+ // A picker opens on every picture, whatever was being looked at last.
239
+ if (state.picking || !sentence.value) sentence.value = { ...BLANK }
240
+ tab.value = state.binOpen ? 'bin' : start
241
+ ownBin.value = state.ownBin
242
+ document.addEventListener('keydown', onKey)
243
+ })
244
+
245
+ onUnmounted(() => {
246
+ document.removeEventListener('keydown', onKey)
247
+ if (typing) clearTimeout(typing)
248
+ })
249
+ </logic>
250
+
251
+ <style>
252
+ .mx-app {
253
+ position: relative;
254
+ min-height: 100%;
255
+ background: var(--px-bg);
256
+ color: var(--px-text);
257
+ }
258
+
259
+ .top {
260
+ position: sticky;
261
+ top: var(--px-frame-top, 0px);
262
+ z-index: 6;
263
+ display: flex;
264
+ align-items: center;
265
+ flex-wrap: wrap;
266
+ gap: 10px 14px;
267
+ padding: 14px var(--px-frame-gutter, 32px);
268
+ border-bottom: 1px solid var(--px-border);
269
+ background: var(--px-surface);
270
+ }
271
+
272
+ .title {
273
+ font-size: var(--px-fs-l);
274
+ }
275
+
276
+ .tabs button[aria-selected="true"] {
277
+ background: var(--px-surface);
278
+ color: var(--px-text);
279
+ box-shadow: 0 1px 2px rgba(0, 0, 0, .12);
280
+ font-weight: 600;
281
+ }
282
+
283
+ .cmd {
284
+ flex: 1 1 280px;
285
+ max-width: 520px;
286
+ display: flex;
287
+ align-items: center;
288
+ gap: 10px;
289
+ min-height: var(--px-target);
290
+ padding: 0 14px;
291
+ border: 1px solid var(--px-border);
292
+ border-radius: 10px;
293
+ background: var(--px-bg);
294
+ color: var(--px-text-3);
295
+ }
296
+
297
+ .cmd:focus-within {
298
+ box-shadow: 0 0 0 3px var(--px-focus);
299
+ border-color: transparent;
300
+ }
301
+
302
+ .cmd input {
303
+ flex: 1;
304
+ min-width: 0;
305
+ border: 0;
306
+ background: transparent;
307
+ font: inherit;
308
+ font-size: 14px;
309
+ color: var(--px-text);
310
+ outline: none;
311
+ }
312
+
313
+ .hintk {
314
+ flex: none;
315
+ }
316
+
317
+ .ctl {
318
+ display: flex;
319
+ align-items: center;
320
+ flex-wrap: wrap;
321
+ gap: 6px;
322
+ margin-left: auto;
323
+ }
324
+
325
+ .icon-btn {
326
+ min-height: var(--px-target);
327
+ min-width: var(--px-target);
328
+ padding: 0 12px;
329
+ border: 1px solid var(--px-border);
330
+ border-radius: 10px;
331
+ background: var(--px-surface);
332
+ color: var(--px-text-2);
333
+ font: inherit;
334
+ font-size: var(--px-fs-s);
335
+ font-weight: 500;
336
+ cursor: pointer;
337
+ display: inline-flex;
338
+ align-items: center;
339
+ gap: 6px;
340
+ }
341
+
342
+ .icon-btn[aria-pressed="true"] {
343
+ background: var(--px-surface-2);
344
+ color: var(--px-text);
345
+ border-color: var(--px-border-strong);
346
+ }
347
+
348
+ .wrap {
349
+ padding: 24px var(--px-frame-gutter, 32px) 120px;
350
+ display: flex;
351
+ flex-direction: column;
352
+ gap: 14px;
353
+ }
354
+
355
+ @media (max-width: 760px) {
356
+ .top,
357
+ .wrap {
358
+ padding-left: 16px;
359
+ padding-right: 16px;
360
+ }
361
+ }
362
+ </style>
@@ -0,0 +1,197 @@
1
+ <view>
2
+ <div class="groups">
3
+ <div class="lede">
4
+ <h1>Groups</h1>
5
+ <p class="px-muted">Every picture belongs to at least one group. A group decides who can change it.</p>
6
+ </div>
7
+
8
+ <h3 $show="state.mine.length">Yours <span class="px-subtle">· you can change and upload</span></h3>
9
+ <div class="gcards" $show="state.mine.length">
10
+ <button class="gcard" type="button" $for="group in state.mine" $key="group.name" @click="open(group.name)">
11
+ <span class="mos"><img $for="pic in group.cover" $key="pic.id" $src="pic.thumb" alt="" loading="lazy" /></span>
12
+ <span class="gtext"><b>{{ group.name }}</b><span class="px-subtle">{{ group.meta }}</span></span>
13
+ </button>
14
+ </div>
15
+
16
+ <h3 $show="state.others.length">{{ state.mine.length ? 'Others' : 'Groups' }} <span class="px-subtle">· you can see and use</span></h3>
17
+ <div class="gcards" $show="state.others.length">
18
+ <button class="gcard" type="button" $for="group in state.others" $key="group.name" @click="open(group.name)">
19
+ <span class="mos"><img $for="pic in group.cover" $key="pic.id" $src="pic.thumb" alt="" loading="lazy" /></span>
20
+ <span class="gtext"><b>{{ group.name }}</b><span class="px-subtle">{{ group.meta }}</span></span>
21
+ </button>
22
+ </div>
23
+
24
+ <div class="px-empty" $show="state.ready && !state.mine.length && !state.others.length">
25
+ <b>No groups to browse</b>
26
+ <p class="px-muted">This library has not said which groups you can see.</p>
27
+ </div>
28
+ </div>
29
+ </view>
30
+
31
+ <logic lang="ts">
32
+ import { reactive, onMounted, shared } from '@lightspeed-js/core'
33
+ import { getLibrary, thumbnailUrl, canWrite, TAB, SENTENCE, CAPABILITIES, CHANGED } from '../config.ts'
34
+ import { BLANK } from '../sentence.ts'
35
+ import { survey, tally, atLeast } from '../survey.ts'
36
+
37
+ /** Every group the person can see, theirs first, each with its newest pictures. */
38
+ const state = reactive({
39
+ ready: false,
40
+ mine: [],
41
+ others: [],
42
+ })
43
+
44
+ const tab = shared(TAB)
45
+ const sentence = shared(SENTENCE)
46
+ const capabilities = shared(CAPABILITIES)
47
+ const changed = shared(CHANGED)
48
+
49
+ let generation = 0
50
+
51
+ const cover = (items) => items.slice(0, 3).map((item) => ({ id: item.id, thumb: thumbnailUrl(item.id) }))
52
+
53
+ async function look() {
54
+ const about = capabilities.value
55
+ if (!about) return
56
+ const writes = canWrite() ? (about.writeGroups && about.writeGroups.length ? about.writeGroups : about.writeGroup ? [about.writeGroup] : []) : []
57
+ const all = [...new Set(writes.concat(about.groups || []))]
58
+ const run = ++generation
59
+ // Names first, so the page has its shape before any picture arrives.
60
+ const card = (name) => ({ name, meta: '', cover: [] })
61
+ if (!state.ready) {
62
+ state.mine = writes.map(card)
63
+ state.others = all.filter((one) => !writes.includes(one)).map(card)
64
+ }
65
+ state.ready = true
66
+ const now = new Date()
67
+ const port = getLibrary()
68
+ const cards = await Promise.all(all.map(async (name) => {
69
+ const own = writes.includes(name)
70
+ try {
71
+ // A group the person looks after is read far enough to count what
72
+ // needs words; another's only far enough for a cover.
73
+ const found = await survey(port, name, own ? undefined : 1)
74
+ const counted = tally(found, now)
75
+ const pictures = atLeast(counted.count, counted.complete) + (counted.count === 1 ? ' picture' : ' pictures')
76
+ return {
77
+ name,
78
+ own,
79
+ cover: cover(found.items),
80
+ meta: own
81
+ ? pictures + (counted.needs.length ? ' · ' + atLeast(counted.needs.length, counted.complete) + (counted.needs.length === 1 ? ' needs words' : ' need words') : '')
82
+ : pictures + ' · view only',
83
+ }
84
+ } catch {
85
+ return { name, own, cover: [], meta: "Couldn't load" }
86
+ }
87
+ }))
88
+ if (run !== generation) return
89
+ state.mine = cards.filter((one) => one.own)
90
+ state.others = cards.filter((one) => !one.own)
91
+ }
92
+
93
+ function open(name) {
94
+ sentence.value = { ...BLANK, group: name }
95
+ tab.value = 'all'
96
+ }
97
+
98
+ tab.subscribe((value) => {
99
+ if (value === 'groups') look()
100
+ })
101
+
102
+ capabilities.subscribe(() => {
103
+ if (tab.value === 'groups') look()
104
+ })
105
+
106
+ let lastChange = changed.value
107
+ changed.subscribe((value) => {
108
+ if (value === lastChange) return
109
+ lastChange = value
110
+ if (tab.value === 'groups') look()
111
+ })
112
+
113
+ onMounted(() => {
114
+ if (tab.value === 'groups') look()
115
+ })
116
+ </logic>
117
+
118
+ <style>
119
+ .groups {
120
+ display: flex;
121
+ flex-direction: column;
122
+ gap: 20px;
123
+ }
124
+
125
+ .lede h1 {
126
+ font-size: clamp(26px, 2vw + 12px, 34px);
127
+ font-weight: 600;
128
+ letter-spacing: -.02em;
129
+ }
130
+
131
+ .lede p {
132
+ font-size: 16px;
133
+ }
134
+
135
+ .groups > h3 {
136
+ font-size: var(--px-fs-m);
137
+ }
138
+
139
+ .groups > h3 .px-subtle {
140
+ font-weight: 400;
141
+ }
142
+
143
+ .gcards {
144
+ display: grid;
145
+ grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
146
+ gap: 16px;
147
+ }
148
+
149
+ .gcard {
150
+ display: flex;
151
+ flex-direction: column;
152
+ border: 1px solid var(--px-border);
153
+ border-radius: 14px;
154
+ background: var(--px-surface);
155
+ overflow: hidden;
156
+ cursor: pointer;
157
+ text-align: left;
158
+ font: inherit;
159
+ color: var(--px-text);
160
+ padding: 0;
161
+ }
162
+
163
+ .gcard:hover {
164
+ border-color: var(--px-accent);
165
+ }
166
+
167
+ .mos {
168
+ display: grid;
169
+ grid-template-columns: 2fr 1fr;
170
+ grid-template-rows: 1fr 1fr;
171
+ gap: 2px;
172
+ height: 140px;
173
+ background: var(--px-surface-2);
174
+ }
175
+
176
+ .mos img {
177
+ width: 100%;
178
+ height: 100%;
179
+ min-height: 0;
180
+ object-fit: cover;
181
+ }
182
+
183
+ .mos img:first-child {
184
+ grid-row: 1 / 3;
185
+ }
186
+
187
+ .gtext {
188
+ padding: 12px 14px 14px;
189
+ display: flex;
190
+ flex-direction: column;
191
+ gap: 6px;
192
+ }
193
+
194
+ .gtext b {
195
+ font-size: 16px;
196
+ }
197
+ </style>