Skip to content

Commit

Permalink
feat: #213 挂件模式用挂载菜单的方式使用-弹窗关闭
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Dec 15, 2022
1 parent 0b08791 commit 430d73a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
9 changes: 9 additions & 0 deletions components/blog/BlogMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ import { SIYUAN_CONSTANTS } from "~/utils/constants/siyuanConstants"
import { getEnv } from "~/utils/envUtil"
import { getPublishCfg } from "~/utils/publishUtil"
import { parseBoolean } from "~/utils/util"
import { getSiyuanNewWinPageId } from "~/utils/otherlib/siyuanBrowserUtil"
const logger = LogFactory.getLogger()
Expand Down Expand Up @@ -365,6 +366,14 @@ const reloadTableData = async () => {
logger.warn("处于生产环境,父文档ID为=>", postid)
}
// 如果是思源笔记新窗口打开
const newWinPageId = getSiyuanNewWinPageId()
logger.warn("思源笔记新窗口,postid为=>", postid)
if (newWinPageId) {
postid = newWinPageId
}
// 检测子文档
postCount = await siyuanApi.getSubPostCount(postid)
if (postCount > 1) {
hasSubdoc = true
Expand Down
24 changes: 24 additions & 0 deletions layouts/default/DefaultHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { onMounted, ref } from "vue"
import {
doCloseExportWin,
doOpenExportWin,
getSiyuanNewWinPageId,
isInSiyuanBrowser,
} from "~/utils/otherlib/siyuanBrowserUtil"
import { getPublishCfg } from "~/utils/publishUtil"
Expand All @@ -68,6 +69,9 @@ const handleWinOpen = async () => {
if (showOpenBtn.value) {
try {
await doOpenExportWin()
// event
pageIdChanged()
} catch (e) {
showOpenBtn.value = false
ElMessage.info(
Expand All @@ -81,6 +85,9 @@ const handleWinClose = () => {
if (showCloseBtn.value) {
try {
doCloseExportWin()
// event
pageIdChanged()
} catch (e) {
showCloseBtn.value = false
ElMessage.info(
Expand All @@ -90,10 +97,27 @@ const handleWinClose = () => {
}
}
const pageIdChanged = () => {
const newWinPageId = getSiyuanNewWinPageId()
const isNewWin = typeof newWinPageId === "undefined"
alert(isNewWin)
// if (newWinPageId) {
// showOpenBtn.value = false
// showCloseBtn.value = true
// } else {
// showOpenBtn.value = true
// showCloseBtn.value = false
// }
}
onMounted(() => {
// init
const publishCfg = getPublishCfg()
showCloseBtn.value = isInSiyuanBrowser() || publishCfg.showCloseBtn
showOpenBtn.value = showCloseBtn.value
// event
pageIdChanged()
})
</script>

Expand Down
11 changes: 5 additions & 6 deletions public/lib/siyuanhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const fetchPost = (url, data, cb, headers) => {
window.terwer = {
renderPublishHelper: () => {},
}
window.terwer.renderPublishHelper = () => {
window.terwer.renderPublishHelper = (pageId) => {
const localData = JSON.parse(
localStorage.getItem("local-exportpdf") ||
JSON.stringify({
Expand Down Expand Up @@ -180,11 +180,15 @@ window.terwer.renderPublishHelper = () => {
webviewTag: true,
webSecurity: false,
},
customProperty: {
pageId: pageId,
},
})
window.siyuan.printWin.webContents.userAgent = `SiYuan/${app.getVersion()} https://b3log.org/siyuan Electron`
window.siyuan.printWin.once("ready-to-show", () => {
window.siyuan.printWin.webContents.setZoomFactor(1)
})
window.siyuan.printWin.terwerPageId = "aaaa"
fetchPost(
"/api/export/exportTempContent",
{ content: html },
Expand All @@ -195,11 +199,6 @@ window.terwer.renderPublishHelper = () => {
})
}

// export const destroyPrintWindow = () => {
// getCurrentWindow().webContents.setZoomFactor(1);
// window.siyuan.printWin.destroy();
// };

/**-- 在所有文档前面加上一个挂件插槽--**/
function showPreviousWidgetsSlot() {
setInterval(DocumentShowPreviousWidget, 300)
Expand Down
22 changes: 16 additions & 6 deletions utils/otherlib/siyuanBrowserUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import { isBrowser } from "~/utils/browserUtil"
import { inSiyuan } from "~/utils/platform/siyuan/siyuanUtil"
import { ElMessage } from "element-plus"

const SIYUAN_BROWSER_CONSTANTS_SIYUAN_EXPORT_CLOSE = "siyuan-export-close"

// 警告⚠️:请勿在非思源笔记浏览器环境调用此文件中的任何方法

const SIYUAN_BROWSER_CONSTANTS_SIYUAN_EXPORT_CLOSE = "siyuan-export-close"

/**
* 是否在思源浏览器
* @returns {boolean}
Expand All @@ -49,23 +49,33 @@ export const doCloseExportWin = () => {
const { ipcRenderer } = require("electron")
ipcRenderer.send(SIYUAN_BROWSER_CONSTANTS_SIYUAN_EXPORT_CLOSE)

// getCurrentWindow().webContents.setZoomFactor(1)
if (window.siyuan.printWin) {
window.siyuan.printWin.destroy()
}
}

/**
* 打开思源导出窗口
* 打开的时候 syWin = window.parent
*/
export const doOpenExportWin = async () => {
export const doOpenExportWin = async (pageId) => {
const syWin = window.parent
if (syWin.terwer && syWin.terwer.renderPublishHelper) {
// 打开弹窗
syWin.terwer.renderPublishHelper()
syWin.terwer.renderPublishHelper(pageId)
} else {
ElMessage.warning(
"未找到hook方法,请在自定义js片段添加 import('/widgets/sy-post-publisher/lib/siyuanhook.js') ,并重启思源笔记"
"renderPublishHelper失败,未找到hook方法,请在自定义js片段添加 import('/widgets/sy-post-publisher/lib/siyuanhook.js') ,并重启思源笔记"
)
}
}

// 如果是思源新窗口
export const getSiyuanNewWinPageId = () => {
let pageId
const syWin = window.parent
if (syWin.terwer && syWin.terwer.pageId) {
pageId = syWin.terwer.pageId
}
return pageId
}

0 comments on commit 430d73a

Please sign in to comment.