Skip to content

Commit 9169d0e

Browse files
committed
feat: Add the functionality to load files from URLs.
1 parent f81af91 commit 9169d0e

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ export class MainWindow extends HTMLElement implements IWindow {
5656

5757
await this.loadCss();
5858
await this.fetchIconFont();
59-
60-
this._initHome(app);
59+
await this._initHome(app);
6160
this._initEditor(app);
6261
this._initSubs(app);
6362
}

packages/chili-web/src/index.ts

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +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 { IApplication, Logger } from "chili-core";
66
import { Loading } from "./loading";
77

88
let 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(async (app) => {
19-
document.body.removeChild(loading);
20-
21-
const params = new URLSearchParams(window.location.search);
22-
const modelUrl = params.get("model");
23-
if (modelUrl) {
24-
try {
25-
const response = await fetch(modelUrl);
26-
if (!response.ok) {
27-
throw new Error(`Failed to fetch model: ${response.statusText}`);
28-
}
29-
const blob = await response.blob();
30-
let filename = modelUrl.substring(modelUrl.lastIndexOf("/") + 1);
31-
if (filename.includes("?")) {
32-
filename = filename.split("?")[0];
33-
}
34-
if (!filename) filename = "model.stp";
35-
36-
const file = new File([blob], filename, { type: blob.type });
37-
const dataTransfer = new DataTransfer();
38-
dataTransfer.items.add(file);
39-
40-
if (typeof (app as any).importFiles === "function") {
41-
await (app as any).importFiles(dataTransfer.files);
42-
} else {
43-
Logger.error("Application does not support importFiles");
44-
}
45-
} catch (err) {
46-
Logger.error(err);
47-
}
48-
}
49-
})
30+
.then(handleApplicaionBuilt)
5031
.catch((err) => {
5132
Logger.error(err);
5233
});

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
IView,
1616
IVisualFactory,
1717
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)