Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/kicanvas/elements/kicanvas-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class KiCanvasEmbedElement extends KCUIElement {
@attribute({ type: String })
zoom: "objects" | "page" | string | null;

custom_resolver: ((name: string) => URL) | null = null;

#schematic_app: KCSchematicAppElement;
#board_app: KCBoardAppElement;

Expand Down Expand Up @@ -115,7 +117,7 @@ class KiCanvasEmbedElement extends KCUIElement {
return;
}

const vfs = new FetchFileSystem(sources);
const vfs = new FetchFileSystem(sources, this.custom_resolver);
await this.#setup_project(vfs);
}

Expand Down
20 changes: 19 additions & 1 deletion src/kicanvas/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,32 @@ export class Project extends EventTarget implements IDisposable {

this.#fs = fs;

const promises = [];
let promises = [];

for (const filename of this.#fs.list()) {
promises.push(this.#load_file(filename));
}

await Promise.all(promises);

while (promises.length) {
// 'Recursively' resolve all schematics until none are remaining
promises = [];
for (const schematic of this.schematics()) {
for (const sheet of schematic.sheets) {
const sheet_sch = this.#files_by_name.get(
sheet.sheetfile ?? "",
) as KicadSch;

if (!sheet_sch && sheet.sheetfile) {
// Missing schematic, attempt to fetch
promises.push(this.#load_file(sheet.sheetfile));
}
}
}
await Promise.all(promises);
}

this.#determine_schematic_hierarchy();

this.loaded.open();
Expand Down
34 changes: 29 additions & 5 deletions src/kicanvas/services/vfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,38 @@ export abstract class VirtualFileSystem {
*/
export class FetchFileSystem extends VirtualFileSystem {
private urls: Map<string, URL> = new Map();
private resolver!: (name: string) => URL;

constructor(urls: (string | URL)[]) {
#default_resolver(name: string): URL {
const url = new URL(name, window.location.toString());
return url;
}

#resolve(filepath: string | URL): URL {
if (typeof filepath === "string") {
const cached_url = this.urls.get(filepath);
if (cached_url) {
return cached_url;
} else {
const url = this.resolver(filepath);
const name = basename(url);
this.urls.set(name, url);
return url;
}
}
return filepath;
}

constructor(
urls: (string | URL)[],
resolve_file: ((name: string) => URL) | null = null,
) {
super();

this.resolver = resolve_file ?? this.#default_resolver;

for (const item of urls) {
const url = new URL(item, window.location.toString());
const name = basename(url);
this.urls.set(name, url);
this.#resolve(item);
}
}

Expand All @@ -66,7 +90,7 @@ export class FetchFileSystem extends VirtualFileSystem {
}

public override async get(name: string): Promise<File> {
const url = this.urls.get(name);
const url = this.#resolve(name);

if (!url) {
throw new Error(`File ${name} not found!`);
Expand Down
Loading