Skip to content

Commit

Permalink
feat: 支持复制文档ID
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 15, 2023
1 parent 70bc06b commit 21a5372
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 32 deletions.
6 changes: 6 additions & 0 deletions plugins/publisher-main/public/i18n/en_US.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"publishTool": "Publish Tool",
"publish": "Publish",
"picbed": "Upload picture to picbed",
"setting": "Setting",
"settingGeneral": "General setting",
"settingPicbed": "Picbed manage",
"settingPublish": "Publish setting",
"settingMenuTips": "Let creation return to fun",
"copyPageId": "Copy pageId",
"publisherWidget": "Publisher Widget",
"cancel": "Cancel",
"save": "Save",
Expand Down
6 changes: 6 additions & 0 deletions plugins/publisher-main/public/i18n/zh_CN.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"publishTool": "发布工具",
"publish": "发布",
"picbed": "上传图片到图床",
"setting": "设置",
"settingGeneral": "偏好设置",
"settingPicbed": "图床管理",
"settingPublish": "发布设置",
"settingMenuTips": "让创作回归乐趣",
"copyPageId": "复制文档ID",
"publisherWidget": "挂件版",
"cancel": "取消",
"save": "保存",
Expand Down
11 changes: 7 additions & 4 deletions plugins/publisher-main/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@

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

<Router {url}>
<div>
<Route path="/setting" component={Preferences} />
<Route path="/" component={Publish} />
<Route path={`/${Constants.Page.Publish}`} component={Publish} />
<Route path={`/${Constants.Page.Picbed}`} component={Picbed} />
<Route path={`/${Constants.Page.Setting}`} component={Preferences} />
</div>
</Router>
89 changes: 61 additions & 28 deletions plugins/publisher-main/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Dialog, isMobile, Menu, Plugin } from "siyuan"
import App from "./App.svelte"
import { DeviceDetection, DeviceTypeEnum } from "zhi-device"
import { Env } from "zhi-env"
import { CustomLogFactory, DefaultLogger } from "zhi-log"
import "./index.styl"
import PageUtil from "./utils/pageUtil"
import Constants from "./utils/constants"
import iconPublish from "./utils/svg"
import HtmlUtils from "./utils/htmlUtils"

const STORAGE_NAME = "menu-config"
const SETTING_CONTAINER = "publish-tool-setting"

