Skip to content

Commit

Permalink
fix: 新增菜单开关
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Nov 9, 2023
1 parent f9105a4 commit 8589364
Show file tree
Hide file tree
Showing 15 changed files with 470 additions and 182 deletions.
1 change: 1 addition & 0 deletions siyuan/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"cancel": "Cancel",
"save": "Save",
"publishTo": "Publish to...",
"publishToQuick": "Publish to..",
"batchSync": "Batch sync",
"publishNormal": "Publish normal",
"extendFunction": "Extended functionality",
Expand Down
1 change: 1 addition & 0 deletions siyuan/i18n/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"cancel": "取消",
"save": "保存",
"publishTo": "一键发布",
"publishToQuick": "发布到..",
"batchSync": "批量分发",
"publishNormal": "常规发布",
"extendFunction": "扩展功能",
Expand Down
81 changes: 79 additions & 2 deletions siyuan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@
* questions.
*/

import { App, getFrontend, IModel, IObject, Plugin, confirm } from "siyuan"
import { App, confirm, getFrontend, IMenuItemOption, IModel, IObject, Menu, Plugin, showMessage } from "siyuan"
import { SiyuanConfig, SiyuanKernelApi } from "zhi-siyuan-api"
import { createSiyuanAppLogger } from "./appLogger"
import { WidgetInvoke } from "./invoke/widgetInvoke"
import { Topbar } from "./topbar"
import { ILogger } from "zhi-lib-base"

import "./index.styl"
import { ConfigManager } from "~/siyuan/store/config.ts"
import MenuUtils from "~/siyuan/utils/menuUtils.ts"
import { PluginInvoke } from "~/siyuan/invoke/pluginInvoke.ts"
import { icons } from "~/siyuan/utils/svg.ts"
import { PreferenceConfigManager } from "~/siyuan/store/preferenceConfigManager.ts"

