Skip to content

Commit d25fce4

Browse files
committed
Merge branch 'main' into dev
2 parents ce4aa46 + 9169d0e commit d25fce4

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

packages/chili-core/src/application.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface IApplication {
2525
newDocument(name: string): Promise<IDocument>;
2626
openDocument(id: string): Promise<IDocument | undefined>;
2727
loadDocument(data: Serialized): Promise<IDocument | undefined>;
28+
loadFileFromUrl(url: string): Promise<void>;
2829
}
2930

3031
let currentApplication: IApplication | undefined;

packages/chili-ui/src/mainWindow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class MainWindow extends HTMLElement implements IWindow {
6262
await this.fetchIconFont();
6363

6464
this.applyTheme();
65-
this._initHome(app);
65+
await this._initHome(app);
6666
this._initEditor(app);
6767
this._initEventHandlers(app);
6868
}

packages/chili-web/src/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,32 @@
22
// See LICENSE file in the project root for full license information.
33

44
import { AppBuilder } from "chili-builder";
5-
import { Logger } from "chili-core";
5+
import { type IApplication, Logger } from "chili-core";
66
import { Loading } from "./loading";
77

88
const loading = new Loading();
99
document.body.appendChild(loading);
1010

11+
async function handleApplicaionBuilt(app: IApplication) {
12+
document.body.removeChild(loading);
13+
14+
const params = new URLSearchParams(window.location.search);
15+
const url = params.get("url") ?? params.get("model");
16+
if (url) {
17+
Logger.info(`load file from url: ${url}`);
18+
19+
await app.loadFileFromUrl(url);
20+
}
21+
}
22+
1123
// prettier-ignore
1224
new AppBuilder()
1325
.useIndexedDB()
1426
.useWasmOcc()
1527
.useThree()
1628
.useUI()
1729
.build()
18-
.then((x) => {
19-
document.body.removeChild(loading);
20-
})
30+
.then(handleApplicaionBuilt)
2131
.catch((err) => {
2232
Logger.error(err);
2333
});

packages/chili/src/application.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type IView,
1616
type IVisualFactory,
1717
type IWindow,
18+
Logger,
1819
Material,
1920
ObservableCollection,
2021
Plane,
@@ -113,7 +114,7 @@ export class Application implements IApplication {
113114
}
114115
};
115116

116-
async importFiles(files: FileList | undefined) {
117+
async importFiles(files: File[] | FileList | undefined) {
117118
if (!files || files.length === 0) {
118119
return;
119120
}
@@ -137,7 +138,7 @@ export class Application implements IApplication {
137138
);
138139
}
139140

140-
private groupFiles(files: FileList) {
141+
private groupFiles(files: FileList | File[]) {
141142
const opens: File[] = [];
142143
const imports: File[] = [];
143144
for (const element of files) {
@@ -171,6 +172,26 @@ export class Application implements IApplication {
171172
return document;
172173
}
173174

175+
async loadFileFromUrl(url: string): Promise<void> {
176+
return Promise.try(async () => {
177+
const filename = url.substring(url.lastIndexOf("/") + 1);
178+
if (!filename || !filename.includes(".")) {
179+
throw new Error(`No file name in url: ${url}`);
180+
}
181+
182+
const response = await fetch(url);
183+
if (!response.ok) {
184+
throw new Error(`Failed to fetch model: ${response.statusText}`);
185+
}
186+
187+
const blob = await response.blob();
188+
const file = new File([blob], filename, { type: blob.type });
189+
await this.importFiles([file]);
190+
}).catch((err) => {
191+
Logger.error(err);
192+
});
193+
}
194+
174195
protected async createActiveView(document: IDocument | undefined) {
175196
if (document === undefined) return undefined;
176197
const view = document.visual.createView("3d", Plane.XY);

0 commit comments

Comments
 (0)