diff --git a/package.json b/package.json index 3676afcc..805ba727 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "siyuan": "^0.7.1", "stylus": "^0.59.0", "svelte": "^3.57.0", + "svelte-routing": "^1.8.9", "ts-node": "^10.9.1", "vite": "^4.3.7", "vite-plugin-dynamic-import": "^1.4.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5ff6a9ab..8658cac3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -37,6 +37,9 @@ devDependencies: svelte: specifier: ^3.57.0 version: 3.57.0 + svelte-routing: + specifier: ^1.8.9 + version: 1.8.9 ts-node: specifier: ^10.9.1 version: 10.9.1(@types/node@16.0.0)(typescript@5.0.4) @@ -2771,6 +2774,10 @@ packages: svelte: 3.57.0 dev: true + /svelte-routing@1.8.9: + resolution: {integrity: sha512-4+J1F57sKkI5+d5nhCQs2ChPo9tn0gIFqFaA38c+yNYZ5UzsGq80mCVA2E0tqPtkO2VzZTJD0Lw2P8N3Z/068g==} + dev: true + /svelte@3.57.0: resolution: {integrity: sha512-WMXEvF+RtAaclw0t3bPDTUe19pplMlfyKDsixbHQYgCWi9+O9VN0kXU1OppzrB9gPAvz4NALuoca2LfW2bOjTQ==} engines: {node: '>= 8'} diff --git a/src/App.svelte b/src/App.svelte new file mode 100644 index 00000000..cbbec770 --- /dev/null +++ b/src/App.svelte @@ -0,0 +1,42 @@ + + + + + +
+ + + +
+
diff --git a/src/constants.ts b/src/constants.ts index 0deb5eb2..73db0e24 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,2 +1,15 @@ export const moduleBase = "/plugins/siyuan-publisher" export const pluginDataPath = "/data/storage/petal/siyuan-publisher" + +/** + * 常量 + * + * @author terwer + * @version 1.0.0 + * @since 1.0.0 + */ +export const Page = { + Publish: "", + Picbed: "picbed", + Setting: "setting", +} diff --git a/src/index.ts b/src/index.ts index cfcf8c07..445423df 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,12 @@ -import { isMobile, Menu, Plugin } from "siyuan" +import { Dialog, isMobile, Menu, Plugin } from "siyuan" import "./index.styl" import { initLibs } from "~/src/loader" import { initTools } from "~/src/tools" import iconPublish from "~/src/utils/svg" -import { Utils } from "~/src/utils/utils"; +import { Utils } from "~/src/utils/utils" +import HtmlUtils from "~/src/utils/htmlUtils" +import PageUtil from "~/src/utils/pageUtil" +import { Page } from "~/src/constants" export default class PublisherPlugin extends Plugin { public fs @@ -70,6 +73,10 @@ export default class PublisherPlugin extends Plugin { this.logger.info(this.i18n.publisherUnloaded) } + public openSetting() { + this.showSettingDialog() + } + // ====================== // private functions // ====================== @@ -110,21 +117,22 @@ export default class PublisherPlugin extends Plugin { label: this.i18n.platformTypecho, disabled: true, click: () => { - this.logger.debug("发布到Typecho") + this.logger.info("发布到Typecho") }, }, { iconHTML: iconPublish.iconWordpress, label: this.i18n.platformWordpress, click: () => { - this.logger.debug("发布到WordPress") + this.showPublisherDialog() + this.logger.info("发布到WordPress") }, }, { iconHTML: iconPublish.iconYuque, label: this.i18n.platformYuque, click: () => { - this.logger.debug("发布到语雀") + this.logger.info("发布到语雀") }, }, { @@ -135,21 +143,21 @@ export default class PublisherPlugin extends Plugin { iconHTML: iconPublish.iconHexo, label: this.i18n.platformHexo, click: () => { - this.logger.debug("发布到Hexo") + this.logger.info("发布到Hexo") }, }, { iconHTML: iconPublish.iconHugo, label: this.i18n.platformHugo, click: () => { - this.logger.debug("发布到Hugo") + this.logger.info("发布到Hugo") }, }, { iconHTML: iconPublish.iconVue, label: this.i18n.platformVitepress, click: () => { - this.logger.debug("发布到Vitepress") + this.logger.info("发布到Vitepress") }, }, ], @@ -162,7 +170,9 @@ export default class PublisherPlugin extends Plugin { menu.addItem({ iconHTML: iconPublish.iconPicture, label: this.i18n.picbed, - // click: () => {}, + click: () => { + this.showPicbedDialog() + }, }) // 设置 @@ -170,7 +180,9 @@ export default class PublisherPlugin extends Plugin { menu.addItem({ icon: "iconSettings", label: this.i18n.setting, - // click: () => {}, + click: () => { + this.showSettingDialog() + }, submenu: [ { iconHTML: iconPublish.iconPreference, @@ -189,18 +201,18 @@ export default class PublisherPlugin extends Plugin { }) // 当前文档ID - // const pageId = PageUtil.getPageId() + const pageId = PageUtil.getPageId() menu.addSeparator() menu.addItem({ iconHTML: iconPublish.iconOl, label: this.i18n.copyPageId, click: async () => { - // await HtmlUtils.copyToClipboard(pageId) - // this.kernelApi.pushMsg({ + await HtmlUtils.copyToClipboard(pageId) + // this.zhiSiyuanApi.kernelApi.pushMsg({ // msg: `当前文档ID已复制=>${pageId}`, // timeout: 3000, // }) - // this.logger.info("当前文档ID已复制", pageId) + this.logger.info("当前文档ID已复制", pageId) }, }) @@ -222,4 +234,37 @@ export default class PublisherPlugin extends Plugin { }) } } + + private showSettingDialog() { + new Dialog({ + title: `${this.i18n.setting} - ${this.i18n.publisher}`, + content: `
`, + width: isMobile() ? "92vw" : "520px", + }) + + // setting + PageUtil.createApp(Page.Setting) + } + + private showPublisherDialog() { + new Dialog({ + title: `${this.i18n.setting} - ${this.i18n.publisher}`, + content: `
`, + width: isMobile() ? "92vw" : "520px", + }) + + // setting + PageUtil.createApp(Page.Publish) + } + + private showPicbedDialog() { + new Dialog({ + title: `${this.i18n.picbed} - ${this.i18n.publisher}`, + content: `
`, + width: isMobile() ? "92vw" : "520px", + }) + + // setting + PageUtil.createApp(Page.Picbed) + } } diff --git a/src/utils/htmlUtils.ts b/src/utils/htmlUtils.ts new file mode 100644 index 00000000..314bbf6b --- /dev/null +++ b/src/utils/htmlUtils.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023, Terwer . All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Terwer designates this + * particular file as subject to the "Classpath" exception as provided + * by Terwer in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com + * or visit www.terwer.space if you need additional information or have any + * questions. + */ + +class HtmlUtils { + public static async copyToClipboard(text: string): Promise { + try { + await navigator.clipboard.writeText(text) + return true + } catch (err) { + throw new Error("复制失败!" + err) + } + } +} + +export default HtmlUtils diff --git a/src/utils/pageUtil.ts b/src/utils/pageUtil.ts new file mode 100644 index 00000000..cab7d140 --- /dev/null +++ b/src/utils/pageUtil.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023, Terwer . All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Terwer designates this + * particular file as subject to the "Classpath" exception as provided + * by Terwer in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com + * or visit www.terwer.space if you need additional information or have any + * questions. + */ + +import App from "../App.svelte" + +/** + * 页面统一入口,根据路由创建不同页面 + */ +class PageUtil { + public static getPageId() { + const element = document.querySelector(".protyle-title") + const result = element ? element.getAttribute("data-node-id") : null + return result ?? "" + } + + public static getElementId(pageId: string) { + return pageId === "" ? "home" : `publisher-${pageId}` + } + + public static createApp(pageId: string): App { + const elementId = this.getElementId(pageId) + return new App({ + target: document.getElementById(elementId) as HTMLElement, + props: { + url: `/${pageId}`, + }, + }) + } +} + +export default PageUtil