Skip to content

Commit

Permalink
fix: 优化体验,非插槽不显示按钮
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jan 5, 2023
1 parent 56786d3 commit b340bd2
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 15 deletions.
9 changes: 7 additions & 2 deletions components/picgo/PicgoIndex.vue
Expand Up @@ -84,7 +84,10 @@
:content="$t('picgo.upload.onclick')"
placement="top-start"
>
<el-button type="success">
<el-button
type="success"
@click="picgoManageMethods.handleUploadAllImagesToBed"
>
<font-awesome-icon icon="fa-solid fa-upload" />
</el-button>
</el-tooltip>
Expand Down Expand Up @@ -133,7 +136,9 @@
placement="bottom"
popper-class="publish-menu-tooltip"
>
<el-button>
<el-button
@click="picgoManageMethods.handleUploadCurrentImageToBed(f)"
>
<font-awesome-icon icon="fa-solid fa-upload" />
</el-button>
</el-tooltip>
Expand Down
31 changes: 23 additions & 8 deletions composables/picgo/picgoInitPageCom.ts
Expand Up @@ -29,7 +29,10 @@ import { SiYuanApi } from "~/utils/platform/siyuan/siYuanApi"
import { getPageId } from "~/utils/platform/siyuan/siyuanUtil"
import { ImageParser } from "~/utils/parser/imageParser"
import { getSiyuanCfg } from "~/utils/platform/siyuan/siYuanConfig"
import { pathJoin } from "~/utils/util"
import { isEmptyString, pathJoin } from "~/utils/util"
import { CONSTANTS } from "~/utils/constants/constants"
import { parseJSONObj } from "~/utils/configUtil"
import { ImageItem } from "~/utils/models/imageItem"

/**
* Picgo页面初始化组件
Expand All @@ -49,6 +52,8 @@ export const usePicgoInitPage = (props, deps) => {
// private methods
const initPage = async () => {
const pageId = await getPageId(true, props.pageId)

// 图片信息
const imageBlocks = await siyuanApi.getImageBlocksByID(pageId)
logger.debug("查询文章中的图片块=>", imageBlocks)

Expand All @@ -68,24 +73,34 @@ export const usePicgoInitPage = (props, deps) => {
})
logger.debug("解析出来的所有的图片地址=>", retImgs)

retImgs.forEach((retImg) => {
for (let i = 0; i < retImgs.length; i++) {
const retImg = retImgs[i]
let isLocal = false
const originUrl = retImg
let imgUrl = retImg
if (!(imgUrl.indexOf("http") > -1) && imgUrl.indexOf("assets") > -1) {
const attrs = await siyuanApi.getBlockAttrs(pageId)
logger.debug("attrs=>", attrs)
if (!isEmptyString(attrs[CONSTANTS.PICGO_FILE_MAP_KEY])) {
const fileMap = parseJSONObj(attrs[CONSTANTS.PICGO_FILE_MAP_KEY])
logger.debug("fileMap=>", fileMap)
}

const baseUrl = getSiyuanCfg().baseUrl
imgUrl = pathJoin(baseUrl, "/" + imgUrl)
isLocal = true
}

const imageItem = {
name: imgUrl.substring(imgUrl.lastIndexOf("/") + 1),
url: imgUrl,
isLocal,
}
const imageItem = new ImageItem(
imgUrl.substring(imgUrl.lastIndexOf("/") + 1),
originUrl,
imgUrl,
isLocal
)

logger.debug("imageItem=>", imageItem)
picgoCommonData.fileList.files.push(imageItem)
})
}
}

/**
Expand Down
65 changes: 64 additions & 1 deletion composables/picgo/picgoManageCom.ts
Expand Up @@ -23,13 +23,29 @@
* questions.
*/

import { copyToClipboardInBrowser, isBrowser } from "~/utils/browserUtil"
import {
copyToClipboardInBrowser,
isBrowser,
isElectron,
} from "~/utils/browserUtil"
import { reactive } from "vue"
import { parseJSONObj } from "~/utils/configUtil"
import { CONSTANTS } from "~/utils/constants/constants"
import { getPageId } from "~/utils/platform/siyuan/siyuanUtil"
import { LogFactory } from "~/utils/logUtil"
import { SiYuanApi } from "~/utils/platform/siyuan/siYuanApi"
import { ImageItem } from "~/utils/models/imageItem"
import { ElMessage } from "element-plus"
import { getSiyuanNewWinDataDir } from "~/utils/otherlib/siyuanBrowserUtil"

