feat(ui): add Monaco changes/files right drawer viewers
Use OpenCode v2 file APIs for browsing and Monaco DiffEditor for session snapshot diffs, with local baseline language metadata and optional CDN language loading.
This commit is contained in:
53
packages/ui/src/lib/monaco/model-cache.ts
Normal file
53
packages/ui/src/lib/monaco/model-cache.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
type MonacoApi = any
|
||||
|
||||
type CachedModel = {
|
||||
model: any
|
||||
}
|
||||
|
||||
const MAX_MODELS = 5
|
||||
|
||||
// LRU map: newest at the end.
|
||||
const models = new Map<string, CachedModel>()
|
||||
|
||||
function touch(key: string, entry: CachedModel) {
|
||||
models.delete(key)
|
||||
models.set(key, entry)
|
||||
}
|
||||
|
||||
function evictIfNeeded() {
|
||||
while (models.size > MAX_MODELS) {
|
||||
const oldestKey = models.keys().next().value as string | undefined
|
||||
if (!oldestKey) return
|
||||
const entry = models.get(oldestKey)
|
||||
models.delete(oldestKey)
|
||||
try {
|
||||
entry?.model.dispose()
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getOrCreateTextModel(params: {
|
||||
monaco: MonacoApi
|
||||
cacheKey: string
|
||||
value: string
|
||||
languageId: string
|
||||
}): any {
|
||||
const existing = models.get(params.cacheKey)
|
||||
if (existing) {
|
||||
touch(params.cacheKey, existing)
|
||||
if (existing.model.getValue() !== params.value) {
|
||||
existing.model.setValue(params.value)
|
||||
}
|
||||
return existing.model
|
||||
}
|
||||
|
||||
const uri = params.monaco.Uri.parse(`opencode://model/${encodeURIComponent(params.cacheKey)}`)
|
||||
// Create as plaintext. We'll set the final language after its contribution is loaded.
|
||||
const model = params.monaco.editor.createModel(params.value, "plaintext", uri)
|
||||
const entry = { model }
|
||||
models.set(params.cacheKey, entry)
|
||||
evictIfNeeded()
|
||||
return model
|
||||
}
|
||||
Reference in New Issue
Block a user