From ee9d82e946171cf2ffab1e09c3c1bf00158cbb5a Mon Sep 17 00:00:00 2001 From: Exelo Date: Sun, 7 Jun 2026 17:18:49 +0900 Subject: [PATCH] feat: add create and import package functionality --- .../client/ecs/component/component-manager.ts | 38 +++++++++++++++---- src/lib/client/ecs/system/system-manager.ts | 38 +++++++++++++++---- src/lib/client/ecs/transformers.ts | 38 +++++++++++-------- src/lib/client/sync-file-system/sfs.ts | 2 + .../ecs-tree/dialogs/dialog-name.svelte | 15 ++++++-- .../packages/dialog-create-package.svelte | 8 ++-- .../ecs-tree/packages/package-row.svelte | 3 +- .../ecs-tree/packages/package-tab.svelte | 38 ++++++++++++------- src/lib/components/dialogs/base-dialog.svelte | 21 ++++++++-- .../components/dialogs/input-dialog.svelte | 3 +- .../server/project/package/package-handler.ts | 4 +- 11 files changed, 150 insertions(+), 58 deletions(-) diff --git a/src/lib/client/ecs/component/component-manager.ts b/src/lib/client/ecs/component/component-manager.ts index d00c912..ec84b2a 100644 --- a/src/lib/client/ecs/component/component-manager.ts +++ b/src/lib/client/ecs/component/component-manager.ts @@ -1,6 +1,9 @@ import { type Unsubscriber, get, writable } from 'svelte/store'; -import { getId, resetSubscriptions } from '../utils'; +import { useProject } from '$lib/client/project'; + +import { componentTransformer, componentsTransformer } from '../transformers'; +import { resetSubscriptions } from '../utils'; import { ComponentHandle } from './component-handle'; import type { Component } from './component.type'; @@ -26,12 +29,27 @@ export class ComponentManager { return get(_storage); } - add(component: Omit & Partial>) { - const components = get(_storage); - const id = getId(this.data, component.name); - components.push({ ...component, id, path: component.path ?? `components/${id}.ts` }); - _storage.set(components); - return id; + async create(name: string) { + const { actions, fs } = useProject(); + const component = await actions.package.createComponent({ componentName: name }); + this._add(componentTransformer(component)); + const dir = await fs.getDirectory(); + await dir.readdir(true); + } + + async import(names: [string, ...string[]]) { + const { actions, ecs, fs } = useProject(); + await actions.package.addComponents({ componentNames: names }); + await this.sync(); + await ecs.components.sync(); + const dir = await fs.getDirectory(); + await dir.readdir(true); + } + + async sync() { + const { actions } = useProject(); + const components = await actions.package.getComponents(); + _storage.set(componentsTransformer(components)); } get(id: string): ComponentHandle { @@ -56,6 +74,12 @@ export class ComponentManager { } } + private _add(component: Component) { + const components = get(_storage); + components.push(component); + _storage.set(components); + } + private _subscribe(id: string, handle: ComponentHandle) { setTimeout(() => { const subscriptions = get(_subscriptions); diff --git a/src/lib/client/ecs/system/system-manager.ts b/src/lib/client/ecs/system/system-manager.ts index 3fdfedc..c2cb561 100644 --- a/src/lib/client/ecs/system/system-manager.ts +++ b/src/lib/client/ecs/system/system-manager.ts @@ -1,6 +1,9 @@ import { type Unsubscriber, get, writable } from 'svelte/store'; -import { getId, resetSubscriptions } from '../utils'; +import { useProject } from '$lib/client/project'; + +import { systemTransformer, systemsTransformer } from '../transformers'; +import { resetSubscriptions } from '../utils'; import { SystemHandle } from './system-handle'; import type { System } from './system.type'; @@ -25,12 +28,27 @@ export class SystemManager { return get(_storage); } - add(system: Omit & Partial>): string { - const systems = get(_storage); - const id = getId(this.data, system.name); - systems.push({ ...system, id, path: system.path ?? `systems/${id}.ts` }); - _storage.set(systems); - return id; + async create(name: string) { + const { actions, fs } = useProject(); + const system = await actions.package.createSystem({ systemName: name }); + this._add(systemTransformer(system)); + const dir = await fs.getDirectory(); + await dir.readdir(true); + } + + async import(names: [string, ...string[]]) { + const { actions, ecs, fs } = useProject(); + await actions.package.addSystems({ systemNames: names }); + await this.sync(); + await ecs.components.sync(); + const dir = await fs.getDirectory(); + await dir.readdir(true); + } + + async sync() { + const { actions } = useProject(); + const systems = await actions.package.getSystems(); + _storage.set(systemsTransformer(systems)); } get(id: string): SystemHandle { @@ -55,6 +73,12 @@ export class SystemManager { } } + private _add(system: System) { + const systems = get(_storage); + systems.push(system); + _storage.set(systems); + } + private _subscribe(id: string, handle: SystemHandle) { setTimeout(() => { const subscriptions = get(_subscriptions); diff --git a/src/lib/client/ecs/transformers.ts b/src/lib/client/ecs/transformers.ts index e85cc43..6505e5e 100644 --- a/src/lib/client/ecs/transformers.ts +++ b/src/lib/client/ecs/transformers.ts @@ -1,28 +1,34 @@ import type { Component, Library, Scene, System } from '$lib/client/ecs'; import type { ComponentPackage, SystemPackage } from '$lib/server/project/package'; -import type { Save } from '@utils/types'; +import type { Save, SaveLibrary } from '@utils/types'; + +export const componentTransformer = (component: ComponentPackage): Component => ({ + id: component.manifest.id, + name: component.manifest.name, + path: component.save.path, + params: component.manifest.params, +}); export const componentsTransformer = (components: ComponentPackage[]): Component[] => - components.map((component) => ({ - id: component.manifest.id, - name: component.manifest.name, - path: component.save.path, - params: component.manifest.params, - })); + components.map(componentTransformer); + +export const systemTransformer = (system: SystemPackage): System => ({ + id: system.manifest.id, + name: system.manifest.name, + path: system.save.path, +}); export const systemsTransformer = (systems: SystemPackage[]): System[] => - systems.map((system) => ({ - id: system.manifest.id, - name: system.manifest.name, - path: system.save.path, - })); + systems.map(systemTransformer); + +export const libraryTransformer = (lib: SaveLibrary): Library => ({ + id: lib.path, + name: lib.name, +}); export const librariesTransformer = (save: Save): Library[] => - save.libraries.map((lib) => ({ - id: lib.path, - name: lib.name, - })); + save.libraries.map(libraryTransformer); export const scenesTransformer = (save: Save): Scene[] => [ { diff --git a/src/lib/client/sync-file-system/sfs.ts b/src/lib/client/sync-file-system/sfs.ts index 4a325e6..587d071 100644 --- a/src/lib/client/sync-file-system/sfs.ts +++ b/src/lib/client/sync-file-system/sfs.ts @@ -28,6 +28,8 @@ export class SyncFileSystem { async init(): Promise { await this.treeCache?.init(); + const dir = await this.getDirectory(); + await dir.readdir(true); } async getFile(path: string): Promise { diff --git a/src/lib/components/Widget/ecs-tree/dialogs/dialog-name.svelte b/src/lib/components/Widget/ecs-tree/dialogs/dialog-name.svelte index d90ac4d..997d72d 100644 --- a/src/lib/components/Widget/ecs-tree/dialogs/dialog-name.svelte +++ b/src/lib/components/Widget/ecs-tree/dialogs/dialog-name.svelte @@ -1,6 +1,7 @@ - + diff --git a/src/lib/components/Widget/ecs-tree/packages/package-row.svelte b/src/lib/components/Widget/ecs-tree/packages/package-row.svelte index a0d8631..b0a832f 100644 --- a/src/lib/components/Widget/ecs-tree/packages/package-row.svelte +++ b/src/lib/components/Widget/ecs-tree/packages/package-row.svelte @@ -123,6 +123,7 @@ {/if} - + + {confirmText ?? 'Confirm'} + diff --git a/src/lib/components/dialogs/input-dialog.svelte b/src/lib/components/dialogs/input-dialog.svelte index 8087d9e..8c7b121 100644 --- a/src/lib/components/dialogs/input-dialog.svelte +++ b/src/lib/components/dialogs/input-dialog.svelte @@ -1,6 +1,7 @@