@antelopejs/dms-media 0.3.1 → 0.4.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.
- package/README.md +5 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/test/antelope.test.js +1 -1
- package/dist/test/helpers/pages.js +3 -2
- package/dist/test/helpers/pages.js.map +1 -1
- package/dist/test/integration/frontend-module.test.d.ts +1 -0
- package/dist/test/integration/frontend-module.test.js +13 -0
- package/dist/test/integration/frontend-module.test.js.map +1 -0
- package/frontend-vue/app/components/FileProperties.vue +9 -3
- package/frontend-vue/app/components/FirstRun.vue +1 -1
- package/frontend-vue/app/components/ImageEditor.vue +43 -26
- package/frontend-vue/app/components/PresetCards.vue +3 -11
- package/frontend-vue/app/components/RecentFiles.vue +6 -14
- package/frontend-vue/app/components/UploadQueue.vue +1 -1
- package/frontend-vue/app/components/UploadStrip.vue +1 -1
- package/frontend-vue/app/components/dialogs/DestinationDialog.vue +1 -2
- package/frontend-vue/app/components/explorer/ExplorerInspector.vue +17 -16
- package/frontend-vue/app/components/explorer/ExplorerShell.vue +11 -3
- package/frontend-vue/app/components/explorer/ExplorerToolbar.vue +2 -1
- package/frontend-vue/app/components/field/AssetValue.vue +12 -13
- package/frontend-vue/app/components/file/FileViewer.vue +1 -14
- package/frontend-vue/app/composables/library/useLibraryListing.ts +180 -79
- package/frontend-vue/app/composables/library/useLibraryTree.ts +31 -15
- package/frontend-vue/app/composables/library/useMediaLibrary.ts +17 -7
- package/frontend-vue/app/composables/uploadLimits.ts +25 -0
- package/frontend-vue/app/composables/useAssetContent.ts +20 -0
- package/frontend-vue/app/composables/useCurrentFile.ts +10 -8
- package/frontend-vue/app/composables/useFieldFolders.ts +19 -18
- package/frontend-vue/app/composables/useFileDetails.ts +117 -90
- package/frontend-vue/app/composables/useMediaApi.ts +18 -24
- package/frontend-vue/package.json +2 -1
- package/frontend-vue/pnpm-lock.yaml +17 -2
- package/frontend-vue/tests/engines.test.ts +20 -0
- package/frontend-vue/tests/fixtures/inertia.ts +8 -0
- package/frontend-vue/tests/fixtures/serverRender.ts +200 -0
- package/frontend-vue/tests/fixtures/unhead.ts +3 -0
- package/frontend-vue/tests/fixtures/vueI18n.ts +4 -0
- package/frontend-vue/tests/libraryListing.test.ts +120 -0
- package/frontend-vue/tests/serverRender.test.ts +215 -0
- package/frontend-vue/vitest.config.ts +21 -0
- package/package.json +11 -5
- package/src/index.ts +19 -2
- package/src/test/antelope.test.ts +1 -1
- package/src/test/helpers/pages.ts +3 -2
- package/src/test/integration/frontend-module.test.ts +13 -0
- package/frontend-vue/app/composables/useSharedTree.ts +0 -73
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ApiDataContext, ApiDataResult } from '#dms-core/app/composables/auth/useApiData'
|
|
1
2
|
import type { AssetDetails, AssetPage, AssetVisibility, MediaAsset } from '../types/media'
|
|
2
3
|
import type { FileListingOrder } from '../utils/mediaFiles'
|
|
3
4
|
|
|
@@ -6,30 +7,109 @@ const SIBLINGS_HALF_WINDOW = SIBLINGS_PAGE_SIZE / 2
|
|
|
6
7
|
const SIBLINGS_EDGE_MARGIN = 20
|
|
7
8
|
const DEFAULT_SIBLINGS_SORT = 'name'
|
|
8
9
|
|
|
10
|
+
/** The page of the file's folder the previous and next files come from. */
|
|
11
|
+
interface SiblingsWindow {
|
|
12
|
+
folderId: string
|
|
13
|
+
/** The order it was read in, as `orderKey` writes it. */
|
|
14
|
+
order: string
|
|
15
|
+
/** A write may have moved the file among them: the next read of the file reads them again. */
|
|
16
|
+
isStale?: boolean
|
|
17
|
+
assets: MediaAsset[]
|
|
18
|
+
offset: number
|
|
19
|
+
total: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** What the file page reads at once: the file, and the files around it in its folder. */
|
|
23
|
+
interface FileRead {
|
|
24
|
+
details: AssetDetails
|
|
25
|
+
siblings: SiblingsWindow
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const orderKey = (order: FileListingOrder) => JSON.stringify([order.sort, order.direction, order.type])
|
|
29
|
+
|
|
30
|
+
function isNearWindowEdge(window: SiblingsWindow, id: string): boolean {
|
|
31
|
+
const position = window.assets.findIndex((entry) => entry.id === id)
|
|
32
|
+
if (position < 0) return true
|
|
33
|
+
const hasBefore = window.offset > 0
|
|
34
|
+
const hasAfter = window.offset + window.assets.length < window.total
|
|
35
|
+
return (hasBefore && position < SIBLINGS_EDGE_MARGIN) || (hasAfter && position >= window.assets.length - SIBLINGS_EDGE_MARGIN)
|
|
36
|
+
}
|
|
37
|
+
|
|
9
38
|
/**
|
|
10
39
|
* One file of the library, its neighbours in the folder (in the order and
|
|
11
|
-
* filter the explorer showed them in) and the writes on it.
|
|
12
|
-
*
|
|
13
|
-
* a
|
|
40
|
+
* filter the explorer showed them in) and the writes on it. The file and its
|
|
41
|
+
* neighbours are read with the page, under one key per file: the page's blocks
|
|
42
|
+
* share the read, and a server render hands it over. Moving to the next file
|
|
43
|
+
* reads that file alone while it sits well inside the neighbours already read.
|
|
44
|
+
* It follows the library's realtime topic, so a change made elsewhere (a
|
|
45
|
+
* revert, a rename, a deletion) shows at once.
|
|
14
46
|
*/
|
|
15
47
|
export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<FileListingOrder> = ref({})) {
|
|
16
48
|
const api = useMediaApi()
|
|
17
49
|
const { t } = useI18n()
|
|
18
50
|
const toast = useToast()
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
51
|
+
const { tree, refresh: refreshTree } = useLibraryTree()
|
|
52
|
+
|
|
53
|
+
async function readSiblings(fetch: ApiDataContext['$authFetch'], signal: AbortSignal, folderId: string, id: string): Promise<SiblingsWindow> {
|
|
54
|
+
const current = order.value
|
|
55
|
+
const page = (offset: number) =>
|
|
56
|
+
fetch<AssetPage>(MEDIA_READS.folderAssets(folderId), {
|
|
57
|
+
query: { ...current, sort: current.sort ?? DEFAULT_SIBLINGS_SORT, offset, limit: SIBLINGS_PAGE_SIZE },
|
|
58
|
+
signal,
|
|
59
|
+
})
|
|
60
|
+
let offset = 0
|
|
61
|
+
let found = await page(offset)
|
|
62
|
+
const holds = (candidate: AssetPage) => candidate.assets.some((entry) => entry.id === id)
|
|
63
|
+
while (!holds(found) && found.assets.length > 0 && offset + found.assets.length < found.total) {
|
|
64
|
+
offset += found.assets.length
|
|
65
|
+
found = await page(offset)
|
|
66
|
+
}
|
|
67
|
+
const position = found.assets.findIndex((entry) => entry.id === id)
|
|
68
|
+
const isInFirstHalf = offset === 0 && position < SIBLINGS_HALF_WINDOW
|
|
69
|
+
const centered = position < 0 || isInFirstHalf ? offset : Math.max(0, offset + position - SIBLINGS_HALF_WINDOW)
|
|
70
|
+
if (centered !== offset) found = await page(centered)
|
|
71
|
+
return { folderId, order: orderKey(current), assets: found.assets, offset: centered, total: found.total }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const read: ApiDataResult<FileRead> = useApiData<FileRead>(
|
|
75
|
+
() => (assetId.value ? `dms-media:file:${assetId.value}:${orderKey(order.value)}` : null),
|
|
76
|
+
async ({ $authFetch, signal }): Promise<FileRead> => {
|
|
77
|
+
const id = assetId.value!
|
|
78
|
+
const details = await $authFetch<AssetDetails>(MEDIA_READS.details(id), { signal })
|
|
79
|
+
// The neighbours last read, by this key or the previous file's.
|
|
80
|
+
const previous = read.data.value?.siblings
|
|
81
|
+
const keepsWindow =
|
|
82
|
+
previous !== undefined &&
|
|
83
|
+
!previous.isStale &&
|
|
84
|
+
previous.folderId === details.asset.folderId &&
|
|
85
|
+
previous.order === orderKey(order.value) &&
|
|
86
|
+
!isNearWindowEdge(previous, id)
|
|
87
|
+
return { details, siblings: keepsWindow ? previous : await readSiblings($authFetch, signal, details.asset.folderId, id) }
|
|
88
|
+
},
|
|
89
|
+
// Read again on each library change, its own trigger: the page's refreshes leave it alone.
|
|
90
|
+
{ refreshOnPageRefresh: false },
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
const error = computed(() => read.error.value)
|
|
94
|
+
/** A failed read shows no file, whatever it showed before. */
|
|
95
|
+
const details = computed(() => (error.value ? null : (read.data.value?.details ?? null)))
|
|
96
|
+
/** The file shown was deleted: it is gone on the next read, not a link to a file that never was. */
|
|
97
|
+
const isDeleted = computed(
|
|
98
|
+
() => isNotFoundError(error.value) && read.data.value?.details.asset.id === assetId.value,
|
|
99
|
+
)
|
|
100
|
+
const siblingsWindow = computed(() => read.data.value?.siblings)
|
|
101
|
+
const siblings = computed(() => {
|
|
102
|
+
const list = siblingsWindow.value?.assets ?? []
|
|
103
|
+
return isDeleted.value ? list.filter((entry) => entry.id !== assetId.value) : list
|
|
104
|
+
})
|
|
105
|
+
const isLoading = computed(() => read.pending.value)
|
|
106
|
+
/** The folder of the file last read, kept once it is gone. */
|
|
107
|
+
const lastFolderId = computed(() => read.data.value?.details.asset.folderId ?? null)
|
|
28
108
|
|
|
29
109
|
const asset = computed(() => details.value?.asset)
|
|
30
110
|
const localIndex = computed(() => siblings.value.findIndex((entry) => entry.id === assetId.value))
|
|
31
|
-
const index = computed(() => (localIndex.value < 0 ? -1 :
|
|
32
|
-
const total = computed(() =>
|
|
111
|
+
const index = computed(() => (localIndex.value < 0 ? -1 : (siblingsWindow.value?.offset ?? 0) + localIndex.value))
|
|
112
|
+
const total = computed(() => siblingsWindow.value?.total ?? 0)
|
|
33
113
|
const previous = computed(() => (localIndex.value > 0 ? siblings.value[localIndex.value - 1] : undefined))
|
|
34
114
|
const next = computed(() =>
|
|
35
115
|
localIndex.value >= 0 && localIndex.value < siblings.value.length - 1 ? siblings.value[localIndex.value + 1] : undefined,
|
|
@@ -39,72 +119,28 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
39
119
|
() => Boolean(details.value?.folder.rights.manage) && Boolean(tree.value?.isOwner),
|
|
40
120
|
)
|
|
41
121
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
/** The page of the folder holding the file, re-read around it when it sits near an end. */
|
|
52
|
-
async function loadSiblings(folderId: string, id: string): Promise<void> {
|
|
53
|
-
let offset = 0
|
|
54
|
-
let page = await fetchSiblings(folderId, offset)
|
|
55
|
-
const holds = (candidate: AssetPage) => candidate.assets.some((entry) => entry.id === id)
|
|
56
|
-
while (!holds(page) && page.assets.length > 0 && offset + page.assets.length < page.total) {
|
|
57
|
-
offset += page.assets.length
|
|
58
|
-
page = await fetchSiblings(folderId, offset)
|
|
59
|
-
}
|
|
60
|
-
const found = page.assets.findIndex((entry) => entry.id === id)
|
|
61
|
-
const isInFirstHalf = offset === 0 && found < SIBLINGS_HALF_WINDOW
|
|
62
|
-
const centered = found < 0 || isInFirstHalf ? offset : Math.max(0, offset + found - SIBLINGS_HALF_WINDOW)
|
|
63
|
-
if (centered !== offset) page = await fetchSiblings(folderId, centered)
|
|
64
|
-
siblings.value = page.assets
|
|
65
|
-
siblingsOffset.value = centered
|
|
66
|
-
siblingsTotal.value = page.total
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function isNearWindowEdge(id: string): boolean {
|
|
70
|
-
const position = siblings.value.findIndex((entry) => entry.id === id)
|
|
71
|
-
if (position < 0) return true
|
|
72
|
-
const hasBefore = siblingsOffset.value > 0
|
|
73
|
-
const hasAfter = siblingsOffset.value + siblings.value.length < siblingsTotal.value
|
|
74
|
-
return (hasBefore && position < SIBLINGS_EDGE_MARGIN) || (hasAfter && position >= siblings.value.length - SIBLINGS_EDGE_MARGIN)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function markUnavailable(cause: unknown): void {
|
|
78
|
-
const wasShown = details.value?.asset.id === assetId.value
|
|
79
|
-
if (wasShown && isNotFoundError(cause)) {
|
|
80
|
-
isDeleted.value = true
|
|
81
|
-
siblings.value = siblings.value.filter((entry) => entry.id !== assetId.value)
|
|
82
|
-
}
|
|
83
|
-
details.value = null
|
|
84
|
-
error.value = cause
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async function load(id: string, refreshSiblings = false): Promise<void> {
|
|
88
|
-
isLoading.value = true
|
|
89
|
-
try {
|
|
90
|
-
const loaded = await api.details(id)
|
|
91
|
-
const folderChanged = loaded.asset.folderId !== details.value?.asset.folderId
|
|
92
|
-
details.value = loaded
|
|
93
|
-
lastFolderId.value = loaded.asset.folderId
|
|
94
|
-
error.value = null
|
|
95
|
-
isDeleted.value = false
|
|
96
|
-
if (refreshSiblings || folderChanged || isNearWindowEdge(id)) await loadSiblings(loaded.asset.folderId, id)
|
|
97
|
-
} catch (cause) {
|
|
98
|
-
markUnavailable(cause)
|
|
99
|
-
} finally {
|
|
100
|
-
isLoading.value = false
|
|
101
|
-
}
|
|
122
|
+
/**
|
|
123
|
+
* Reads the file again; with `rereadSiblings`, its neighbours too. The
|
|
124
|
+
* blocks asking at once share one request.
|
|
125
|
+
*/
|
|
126
|
+
function load(rereadSiblings = false): Promise<void> {
|
|
127
|
+
const shown = read.data.value
|
|
128
|
+
if (rereadSiblings && shown) read.data.value = { ...shown, siblings: { ...shown.siblings, isStale: true } }
|
|
129
|
+
return read.refresh({ dedupe: 'defer' })
|
|
102
130
|
}
|
|
103
131
|
|
|
132
|
+
/** Shows a write at once, before the read that confirms it. */
|
|
104
133
|
function patch(changes: Partial<MediaAsset>): void {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
134
|
+
const shown = read.data.value
|
|
135
|
+
if (!shown || error.value) return
|
|
136
|
+
const id = shown.details.asset.id
|
|
137
|
+
read.data.value = {
|
|
138
|
+
details: { ...shown.details, asset: { ...shown.details.asset, ...changes } },
|
|
139
|
+
siblings: {
|
|
140
|
+
...shown.siblings,
|
|
141
|
+
assets: shown.siblings.assets.map((entry) => (entry.id === id ? { ...entry, ...changes } : entry)),
|
|
142
|
+
},
|
|
143
|
+
}
|
|
108
144
|
}
|
|
109
145
|
|
|
110
146
|
async function rename(name: string): Promise<boolean> {
|
|
@@ -113,7 +149,7 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
113
149
|
patch({ name })
|
|
114
150
|
try {
|
|
115
151
|
await api.updateAsset(current.id, { name })
|
|
116
|
-
await load(
|
|
152
|
+
await load(true)
|
|
117
153
|
return true
|
|
118
154
|
} catch (cause) {
|
|
119
155
|
patch({ name: current.name })
|
|
@@ -139,7 +175,7 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
139
175
|
try {
|
|
140
176
|
const outcome = await api.setAssetsVisibility([id], visibility)
|
|
141
177
|
if (outcome.refused.length) throw { data: { message: outcome.refused[0]?.message } }
|
|
142
|
-
await load(
|
|
178
|
+
await load()
|
|
143
179
|
return true
|
|
144
180
|
} catch (cause) {
|
|
145
181
|
useApiError(cause, { title: t('dms_media.toasts.visibility_failed') })
|
|
@@ -165,7 +201,7 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
165
201
|
try {
|
|
166
202
|
const outcome = await api.moveAssets([current.id], folderId)
|
|
167
203
|
if (outcome.refused.length) throw { data: { message: outcome.refused[0]?.message } }
|
|
168
|
-
await load(
|
|
204
|
+
await load()
|
|
169
205
|
toast.add({ title: t('dms_media.toasts.moved', { count: 1 }, 1), color: 'success' })
|
|
170
206
|
} catch (cause) {
|
|
171
207
|
useApiError(cause, { title: t('dms_media.toasts.move_failed') })
|
|
@@ -187,17 +223,9 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
187
223
|
}
|
|
188
224
|
|
|
189
225
|
function refresh(): void {
|
|
190
|
-
if (assetId.value) void load(
|
|
226
|
+
if (assetId.value) void load(true)
|
|
191
227
|
}
|
|
192
228
|
|
|
193
|
-
watch(
|
|
194
|
-
assetId,
|
|
195
|
-
(id) => {
|
|
196
|
-
if (id) void load(id)
|
|
197
|
-
},
|
|
198
|
-
{ immediate: true },
|
|
199
|
-
)
|
|
200
|
-
watch(() => JSON.stringify(order.value), refresh)
|
|
201
229
|
onLibraryChange(refresh)
|
|
202
230
|
|
|
203
231
|
return {
|
|
@@ -215,7 +243,6 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
215
243
|
lastFolderId,
|
|
216
244
|
canWrite,
|
|
217
245
|
canChangeVisibility,
|
|
218
|
-
load,
|
|
219
246
|
refresh,
|
|
220
247
|
refreshTree,
|
|
221
248
|
rename,
|
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AssetDetails,
|
|
3
|
-
AssetPage,
|
|
4
3
|
AssetVisibility,
|
|
5
|
-
BoundFolderAnswer,
|
|
6
4
|
BulkOutcome,
|
|
7
5
|
FolderImpact,
|
|
8
6
|
FolderVisibility,
|
|
9
|
-
ListingParams,
|
|
10
7
|
MediaAsset,
|
|
11
|
-
MediaTree,
|
|
12
|
-
PresetsResponse,
|
|
13
|
-
SearchParams,
|
|
14
8
|
SmartView,
|
|
15
9
|
} from '../types/media'
|
|
16
10
|
|
|
@@ -63,34 +57,35 @@ export interface ReadUrl {
|
|
|
63
57
|
expiresAt: number
|
|
64
58
|
}
|
|
65
59
|
|
|
66
|
-
/**
|
|
60
|
+
/** The URLs of the reads the pages render, which `useApiData` reads and shares by URL. */
|
|
61
|
+
export const MEDIA_READS = {
|
|
62
|
+
tree: `${API}/tree`,
|
|
63
|
+
recent: `${API}/stats/recent`,
|
|
64
|
+
presets: `${API}/presets`,
|
|
65
|
+
boundFolder: (bindingId: string) => `${API}/bindings/${encodeURIComponent(bindingId)}/folder`,
|
|
66
|
+
asset: (assetId: string) => `${API}/assets/${assetId}`,
|
|
67
|
+
details: (assetId: string) => `${API}/assets/${assetId}/details`,
|
|
68
|
+
readUrl: (assetId: string) => `${API}/assets/${assetId}/read-url`,
|
|
69
|
+
folderAssets: (folderId: string) => `${API}/folders/${folderId}/assets`,
|
|
70
|
+
smartView: (view: SmartView) => `${API}/views/${view}`,
|
|
71
|
+
search: `${API}/search`,
|
|
72
|
+
} as const
|
|
73
|
+
|
|
74
|
+
/** Typed calls to the media API, through the authenticated DMS fetch: the writes, and the reads a click or a viewer makes itself. */
|
|
67
75
|
export function useMediaApi() {
|
|
68
76
|
const { $authFetch } = useAuthFetch()
|
|
69
77
|
const post = <T>(path: string, body?: object) =>
|
|
70
78
|
$authFetch<T>(`${API}${path}`, { method: 'POST', body })
|
|
71
79
|
|
|
72
80
|
return {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
boundFolder: (bindingId: string) =>
|
|
76
|
-
$authFetch<BoundFolderAnswer>(`${API}/bindings/${encodeURIComponent(bindingId)}/folder`),
|
|
77
|
-
folderAssets: (folderId: string, query: ListingParams = {}) =>
|
|
78
|
-
$authFetch<AssetPage>(`${API}/folders/${folderId}/assets`, { query }),
|
|
79
|
-
smartView: (view: SmartView, query: ListingParams = {}) =>
|
|
80
|
-
$authFetch<AssetPage>(`${API}/views/${view}`, { query }),
|
|
81
|
-
search: (query: SearchParams) =>
|
|
82
|
-
$authFetch<AssetPage>(`${API}/search`, { query }),
|
|
83
|
-
asset: (assetId: string) =>
|
|
84
|
-
$authFetch<{ asset: MediaAsset }>(`${API}/assets/${assetId}`),
|
|
85
|
-
details: (assetId: string) =>
|
|
86
|
-
$authFetch<AssetDetails>(`${API}/assets/${assetId}/details`),
|
|
81
|
+
asset: (assetId: string) => $authFetch<{ asset: MediaAsset }>(MEDIA_READS.asset(assetId)),
|
|
82
|
+
details: (assetId: string) => $authFetch<AssetDetails>(MEDIA_READS.details(assetId)),
|
|
87
83
|
previews: (ids: string[]) =>
|
|
88
84
|
post<{ previews: Record<string, string> }>('/previews', { ids }),
|
|
89
85
|
readUrl: (assetId: string, preset?: string) =>
|
|
90
|
-
$authFetch<ReadUrl>(
|
|
86
|
+
$authFetch<ReadUrl>(MEDIA_READS.readUrl(assetId), {
|
|
91
87
|
query: preset ? { preset } : {},
|
|
92
88
|
}),
|
|
93
|
-
recent: () => $authFetch<{ assets: MediaAsset[] }>(`${API}/stats/recent`),
|
|
94
89
|
createFolder: (name: string, parentId?: string) =>
|
|
95
90
|
post<{ id: string }>('/folders', { name, parentId }),
|
|
96
91
|
createStarterFolders: (folders: StarterFolder[]) =>
|
|
@@ -132,7 +127,6 @@ export function useMediaApi() {
|
|
|
132
127
|
post<{ asset: MediaAsset }>(`/assets/${assetId}/revert`),
|
|
133
128
|
star: (kind: 'asset' | 'folder', targetId: string, starred: boolean) =>
|
|
134
129
|
post('/favorites', { kind, targetId, starred }),
|
|
135
|
-
presets: () => $authFetch<PresetsResponse>(`${API}/presets`),
|
|
136
130
|
}
|
|
137
131
|
}
|
|
138
132
|
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"main": "./dms.frontend.ts",
|
|
7
7
|
"packageManager": "pnpm@10.6.5",
|
|
8
8
|
"engines": {
|
|
9
|
-
"@antelopejs/dms-frontend": ">=0.
|
|
9
|
+
"@antelopejs/dms-frontend": ">=0.6.0 <0.7.0"
|
|
10
10
|
},
|
|
11
11
|
"scripts": {
|
|
12
12
|
"dev": "pnpm --dir ../playground frontend:dev",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@nuxt/ui": "4.10.0",
|
|
30
30
|
"@typescript-eslint/parser": "^8.0.0",
|
|
31
|
+
"@vitejs/plugin-vue": "^6.0.10",
|
|
31
32
|
"eslint": "^9.0.0",
|
|
32
33
|
"eslint-plugin-vue": "^10.0.0",
|
|
33
34
|
"prettier": "^3.5.3",
|
|
@@ -33,6 +33,9 @@ importers:
|
|
|
33
33
|
'@typescript-eslint/parser':
|
|
34
34
|
specifier: ^8.0.0
|
|
35
35
|
version: 8.65.0(eslint@9.39.5(jiti@2.7.0))(typescript@6.0.3)
|
|
36
|
+
'@vitejs/plugin-vue':
|
|
37
|
+
specifier: ^6.0.10
|
|
38
|
+
version: 6.0.10(vite@7.3.6(jiti@2.7.0)(lightningcss@1.33.0)(terser@5.49.0)(yaml@2.9.0))(vue@3.5.40(typescript@6.0.3))
|
|
36
39
|
eslint:
|
|
37
40
|
specifier: ^9.0.0
|
|
38
41
|
version: 9.39.5(jiti@2.7.0)
|
|
@@ -1383,6 +1386,13 @@ packages:
|
|
|
1383
1386
|
peerDependencies:
|
|
1384
1387
|
vue: '>=3.5.18'
|
|
1385
1388
|
|
|
1389
|
+
'@vitejs/plugin-vue@6.0.10':
|
|
1390
|
+
resolution: {integrity: sha512-b80aUQh3n+qEzzZm5oQp3rT2QImeCx58K6eFl/RkJ3d65KW0kTuv5lNsLRVAjbW9YRUpo/wzmvzMgV34bTAItA==}
|
|
1391
|
+
engines: {node: ^20.19.0 || >=22.12.0}
|
|
1392
|
+
peerDependencies:
|
|
1393
|
+
vite: ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0
|
|
1394
|
+
vue: ^3.2.25 || ^3.6.0-rc.10
|
|
1395
|
+
|
|
1386
1396
|
'@vitest/expect@3.2.7':
|
|
1387
1397
|
resolution: {integrity: sha512-E8eBXaKibuvH2pSZErOjdVb5vF4PbKYcrnluBTYxEk1l/VhhwZg1kZQsdtjq+CsF5CFydf2Rdkz7jDHKSisi3w==}
|
|
1388
1398
|
|
|
@@ -3846,8 +3856,7 @@ snapshots:
|
|
|
3846
3856
|
'@rolldown/binding-win32-x64-msvc@1.2.0':
|
|
3847
3857
|
optional: true
|
|
3848
3858
|
|
|
3849
|
-
'@rolldown/pluginutils@1.0.1':
|
|
3850
|
-
optional: true
|
|
3859
|
+
'@rolldown/pluginutils@1.0.1': {}
|
|
3851
3860
|
|
|
3852
3861
|
'@rollup/rollup-android-arm-eabi@4.62.2':
|
|
3853
3862
|
optional: true
|
|
@@ -4355,6 +4364,12 @@ snapshots:
|
|
|
4355
4364
|
unhead: 2.1.16
|
|
4356
4365
|
vue: 3.5.40(typescript@6.0.3)
|
|
4357
4366
|
|
|
4367
|
+
'@vitejs/plugin-vue@6.0.10(vite@7.3.6(jiti@2.7.0)(lightningcss@1.33.0)(terser@5.49.0)(yaml@2.9.0))(vue@3.5.40(typescript@6.0.3))':
|
|
4368
|
+
dependencies:
|
|
4369
|
+
'@rolldown/pluginutils': 1.0.1
|
|
4370
|
+
vite: 7.3.6(jiti@2.7.0)(lightningcss@1.33.0)(terser@5.49.0)(yaml@2.9.0)
|
|
4371
|
+
vue: 3.5.40(typescript@6.0.3)
|
|
4372
|
+
|
|
4358
4373
|
'@vitest/expect@3.2.7':
|
|
4359
4374
|
dependencies:
|
|
4360
4375
|
'@types/chai': 5.2.3
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { describe, expect, it } from 'vitest'
|
|
4
|
+
|
|
5
|
+
type Manifest = { engines?: Record<string, string> }
|
|
6
|
+
|
|
7
|
+
function rendererRange(dir: string): string | undefined {
|
|
8
|
+
const manifest = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')) as Manifest
|
|
9
|
+
return manifest.engines?.['@antelopejs/dms-frontend']
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// `ajs dms dev` refuses to start when a frontend module's range excludes the
|
|
13
|
+
// installed renderer, so the playground's own frontend must follow this layer.
|
|
14
|
+
describe('renderer range', () => {
|
|
15
|
+
it('is the same for the playground frontend as for this layer', () => {
|
|
16
|
+
const layer = rendererRange(join(__dirname, '..'))
|
|
17
|
+
expect(layer).toBeDefined()
|
|
18
|
+
expect(rendererRange(join(__dirname, '../../playground/frontend-vue'))).toBe(layer)
|
|
19
|
+
})
|
|
20
|
+
})
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// What the renderer imports of Inertia, which a server render of a component
|
|
2
|
+
// does not reach: the browser's page, links and visits.
|
|
3
|
+
export const Link = {}
|
|
4
|
+
export const router = {
|
|
5
|
+
visit: () => undefined,
|
|
6
|
+
on: () => () => undefined,
|
|
7
|
+
}
|
|
8
|
+
export const usePage = () => ({ url: undefined })
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server renders of the module's blocks on the renderer's real data layer
|
|
3
|
+
* (`#dms/frontend-module`) and the DMS's real `useApiData`, as the generated
|
|
4
|
+
* application runs them: what each render reads from the backend, and what it
|
|
5
|
+
* hands the browser with the page.
|
|
6
|
+
*/
|
|
7
|
+
import * as vue from 'vue'
|
|
8
|
+
import { createSSRApp, h, type Component } from 'vue'
|
|
9
|
+
import { renderToString } from 'vue/server-renderer'
|
|
10
|
+
import { vi } from 'vitest'
|
|
11
|
+
import type { DmsPayload } from '#dms/frontend-module'
|
|
12
|
+
|
|
13
|
+
const engine = await import('#dms/frontend-module')
|
|
14
|
+
|
|
15
|
+
/** A backend request a render made. */
|
|
16
|
+
export interface BackendRead {
|
|
17
|
+
url: string
|
|
18
|
+
method: string
|
|
19
|
+
query: Record<string, unknown>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type Backend = (read: BackendRead) => unknown
|
|
23
|
+
|
|
24
|
+
let backend: Backend = () => ({})
|
|
25
|
+
const reads: BackendRead[] = []
|
|
26
|
+
|
|
27
|
+
/** The query a request was given, as the renderer keys its response: none when it had none. */
|
|
28
|
+
const givenQueries = new WeakMap<BackendRead, Record<string, unknown> | undefined>()
|
|
29
|
+
|
|
30
|
+
const $authFetch = async (url: string, options: { method?: string; query?: Record<string, unknown> } = {}) => {
|
|
31
|
+
const read = { url, method: (options.method ?? 'GET').toUpperCase(), query: options.query ?? {} }
|
|
32
|
+
givenQueries.set(read, options.query)
|
|
33
|
+
reads.push(read)
|
|
34
|
+
return backend(read)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const noop = () => undefined
|
|
38
|
+
|
|
39
|
+
/** The auto-imports, which templates reach through the application's global properties. */
|
|
40
|
+
const autoImports: Record<string, unknown> = {}
|
|
41
|
+
function provideGlobal(name: string, value: unknown): void {
|
|
42
|
+
autoImports[name] = value
|
|
43
|
+
vi.stubGlobal(name, value)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// The application's auto-imports: Vue's API, the renderer's, the DMS layers'
|
|
47
|
+
// the module calls, and the module's own composables and utilities.
|
|
48
|
+
for (const [name, value] of Object.entries(vue)) if (/^[a-z]/.test(name)) vi.stubGlobal(name, value)
|
|
49
|
+
for (const name of ['useDmsFetch', 'useDmsAsyncData', 'useDmsPagedData', 'useDmsState', 'useDmsRoute', 'useDmsRouter'] as const)
|
|
50
|
+
provideGlobal(name, engine[name])
|
|
51
|
+
const { useApiData, useApiPagedData } = await import('#dms-core/app/composables/auth/useApiData')
|
|
52
|
+
provideGlobal('useApiData', useApiData)
|
|
53
|
+
provideGlobal('useApiPagedData', useApiPagedData)
|
|
54
|
+
provideGlobal('useAuthFetch', () => ({ $authFetch }))
|
|
55
|
+
provideGlobal('useI18n', () => ({
|
|
56
|
+
t: (key: string, params?: Record<string, unknown>) => (params ? `${key} ${Object.values(params).join(' ')}` : key),
|
|
57
|
+
te: () => false,
|
|
58
|
+
locale: vue.ref('en'),
|
|
59
|
+
}))
|
|
60
|
+
provideGlobal('useRegionalFormat', () => ({ formatNumber: (value: number) => String(value) }))
|
|
61
|
+
provideGlobal('useToast', () => ({ add: noop }))
|
|
62
|
+
provideGlobal('useConfirm', () => ({ confirm: async () => false }))
|
|
63
|
+
provideGlobal('useOverlay', () => ({ create: () => ({ open: async () => undefined }) }))
|
|
64
|
+
provideGlobal('useApiError', noop)
|
|
65
|
+
provideGlobal('useUnsavedChanges', noop)
|
|
66
|
+
provideGlobal('usePageLiveRefresh', noop)
|
|
67
|
+
provideGlobal('defineShortcuts', noop)
|
|
68
|
+
const keyboard = await import('#dms-ui/app/composables/global/keyboardPlatform')
|
|
69
|
+
// The labels a server render prints before the browser names its platform.
|
|
70
|
+
provideGlobal('useKeyboardPlatform', () => ({
|
|
71
|
+
keyLabel: (key: string) => keyboard.keyboardKeyLabel(key, keyboard.DEFAULT_KEYBOARD_PLATFORM),
|
|
72
|
+
formatShortcut: (keys: readonly string[]) => keyboard.formatKeyboardShortcut(keys, keyboard.DEFAULT_KEYBOARD_PLATFORM),
|
|
73
|
+
}))
|
|
74
|
+
provideGlobal('navigateDms', noop)
|
|
75
|
+
for (const util of [await import('#dms-core/app/utils/http-status'), await import('#dms-core/app/composables/useFieldErrors')])
|
|
76
|
+
for (const [name, value] of Object.entries(util)) provideGlobal(name, value)
|
|
77
|
+
const own = import.meta.glob<Record<string, unknown>>(['../../app/composables/**/*.ts', '../../app/utils/**/*.ts'])
|
|
78
|
+
for (const load of Object.values(own))
|
|
79
|
+
for (const [name, value] of Object.entries(await load())) provideGlobal(name, value)
|
|
80
|
+
|
|
81
|
+
// The DMS's and Nuxt UI's components the module's templates use: each renders
|
|
82
|
+
// its default slot, its props as attributes.
|
|
83
|
+
const UI_COMPONENTS = [
|
|
84
|
+
'DmsCard',
|
|
85
|
+
'DmsCopyButton',
|
|
86
|
+
'DmsEmptyState',
|
|
87
|
+
'DmsEyebrow',
|
|
88
|
+
'DmsFieldRow',
|
|
89
|
+
'DmsIconWell',
|
|
90
|
+
'DmsSaveStatus',
|
|
91
|
+
'DmsTextarea',
|
|
92
|
+
'UBadge',
|
|
93
|
+
'UButton',
|
|
94
|
+
'UFormField',
|
|
95
|
+
'UIcon',
|
|
96
|
+
'UInput',
|
|
97
|
+
'UKbd',
|
|
98
|
+
'ULink',
|
|
99
|
+
'USkeleton',
|
|
100
|
+
'UTooltip',
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
const uiStub = (name: string): Component =>
|
|
104
|
+
vue.defineComponent({
|
|
105
|
+
name,
|
|
106
|
+
inheritAttrs: false,
|
|
107
|
+
setup:
|
|
108
|
+
(_, { slots, attrs }) =>
|
|
109
|
+
() =>
|
|
110
|
+
h(
|
|
111
|
+
'div',
|
|
112
|
+
{
|
|
113
|
+
'data-stub': name,
|
|
114
|
+
...Object.fromEntries(
|
|
115
|
+
Object.entries(attrs).filter(([, value]) => ['string', 'number', 'boolean'].includes(typeof value)),
|
|
116
|
+
),
|
|
117
|
+
},
|
|
118
|
+
// Every slot, named ones included: a field's trailing hint renders with it.
|
|
119
|
+
Object.values(slots).flatMap((slot) => slot?.() ?? []),
|
|
120
|
+
),
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
export interface ServerRender {
|
|
124
|
+
html: string
|
|
125
|
+
payload: DmsPayload
|
|
126
|
+
/** Every backend request the render made, as `METHOD url?query`. */
|
|
127
|
+
reads: string[]
|
|
128
|
+
/**
|
|
129
|
+
* What the payload hands the browser, by key: a `useApiData(url)` read as
|
|
130
|
+
* `METHOD url?query`, a handler's read by its own key.
|
|
131
|
+
*/
|
|
132
|
+
handed: string[]
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const describeRead = (method: string, url: string, query: Record<string, unknown> | null) => {
|
|
136
|
+
const sent = Object.entries(query ?? {}).filter(([, value]) => value !== undefined)
|
|
137
|
+
const search = new URLSearchParams(sent.map(([name, value]) => [name, String(value)])).toString()
|
|
138
|
+
return `${method} ${url}${search ? `?${search}` : ''}`
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The payload key a `useApiData(url)` read's response is handed over under. */
|
|
142
|
+
const fetchKey = (read: BackendRead) => engine.dmsRequestKey(read.url, { method: read.method, query: givenQueries.get(read) })
|
|
143
|
+
|
|
144
|
+
/** A payload key, as `describeRead` writes the request it answers for `$dmsfetch` keys. */
|
|
145
|
+
function handedKey(key: string, made: BackendRead[]): string {
|
|
146
|
+
const read = made.find((candidate) => fetchKey(candidate) === key)
|
|
147
|
+
return read ? describeRead(read.method, read.url, read.query) : key
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Renders `component` with `props` as a server renders the page `url`,
|
|
152
|
+
* answering each backend request with `respond`.
|
|
153
|
+
*/
|
|
154
|
+
export async function serverRender(
|
|
155
|
+
component: Component,
|
|
156
|
+
props: Record<string, unknown> = {},
|
|
157
|
+
options: { url?: string; respond?: Backend } = {},
|
|
158
|
+
): Promise<ServerRender> {
|
|
159
|
+
backend = options.respond ?? (() => ({}))
|
|
160
|
+
reads.length = 0
|
|
161
|
+
const runtime = engine.createDmsFrontendRuntime(undefined, {}, true)
|
|
162
|
+
runtime.route.fullPath = options.url ?? '/'
|
|
163
|
+
const app = createSSRApp({ render: () => h(component, props) })
|
|
164
|
+
engine.provideDmsFrontendRuntime(app, runtime)
|
|
165
|
+
for (const name of UI_COMPONENTS) app.component(name, uiStub(name))
|
|
166
|
+
Object.assign(app.config.globalProperties, autoImports)
|
|
167
|
+
app.config.warnHandler = noop
|
|
168
|
+
const html = await renderToString(app)
|
|
169
|
+
const payload = engine.serializeDmsPayload(runtime)
|
|
170
|
+
return {
|
|
171
|
+
html,
|
|
172
|
+
payload,
|
|
173
|
+
reads: reads.map((read) => describeRead(read.method, read.url, read.query)),
|
|
174
|
+
handed: Object.keys({ ...payload.data, ...payload.errors }).map((key) => handedKey(key, reads)),
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** What `inBrowserApp` gives back: what `setup` returned, and the backend requests made so far. */
|
|
179
|
+
export interface BrowserApp<T> {
|
|
180
|
+
result: T
|
|
181
|
+
/** Every backend request made so far, as `METHOD url?query`. */
|
|
182
|
+
reads: () => string[]
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Runs `setup` as a browser application runs a call outside any component, on
|
|
187
|
+
* the renderer's real data layer, answering each backend request with
|
|
188
|
+
* `respond`.
|
|
189
|
+
*/
|
|
190
|
+
export function inBrowserApp<T>(setup: () => T, respond: Backend = () => ({})): BrowserApp<T> {
|
|
191
|
+
backend = respond
|
|
192
|
+
reads.length = 0
|
|
193
|
+
const runtime = engine.createDmsFrontendRuntime(undefined, {}, false)
|
|
194
|
+
const app = vue.createApp({ render: () => null })
|
|
195
|
+
engine.provideDmsFrontendRuntime(app, runtime)
|
|
196
|
+
return {
|
|
197
|
+
result: app.runWithContext(setup),
|
|
198
|
+
reads: () => reads.map((read) => describeRead(read.method, read.url, read.query)),
|
|
199
|
+
}
|
|
200
|
+
}
|