Skip to content

Commit

Permalink
refactor: 新增页面路由
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 20, 2023
1 parent 7d275f7 commit a8cd654
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!--
- 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.
-->

<script lang="ts">
import { Route, Router } from "svelte-routing"
import Publish from "./routes/Publish.svelte"
import Picbed from "./routes/Picbed.svelte"
import Preferences from "./routes/Preferences.svelte"
import { Page } from "~/src/constants"
export let url = `/${Page.Publish}`
</script>

<Router {url}>
<div>
<Route path={`/${Page.Publish}`} component={Publish} />
<Route path={`/${Page.Picbed}`} component={Picbed} />
<Route path={`/${Page.Setting}`} component={Preferences} />
</div>
</Router>
13 changes: 13 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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",
}
73 changes: 59 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -70,6 +73,10 @@ export default class PublisherPlugin extends Plugin {
this.logger.info(this.i18n.publisherUnloaded)
}

public openSetting() {
this.showSettingDialog()
}

// ======================
// private functions
// ======================
Expand Down Expand Up @@ -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("发布到语雀")
},
},
{
Expand All @@ -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")
},
},
],
Expand All @@ -162,15 +170,19 @@ export default class PublisherPlugin extends Plugin {
menu.addItem({
iconHTML: iconPublish.iconPicture,
label: this.i18n.picbed,
// click: () => {},
click: () => {
this.showPicbedDialog()
},
})

// 设置
menu.addSeparator()
menu.addItem({
icon: "iconSettings",
label: this.i18n.setting,
// click: () => {},
click: () => {
this.showSettingDialog()
},
submenu: [
{
iconHTML: iconPublish.iconPreference,
Expand All @@ -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)
},
})

Expand All @@ -222,4 +234,37 @@ export default class PublisherPlugin extends Plugin {
})
}
}

private showSettingDialog() {
new Dialog({
title: `${this.i18n.setting} - ${this.i18n.publisher}`,
content: `<div id="${PageUtil.getElementId(Page.Setting)}"></div>`,
width: isMobile() ? "92vw" : "520px",
})

// setting
PageUtil.createApp(Page.Setting)
}

private showPublisherDialog() {
new Dialog({
title: `${this.i18n.setting} - ${this.i18n.publisher}`,
content: `<div id="${PageUtil.getElementId(Page.Publish)}"></div>`,
width: isMobile() ? "92vw" : "520px",
})

// setting
PageUtil.createApp(Page.Publish)
}

private showPicbedDialog() {
new Dialog({
title: `${this.i18n.picbed} - ${this.i18n.publisher}`,
content: `<div id="${PageUtil.getElementId(Page.Picbed)}"></div>`,
width: isMobile() ? "92vw" : "520px",
})

// setting
PageUtil.createApp(Page.Picbed)
}
}
37 changes: 37 additions & 0 deletions src/utils/htmlUtils.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
try {
await navigator.clipboard.writeText(text)
return true
} catch (err) {
throw new Error("复制失败!" + err)
}
}
}

export default HtmlUtils
53 changes: 53 additions & 0 deletions src/utils/pageUtil.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a8cd654

Please sign in to comment.