// https://github.com/siyuan-note/siyuan/pull/8188/
// https://github.com/vitejs/vite/issues/6582#issuecomment-1546954468
// https://github.com/sveltejs/svelte-preprocess/issues/91#issuecomment-548527600
export default class PublishTool extends Plugin {
Expand All @@ -31,7 +34,7 @@ export default class PublishTool extends Plugin {
// private functions
private _addTopBar() {
const topBarElement = this.addTopBar({
icon: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M16.1 260.2c-22.6 12.9-20.5 47.3 3.6 57.3L160 376V479.3c0 18.1 14.6 32.7 32.7 32.7c9.7 0 18.9-4.3 25.1-11.8l62-74.3 123.9 51.6c18.9 7.9 40.8-4.5 43.9-24.7l64-416c1.9-12.1-3.4-24.3-13.5-31.2s-23.3-7.5-34-1.4l-448 256zm52.1 25.5L409.7 90.6 190.1 336l1.2 1L68.2 285.7zM403.3 425.4L236.7 355.9 450.8 116.6 403.3 425.4z"/></svg>`,
icon: iconPublish.iconPlane,
title: this.i18n.publishTool,
position: "right",
callback: () => {
Expand All @@ -52,7 +55,7 @@ export default class PublishTool extends Plugin {
label: this.i18n.publishTo,
submenu: [
{
iconHTML: `<span class="pt-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M0 64C0 46.3 14.3 32 32 32c229.8 0 416 186.2 416 416c0 17.7-14.3 32-32 32s-32-14.3-32-32C384 253.6 226.4 96 32 96C14.3 96 0 81.7 0 64zM0 416a64 64 0 1 1 128 0A64 64 0 1 1 0 416zM32 160c159.1 0 288 128.9 288 288c0 17.7-14.3 32-32 32s-32-14.3-32-32c0-123.7-100.3-224-224-224c-17.7 0-32-14.3-32-32s14.3-32 32-32z"/></svg></span>`,
iconHTML: iconPublish.iconCnblogs,
label: this.i18n.platformCnblogs,
click: () => {
this.logger.debug("发布到博客园")
Expand All @@ -61,6 +64,16 @@ export default class PublishTool extends Plugin {
],
})

// 图床
menu.addSeparator()
menu.addItem({
iconHTML: iconPublish.iconPicture,
label: this.i18n.picbed,
click: () => {
this._showPicbedDialog()
},
})

// 设置
menu.addSeparator()
menu.addItem({
Expand All @@ -69,6 +82,20 @@ export default class PublishTool extends Plugin {
click: () => {
this._showSettingDialog()
},
submenu: [
{
label: this.i18n.settingGeneral,
click: () => {
console.log(11111)
},
},
{
label: this.i18n.settingPicbed,
},
{
label: this.i18n.settingPublish,
},
],
})

// 挂件版
Expand All @@ -81,6 +108,18 @@ export default class PublishTool extends Plugin {
},
})

// 当前文档ID
const pageId = PageUtil.getPageId()
menu.addSeparator()
menu.addItem({
iconHTML: iconPublish.iconOl,
label: this.i18n.copyPageId,
click: async () => {
await HtmlUtils.copyToClipboard(pageId)
this.logger.info("当前文档ID已复制", pageId)
},
})

// slogan
menu.addSeparator()
menu.addItem({
Expand All @@ -89,18 +128,6 @@ export default class PublishTool extends Plugin {
type: "readonly",
})

// 调试阶段显示当前文档ID
if (this.env.isDev()) {
const pageId = "112123e343242-2323"
menu.addSeparator()
menu.addItem({
label: pageId,
type: "readonly",
click: () => {
this.logger.debug("当前文档ID已复制", pageId)
},
})
}
if (isMobile()) {
menu.fullscreen()
} else {
Expand All @@ -112,20 +139,26 @@ export default class PublishTool extends Plugin {
}
}

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

// setting
PageUtil.createApp(Constants.Page.Picbed)
}

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

// setting
new App({
target: document.getElementById(SETTING_CONTAINER) as HTMLElement,
props: {
url: "/setting",
},
})
PageUtil.createApp(Constants.Page.Setting)
}

private _showPublisherDialog(publisherIndex: string) {
Expand Down Expand Up @@ -159,15 +192,15 @@ export default class PublishTool extends Plugin {
const publisherBridge = new bridge.default()
publisherBridge.init().then(() => {
// 发布首页
// const pageId: any = "111111"
// const pageUrl = "index.html"
const pageId = PageUtil.getPageId()
const pageUrl = "index.html"

// 博客首页
const pageId: any = undefined
const pageUrl = "blog/index.html"
// const pageId: any = undefined
// const pageUrl = "blog/index.html"

// 详情
// const widgetId = "1111111"
// const pageId = PageUtil.getPageId()
// const pageUrl = "detail/index.html"

win.syp.renderPublishHelper(pageId, pageUrl, win, this.env.isDev())
Expand Down
25 changes: 25 additions & 0 deletions plugins/publisher-main/src/routes/Picbed.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!--
- 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.
-->
<div>Picbed</div>
41 changes: 41 additions & 0 deletions plugins/publisher-main/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.
*/

/**
* 常量
*
* @author terwer
* @version 1.0.0
* @since 1.0.0
*/
const Constants = {
Page: {
Publish: "",
Picbed: "picbed",
Setting: "setting",
},
}

export default Constants
37 changes: 37 additions & 0 deletions plugins/publisher-main/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 plugins/publisher-main/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" : `publish-tool-${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
Loading

0 comments on commit 21a5372

Please sign in to comment.