diff --git a/src/app/Config.ts b/src/app/Config.ts index f1e92638..7f799e5f 100644 --- a/src/app/Config.ts +++ b/src/app/Config.ts @@ -1,5 +1,5 @@ import config from '../config.json' -import type { VersionId } from './services/Schemas.js' +import type { VersionId } from './services/Versions.js' export interface ConfigLanguage { code: string, diff --git a/src/app/components/customized/CustomizedGenerator.ts b/src/app/components/customized/CustomizedGenerator.ts index 55f2d03a..eec763ff 100644 --- a/src/app/components/customized/CustomizedGenerator.ts +++ b/src/app/components/customized/CustomizedGenerator.ts @@ -1,7 +1,7 @@ import { Identifier } from 'deepslate' -import { deepClone, deepEqual } from '../../Utils.js' import { fetchAllPresets, fetchBlockStates } from '../../services/DataFetcher.js' -import type { VersionId } from '../../services/Schemas.js' +import type { VersionId } from '../../services/Versions.js' +import { deepClone, deepEqual } from '../../Utils.js' import type { CustomizedOreModel } from './CustomizedModel.js' import { CustomizedModel } from './CustomizedModel.js' diff --git a/src/app/components/customized/CustomizedModel.ts b/src/app/components/customized/CustomizedModel.ts index 68f2ded0..da9851e4 100644 --- a/src/app/components/customized/CustomizedModel.ts +++ b/src/app/components/customized/CustomizedModel.ts @@ -1,4 +1,4 @@ -import type { VersionId } from '../../services/Schemas.js' +import type { VersionId } from '../../services/Versions.js' export interface CustomizedOreModel { size: number, diff --git a/src/app/components/generator/FileCreation.tsx b/src/app/components/generator/FileCreation.tsx index 740bbab8..bd349105 100644 --- a/src/app/components/generator/FileCreation.tsx +++ b/src/app/components/generator/FileCreation.tsx @@ -1,18 +1,18 @@ +import type { DocAndNode } from '@spyglassmc/core' import { useState } from 'preact/hooks' import { Analytics } from '../../Analytics.js' import { useLocale, useProject } from '../../contexts/index.js' -import type { FileModel } from '../../services/index.js' import { Btn } from '../Btn.js' import { TextInput } from '../forms/index.js' import { Modal } from '../Modal.js' interface Props { - model: FileModel, + docAndNode: DocAndNode, id: string, method: string, onClose: () => void, } -export function FileCreation({ model, id, method, onClose }: Props) { +export function FileCreation({ docAndNode, id, method, onClose }: Props) { const { locale } = useLocale() const { projects, project, updateFile } = useProject() const [fileId, setFileId] = useState(id === 'pack_mcmeta' ? 'pack' : '') @@ -29,7 +29,8 @@ export function FileCreation({ model, id, method, onClose }: Props) { return } Analytics.saveProjectFile(id, projects.length, project.files.length, method as any) - updateFile(id, undefined, { type: id, id: fileId, data: model.data }) + const data = JSON.parse(docAndNode.doc.getText()) + updateFile(id, undefined, { type: id, id: fileId, data }) onClose() } diff --git a/src/app/components/generator/GeneratorCard.tsx b/src/app/components/generator/GeneratorCard.tsx index 0ddc039a..dc03e8e6 100644 --- a/src/app/components/generator/GeneratorCard.tsx +++ b/src/app/components/generator/GeneratorCard.tsx @@ -2,8 +2,8 @@ import { useMemo } from 'preact/hooks' import type { ConfigGenerator } from '../../Config.js' import config from '../../Config.js' import { useLocale } from '../../contexts/Locale.jsx' -import type { VersionId } from '../../services/Schemas.js' -import { checkVersion } from '../../services/Schemas.js' +import type { VersionId } from '../../services/Versions.js' +import { checkVersion } from '../../services/Versions.js' import { cleanUrl } from '../../Utils.js' import { Badge, Card, Icons, ToolCard } from '../index.js' diff --git a/src/app/components/generator/GeneratorList.tsx b/src/app/components/generator/GeneratorList.tsx index 7faf0cb9..13addca2 100644 --- a/src/app/components/generator/GeneratorList.tsx +++ b/src/app/components/generator/GeneratorList.tsx @@ -2,7 +2,7 @@ import { useMemo, useState } from 'preact/hooks' import type { ConfigGenerator } from '../../Config.js' import config from '../../Config.js' import { useLocale, useVersion } from '../../contexts/index.js' -import { checkVersion } from '../../services/Schemas.js' +import { checkVersion } from '../../services/Versions.js' import { GeneratorCard, TextInput, VersionSwitcher } from '../index.js' interface Props { diff --git a/src/app/components/generator/PreviewPanel.tsx b/src/app/components/generator/PreviewPanel.tsx index c2576361..f8222fcc 100644 --- a/src/app/components/generator/PreviewPanel.tsx +++ b/src/app/components/generator/PreviewPanel.tsx @@ -1,59 +1,59 @@ +import type { DocAndNode } from '@spyglassmc/core' import { useVersion } from '../../contexts/Version.jsx' -import type { FileModel } from '../../services/index.js' import { checkVersion } from '../../services/index.js' import { BiomeSourcePreview, BlockStatePreview, DecoratorPreview, DensityFunctionPreview, LootTablePreview, ModelPreview, NoisePreview, NoiseSettingsPreview, RecipePreview, StructureSetPreview } from '../previews/index.js' -export const HasPreview = ['loot_table', 'recipe', 'dimension', 'worldgen/density_function', 'worldgen/noise', 'worldgen/noise_settings', 'worldgen/configured_feature', 'worldgen/placed_feature', 'worldgen/structure_set', 'block_definition', 'model'] +export const HasPreview = ['loot_table', 'recipe', 'dimension', 'worldgen/density_function', 'worldgen/noise', 'worldgen/noise_settings', 'worldgen/configured_feature', 'worldgen/placed_feature', 'worldgen/structure_set', 'block_definition', 'docAndNode'] type PreviewPanelProps = { - model: FileModel | undefined, + docAndNode: DocAndNode | undefined, id: string, shown: boolean, onError: (message: string) => unknown, } -export function PreviewPanel({ model, id, shown }: PreviewPanelProps) { +export function PreviewPanel({ docAndNode, id, shown }: PreviewPanelProps) { const { version } = useVersion() - if (!model) return <> + if (!docAndNode) return <> if (id === 'loot_table') { - return + return } if (id === 'recipe') { - return + return } - if (id === 'dimension' && model.data.generator?.type?.endsWith('noise')) { - return + if (id === 'dimension' && JSON.parse(docAndNode.doc.getText()).generator?.type?.endsWith('noise')) { + return } if (id === 'worldgen/density_function') { - return + return } if (id === 'worldgen/noise') { - return + return } if (id === 'worldgen/noise_settings' && checkVersion(version, '1.18')) { - return + return } if ((id === 'worldgen/placed_feature' || (id === 'worldgen/configured_feature' && checkVersion(version, '1.16', '1.17')))) { - return + return } if (id === 'worldgen/structure_set' && checkVersion(version, '1.19')) { - return + return } if (id === 'block_definition') { - return + return } if (id === 'model') { - return + return } return <> diff --git a/src/app/components/generator/SchemaGenerator.tsx b/src/app/components/generator/SchemaGenerator.tsx index b3c4663b..c017d42b 100644 --- a/src/app/components/generator/SchemaGenerator.tsx +++ b/src/app/components/generator/SchemaGenerator.tsx @@ -5,11 +5,11 @@ import type { ConfigGenerator } from '../../Config.js' import config from '../../Config.js' import { DRAFT_PROJECT, useLocale, useProject, useVersion } from '../../contexts/index.js' import { AsyncCancel, useActiveTimeout, useAsync, useSearchParam } from '../../hooks/index.js' -import type { FileModel, VersionId } from '../../services/index.js' -import { checkVersion, createMockFileModel, fetchPreset, fetchRegistries, getSnippet, shareSnippet } from '../../services/index.js' -import { setupSpyglass } from '../../services/Spyglass.js' +import type { VersionId } from '../../services/index.js' +import { checkVersion, fetchPreset, fetchRegistries, getSnippet, shareSnippet } from '../../services/index.js' +import { Spyglass } from '../../services/Spyglass.js' import { Store } from '../../Store.js' -import { cleanUrl, deepEqual, genPath } from '../../Utils.js' +import { cleanUrl, genPath } from '../../Utils.js' import { Ad, Btn, BtnMenu, ErrorPanel, FileCreation, FileRenaming, Footer, HasPreview, Octicon, PreviewPanel, ProjectCreation, ProjectDeletion, ProjectPanel, SearchList, SourcePanel, TextInput, Tree, VersionSwitcher } from '../index.js' export const SHARE_KEY = 'share' @@ -31,10 +31,15 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) { useEffect(() => Store.visitGenerator(gen.id), [gen.id]) - useEffect(() => { - setupSpyglass(version) + const { value: spyglass, loading: spyglassLoading } = useAsync(() => { + return Spyglass.initialize(version) }, [version]) + const uri = useMemo(() => { + // TODO: return different uri when project file is open + return spyglass?.getUnsavedFileUri(gen) + }, [spyglass, gen.id]) + const [currentPreset, setCurrentPreset] = useSearchParam('preset') const [sharedSnippetId, setSharedSnippetId] = useSearchParam(SHARE_KEY) const backup = useMemo(() => Store.getBackup(gen.id), [gen.id]) @@ -45,7 +50,10 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) { } } - const {} = useAsync(async () => { + const { value: docAndNode } = useAsync(async () => { + if (spyglassLoading || !spyglass || !uri) { + return AsyncCancel + } let data: unknown = undefined if (currentPreset && sharedSnippetId) { setSharedSnippetId(undefined) @@ -83,14 +91,12 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) { } data = file.data } - if (data) { - // TODO: set file contents to data - } + const docAndNode = await spyglass.setFileContents(uri, JSON.stringify(data ?? {})) Analytics.setGenerator(gen.id) - return {} - }, [gen.id, version, sharedSnippetId, currentPreset, project.name, file?.id]) + return docAndNode + }, [gen.id, version, sharedSnippetId, currentPreset, project.name, file?.id, spyglass, spyglassLoading]) - const model: FileModel = createMockFileModel() + const { doc } = docAndNode ?? {} // TODO: when contents of file change: // - remove preset and share id from url @@ -184,13 +190,13 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) { setShareShown(true) copySharedId() } else { - // TODO: get contents from file, and compare to default of type - if (deepEqual(model.data, {})) { + // TODO: check if files hasn't been modified compared to the default + if (false) { setShareUrl(`${location.origin}/${gen.url}/?version=${version}`) setShareShown(true) - } else { + } else if (doc) { setShareLoading(true) - shareSnippet(gen.id, version, model.data, previewShown) + shareSnippet(gen.id, version, JSON.parse(doc.getText()), previewShown) .then(({ id, length, compressed, rate }) => { Analytics.createSnippet(gen.id, id, version, length, compressed, rate) const url = `${location.origin}/${gen.url}/?${SHARE_KEY}=${id}` @@ -306,7 +312,7 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) { {error && setError(null)} />} - + {docAndNode && }