/**
* Picgo图片管理组件
*/
export const usePicgoManage = (props, deps) => {
// private data
const logger = LogFactory.getLogger("composables/picgo/picgoManageCom.ts")
const siyuanApi = new SiYuanApi()

// public data
const picgoManageData = reactive({
dialogImageUrl: "",
Expand All @@ -38,6 +54,53 @@ export const usePicgoManage = (props, deps) => {

// public methods
const picgoManageMethods = {
handleUploadAllImagesToBed: () => {
alert("handleUploadAllImagesToBed")
},

handleUploadCurrentImageToBed: async (imageItem: ImageItem) => {
if (!imageItem.isLocal) {
ElMessage.error("已经上传过图床,请勿重复上传")
return
}

const pageId = await getPageId(true, props.pageId)
const attrs = await siyuanApi.getBlockAttrs(pageId)

const mapInfoStr = attrs[CONSTANTS.PICGO_FILE_MAP_KEY] ?? "{}"
const fileMap = parseJSONObj(mapInfoStr)
logger.debug("fileMap=>", fileMap)

// 处理上传
let imageFullPath
if (isElectron) {
const imagePath = imageItem.originUrl
const dataDir: string = getSiyuanNewWinDataDir()
imageFullPath = `${dataDir}/imagePath`
} else {
imageFullPath = imageItem.url
}
logger.info(
"isElectron=>" + isElectron + ", imageFullPath=>",
imageFullPath
)

// fileMap[imageItem.originUrl] = new ImageItem(
// imageItem.name,
// imageItem.originUrl,
// imageItem.url,
// false
// )
// console.log("newFileMap=>", fileMap)
//
// const newFileMapStr = toJSONString(fileMap)
// await siyuanApi.setBlockAttrs(pageId, {
// [CONSTANTS.PICGO_FILE_MAP_KEY]: newFileMapStr,
// })
},

doUploadImagesToBed: (urls: string[]) => {},

onImageUrlCopy: (url: string) => {
if (isBrowser()) {
const mdUrl = `![](${url})`
Expand Down
10 changes: 9 additions & 1 deletion layouts/default/DefaultHeader.vue
Expand Up @@ -194,7 +194,7 @@ import { appendStr } from "~/utils/strUtil"
import { useI18n } from "vue-i18n"
import { LogFactory } from "~/utils/logUtil"
import { isElectron } from "~/utils/browserUtil"
import { isWindows } from "~/utils/otherlib/ChromeUtil"
import { isSlot, isWindows } from "~/utils/otherlib/ChromeUtil"
const { t } = useI18n()
const logger = LogFactory.getLogger("layouts/default/DefaultHeader.vue")
Expand Down Expand Up @@ -314,6 +314,14 @@ const pageIdChanged = () => {
if (isWindows && isElectron) {
showCloseBtn.value = false
}
// 非插槽不显示按钮
if (!isSlot) {
showOpenBtn.value = false
showCloseBtn.value = false
showTitle.value = true
}
}
onMounted(() => {
Expand Down
2 changes: 1 addition & 1 deletion public/lib/siyuanhook.js
Expand Up @@ -330,7 +330,7 @@ window.terwer.widgetsSlot = () => {
.cloneNode(false)
cloneNode.innerHTML = `
<div class="iframe-content">
<iframe src="/widgets/sy-post-publisher/" scrolling="no"></iframe>
<iframe src="/widgets/sy-post-publisher/?isSlot=true" scrolling="no"></iframe>
</div>
`
let id = element.parentElement.parentElement
Expand Down
41 changes: 40 additions & 1 deletion utils/configUtil.ts
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Terwer . All rights reserved.
* Copyright (c) 2022-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
Expand Down Expand Up @@ -88,6 +88,31 @@ export function getJSONConf<T>(key: string): T {
return valueObj
}

/**
* 从字符串解析Object
* @param objstr Object字符串
* @author terwer
* @since 0.6.1
*/
export function parseJSONObj(objstr: string): Object {
logger.debug("------------------------------")
logger.debug("尝试从JSON字符串解析Object,objstr=>", objstr)

let valueObj = {}
const value = objstr
if (value !== "") {
try {
valueObj = JSON.parse(value)
} catch (e) {
logger.error("JSON格式不正确,将直接返回{}=>", e)
}
}

logger.debug("从JSON字符串解析Object=>", valueObj)
logger.debug("------------------------------")
return valueObj
}

/**
* 保存配置:这个是所有数据保存的根方法
* @param key
Expand Down Expand Up @@ -136,6 +161,20 @@ export const setJSONConf = <T>(key: string, value: T): void => {
logger.debug("++++++++++++++++++++++++++++++")
}

/**
* Object解析为JSON字符串
* @param value JSON字符串
*/
export const toJSONString = (value: Object): String => {
logger.debug("++++++++++++++++++++++++++++++")
logger.debug("尝试将Object转换为JSON字符串", value)

const valueString = JSON.stringify(value)
logger.debug("将Object转换为JSON字符串=>", valueString)
logger.debug("++++++++++++++++++++++++++++++")
return valueString
}

/**
* 检测key是否冲突
* @param key
Expand Down
8 changes: 7 additions & 1 deletion utils/constants/constants.ts
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Terwer . All rights reserved.
* Copyright (c) 2022-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
Expand Down Expand Up @@ -48,6 +48,11 @@ const PUBLISH_PREFERENCE_CONFIG_KEY = "publish-preference"
*/
const PUBLISH_DYNAMIC_SLUG = "[dynamic-generated-on-publish]"

/**
* 文章PicGO图片信息Key
*/
const PICGO_FILE_MAP_KEY = "custom-picgo-file-map-key"

/**
* 分词最大数目
*/
Expand All @@ -60,4 +65,5 @@ export const CONSTANTS = {
DEFAULT_JIEBA_WORD_LENGTH,
PUBLISH_PREFERENCE_CONFIG_KEY,
PUBLISH_DYNAMIC_SLUG,
PICGO_FILE_MAP_KEY,
}
41 changes: 41 additions & 0 deletions utils/models/imageItem.ts
@@ -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.
*/

/**
* 图片信息
*/
export class ImageItem {
name: string
originUrl: string
url: string
isLocal: boolean

constructor(name: string, originUrl: string, url: string, isLocal: boolean) {
this.name = name
this.originUrl = originUrl
this.url = url
this.isLocal = isLocal
}
}
6 changes: 6 additions & 0 deletions utils/otherlib/ChromeUtil.js
Expand Up @@ -190,3 +190,9 @@ export const importJSONToLocalStorage = async () => {
* 检测是否是Windows
*/
export const isWindows = "Windows" === navigator?.userAgentData?.platform

/**
* 是否在插槽里面
* @type {boolean}
*/
export const isSlot = getQueryString("isSlot") === "true"

0 comments on commit b340bd2

Please sign in to comment.