forked from lightningpixel/modly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassetLibraryUi.ts
More file actions
250 lines (211 loc) · 10.7 KB
/
Copy pathassetLibraryUi.ts
File metadata and controls
250 lines (211 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import type { GenerationJob } from '../../shared/stores/appStore'
import type { AssetLibraryOpenRequest } from '../../shared/types/assetLibrary'
import { resolveAssetLibraryOpenTarget, type AssetLibraryOpenTarget, type ProjectedAssetLibraryEntry } from './assetLibraryProjection'
export type GenerateOpenPanel = 'export' | 'decimate' | 'smooth' | 'import' | 'library' | 'light' | null
export type AssetLibrarySortMode = 'type' | 'name' | 'date'
export interface AssetLibraryEntryGroup {
capability: NonNullable<ProjectedAssetLibraryEntry['capability']>
capabilityLabel: string
sectionKey: string
entries: ProjectedAssetLibraryEntry[]
}
export interface AssetLibrarySourceScopeGroup {
sourceScope: ProjectedAssetLibraryEntry['sourceScope']
sourceScopeLabel: string
sectionKey: string
entryGroups: AssetLibraryEntryGroup[]
}
export interface AssetLibraryOpenSelection {
historyUrl: string
job: GenerationJob
}
const ASSET_LIBRARY_CAPABILITY_SECTIONS = [
{ capability: 'mesh', label: 'Mesh' },
{ capability: 'rigged-mesh', label: 'Rigged mesh' },
{ capability: 'animation-motion', label: 'Animations/motions' },
{ capability: 'landmarks-sidecar', label: 'Landmarks sidecars' },
{ capability: 'generated-world', label: 'Generated worlds' },
{ capability: 'scene-manifest', label: 'Scene manifests' },
] as const satisfies ReadonlyArray<{ capability: NonNullable<ProjectedAssetLibraryEntry['capability']>, label: string }>
const ASSET_LIBRARY_SOURCE_SCOPE_SECTIONS = [
{ sourceScope: 'workflows', label: 'Workflows' },
{ sourceScope: 'exports', label: 'Exports' },
] as const satisfies ReadonlyArray<{ sourceScope: ProjectedAssetLibraryEntry['sourceScope'], label: string }>
const ASSET_LIBRARY_CAPABILITY_ORDER = new Map(
ASSET_LIBRARY_CAPABILITY_SECTIONS.map((section, index) => [section.capability, index]),
)
const ASSET_LIBRARY_INTERNAL_DIRECTORY_NAMES = new Set(['tmp', 'temp', 'cache'])
export const ASSET_LIBRARY_SORT_OPTIONS = [
{ value: 'type', label: 'Type' },
{ value: 'name', label: 'Name' },
{ value: 'date', label: 'Date' },
] as const satisfies ReadonlyArray<{ value: AssetLibrarySortMode, label: string }>
export function getDefaultAssetLibraryCollapsedSectionKeys(): string[] {
const sectionKeys = ASSET_LIBRARY_SOURCE_SCOPE_SECTIONS.flatMap((scopeSection) => {
const capabilityKeys = ASSET_LIBRARY_CAPABILITY_SECTIONS.map(
(capabilitySection) => `capability:${scopeSection.sourceScope}:${capabilitySection.capability}`,
)
return [`scope:${scopeSection.sourceScope}`, ...capabilityKeys]
})
return [...sectionKeys]
}
export function toggleAssetLibrarySectionKey(currentKeys: string[], sectionKey: string): string[] {
return currentKeys.includes(sectionKey)
? currentKeys.filter((value) => value !== sectionKey)
: [...currentKeys, sectionKey]
}
export function buildAssetLibraryOpenRequest(entry: ProjectedAssetLibraryEntry): AssetLibraryOpenRequest {
const target = resolveAssetLibraryOpenTarget(entry)
return target.kind === 'linked-source'
? { workspacePath: target.workspacePath, sourceWorkspacePath: target.sourceWorkspacePath }
: { workspacePath: entry.workspacePath }
}
export function isAssetLibraryEntryOpenable(entry: ProjectedAssetLibraryEntry | null | undefined): entry is ProjectedAssetLibraryEntry {
return Boolean(entry && resolveAssetLibraryOpenTarget(entry).kind !== 'unavailable')
}
export function describeAssetLibraryOpenability(entry: ProjectedAssetLibraryEntry): string {
if (entry.state === 'unknown-metadata') return 'Missing metadata prevents a safe open in Generate.'
if (entry.state === 'unsupported') return 'This asset is tracked in the library but is not supported in Generate.'
if (entry.state === 'unsafe') return 'This asset was rejected because its workspace path is unsafe.'
const target = resolveAssetLibraryOpenTarget(entry)
if (target.kind === 'linked-source') return `Ready to open linked source ${entry.source?.displayName ?? target.sourceWorkspacePath} in Generate.`
if (target.kind === 'self') return 'Ready to open this asset directly in Generate.'
if (entry.nonOpenableReason) return entry.nonOpenableReason
if (!/\.(glb|gltf)$/i.test(entry.workspacePath)) return 'Only .glb/.gltf workspace assets are openable in this release.'
return target.reason
}
export function filterVisibleAssetLibraryEntries(entries: ProjectedAssetLibraryEntry[]): ProjectedAssetLibraryEntry[] {
return entries.filter((entry) => entry.state !== 'unsupported' && !hasInternalAssetLibraryDirectory(entry.workspacePath))
}
export function filterAssetLibraryScopeGroups(
entries: ProjectedAssetLibraryEntry[],
searchQuery: string,
sortMode: AssetLibrarySortMode,
): AssetLibrarySourceScopeGroup[] {
const normalizedSearchQuery = normalizeAssetLibrarySearchQuery(searchQuery)
const visibleEntries = filterVisibleAssetLibraryEntries(entries)
return ASSET_LIBRARY_SOURCE_SCOPE_SECTIONS
.map((scopeSection) => {
const scopeEntries = visibleEntries.filter((entry) => entry.sourceScope === scopeSection.sourceScope)
const scopeMatches = normalizedSearchQuery.length > 0 && matchesAssetLibrarySearch(scopeSection.label, normalizedSearchQuery)
const entryGroups = ASSET_LIBRARY_CAPABILITY_SECTIONS
.map((capabilitySection) => {
const capabilityEntries = scopeEntries.filter((entry) => entry.capability === capabilitySection.capability)
if (capabilityEntries.length === 0) return null
const capabilityMatches = scopeMatches || (normalizedSearchQuery.length > 0 && matchesAssetLibrarySearch(capabilitySection.label, normalizedSearchQuery))
const visibleCapabilityEntries = !normalizedSearchQuery || capabilityMatches
? capabilityEntries
: capabilityEntries.filter((entry) => matchesAssetLibraryEntrySearch(entry, normalizedSearchQuery))
if (visibleCapabilityEntries.length === 0) return null
return {
capability: capabilitySection.capability,
capabilityLabel: capabilitySection.label,
sectionKey: `capability:${scopeSection.sourceScope}:${capabilitySection.capability}`,
entries: sortAssetLibraryEntries(visibleCapabilityEntries, sortMode),
}
})
.filter((group): group is AssetLibraryEntryGroup => group !== null)
const sortedEntryGroups = sortMode === 'type'
? entryGroups
: [...entryGroups].sort((left, right) => compareAssetLibraryEntryGroups(left, right, sortMode))
if (sortedEntryGroups.length === 0) return null
return {
sourceScope: scopeSection.sourceScope,
sourceScopeLabel: scopeSection.label,
sectionKey: `scope:${scopeSection.sourceScope}`,
entryGroups: sortedEntryGroups,
}
})
.filter((group): group is AssetLibrarySourceScopeGroup => group !== null)
}
export function createAssetLibraryOpenJob(
entry: ProjectedAssetLibraryEntry,
target: AssetLibraryOpenTarget,
now = Date.now(),
): AssetLibraryOpenSelection | null {
if (target.kind === 'unavailable') return null
return {
historyUrl: target.url,
job: {
id: `library-${now}`,
imageFile: '',
status: 'done',
progress: 100,
outputUrl: target.url,
originalOutputUrl: target.url,
createdAt: now,
},
}
}
export function resolveOpenPanelAfterLibrarySelection(currentPanel: GenerateOpenPanel): GenerateOpenPanel {
return currentPanel === 'library' ? 'library' : currentPanel
}
function sortAssetLibraryEntries(
entries: ProjectedAssetLibraryEntry[],
sortMode: AssetLibrarySortMode,
): ProjectedAssetLibraryEntry[] {
return [...entries].sort((left, right) => compareAssetLibraryEntries(left, right, sortMode))
}
function compareAssetLibraryEntryGroups(
left: AssetLibraryEntryGroup,
right: AssetLibraryEntryGroup,
sortMode: Exclude<AssetLibrarySortMode, 'type'>,
): number {
const entryComparison = compareAssetLibraryEntries(left.entries[0], right.entries[0], sortMode)
if (entryComparison !== 0) return entryComparison
return (ASSET_LIBRARY_CAPABILITY_ORDER.get(left.capability) ?? Number.MAX_SAFE_INTEGER)
- (ASSET_LIBRARY_CAPABILITY_ORDER.get(right.capability) ?? Number.MAX_SAFE_INTEGER)
}
function compareAssetLibraryEntries(
left: ProjectedAssetLibraryEntry,
right: ProjectedAssetLibraryEntry,
sortMode: AssetLibrarySortMode,
): number {
if (sortMode === 'date') {
const leftTime = resolveAssetLibrarySortTimestamp(left)
const rightTime = resolveAssetLibrarySortTimestamp(right)
if (leftTime !== null && rightTime !== null && leftTime !== rightTime) return rightTime - leftTime
if (leftTime !== null && rightTime === null) return -1
if (leftTime === null && rightTime !== null) return 1
}
return compareAssetLibraryEntryNames(left, right)
}
function compareAssetLibraryEntryNames(left: ProjectedAssetLibraryEntry, right: ProjectedAssetLibraryEntry): number {
const displayNameComparison = left.displayName.localeCompare(right.displayName, undefined, { sensitivity: 'base' })
if (displayNameComparison !== 0) return displayNameComparison
const workspacePathComparison = left.workspacePath.localeCompare(right.workspacePath, undefined, { sensitivity: 'base' })
if (workspacePathComparison !== 0) return workspacePathComparison
return left.id.localeCompare(right.id, undefined, { sensitivity: 'base' })
}
function resolveAssetLibrarySortTimestamp(entry: ProjectedAssetLibraryEntry): number | null {
return parseAssetLibrarySortTimestamp(entry.createdAt) ?? parseAssetLibrarySortTimestamp(entry.updatedAt)
}
function parseAssetLibrarySortTimestamp(value: string | undefined): number | null {
if (typeof value !== 'string' || value.length === 0) return null
const epochMs = Date.parse(value)
return Number.isFinite(epochMs) ? epochMs : null
}
function normalizeAssetLibrarySearchQuery(searchQuery: string): string {
return searchQuery.trim().toLocaleLowerCase()
}
function matchesAssetLibraryEntrySearch(entry: ProjectedAssetLibraryEntry, normalizedSearchQuery: string): boolean {
return [
entry.displayName,
entry.workspacePath,
entry.source?.workspacePath,
entry.source?.displayName,
entry.manifest?.workspacePath,
entry.capability,
entry.sourceScope,
...entry.warnings,
]
.filter((value): value is string => typeof value === 'string' && value.length > 0)
.some((value) => matchesAssetLibrarySearch(value, normalizedSearchQuery))
}
function matchesAssetLibrarySearch(value: string, normalizedSearchQuery: string): boolean {
return value.toLocaleLowerCase().includes(normalizedSearchQuery)
}
function hasInternalAssetLibraryDirectory(workspacePath: string): boolean {
const segments = workspacePath.replace(/\\/g, '/').trim().split('/').filter(Boolean)
return segments.slice(1, -1).some((segment) => segment.startsWith('.') || ASSET_LIBRARY_INTERNAL_DIRECTORY_NAMES.has(segment.toLocaleLowerCase()))
}