diff --git a/src/app/Utils.ts b/src/app/Utils.ts index 207f5067..43cc03bb 100644 --- a/src/app/Utils.ts +++ b/src/app/Utils.ts @@ -1,3 +1,4 @@ +import type { ExternalFileSystem } from '@spyglassmc/core' import * as zip from '@zip.js/zip.js' import type { Identifier, NbtTag, Random } from 'deepslate' import { Matrix3, Matrix4, NbtByte, NbtCompound, NbtDouble, NbtInt, NbtList, NbtString, Vector } from 'deepslate' @@ -635,3 +636,12 @@ export function safeJsonParse(text: string): any { return undefined } } + +export async function clearFolder(fs: ExternalFileSystem, uri: string) { + const entries = await fs.readdir(uri) + return Promise.all(entries.map(async e => { + if (e.name !== uri) { + return await fs.unlink(e.name) + } + })) +} diff --git a/src/app/components/generator/McdocRenderer.tsx b/src/app/components/generator/McdocRenderer.tsx index 78c7dd58..a80abf54 100644 --- a/src/app/components/generator/McdocRenderer.tsx +++ b/src/app/components/generator/McdocRenderer.tsx @@ -381,7 +381,7 @@ function formatUnionMember(type: SimplifiedMcdocTypeNoUnion, others: SimplifiedM } if (!others.some(o => o.kind === type.kind)) { // No other member is of this kind - return formatIdentifier(type.kind) + return formatIdentifier(type.kind === 'struct' ? 'object' : type.kind) } if (type.kind === 'struct') { // Show the first literal key @@ -390,7 +390,7 @@ function formatUnionMember(type: SimplifiedMcdocTypeNoUnion, others: SimplifiedM return formatUnionMember(firstKey, []) } } - return formatIdentifier(type.kind) + return formatIdentifier(type.kind === 'struct' ? 'object' : type.kind) } function UnionBody({ type, optional, node, ctx }: Props>) { diff --git a/src/app/components/generator/ProjectPanel.tsx b/src/app/components/generator/ProjectPanel.tsx index 1d71ca16..e817c95b 100644 --- a/src/app/components/generator/ProjectPanel.tsx +++ b/src/app/components/generator/ProjectPanel.tsx @@ -29,7 +29,7 @@ export function ProjectPanel() { setEntries(undefined) client.fs.readdir(projectRoot).then(entries => { setEntries(entries.flatMap(e => { - return e.isFile() && e.name.startsWith(projectRoot) ? [e.name.slice(projectRoot.length)] : [] + return e.isFile() ? [e.name.slice(projectRoot.length)] : [] })) }) }, [projectRoot]) diff --git a/src/app/components/generator/SchemaGenerator.tsx b/src/app/components/generator/SchemaGenerator.tsx index 4b587abe..4bc95205 100644 --- a/src/app/components/generator/SchemaGenerator.tsx +++ b/src/app/components/generator/SchemaGenerator.tsx @@ -12,7 +12,7 @@ import type { VersionId } from '../../services/index.js' import { checkVersion, fetchDependencyMcdoc, fetchPreset, fetchRegistries, getSnippet, shareSnippet } from '../../services/index.js' import { DEPENDENCY_URI, SpyglassClient } from '../../services/Spyglass.js' import { Store } from '../../Store.js' -import { cleanUrl, genPath } from '../../Utils.js' +import { cleanUrl, clearFolder, genPath } from '../../Utils.js' import { Ad, Btn, BtnMenu, ErrorPanel, FileCreation, FileView, Footer, HasPreview, Octicon, PreviewPanel, ProjectPanel, SearchList, SourcePanel, TextInput, VersionSwitcher } from '../index.js' import { getRootDefault } from './McdocHelpers.js' @@ -94,10 +94,7 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) { if (!service || !uri) { return AsyncCancel } - const dependencies = await SpyglassClient.FS.readdir(DEPENDENCY_URI) - await Promise.all(dependencies.flatMap(async d => { - d.isFile() && d.name.startsWith(DEPENDENCY_URI) ? [await SpyglassClient.FS.unlink(d.name)] : [] - })) + await clearFolder(SpyglassClient.FS, DEPENDENCY_URI) if (gen.dependency) { const dependency = await fetchDependencyMcdoc(gen.dependency) const dependencyUri = `${DEPENDENCY_URI}${gen.dependency}.mcdoc` diff --git a/src/app/contexts/Project.tsx b/src/app/contexts/Project.tsx index 03cb287e..04c352e4 100644 --- a/src/app/contexts/Project.tsx +++ b/src/app/contexts/Project.tsx @@ -134,12 +134,7 @@ export function ProjectProvider({ children }: { children: ComponentChildren }) { if (project) { const projectRoot = getProjectRoot(project) const entries = await SpyglassClient.FS.readdir(projectRoot) - await Promise.all(entries.flatMap(async e => { - if (e.name.startsWith(projectRoot)) { - return [await SpyglassClient.FS.unlink(e.name)] - } - return [] - })) + await Promise.all(entries.map(async e => SpyglassClient.FS.unlink(e.name))) } changeProjects(projects.filter(p => p.name !== name)) setOpenProject(undefined) diff --git a/src/app/services/FileSystem.ts b/src/app/services/FileSystem.ts index e13d4fbd..8fd3fcfd 100644 --- a/src/app/services/FileSystem.ts +++ b/src/app/services/FileSystem.ts @@ -311,8 +311,7 @@ export class IndexedDbFileSystem implements core.ExternalFileSystem { return new Promise((res, rej) => { const transaction = db.transaction(IndexedDbFileSystem.storeName, 'readonly') const store = transaction.objectStore(IndexedDbFileSystem.storeName) - // TODO: specify range - const request = store.openCursor() + const request = store.openCursor(IDBKeyRange.bound(location, location + '\uffff')) const result: { name: string, isDirectory(): boolean, isFile(): boolean, isSymbolicLink(): boolean }[] = [] request.onsuccess = () => { if (request.result) { diff --git a/src/app/services/Spyglass.ts b/src/app/services/Spyglass.ts index b59e73f6..1f3ec5ab 100644 --- a/src/app/services/Spyglass.ts +++ b/src/app/services/Spyglass.ts @@ -14,7 +14,7 @@ import siteConfig from '../Config.js' import { computeIfAbsent, genPath } from '../Utils.js' import type { VersionMeta } from './DataFetcher.js' import { fetchBlockStates, fetchRegistries, fetchVanillaMcdoc, fetchVersions, getVersionChecksum } from './DataFetcher.js' -import { IndexedDbFileSystem, MixedFileSystem } from './FileSystem.js' +import { IndexedDbFileSystem } from './FileSystem.js' import type { VersionId } from './Versions.js' export const CACHE_URI = 'file:///cache/' @@ -52,7 +52,7 @@ interface ClientDocument { } export class SpyglassClient { - public static readonly FS = new MixedFileSystem(new IndexedDbFileSystem(), []) + public static readonly FS = new IndexedDbFileSystem() public readonly fs = SpyglassClient.FS public readonly externals: core.Externals = { ...BrowserExternals, @@ -108,7 +108,7 @@ export class SpyglassService { await Promise.all(this.treeWatchers.map(async ({ prefix, handler }) => { const entries = await client.fs.readdir(prefix) handler(entries.flatMap(e => { - return e.isFile() && e.name.startsWith(prefix) ? [e.name.slice(prefix.length)] : [] + return e.isFile() ? [e.name.slice(prefix.length)] : [] })) })) })