diff --git a/packages/chili-ui/src/home/home.ts b/packages/chili-ui/src/home/home.ts index 4979bac6..70f25bc6 100644 --- a/packages/chili-ui/src/home/home.ts +++ b/packages/chili-ui/src/home/home.ts @@ -54,8 +54,9 @@ export const Home = async (app: IApplication) => { div( { className: style.document, - onclick: () => { - app.openDocument(item.id); + onclick: async () => { + let document = await app.openDocument(item.id); + document?.visual.viewer.activeView?.cameraController.fitContent(); }, }, img({ className: style.img, src: item.image }), diff --git a/packages/chili/src/commands/application/openDocument.ts b/packages/chili/src/commands/application/openDocument.ts index bdf15525..1b65c9ca 100644 --- a/packages/chili/src/commands/application/openDocument.ts +++ b/packages/chili/src/commands/application/openDocument.ts @@ -1,6 +1,6 @@ // Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license. -import { IApplication, ICommand, Serialized, command, readFileAsync } from "chili-core"; +import { IApplication, ICommand, IView, PubSub, Serialized, command, readFileAsync } from "chili-core"; @command({ name: "doc.open", @@ -9,22 +9,11 @@ import { IApplication, ICommand, Serialized, command, readFileAsync } from "chil }) export class OpenDocument implements ICommand { async execute(app: IApplication): Promise { - let input = document.createElement("input"); - input.type = "file"; - input.style.visibility = "hidden"; - input.accept = ".cd"; - input.onchange = () => { - let file = input.files?.item(0); - if (!file) return; - let reader = new FileReader(); - reader.onload = async (e) => { - let data = e.target?.result as string; - let json: Serialized = JSON.parse(data); - await app.loadDocument(json); - }; - reader.readAsText(file); - }; - document.body.appendChild(input); - input.click(); + let files = await readFileAsync(".cd", false); + if (files.status === "success") { + let json: Serialized = JSON.parse(files.value[0].data); + let document = await app.loadDocument(json); + document.visual.viewer.activeView?.cameraController.fitContent(); + } } }