copy from plugin-sample:index.ts

This commit is contained in:
frostime 2023-06-03 15:50:28 +08:00
parent c00d5c9373
commit fbfc7467b5
4 changed files with 630 additions and 90 deletions

View file

@ -1,4 +1,15 @@
import { Plugin, showMessage, confirm, Dialog, Menu, isMobile, openTab, adaptHotkey } from "siyuan";
import {
Plugin,
showMessage,
confirm,
Dialog,
Menu,
openTab,
adaptHotkey,
getFrontend,
getBackend,
IModel
} from "siyuan";
import "./index.scss";
import HelloExample from "./hello.svelte";
@ -11,20 +22,53 @@ const DOCK_TYPE = "dock_tab";
export default class SamplePlugin extends Plugin {
private customTab: () => any;
private isMobile: boolean;
async onload() {
showMessage("Hello SiYuan Plugin");
this.data[STORAGE_NAME] = {readonlyText: "Readonly"};
this.data[STORAGE_NAME] = { readonlyText: "Readonly" };
const frontEnd = getFrontend();
this.isMobile = frontEnd === "mobile" || frontEnd === "browser-mobile";
// 图标的制作参见帮助文档
this.addIcons(`<symbol id="iconFace" viewBox="0 0 32 32">
<path d="M13.667 17.333c0 0.92-0.747 1.667-1.667 1.667s-1.667-0.747-1.667-1.667 0.747-1.667 1.667-1.667 1.667 0.747 1.667 1.667zM20 15.667c-0.92 0-1.667 0.747-1.667 1.667s0.747 1.667 1.667 1.667 1.667-0.747 1.667-1.667-0.747-1.667-1.667-1.667zM29.333 16c0 7.36-5.973 13.333-13.333 13.333s-13.333-5.973-13.333-13.333 5.973-13.333 13.333-13.333 13.333 5.973 13.333 13.333zM14.213 5.493c1.867 3.093 5.253 5.173 9.12 5.173 0.613 0 1.213-0.067 1.787-0.16-1.867-3.093-5.253-5.173-9.12-5.173-0.613 0-1.213 0.067-1.787 0.16zM5.893 12.627c2.28-1.293 4.040-3.4 4.88-5.92-2.28 1.293-4.040 3.4-4.88 5.92zM26.667 16c0-1.040-0.16-2.040-0.44-2.987-0.933 0.2-1.893 0.32-2.893 0.32-4.173 0-7.893-1.92-10.347-4.92-1.4 3.413-4.187 6.093-7.653 7.4 0.013 0.053 0 0.12 0 0.187 0 5.88 4.787 10.667 10.667 10.667s10.667-4.787 10.667-10.667z"></path>
</symbol>
<symbol id="iconSaving" viewBox="0 0 32 32">
<path d="M20 13.333c0-0.733 0.6-1.333 1.333-1.333s1.333 0.6 1.333 1.333c0 0.733-0.6 1.333-1.333 1.333s-1.333-0.6-1.333-1.333zM10.667 12h6.667v-2.667h-6.667v2.667zM29.333 10v9.293l-3.76 1.253-2.24 7.453h-7.333v-2.667h-2.667v2.667h-7.333c0 0-3.333-11.28-3.333-15.333s3.28-7.333 7.333-7.333h6.667c1.213-1.613 3.147-2.667 5.333-2.667 1.107 0 2 0.893 2 2 0 0.28-0.053 0.533-0.16 0.773-0.187 0.453-0.347 0.973-0.427 1.533l3.027 3.027h2.893zM26.667 12.667h-1.333l-4.667-4.667c0-0.867 0.12-1.72 0.347-2.547-1.293 0.333-2.347 1.293-2.787 2.547h-8.227c-2.573 0-4.667 2.093-4.667 4.667 0 2.507 1.627 8.867 2.68 12.667h2.653v-2.667h8v2.667h2.68l2.067-6.867 3.253-1.093v-4.707z"></path>
</symbol>`);
const topBarElement = this.addTopBar({
icon: "iconEmoji",
icon: "iconFace",
title: this.i18n.addTopBarIcon,
position: "left",
position: "right",
callback: () => {
this.addMenu(topBarElement.getBoundingClientRect());
let rect = topBarElement.getBoundingClientRect();
// 如果被隐藏,则使用更多按钮
if (rect.width === 0) {
rect = document.querySelector("#barMore").getBoundingClientRect();
}
this.addMenu(rect);
}
});
const statusIconTemp = document.createElement("template");
statusIconTemp.innerHTML = `<div class="toolbar__item b3-tooltips b3-tooltips__w" aria-label="Remove plugin-sample Data">
<svg>
<use xlink:href="#iconTrashcan"></use>
</svg>
</div>`;
statusIconTemp.content.firstElementChild.addEventListener("click", () => {
confirm("⚠️", this.i18n.confirmRemove.replace("${name}", this.name), () => {
this.removeData(STORAGE_NAME).then(() => {
this.data[STORAGE_NAME] = { readonlyText: "Readonly" };
showMessage(`[${this.name}]: ${this.i18n.removedData}`);
});
});
});
this.addStatusBar({
element: statusIconTemp.content.firstElementChild as HTMLElement,
});
let div = document.createElement("div");
new HelloExample({
target: div,
@ -44,10 +88,18 @@ export default class SamplePlugin extends Plugin {
}
});
this.addCommand({
langKey: "showDialog",
hotkey: "⇧⌘M",
callback: () => {
this.showDialog();
}
});
this.addDock({
config: {
position: "LeftBottom",
size: {width: 200, height: 0},
size: { width: 200, height: 0 },
icon: "iconEmoji",
title: "Custom Dock",
},
@ -79,6 +131,7 @@ export default class SamplePlugin extends Plugin {
onLayoutReady() {
this.loadData(STORAGE_NAME);
console.log(`frontend: ${getFrontend()}; backend: ${getBackend()}`);
}
onunload() {
@ -87,11 +140,27 @@ export default class SamplePlugin extends Plugin {
console.log("onunload");
}
private wsEvent({ detail }: any) {
openSetting(): void {
let dialog = new Dialog({
title: "SettingPannel",
content: `<div id="SettingPanel"></div>`,
width: "600px",
destroyCallback: (options) => {
console.log("destroyCallback", options);
//You must destroy the component when the dialog is closed
pannel.$destroy();
}
});
let pannel = new SettingPannel({
target: dialog.element.querySelector("#SettingPanel"),
});
}
private eventBusLog({ detail }: any) {
console.log(detail);
}
private blockIconEvent({detail}: any) {
private blockIconEvent({ detail }: any) {
console.log(detail);
detail.menu.addSeparator(0);
const ids: string[] = [];
@ -106,70 +175,108 @@ export default class SamplePlugin extends Plugin {
});
}
private async addMenu(rect: DOMRect) {
private showDialog() {
new Dialog({
title: "Info",
content: '<div class="b3-dialog__content">This is a dialog</div>',
width: this.isMobile ? "92vw" : "520px",
});
}
private addMenu(rect: DOMRect) {
const menu = new Menu("topBarSample", () => {
console.log(this.i18n.byeMenu);
});
menu.addItem({
icon: "iconHelp",
label: "Confirm",
click() {
confirm("Confirm", "Is this a confirm?", () => {
showMessage("confirm");
}, () => {
showMessage("cancel");
});
}
});
menu.addItem({
icon: "iconFeedback",
label: "Message",
click: () => {
showMessage(this.i18n.helloPlugin);
}
});
menu.addItem({
icon: "iconInfo",
label: "Dialog",
click: () => this.openHelloInDialog()
accelerator: this.commands[0].customHotkey,
click: this.showDialog
});
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Tab",
click: () => {
openTab({
custom: {
icon: "iconEmoji",
title: "Custom Tab",
data: {
text: "This is my custom tab",
if (!this.isMobile) {
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Custom Tab",
click: () => {
const tab = openTab({
app: this.app,
custom: {
icon: "iconFace",
title: "Custom Tab",
data: {
text: "This is my custom tab",
},
fn: this.customTab
},
fn: this.customTab
},
});
}
});
menu.addItem({
icon: "iconLayout",
label: "Open Float Layer(open help)",
click: () => {
this.addFloatLayer({
ids: ["20230523173319-xj1l3qu", "20230523173321-55o0w2n"],
defIds: ["20230523173323-imgm9tp", "20230523173324-cxu98t3"],
x: window.innerWidth - 768 - 120,
y: 32
});
}
});
menu.addItem({
icon: "iconTrashcan",
label: "Remove Data",
click: () => {
this.removeData(STORAGE_NAME).then(() => {
this.data[STORAGE_NAME] = {readonlyText: "Readonly"};
});
}
});
});
console.log(tab)
}
});
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Asset Tab(open help first)",
click: () => {
const tab = openTab({
app: this.app,
asset: {
path: "assets/paragraph-20210512165953-ag1nib4.svg"
}
});
console.log(tab)
}
});
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Doc Tab(open help first)",
click: async () => {
const tab = await openTab({
app: this.app,
doc: {
id: "20200812220555-lj3enxa",
}
});
console.log(tab)
}
});
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Search Tab",
click: () => {
const tab = openTab({
app: this.app,
search: {
k: "SiYuan"
}
});
console.log(tab)
}
});
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Card Tab",
click: () => {
const tab = openTab({
app: this.app,
card: {
type: "all"
}
});
console.log(tab)
}
});
menu.addItem({
icon: "iconLayout",
label: "Open Float Layer(open help first)",
click: () => {
this.addFloatLayer({
ids: ["20210428212840-8rqwn5o", "20201225220955-l154bn4"],
defIds: ["20230415111858-vgohvf3", "20200813131152-0wk5akh"],
x: window.innerWidth - 768 - 120,
y: 32
});
}
});
}
menu.addItem({
icon: "iconScrollHoriz",
label: "Event Bus",
@ -178,13 +285,13 @@ export default class SamplePlugin extends Plugin {
icon: "iconSelect",
label: "On ws-main",
click: () => {
this.eventBus.on("ws-main", this.wsEvent);
this.eventBus.on("ws-main", this.eventBusLog);
}
}, {
icon: "iconClose",
label: "Off ws-main",
click: () => {
this.eventBus.off("ws-main", this.wsEvent);
this.eventBus.off("ws-main", this.eventBusLog);
}
}, {
icon: "iconSelect",
@ -202,35 +309,59 @@ export default class SamplePlugin extends Plugin {
icon: "iconSelect",
label: "On click-pdf",
click: () => {
this.eventBus.on("click-pdf", this.wsEvent);
this.eventBus.on("click-pdf", this.eventBusLog);
}
}, {
icon: "iconClose",
label: "Off click-pdf",
click: () => {
this.eventBus.off("click-pdf", this.wsEvent);
this.eventBus.off("click-pdf", this.eventBusLog);
}
}, {
icon: "iconSelect",
label: "On click-editorcontent",
click: () => {
this.eventBus.on("click-editorcontent", this.wsEvent);
this.eventBus.on("click-editorcontent", this.eventBusLog);
}
}, {
icon: "iconClose",
label: "Off click-editorcontent",
click: () => {
this.eventBus.off("click-editorcontent", this.wsEvent);
this.eventBus.off("click-editorcontent", this.eventBusLog);
}
}, {
icon: "iconSelect",
label: "On click-editortitleicon",
click: () => {
this.eventBus.on("click-editortitleicon", this.eventBusLog);
}
}, {
icon: "iconClose",
label: "Off click-editortitleicon",
click: () => {
this.eventBus.off("click-editortitleicon", this.eventBusLog);
}
}, {
icon: "iconSelect",
label: "On open-noneditableblock",
click: () => {
this.eventBus.on("open-noneditableblock", this.eventBusLog);
}
}, {
icon: "iconClose",
label: "Off open-noneditableblock",
click: () => {
this.eventBus.off("open-noneditableblock", this.eventBusLog);
}
}]
});
menu.addSeparator();
menu.addItem({
icon: "iconSparkles",
label: this.data[STORAGE_NAME] || "Readonly",
label: this.data[STORAGE_NAME].readonlyText || "Readonly",
type: "readonly",
});
if (isMobile()) {
if (this.isMobile) {
menu.fullscreen();
} else {
menu.open({
@ -241,22 +372,6 @@ export default class SamplePlugin extends Plugin {
}
}
openSetting(): void {
let dialog = new Dialog({
title: "SettingPannel",
content: `<div id="SettingPanel"></div>`,
width: "600px",
destroyCallback: (options) => {
console.log("destroyCallback", options);
//You must destroy the component when the dialog is closed
pannel.$destroy();
}
});
let pannel = new SettingPannel({
target: dialog.element.querySelector("#SettingPanel"),
});
}
private openHelloInDialog() {
let dialog = new Dialog({
title: "Hello World",