/**
* 发布工具插件入口
Expand All @@ -38,15 +44,20 @@ import "./index.styl"
* @since 0.1.0
*/
export default class PublisherPlugin extends Plugin {
private logger: any
private logger: ILogger
private topbar: Topbar

public isMobile: boolean
public kernelApi: SiyuanKernelApi
private widgetInvoke: WidgetInvoke
private pluginInvoke: PluginInvoke

customTabObject: () => IModel
public tabInstance: any

private publishSetting: any
private prefSetting: any

constructor(options: { app: App; id: string; name: string; i18n: IObject }) {
super(options)

Expand All @@ -60,6 +71,7 @@ export default class PublisherPlugin extends Plugin {

this.topbar = new Topbar(this)
this.widgetInvoke = new WidgetInvoke(this)
this.pluginInvoke = new PluginInvoke(this)
}

openSetting(): void {
Expand All @@ -75,6 +87,18 @@ export default class PublisherPlugin extends Plugin {
this.mountFn()
}

onLayoutReady() {
// onEvent
this.onEvent()
}

onunload() {
// unmountFn
this.unmountFn()
// offEvent
this.offEvent()
}

// ================
// private methods
// ================
Expand All @@ -101,4 +125,57 @@ export default class PublisherPlugin extends Plugin {
win.syp = win.syp ?? {}
win.syp.alert = elAlertBox
}

private unmountFn() {
const win = window as any
win.syp = undefined
}

private async onEvent() {
// 预加载数据
this.publishSetting = await ConfigManager.loadConfig(this)
this.prefSetting = await PreferenceConfigManager.loadConfig(this)
this.eventBus.on("click-editortitleicon", this.blockMenuEventListener)
}

private offEvent() {
this.eventBus.off("click-editortitleicon", () => {})
}

/**
* 添加文档菜单项
*/
protected readonly blockMenuEventListener = async (e: CustomEvent) => {
// 获取菜单信息
const detail = e.detail
this.logger.info("detail =>", detail)

// 获取块菜单上下文
const context: any = detail?.menu?.menus
if (!context) {
this.logger.error("获取发布菜单失败")
return
}
this.logger.info("当前上下文 =>", context)

const pageId = detail?.protyle?.block.rootID
if (!pageId) {
this.logger.error("无法获取文档 ID")
return
}
this.logger.info("当前文档 ID =>", detail)

if (this.prefSetting.showDocQuickMenu === false) {
this.logger.warn("文档发布菜单已关闭")
return
}

// 快速发布
const quickMenus = MenuUtils.getQuickMenus(this, this.widgetInvoke, this.publishSetting, pageId)
context.push({
iconHTML: `<span class="iconfont-icon">${icons.iconPlane}</span>`,
label: this.i18n.publishToQuick,
submenu: quickMenus,
})
}
}
8 changes: 4 additions & 4 deletions siyuan/invoke/pluginInvoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import PublisherPlugin from "../index"
import { createSiyuanAppLogger } from "../appLogger"
import { showIframeDialog } from "../iframeDialog"
import PageUtil from "../utils/pageUtil"
import PageUtils from "../utils/pageUtils.ts"
import { IObject } from "siyuan"
import { isSiyuanFileExists } from "../utils/utils"

Expand All @@ -45,7 +45,7 @@ export class PluginInvoke {
}

public async showBlogDialog() {
const pageId: string | undefined = PageUtil.getPageId()
const pageId: string | undefined = PageUtils.getPageId()
const pageUrl = `${this.blogPluginBase}/post/${pageId}`

// 临时开启预览权限
Expand Down Expand Up @@ -74,13 +74,13 @@ export class PluginInvoke {
}

public async showPicbedDialog() {
const pageId: string | undefined = PageUtil.getPageId()
const pageId: string | undefined = PageUtils.getPageId()
const pageUrl = `${this.picgoPluginBase}/?pageId=${pageId}`
showIframeDialog(this.pluginInstance, pageUrl)
}

public async showPicbedSettingDialog() {
const pageId: string | undefined = PageUtil.getPageId()
const pageId: string | undefined = PageUtils.getPageId()
const pageUrl = `${this.picgoPluginBase}/setting?pageId=${pageId}`
showIframeDialog(this.pluginInstance, pageUrl)
}
Expand Down
20 changes: 8 additions & 12 deletions siyuan/invoke/widgetInvoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import { DeviceDetection, DeviceTypeEnum } from "zhi-device"
import { createSiyuanAppLogger } from "../appLogger"
import PageUtil from "../utils/pageUtil"
import PageUtils from "../utils/pageUtils.ts"
import { showIframeDialog } from "../iframeDialog"
import PublisherPlugin from "../index"
import { StrUtil } from "zhi-common"
Expand All @@ -44,7 +44,7 @@ export class WidgetInvoke {
}

public async showPublisherBatchPublishDialog() {
let pageId: string | undefined = PageUtil.getPageId()
let pageId: string | undefined = PageUtils.getPageId()
if (pageId == "") {
pageId = undefined
}
Expand All @@ -57,23 +57,23 @@ export class WidgetInvoke {
}

public async showPublisherAiChatDialog() {
let pageId: string | undefined = PageUtil.getPageId()
let pageId: string | undefined = PageUtils.getPageId()
if (pageId == "") {
pageId = undefined
}
await this.showPage(`/ai/chat?id=${pageId}`)
}

public async showPublisherAiChatTab() {
let pageId: string | undefined = PageUtil.getPageId()
let pageId: string | undefined = PageUtils.getPageId()
if (pageId == "") {
pageId = undefined
}
this.showTab(`/ai/chat?id=${pageId}`)
}

public async showPublisherSinglePublishDialog() {
let pageId: string | undefined = PageUtil.getPageId()
let pageId: string | undefined = PageUtils.getPageId()
if (pageId == "") {
pageId = undefined
}
Expand All @@ -85,11 +85,7 @@ export class WidgetInvoke {
await this.showPage(`/publish/singlePublish?id=${pageId}`)
}

public async showPublisherQuickPublishDialog(key: string) {
let pageId: string | undefined = PageUtil.getPageId()
if (pageId == "") {
pageId = undefined
}
public async showPublisherQuickPublishDialog(key: string, pageId?: string) {
this.logger.debug("pageId=>", pageId)
if (StrUtil.isEmptyString(key) || StrUtil.isEmptyString(pageId)) {
showMessage(`平台key和文档ID不能为空,注意:您必须打开当前文档才能进行发布操作`, 2000, "error")
Expand All @@ -103,7 +99,7 @@ export class WidgetInvoke {
}

public async showPublisherGeneralSettingDialog() {
let pageId: string | undefined = PageUtil.getPageId()
let pageId: string | undefined = PageUtils.getPageId()
if (pageId == "") {
pageId = undefined
}
Expand All @@ -112,7 +108,7 @@ export class WidgetInvoke {
}

public async showPublisherAboutDialog() {
let pageId: string | undefined = PageUtil.getPageId()
let pageId: string | undefined = PageUtils.getPageId()
if (pageId == "") {
pageId = undefined
}
Expand Down
51 changes: 51 additions & 0 deletions siyuan/store/preferenceConfigManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 PublisherPlugin from "../index"
import { JsonUtil } from "zhi-common"

/**
* 配置管理类
* 提供配置的加载、保存和删除功能
*/
export class PreferenceConfigManager {
private static storageKey = "/data/storage/syp/publish-preference-cfg.json"
private static cfgKey="publish-preference-cfg"

/**
* 加载配置
*
* @param pluginInstance PublisherPlugin的实例
* @returns 返回配置对象
*/
public static async loadConfig(pluginInstance: PublisherPlugin): Promise<any> {
const configStr = await pluginInstance.kernelApi.getFile(this.storageKey, "text")
const config= JsonUtil.safeParse<any>(configStr, {} as any)
if(!config[this.cfgKey]){
return {}
}
return JsonUtil.safeParse<any>(config[this.cfgKey],{} as any)
}
}
Loading

0 comments on commit 8589364

Please sign in to comment.