Skip to content

Commit 64c40e9

Browse files
committed
svelte typing finally works
1 parent a6bd331 commit 64c40e9

File tree

5 files changed

+47
-25
lines changed

5 files changed

+47
-25
lines changed

jsEngine/api/PromptAPI.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { API } from 'jsEngine/api/API';
22
import { ButtonStyleType } from 'jsEngine/utils/Util';
33
import { SvelteModal } from 'jsEngine/api/prompts/SvelteModal';
4-
import { mount } from 'svelte';
5-
import ButtonModalComponent from 'jsEngine/api/prompts/ButtonModalComponent.svelte';
4+
import { ButtonModalComponent } from 'jsEngine/api/prompts/ButtonModalComponent';
65
import { Suggester } from 'jsEngine/api/prompts/Suggester';
6+
import { customMount } from 'jsEngine/utils/SvelteUtils';
77

88
export interface ModalPromptOptions {
99
/**
@@ -73,13 +73,9 @@ export class PromptAPI {
7373
this.apiInstance.app,
7474
options,
7575
(modal, targetEl) => {
76-
return mount(ButtonModalComponent, {
77-
target: targetEl,
78-
props: {
79-
options,
80-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
81-
modal: modal as any,
82-
},
76+
return customMount<ButtonModalComponent<T>>(ButtonModalComponent, targetEl, {
77+
options,
78+
modal,
8379
});
8480
},
8581
// we cast to narrow the type, so that it is inferred correctly
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Component } from 'svelte';
2+
import type { SvelteModal } from 'jsEngine/api/prompts/SvelteModal';
3+
4+
export type ButtonModalComponent<T> = Component<{
5+
options: ButtonPromptOptions<T>;
6+
modal: SvelteModal<ButtonModalComponent<T>, T>;
7+
}>;
8+
9+
export declare const ButtonModalComponent: ButtonModalComponent<T>;

jsEngine/engine/ExecutionStatsModal.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { type App, Modal } from 'obsidian';
22
import type JsEnginePlugin from 'jsEngine/main';
33
import ExecutionStatsComponent from 'jsEngine/engine/ExecutionStatsComponent.svelte';
44
import { type JsExecution } from 'jsEngine/engine/JsExecution';
5+
import { unmount } from 'svelte';
6+
import { customMount, type MountedComponent } from 'jsEngine/utils/SvelteUtils';
57

68
/**
79
* @internal
810
*/
911
export class ExecutionStatsModal extends Modal {
1012
private readonly plugin: JsEnginePlugin;
11-
private component: ExecutionStatsComponent | undefined;
13+
private component: MountedComponent<ExecutionStatsComponent> | undefined;
1214
private execution: JsExecution | undefined;
1315

1416
constructor(app: App, plugin: JsEnginePlugin) {
@@ -23,7 +25,7 @@ export class ExecutionStatsModal extends Modal {
2325
public onOpen(): void {
2426
this.contentEl.empty();
2527
if (this.component) {
26-
this.component.$destroy();
28+
unmount(this.component);
2729
}
2830

2931
if (!this.contentEl.hasClass('js-engine-execution-stats-modal')) {
@@ -35,18 +37,15 @@ export class ExecutionStatsModal extends Modal {
3537
return;
3638
}
3739

38-
this.component = new ExecutionStatsComponent({
39-
target: this.contentEl,
40-
props: {
41-
execution: this.execution,
42-
},
40+
this.component = customMount(ExecutionStatsComponent, this.contentEl, {
41+
execution: this.execution,
4342
});
4443
}
4544

4645
public onClose(): void {
4746
this.contentEl.empty();
4847
if (this.component) {
49-
this.component.$destroy();
48+
unmount(this.component);
5049
}
5150
}
5251
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { type App, Modal } from 'obsidian';
22
import type JsEnginePlugin from 'jsEngine/main';
33
import MessageDisplayComponent from 'jsEngine/messages/MessageDisplayComponent.svelte';
4+
import { unmount } from 'svelte';
5+
import { customMount, type MountedComponent } from 'jsEngine/utils/SvelteUtils';
46

57
export class MessageDisplay extends Modal {
68
/**
79
* Reference the JS Engine plugin.
810
*/
911
private readonly plugin: JsEnginePlugin;
10-
private component: MessageDisplayComponent | undefined;
12+
private component: MountedComponent<MessageDisplayComponent> | undefined;
1113

1214
constructor(app: App, plugin: JsEnginePlugin) {
1315
super(app);
@@ -17,21 +19,18 @@ export class MessageDisplay extends Modal {
1719
public onOpen(): void {
1820
this.contentEl.empty();
1921
if (this.component) {
20-
this.component.$destroy();
22+
unmount(this.component);
2123
}
2224

23-
this.component = new MessageDisplayComponent({
24-
target: this.contentEl,
25-
props: {
26-
messageManager: this.plugin.messageManager,
27-
},
25+
this.component = customMount<MessageDisplayComponent>(MessageDisplayComponent, this.contentEl, {
26+
messageManager: this.plugin.messageManager,
2827
});
2928
}
3029

3130
public onClose(): void {
3231
this.contentEl.empty();
3332
if (this.component) {
34-
this.component.$destroy();
33+
unmount(this.component);
3534
}
3635
}
3736
}

jsEngine/utils/SvelteUtils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument */
2+
3+
import { mount, type Component, type ComponentProps, type SvelteComponent } from 'svelte';
4+
5+
export function customMount<Comp extends Component<any, any> | SvelteComponent>(
6+
component: Component<any, any> | SvelteComponent,
7+
target: HTMLElement,
8+
props: ComponentProps<Comp>,
9+
): ComponentExports<Comp> {
10+
return mount(component as any, { target, props });
11+
}
12+
13+
export type AnyRecord = Record<string, any>;
14+
export type UnknownRecord = Record<string, unknown>;
15+
16+
export type ComponentExports<Comp extends Component<any, any> | SvelteComponent> =
17+
Comp extends SvelteComponent<any, infer Exports> ? (Exports extends any ? AnyRecord : Exports) : AnyRecord;
18+
19+
export type MountedComponent<Comp extends Component<any, any> | SvelteComponent> = ComponentExports<Comp>;

0 commit comments

Comments
 (0)