Skip to content

Commit

Permalink
feat: support validate iamge block
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyue committed Mar 27, 2024
1 parent 378fdeb commit 6c53003
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions packages/picgo-plugin-bootstrap/src/index.ts
Expand Up @@ -55,8 +55,7 @@ export default class PicgoPlugin extends Plugin {
}

private offEvent() {
this.eventBus.off("paste", () => {
})
this.eventBus.off("paste", () => {})
}

/**
Expand All @@ -83,14 +82,14 @@ export default class PicgoPlugin extends Plugin {

const siyuanConfig = {
apiUrl: siyuanApiUrl,
password: siyuanApiToken
password: siyuanApiToken,
}
const picgoPostApi = await SiyuanPicGo.getInstance(siyuanConfig as any, isDev)
const siyuanApi = picgoPostApi.siyuanApi
if (files.length > 1) {
siyuanApi.pushErrMsg({
msg: "仅支持一次性上传单张图片",
timeout: 7000
timeout: 7000,
})
return
}
Expand All @@ -99,7 +98,7 @@ export default class PicgoPlugin extends Plugin {
try {
siyuanApi.pushMsg({
msg: "检测到剪贴板图片,正在上传,请勿进行任何操作...",
timeout: 1000
timeout: 1000,
})

// pageId: string
Expand All @@ -126,7 +125,7 @@ export default class PicgoPlugin extends Plugin {
} catch (e) {
siyuanApi.pushErrMsg({
msg: "剪贴板图片上传失败 =>" + e.toString(),
timeout: 7000
timeout: 7000,
})
}
}
Expand All @@ -135,7 +134,7 @@ export default class PicgoPlugin extends Plugin {
const WAIT_SECONDS = 10
siyuanApi.pushMsg({
msg: `剪贴板图片上传完成。准备延迟${WAIT_SECONDS}秒更新元数据,请勿刷新笔记!`,
timeout: 7000
timeout: 7000,
})
setTimeout(async () => {
const formData = new FormData()
Expand Down Expand Up @@ -168,37 +167,46 @@ export default class PicgoPlugin extends Plugin {
if (!newImageItem) {
siyuanApi.pushErrMsg({
msg: `未找到图片元数据`,
timeout: 7000
timeout: 7000,
})
}
const newFileMapStr = JSON.stringify(fileMap)
await siyuanApi.setBlockAttrs(pageId, {
[SIYUAN_PICGO_FILE_MAP_KEY]: newFileMapStr
[SIYUAN_PICGO_FILE_MAP_KEY]: newFileMapStr,
})

// 更新块
const nodeId = this.getDataNodeIdFromImgWithSrc(newImageItem.originUrl)
if (!nodeId) {
siyuanApi.pushErrMsg({
msg: `未找到图片块 ID`,
timeout: 7000
timeout: 7000,
})
return
}
this.logger.info("😆found image nodeId=>", nodeId)
const newImageBlock = await siyuanApi.getBlockByID(nodeId)
// newImageBlock.markdown
// "![image](assets/image-20240327190812-yq6esh4.png)"
this.logger.debug("new image block=>", newImageBlock)
// 如果查询出来的块信息不对,不更新,防止误更新
if (!newImageBlock.markdown.includes(newImageItem.originUrl)) {
siyuanApi.pushErrMsg({
msg: `块信息不符合,取消更新`,
timeout: 7000,
})
return
}

// id: string
// data: string
// dataType?: "markdown" | "dom"
this.logger.debug("new image block=>", newImageBlock)
const newImageContent = `![${newImageItem.alt}](${newImageItem.url})`
await siyuanApi.updateBlock(nodeId, newImageContent, "markdown")

siyuanApi.pushMsg({
msg: `图片元数据更新成功`,
timeout: 7000
timeout: 7000,
})
}, WAIT_SECONDS * 1000)
}
Expand Down

0 comments on commit 6c53003

Please sign in to comment.