🐛 fix some svelte bugs

This commit is contained in:
frostime 2025-08-16 16:28:25 +08:00
parent fc94ff3447
commit e58da9f082
2 changed files with 32 additions and 32 deletions

View file

@ -8,35 +8,35 @@
--> -->
<script lang="ts"> <script lang="ts">
import { onDestroy, onMount } from "svelte"; import { onDestroy, onMount } from "svelte";
import { version, sql as query } from "@/api"; // import { version } from "@/api";
import { showMessage, fetchPost, Protyle } from "siyuan"; import { showMessage, fetchPost, Protyle } from "siyuan";
export let app; export let app;
export let blockID: string;
let time: string = ""; let time: string = "";
let ver: string = "";
let divProtyle: HTMLDivElement; let divProtyle: HTMLDivElement;
let protyle: any; let protyle: any;
let blockID: string = '';
onMount(async () => { onMount(async () => {
ver = await version(); // ver = await version();
fetchPost("/api/system/currentTime", {}, (response) => { fetchPost("/api/system/currentTime", {}, (response) => {
time = new Date(response.data).toString(); time = new Date(response.data).toString();
}); });
protyle = await initProtyle(); if (blockID) {
protyle = await initProtyle();
} else {
divProtyle.innerHTML = "Please open a document first";
}
}); });
onDestroy(() => { onDestroy(() => {
showMessage("Hello panel closed"); showMessage("Hello panel closed");
protyle.destroy(); protyle?.destroy();
}); });
async function initProtyle() { async function initProtyle() {
let sql = "SELECT * FROM blocks ORDER BY RANDOM () LIMIT 1;";
let blocks: Block[] = await query(sql);
blockID = blocks[0].id;
return new Protyle(app, divProtyle, { return new Protyle(app, divProtyle, {
blockId: blockID blockId: blockID
}); });

View file

@ -80,15 +80,17 @@ export default class PluginSample extends Plugin {
</symbol>`); </symbol>`);
let tabDiv = document.createElement("div"); let tabDiv = document.createElement("div");
new HelloExample({ let app = null;
target: tabDiv,
props: {
app: this.app,
}
});
this.custom = this.addTab({ this.custom = this.addTab({
type: TAB_TYPE, type: TAB_TYPE,
init() { init() {
app = new HelloExample({
target: tabDiv,
props: {
app: this.app,
blockID: this.data.blockID
}
});
this.element.appendChild(tabDiv); this.element.appendChild(tabDiv);
console.log(this.element); console.log(this.element);
}, },
@ -96,6 +98,7 @@ export default class PluginSample extends Plugin {
console.log("before destroy tab:", TAB_TYPE); console.log("before destroy tab:", TAB_TYPE);
}, },
destroy() { destroy() {
app?.$destroy();
console.log("destroy tab:", TAB_TYPE); console.log("destroy tab:", TAB_TYPE);
} }
}); });
@ -464,20 +467,7 @@ export default class PluginSample extends Plugin {
} }
private showDialog() { private showDialog() {
// let dialog = new Dialog({ const docId = this.getEditor().protyle.block.rootID;
// title: `SiYuan ${Constants.SIYUAN_VERSION}`,
// content: `<div id="helloPanel" class="b3-dialog__content"></div>`,
// width: this.isMobile ? "92vw" : "720px",
// destroyCallback() {
// // hello.$destroy();
// },
// });
// new HelloExample({
// target: dialog.element.querySelector("#helloPanel"),
// props: {
// app: this.app,
// }
// });
svelteDialog({ svelteDialog({
title: `SiYuan ${Constants.SIYUAN_VERSION}`, title: `SiYuan ${Constants.SIYUAN_VERSION}`,
width: this.isMobile ? "92vw" : "720px", width: this.isMobile ? "92vw" : "720px",
@ -486,6 +476,7 @@ export default class PluginSample extends Plugin {
target: container, target: container,
props: { props: {
app: this.app, app: this.app,
blockID: docId
} }
}); });
} }
@ -498,11 +489,19 @@ export default class PluginSample extends Plugin {
}); });
menu.addItem({ menu.addItem({
icon: "iconSettings", icon: "iconSettings",
label: "Open Setting", label: "Open SiYuan Setting",
click: () => {
openSetting(this.app);
}
});
menu.addItem({
icon: "iconSettings",
label: "Open Plugin Setting",
click: () => { click: () => {
this.openSetting(); this.openSetting();
} }
}); });
menu.addSeparator();
menu.addItem({ menu.addItem({
icon: "iconDrag", icon: "iconDrag",
label: "Open Attribute Panel", label: "Open Attribute Panel",
@ -532,7 +531,7 @@ export default class PluginSample extends Plugin {
if (!this.isMobile) { if (!this.isMobile) {
menu.addItem({ menu.addItem({
icon: "iconFace", icon: "iconFace",
label: "Open Custom Tab", label: "Open Custom Tab(open doc first)",
click: () => { click: () => {
const tab = openTab({ const tab = openTab({
app: this.app, app: this.app,
@ -540,7 +539,8 @@ export default class PluginSample extends Plugin {
icon: "iconFace", icon: "iconFace",
title: "Custom Tab", title: "Custom Tab",
data: { data: {
text: platformUtils.isHuawei() ? "Hello, Huawei!" : "This is my custom tab", // text: platformUtils.isHuawei() ? "Hello, Huawei!" : "This is my custom tab",
blockID: this.getEditor().protyle.block.rootID,
}, },
id: this.name + TAB_TYPE id: this.name + TAB_TYPE
}, },