@open-commune/web 0.2.21 → 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.
@@ -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
- {{ row.original[titleKey] }}
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
- {{ row.original[descriptionKey] }}
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>
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.21",
23
- "@open-commune/editor": "^0.2.21",
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.21",
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
+ }