Skip to content

Commit

Permalink
fix: 修复挂件获取文档ID错误的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 8, 2023
1 parent 0021c29 commit 364f689
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"vercelBuild": "python3 scripts/vercel_build.py",
"nginxBuild": "python scripts/nginx_build.py",
"widgetBuild": "python scripts/widget_build.py",
"widgetTest": "python scripts/widget_build.py -t",
"extBuild": "python scripts/ext_build.py",
"syncVersion": "python scripts/version.py",
"parseChangelog": "python scripts/parse_changelog.py",
Expand Down
8 changes: 6 additions & 2 deletions src/components/publish/form/PublishPlatform.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
-->

<script setup lang="ts">
import { onMounted, reactive } from "vue"
import {onMounted, reactive, toRaw} from "vue"
import { JsonUtil, StrUtil } from "zhi-common"
import { DynamicConfig, DynamicJsonCfg, getDynPostidKey } from "~/src/platforms/dynamicConfig.ts"
import { DYNAMIC_CONFIG_KEY } from "~/src/utils/constants.ts"
Expand Down Expand Up @@ -83,14 +83,18 @@ onMounted(async () => {
const enabledConfigs = dynJsonCfg.totalCfg?.filter(
(config: DynamicConfig) => config.isEnabled === true && config.isAuth === true
)
logger.info("setting=>", toRaw(setting))
logger.info("props.id=>", props.id)
const postMeta = setting[props.id] ?? {}
// 默认展示通用平台
formData.dynamicConfigArray = enabledConfigs || []
// 检测是否已经发布
formData.dynamicConfigArray.forEach((item) => {
const key = item.platformKey
const posidKey = getDynPostidKey(key)
logger.info("postMeta=>", toRaw(postMeta))
logger.info("posidKey=>", posidKey)
if (!StrUtil.isEmptyString(posidKey)) {
const postMeta = setting[props.id] ?? {}
const postid = postMeta[posidKey] ?? ""
if (!StrUtil.isEmptyString(postid)) {
handleCheck(key)
Expand Down
46 changes: 21 additions & 25 deletions src/utils/widgetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,34 +168,30 @@ const doOpenBrowserWindow = (
}

/**
* 获取挂件所在的块ID
* 如果挂件未找到或块ID无效,则返回空字符串。
* 获取主窗口页面的节点ID
*
* @param doc - 父窗口的 document 对象
* @returns 返回页面的节点ID,如果不存在,则返回undefined
*/
export const getWidgetId = (): string => {
// 检查是否在 iframe 中
if (
window.frameElement == null ||
window.frameElement.parentElement == null ||
window.frameElement.parentElement.parentElement == null
) {
// 如果不在 iframe 中,返回空字符串
return ""
}
const getMainWindowPageId = (doc: Document): string | undefined => {
// 查找包含 protyle 类但不包含 fn__none 的 div 元素
const protyleElement = doc.querySelector("div.protyle:not(.fn__none)")

// 获取 iframe 的父级父级元素(即挂件所在的块)
const widgetContainer = window.frameElement.parentElement.parentElement
if (!widgetContainer) {
// 如果父级父级元素不存在,返回空字符串
return ""
}
// 在该 div 元素下查找包含 protyle-title 类的 div 元素,并获取 data-node-id 属性的值
const protyleTitleElement = protyleElement?.querySelector("div.protyle-title")
const nodeId = protyleTitleElement?.getAttribute("data-node-id")

// 获取块的唯一标识符(数据节点ID)
const widgetId = widgetContainer.getAttribute("data-node-id")
if (!widgetId) {
// 如果块的唯一标识符不存在,返回空字符串
return ""
}
return nodeId
}

/**
* 获取挂件所在的块ID
*
* 如果挂件未找到或块ID无效,则返回undefined
* @returns 返回挂件所在的块ID,如果不存在,则返回undefined
*/
export const getWidgetId = (): string | undefined => {
const parentDocument = window.parent.document
// 返回挂件所在的块ID
return widgetId
return getMainWindowPageId(parentDocument)
}

0 comments on commit 364f689

Please sign in to comment.