@open-commune/web 0.2.20 → 0.2.22
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.
- package/components/data/lists/data-table.vue +46 -2
- package/components/elements/text/text-found.vue +28 -0
- package/composables/core/domains/use-entries.ts +5 -2
- package/composables/core/domains/use-media.ts +9 -23
- package/composables/use-batch.ts +69 -0
- package/package.json +3 -3
- package/utils/found.ts +79 -0
|
@@ -31,14 +31,17 @@
|
|
|
31
31
|
<status-badge :status="row.original.status" class="sm:hidden" />
|
|
32
32
|
<div class="flex w-full min-w-0 items-center space-x-2">
|
|
33
33
|
<p class="min-w-0 truncate text-base font-semibold text-highlighted plain:text-sm plain:font-normal plain:text-default">
|
|
34
|
-
|
|
34
|
+
<text-found :pieces="titleKey === 'title' ? _hitInSight(_foundOf(row.original)?.title) : undefined" :text="row.original[titleKey]" />
|
|
35
35
|
</p>
|
|
36
36
|
<status-badge :status="row.original.status" class="hidden sm:inline-flex" />
|
|
37
37
|
</div>
|
|
38
38
|
<!-- One line, cut with an ellipsis: the description places the entry,
|
|
39
39
|
it is not read here, and a fixed line keeps rows of equal height. -->
|
|
40
40
|
<p class="hidden w-full truncate text-[13px] font-normal text-muted sm:block plain:line-clamp-2 plain:whitespace-normal plain:text-sm plain:font-light">
|
|
41
|
-
|
|
41
|
+
<!-- Under a search, a word found outside the title shows where:
|
|
42
|
+
the passage around it stands in for the description. -->
|
|
43
|
+
<span v-if="_foundOf(row.original)?.field" class="text-dimmed">dans {{ _foundOf(row.original).field }} · </span>
|
|
44
|
+
<text-found :pieces="_hitInSight(_foundOf(row.original)?.excerpt, 36, 32)" :text="row.original[descriptionKey]" />
|
|
42
45
|
</p>
|
|
43
46
|
</div>
|
|
44
47
|
</div>
|
|
@@ -94,6 +97,7 @@ import { DateTime } from 'luxon'
|
|
|
94
97
|
import rowActions from '~/components/data/lists/row-actions'
|
|
95
98
|
import statusBadge from '~/components/elements/badges/status-badge'
|
|
96
99
|
import mediaImage from '~/components/elements/media/media-image'
|
|
100
|
+
import textFound from '~/components/elements/text/text-found'
|
|
97
101
|
import { swatchOf } from '~/utils/colors'
|
|
98
102
|
|
|
99
103
|
// Prepare props
|
|
@@ -144,6 +148,9 @@ const $emitters = defineEmits(['update', 'remove'])
|
|
|
144
148
|
// Prepare composables
|
|
145
149
|
const { public: { appearance } } = useRuntimeConfig()
|
|
146
150
|
const plain = appearance === 'plain'
|
|
151
|
+
// Where a search found each row is the new look's: today's look shows its
|
|
152
|
+
// rows as it always did, until its users move to the new one.
|
|
153
|
+
const _foundOf = item => foundById.value.get(item.id)
|
|
147
154
|
const $entries = useEntries()
|
|
148
155
|
const $members = useMembers()
|
|
149
156
|
|
|
@@ -190,10 +197,47 @@ const columns = computed(() => {
|
|
|
190
197
|
})
|
|
191
198
|
|
|
192
199
|
const linkedById = computed(() => new Map(linked.value.map(entry => [entry.id, entry])))
|
|
200
|
+
// Where a search found each row, cut once for the table.
|
|
201
|
+
const foundById = computed(() => new Map(plain ? [] : $props.items.filter(item => item.found).map(item => [item.id, foundOf(item.title, item.found)])))
|
|
193
202
|
|
|
194
203
|
// Prepare methods
|
|
195
204
|
const _labelsOf = item => [item[$props.labelsKey] ?? []].flat().map(id => linkedById.value.get(id)).filter(Boolean)
|
|
196
205
|
const _authorOf = item => members.value?.find(({ id }) => id === item.updatedBy)?.name ?? ''
|
|
206
|
+
// A long title or passage is cut at its end: when the word found sits past
|
|
207
|
+
// the cut, it starts a few words before the word instead, so the reason the
|
|
208
|
+
// row is here stays in sight. The passage shares its line with the field's
|
|
209
|
+
// name, so it keeps less before the word.
|
|
210
|
+
const _hitInSight = (pieces, lead = 48, room = undefined) => {
|
|
211
|
+
if (!pieces) return pieces
|
|
212
|
+
const first = pieces.findIndex(({ hit }) => hit)
|
|
213
|
+
const before = pieces.slice(0, first).map(({ text }) => text).join('')
|
|
214
|
+
if (first < 0) return pieces
|
|
215
|
+
// About three words of context, cut on a space.
|
|
216
|
+
const kept = before.slice(-24)
|
|
217
|
+
const space = kept.indexOf(' ')
|
|
218
|
+
const opening = before.length <= lead ? pieces.slice(0, first) : [{ text: `…${space > 0 ? kept.slice(space) : kept}` }]
|
|
219
|
+
const rest = pieces.slice(first + 1)
|
|
220
|
+
return [...opening, pieces[first], ...(room ? _trailing(rest, room) : rest)]
|
|
221
|
+
}
|
|
222
|
+
// What follows the first word found, cut on a space well before the line's
|
|
223
|
+
// end: the line's own ellipsis would otherwise fall inside a later word found,
|
|
224
|
+
// and show as a marked « … » with no word.
|
|
225
|
+
const _trailing = (pieces, room) => {
|
|
226
|
+
const kept = []
|
|
227
|
+
for (const piece of pieces) {
|
|
228
|
+
if (piece.text.length <= room) {
|
|
229
|
+
kept.push(piece)
|
|
230
|
+
room -= piece.text.length
|
|
231
|
+
continue
|
|
232
|
+
}
|
|
233
|
+
const cut = piece.text.slice(0, room)
|
|
234
|
+
const space = cut.lastIndexOf(' ')
|
|
235
|
+
if (!piece.hit && space > 0) kept.push({ text: `${cut.slice(0, space)}…` })
|
|
236
|
+
else kept.push({ text: '…' })
|
|
237
|
+
break
|
|
238
|
+
}
|
|
239
|
+
return kept
|
|
240
|
+
}
|
|
197
241
|
const _fromNow = value => (value ? DateTime.fromISO(value).setLocale('fr').toRelative() : '')
|
|
198
242
|
/** A media field may hold one id or several; the cover is the first. */
|
|
199
243
|
const _coverOf = (item) => {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<!-- Text, never HTML: what was found is the editors' own writing. -->
|
|
3
|
+
<template v-if="pieces?.length">
|
|
4
|
+
<template v-for="(piece, i) in pieces" :key="i">
|
|
5
|
+
<mark v-if="piece.hit" data-testid="found" class="rounded-[3px] bg-amber-200/80 px-0.5 text-highlighted [box-decoration-break:clone] dark:bg-amber-400/30">{{ piece.text }}</mark>
|
|
6
|
+
<template v-else>{{ piece.text }}</template>
|
|
7
|
+
</template>
|
|
8
|
+
</template>
|
|
9
|
+
<template v-else>{{ text }}</template>
|
|
10
|
+
</template>
|
|
11
|
+
|
|
12
|
+
<script setup>
|
|
13
|
+
/**
|
|
14
|
+
* A text with the words a search found highlighted, as with a marker. Without pieces, the text as
|
|
15
|
+
* it is.
|
|
16
|
+
*/
|
|
17
|
+
defineProps({
|
|
18
|
+
// The text cut where the search found its words, as the core gives it.
|
|
19
|
+
pieces: {
|
|
20
|
+
type: Array,
|
|
21
|
+
default: undefined
|
|
22
|
+
},
|
|
23
|
+
text: {
|
|
24
|
+
type: String,
|
|
25
|
+
default: ''
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
</script>
|
|
@@ -116,9 +116,12 @@ export default function useEntries() {
|
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
const load = useBatch<Entry & { id: string }>('entries', ids => trpc.collections.entries.getMany.query({ ids }))
|
|
120
|
+
|
|
119
121
|
/**
|
|
120
122
|
* Several entries by id: a relation column shows titles, the server sends
|
|
121
|
-
* ids. Same keys as `get`, so an entry is not asked for twice
|
|
123
|
+
* ids. Same keys as `get`, so an entry is not asked for twice, and the ids
|
|
124
|
+
* not yet cached travel together in one call rather than one call each.
|
|
122
125
|
*/
|
|
123
126
|
const getMany = (ids: MaybeRefOrGetter<string[] | string | undefined>) => {
|
|
124
127
|
const wanted = computed(() => {
|
|
@@ -128,7 +131,7 @@ export default function useEntries() {
|
|
|
128
131
|
return useQueries({
|
|
129
132
|
queries: computed(() => wanted.value.map(id => ({
|
|
130
133
|
queryKey: ['entries', 'byId', id],
|
|
131
|
-
queryFn: () =>
|
|
134
|
+
queryFn: () => load(id),
|
|
132
135
|
}))),
|
|
133
136
|
combine: results => results.map(({ data }) => data).filter(Boolean) as Entry[],
|
|
134
137
|
})
|
|
@@ -4,32 +4,19 @@ import { readonly, ref, toValue, watch, type MaybeRefOrGetter } from 'vue'
|
|
|
4
4
|
/**
|
|
5
5
|
* Media domain composable: tRPC calls plus the direct-to-S3 upload step.
|
|
6
6
|
*
|
|
7
|
-
* Resolution is shared app-wide (module cache) and
|
|
8
|
-
* resolves every media on mount,
|
|
9
|
-
*
|
|
7
|
+
* Resolution is shared app-wide (module cache) and batched: a listing shows a
|
|
8
|
+
* cover per row and a rich document resolves every media on mount, so the
|
|
9
|
+
* ids asked for together travel in one call (a hundred at most) instead of
|
|
10
|
+
* one request each. Failures are not cached — they retry on next mount.
|
|
10
11
|
*/
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
type ResolvedMedia = { id: string; filename?: string | null; objects?: Record<string, string> }
|
|
13
14
|
|
|
14
15
|
const mediaCache = new Map<string, Promise<{ src?: string; title?: string }>>()
|
|
15
|
-
let inFlight = 0
|
|
16
|
-
const waiters: Array<() => void> = []
|
|
17
|
-
|
|
18
|
-
async function withSlot<T>(task: () => Promise<T>): Promise<T> {
|
|
19
|
-
while (inFlight >= FETCH_CONCURRENCY) {
|
|
20
|
-
await new Promise<void>((resolve) => waiters.push(resolve))
|
|
21
|
-
}
|
|
22
|
-
inFlight += 1
|
|
23
|
-
try {
|
|
24
|
-
return await task()
|
|
25
|
-
} finally {
|
|
26
|
-
inFlight -= 1
|
|
27
|
-
waiters.shift()?.()
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
16
|
|
|
31
17
|
export default function useMedia() {
|
|
32
18
|
const trpc = useTrpc()
|
|
19
|
+
const load = useBatch<ResolvedMedia>('media', ids => trpc.media.getMany.query({ ids }))
|
|
33
20
|
|
|
34
21
|
/** Presigned browser PUT. The Content-Type must match the presign. */
|
|
35
22
|
const uploadToS3 = async (url: string, file: File) => {
|
|
@@ -64,10 +51,9 @@ export default function useMedia() {
|
|
|
64
51
|
if (!mediaCache.has(id)) {
|
|
65
52
|
mediaCache.set(
|
|
66
53
|
id,
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}).catch((error) => {
|
|
54
|
+
load(id).then(
|
|
55
|
+
media => ({ src: media.objects?.original, title: media.filename ?? undefined }),
|
|
56
|
+
).catch((error) => {
|
|
71
57
|
mediaCache.delete(id)
|
|
72
58
|
throw error
|
|
73
59
|
}),
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Many lookups by id turned into one call. Every `load(id)` asked for during
|
|
3
|
+
* the same turn of the event loop is gathered, and the set goes to the server
|
|
4
|
+
* at once, in slices of `max` (what the endpoint accepts). A listing that
|
|
5
|
+
* mounts twenty-five thumbnails sends one request, not twenty-five.
|
|
6
|
+
*
|
|
7
|
+
* One loader per `name` for the whole app: the domain composables are called
|
|
8
|
+
* by every component that mounts, and their lookups only share a call if they
|
|
9
|
+
* share the loader. The first caller's `fetchMany` is the one kept.
|
|
10
|
+
*
|
|
11
|
+
* An id the server did not send back rejects, as a lookup of one would.
|
|
12
|
+
*/
|
|
13
|
+
type Loader = (id: string) => Promise<unknown>
|
|
14
|
+
|
|
15
|
+
const loaders = new Map<string, Loader>()
|
|
16
|
+
|
|
17
|
+
const createLoader = <T extends { id: string }>(
|
|
18
|
+
fetchMany: (ids: string[]) => Promise<T[]>,
|
|
19
|
+
max: number,
|
|
20
|
+
) => {
|
|
21
|
+
type Waiter = { resolve: (value: T) => void; reject: (error: unknown) => void }
|
|
22
|
+
let pending = new Map<string, Waiter[]>()
|
|
23
|
+
let scheduled = false
|
|
24
|
+
|
|
25
|
+
const settle = async (ids: string[], waiters: Map<string, Waiter[]>) => {
|
|
26
|
+
try {
|
|
27
|
+
const found = new Map((await fetchMany(ids)).map((item) => [item.id, item]))
|
|
28
|
+
for (const id of ids) {
|
|
29
|
+
const item = found.get(id)
|
|
30
|
+
for (const waiter of waiters.get(id) ?? []) {
|
|
31
|
+
if (item) waiter.resolve(item)
|
|
32
|
+
else waiter.reject(new Error(`not found: ${id}`))
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
} catch (error) {
|
|
36
|
+
for (const id of ids) for (const waiter of waiters.get(id) ?? []) waiter.reject(error)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const flush = () => {
|
|
41
|
+
const waiters = pending
|
|
42
|
+
pending = new Map()
|
|
43
|
+
scheduled = false
|
|
44
|
+
const ids = [...waiters.keys()]
|
|
45
|
+
for (let start = 0; start < ids.length; start += max) {
|
|
46
|
+
void settle(ids.slice(start, start + max), waiters)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return (id: string) =>
|
|
51
|
+
new Promise<T>((resolve, reject) => {
|
|
52
|
+
pending.set(id, [...(pending.get(id) ?? []), { resolve, reject }])
|
|
53
|
+
if (!scheduled) {
|
|
54
|
+
scheduled = true
|
|
55
|
+
// A macrotask, not a microtask: the components of one render, and the
|
|
56
|
+
// queries they subscribe, have all asked by then.
|
|
57
|
+
setTimeout(flush, 0)
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default function useBatch<T extends { id: string }>(
|
|
63
|
+
name: string,
|
|
64
|
+
fetchMany: (ids: string[]) => Promise<T[]>,
|
|
65
|
+
{ max = 100 }: { max?: number } = {},
|
|
66
|
+
): (id: string) => Promise<T> {
|
|
67
|
+
if (!loaders.has(name)) loaders.set(name, createLoader(fetchMany, max) as Loader)
|
|
68
|
+
return loaders.get(name) as (id: string) => Promise<T>
|
|
69
|
+
}
|
package/package.json
CHANGED
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"@iconify/vue": "^5.0.3",
|
|
20
20
|
"@nuxt/kit": "^4.6.1",
|
|
21
21
|
"@nuxt/ui": "^4.12.0",
|
|
22
|
-
"@open-commune/core": "^0.2.
|
|
23
|
-
"@open-commune/editor": "^0.2.
|
|
22
|
+
"@open-commune/core": "^0.2.22",
|
|
23
|
+
"@open-commune/editor": "^0.2.22",
|
|
24
24
|
"@pinia-orm/nuxt": "^2.0.0",
|
|
25
25
|
"@pinia/nuxt": "^1.0.3",
|
|
26
26
|
"@tanstack/vue-query": "^5.104.3",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
},
|
|
44
44
|
"description": "The Open Commune application: the admin interface and the server, on a single origin.",
|
|
45
45
|
"license": "AGPL-3.0-or-later",
|
|
46
|
-
"version": "0.2.
|
|
46
|
+
"version": "0.2.22",
|
|
47
47
|
"type": "module",
|
|
48
48
|
"main": "./nuxt.config.ts",
|
|
49
49
|
"files": [
|
package/utils/found.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a search found an entry, as a list shows it. The core gives the
|
|
3
|
+
* entry's title and texts as the full-text index marked them, the words found
|
|
4
|
+
* between FOUND_OPEN and FOUND_CLOSE, and the field the first one comes from;
|
|
5
|
+
* cutting them into what the screen marks is done here.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Marks no editor types, so that whatever was written stays text.
|
|
9
|
+
const FOUND_OPEN = '\u0002';
|
|
10
|
+
const FOUND_CLOSE = '\u0003';
|
|
11
|
+
|
|
12
|
+
/** A stretch of text, and whether a search found it. */
|
|
13
|
+
export interface FoundPiece {
|
|
14
|
+
text: string;
|
|
15
|
+
hit?: true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** What the core says of an entry found by a search. */
|
|
19
|
+
export interface FoundMarks {
|
|
20
|
+
title: string;
|
|
21
|
+
body: string;
|
|
22
|
+
extra: string;
|
|
23
|
+
field?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Text marked by the index, cut into its stretches. */
|
|
27
|
+
const piecesOf = (marked: string): FoundPiece[] =>
|
|
28
|
+
marked
|
|
29
|
+
.split(FOUND_OPEN)
|
|
30
|
+
.flatMap((part, i) => {
|
|
31
|
+
if (i === 0) return [{ text: part }];
|
|
32
|
+
const [hit = '', rest = ''] = part.split(FOUND_CLOSE);
|
|
33
|
+
return [{ text: hit, hit: true as const }, { text: rest }];
|
|
34
|
+
})
|
|
35
|
+
.filter(({ text }) => text);
|
|
36
|
+
|
|
37
|
+
/** As the index holds a text: with « œ » written out « oe ». */
|
|
38
|
+
const withoutLigatures = (text: string) =>
|
|
39
|
+
text.replace(/œ/g, 'oe').replace(/Œ/g, 'OE').replace(/æ/g, 'ae').replace(/Æ/g, 'AE');
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The marks of the indexed title laid back on the title itself: the index
|
|
43
|
+
* holds « Voeux », and the screen shows « Vœux ».
|
|
44
|
+
*/
|
|
45
|
+
const onTitle = (title: string, marked: string): FoundPiece[] => {
|
|
46
|
+
const ranges: [number, number][] = [];
|
|
47
|
+
let at = 0;
|
|
48
|
+
for (const piece of piecesOf(marked)) {
|
|
49
|
+
if (piece.hit) ranges.push([at, at + piece.text.length]);
|
|
50
|
+
at += piece.text.length;
|
|
51
|
+
}
|
|
52
|
+
const pieces: FoundPiece[] = [];
|
|
53
|
+
let from = 0;
|
|
54
|
+
for (const char of title) {
|
|
55
|
+
const to = from + withoutLigatures(char).length;
|
|
56
|
+
const hit = ranges.some(([start, end]) => from < end && start < to);
|
|
57
|
+
const last = pieces.at(-1);
|
|
58
|
+
if (last && Boolean(last.hit) === hit) last.text += char;
|
|
59
|
+
else pieces.push(hit ? { text: char, hit: true } : { text: char });
|
|
60
|
+
from = to;
|
|
61
|
+
}
|
|
62
|
+
return pieces;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The title with its found words marked and, when none of them is in it, the
|
|
67
|
+
* passage around them, from the text a reader searches before the one only
|
|
68
|
+
* the admin does, and the field it comes from.
|
|
69
|
+
*/
|
|
70
|
+
export function foundOf(
|
|
71
|
+
title: string,
|
|
72
|
+
marks: FoundMarks,
|
|
73
|
+
): { title: FoundPiece[]; excerpt?: FoundPiece[]; field?: string } {
|
|
74
|
+
const pieces = onTitle(title, marks.title);
|
|
75
|
+
if (pieces.some(({ hit }) => hit)) return { title: pieces };
|
|
76
|
+
const passage = [marks.body, marks.extra].find((text) => text.includes(FOUND_OPEN));
|
|
77
|
+
if (!passage) return { title: pieces };
|
|
78
|
+
return { title: pieces, excerpt: piecesOf(passage), field: marks.field };
|
|
79
|
+
}
|