diff --git a/package.json b/package.json index bf6332d..25e8450 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "make-install": "vite build && node --no-warnings ./scripts/make_install.js" }, "devDependencies": { - "@sveltejs/vite-plugin-svelte": "^3.1.0", + "@sveltejs/vite-plugin-svelte": "^4.0.0", "@tsconfig/svelte": "^4.0.1", "@types/node": "^20.3.0", "cross-env": "^7.0.3", @@ -27,10 +27,10 @@ "rollup-plugin-livereload": "^2.0.5", "sass": "^1.63.3", "siyuan": "1.1.2", - "svelte": "^4.2.20", + "svelte": "^5.0.0", "ts-node": "^10.9.1", - "typescript": "^5.1.3", - "vite": "^5.2.9", + "typescript": "^5.5.0", + "vite": "^5.4.4", "vite-plugin-static-copy": "^1.0.2", "vite-plugin-zip-pack": "^1.0.5" } diff --git a/src/hello.svelte b/src/hello.svelte index 23353c7..c048c95 100644 --- a/src/hello.svelte +++ b/src/hello.svelte @@ -11,13 +11,19 @@ // import { version } from "@/api"; import { showMessage, fetchPost, Protyle } from "siyuan"; - export let app; - export let blockID: string; + interface Props { + app: any; + blockID: string; + } - let time: string = ""; + let { app, blockID }: Props = $props(); - let divProtyle: HTMLDivElement; + let time: string = $state(""); + // let ver: string = ""; + + let divProtyle: HTMLDivElement = $state(); let protyle: any; + // let blockID: string = $state(''); onMount(async () => { // ver = await version(); @@ -50,14 +56,14 @@
API demo:
-
+
System current time: {time}
-
-
+
+
Protyle demo: id = {blockID}
-
-
+
+
diff --git a/src/index.ts b/src/index.ts index 614e0a4..6775dff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,9 +23,9 @@ import { getModelByDockType, getAllEditor, Files, - platformUtils, + // platformUtils, openSetting, - openAttributePanel, + openAttributePanel, saveLayout } from "siyuan"; import "./index.scss"; @@ -36,6 +36,7 @@ import SettingExample from "@/setting-example.svelte"; import { SettingUtils } from "./libs/setting-utils"; import { svelteDialog } from "./libs/dialog"; +import { mount, unmount } from "svelte"; const STORAGE_NAME = "menu-config"; const TAB_TYPE = "custom_tab"; @@ -84,7 +85,7 @@ export default class PluginSample extends Plugin { this.custom = this.addTab({ type: TAB_TYPE, init() { - app = new HelloExample({ + app = mount(HelloExample, { target: tabDiv, props: { app: this.app, @@ -98,7 +99,8 @@ export default class PluginSample extends Plugin { console.log("before destroy tab:", TAB_TYPE); }, destroy() { - app?.$destroy(); + // app?.$destroy(); + app && unmount(app); console.log("destroy tab:", TAB_TYPE); } }); @@ -415,19 +417,28 @@ export default class PluginSample extends Plugin { * A custom setting pannel provided by svelte */ openSetting(): void { - let dialog = new Dialog({ + // let dialog = new Dialog({ + // title: "SettingPannel", + // content: `
`, + // width: "800px", + // destroyCallback: (options) => { + // console.log("destroyCallback", options); + // //You'd better destroy the component when the dialog is closed + // unmount(pannel); + // } + // }); + // let pannel = mount(SettingExample, { + // target: dialog.element.querySelector("#SettingPanel"), + // }); + svelteDialog({ title: "SettingPannel", - content: `
`, width: "800px", - destroyCallback: (options) => { - console.log("destroyCallback", options); - //You'd better destroy the component when the dialog is closed - pannel.$destroy(); + height: "35rem", + component: SettingExample, + props: { + app: this.app, } }); - let pannel = new SettingExample({ - target: dialog.element.querySelector("#SettingPanel"), - }); } private eventBusPaste(event: any) { @@ -471,14 +482,10 @@ export default class PluginSample extends Plugin { svelteDialog({ title: `SiYuan ${Constants.SIYUAN_VERSION}`, width: this.isMobile ? "92vw" : "720px", - constructor: (container: HTMLElement) => { - return new HelloExample({ - target: container, - props: { - app: this.app, - blockID: docId - } - }); + component: HelloExample, + props: { + app: this.app, + blockID: docId } }); } diff --git a/src/libs/components/Form/form-input.svelte b/src/libs/components/Form/form-input.svelte index cbf5a7e..e6caf8d 100644 --- a/src/libs/components/Form/form-input.svelte +++ b/src/libs/components/Form/form-input.svelte @@ -1,33 +1,47 @@ @@ -38,7 +52,7 @@ id={key} type="checkbox" bind:checked={value} - on:change={changed} + onchange={handleChanged} style={style} /> {:else if type === "textinput"} @@ -50,7 +64,7 @@ id={key} {placeholder} bind:value={value} - on:change={changed} + onchange={handleChanged} style={style} /> {:else if type === "textarea"} @@ -58,8 +72,8 @@ class="b3-text-field fn__block" style={`resize: vertical; height: 10em; white-space: nowrap; ${style}`} bind:value={value} - on:change={changed} - /> + onchange={handleChanged} +> {:else if type === "number"} {:else if type === "button"} @@ -79,7 +93,7 @@ class:fn__flex-center={true} class:fn__size200={fnSize} id={key} - on:click={click} + onclick={handleClick} style={style} > {button.label} @@ -92,7 +106,7 @@ class:fn__size200={fnSize} id="iconPosition" bind:value={value} - on:change={changed} + onchange={handleChanged} style={style} > {#each Object.entries(options) as [value, text]} @@ -111,7 +125,7 @@ step={slider.step} type="range" bind:value={value} - on:change={changed} + onchange={handleChanged} style={style} />
diff --git a/src/libs/components/Form/form-wrap.svelte b/src/libs/components/Form/form-wrap.svelte index 4d8092e..e623a8f 100644 --- a/src/libs/components/Form/form-wrap.svelte +++ b/src/libs/components/Form/form-wrap.svelte @@ -7,9 +7,19 @@ Description : The setting item container --> {#if direction === "row"} @@ -19,7 +29,7 @@
{@html description}
- + {@render children?.()}
@@ -31,8 +41,8 @@ {@html description}
- - + + {@render children?.()} {/if} diff --git a/src/libs/components/b3-typography.svelte b/src/libs/components/b3-typography.svelte index 60227cd..2348359 100644 --- a/src/libs/components/b3-typography.svelte +++ b/src/libs/components/b3-typography.svelte @@ -1,3 +1,8 @@ + +
- + {@render children?.()}
\ No newline at end of file diff --git a/src/libs/components/setting-panel.svelte b/src/libs/components/setting-panel.svelte index 783e2b6..8dd34e8 100644 --- a/src/libs/components/setting-panel.svelte +++ b/src/libs/components/setting-panel.svelte @@ -7,28 +7,42 @@ Description : -->
- + {@render children?.()} {#each settingItems as item (item.key)} {/each} diff --git a/src/libs/dialog.ts b/src/libs/dialog.ts index d0fe582..842d545 100644 --- a/src/libs/dialog.ts +++ b/src/libs/dialog.ts @@ -3,11 +3,11 @@ * @Author : frostime * @Date : 2024-03-23 21:37:33 * @FilePath : /src/libs/dialog.ts - * @LastEditTime : 2024-10-16 14:31:04 + * @LastEditTime : 2025-08-16 15:39:48 * @Description : Kits about dialogs */ import { Dialog } from "siyuan"; -import { type SvelteComponent } from "svelte"; +import { Component, mount, unmount } from "svelte"; export const inputDialog = (args: { title: string, placeholder?: string, defaultText?: string, @@ -143,21 +143,34 @@ export const simpleDialog = (args: { export const svelteDialog = (args: { - title: string, constructor: (container: HTMLElement) => SvelteComponent, - width?: string, height?: string, + title: string, + component: Component, // Svelte 5 component constructor + props?: Record, + width?: string, + height?: string, callback?: () => void; }) => { let container = document.createElement('div') container.style.display = 'contents'; - let component = args.constructor(container); + + // 内部处理 mount + let componentInstance = mount(args.component, { + target: container, + props: args.props || {} + }); + const { dialog, close } = simpleDialog({ - ...args, ele: container, callback: () => { - component.$destroy(); + ...args, + ele: container, + callback: () => { + // 内部处理 unmount + unmount(componentInstance); if (args.callback) args.callback(); } }); + return { - component, + component: componentInstance, dialog, close } diff --git a/src/setting-example.svelte b/src/setting-example.svelte index 2a2c809..527def2 100644 --- a/src/setting-example.svelte +++ b/src/setting-example.svelte @@ -3,7 +3,7 @@ import SettingPanel from "./libs/components/setting-panel.svelte"; let groups: string[] = ["🌈 Group 1", "✨ Group 2"]; - let focusGroup = groups[0]; + let focusGroup = $state(groups[0]); const group1Items: ISettingItem[] = [ { @@ -79,7 +79,7 @@ value: any; } - const onChanged = ({ detail }: CustomEvent) => { + const onChanged = (detail: ChangeEvent) => { if (detail.group === groups[0]) { // setting.set(detail.key, detail.value); //Please add your code here @@ -91,15 +91,15 @@
    {#each groups as group} - +
  • { + onclick={() => { focusGroup = group; }} - on:keydown={() => {}} + onkeydown={() => {}} > {group}
  • @@ -110,8 +110,8 @@ group={groups[0]} settingItems={group1Items} display={focusGroup === groups[0]} - on:changed={onChanged} - on:click={({ detail }) => { console.debug("Click:", detail.key); }} + onchanged={onChanged} + onclick={(detail) => { console.debug("Click:", detail.key); }} >
    💡 This is our default settings. @@ -121,8 +121,8 @@ group={groups[1]} settingItems={group2Items} display={focusGroup === groups[1]} - on:changed={onChanged} - on:click={({ detail }) => { console.debug("Click:", detail.key); }} + onchanged={onChanged} + onclick={(detail) => { console.debug("Click:", detail.key); }} >