Skip to content

Commit bc8b880

Browse files
committed
feat: show drawio label and page counts
1 parent 9a381dc commit bc8b880

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Click the menu button in the top-right corner of the image. If the block is reco
5151

5252
## Changelog
5353

54+
+ v0.2.0
55+
+ Feature: show draw.io image label and page counts
5456
+ v0.1.6
5557
+ Fix: load cache image mistakely
5658
+ v0.1.5

README_zh_CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050

5151
## 更新日志
5252

53+
+ v0.2.0
54+
+ 新增功能:draw.io图像右上侧显示标签和页数
5355
+ v0.1.6
5456
+ 修复缺陷:错误地加载到浏览器缓存图像
5557
+ v0.1.5

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "siyuan-embed-drawio",
3-
"version": "0.1.6",
3+
"version": "0.2.0",
44
"type": "module",
55
"description": "This is a plugin for siyuan",
66
"author": "",

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "siyuan-embed-drawio",
33
"author": "Yuxin Zhao",
44
"url": "https://github.com/YuxinZhaozyx/siyuan-embed-drawio",
5-
"version": "0.1.6",
5+
"version": "0.2.0",
66
"minAppVersion": "3.3.0",
77
"disabledInPublish": true,
88
"backends": [

src/index.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,23 @@ export default class DrawioPlugin extends Plugin {
3636
public platform: SyFrontendTypes
3737
public readonly version = version
3838

39+
private _mutationObserver;
3940
private _openMenuImageHandler;
4041
private _globalKeyDownHandler;
4142

4243
async onload() {
4344
this.initMetaInfo();
4445

46+
this._mutationObserver = this.setAddImageBlockMuatationObserver(document.body, (blockElement: HTMLElement) => {
47+
const imageElement = blockElement.querySelector("img") as HTMLImageElement;
48+
if (imageElement) {
49+
const imageURL = imageElement.getAttribute("data-src");
50+
this.getDrawioImageInfo(imageURL).then((imageInfo) => {
51+
this.updateAttrLabel(imageInfo, blockElement);
52+
});
53+
}
54+
});
55+
4556
this.protyleSlash = [{
4657
filter: ["drawio", "draw.io"],
4758
id: "drawio",
@@ -61,6 +72,7 @@ export default class DrawioPlugin extends Plugin {
6172
}
6273

6374
onunload() {
75+
if (this._mutationObserver) this._mutationObserver.disconnect();
6476
if (this._openMenuImageHandler) this.eventBus.off("open-menu-image", this._openMenuImageHandler);
6577
if (this._globalKeyDownHandler) document.documentElement.removeEventListener("keydown", this._globalKeyDownHandler);
6678
}
@@ -85,6 +97,38 @@ export default class DrawioPlugin extends Plugin {
8597
}
8698
}
8799

100+
public setAddImageBlockMuatationObserver(element: HTMLElement, callback: (blockElement: HTMLElement) => void): MutationObserver {
101+
const mutationObserver = new MutationObserver(mutations => {
102+
for (const mutation of mutations) {
103+
if (mutation.type === 'childList') {
104+
mutation.addedNodes.forEach(node => {
105+
if (node.nodeType === Node.ELEMENT_NODE) {
106+
const addedElement = node as HTMLElement;
107+
if (addedElement.matches("div[data-type='NodeParagraph']")) {
108+
if (addedElement.querySelector(".img[data-type='img'] img")) {
109+
callback(addedElement as HTMLElement);
110+
}
111+
} else {
112+
addedElement.querySelectorAll("div[data-type='NodeParagraph']").forEach((blockElement: HTMLElement) => {
113+
if (blockElement.querySelector(".img[data-type='img'] img")) {
114+
callback(blockElement);
115+
}
116+
})
117+
}
118+
}
119+
});
120+
}
121+
}
122+
});
123+
124+
mutationObserver.observe(element, {
125+
childList: true,
126+
subtree: true
127+
});
128+
129+
return mutationObserver;
130+
}
131+
88132
public async getDrawioImageInfo(imageURL: string): Promise<DrawioImageInfo | null> {
89133
const imageURLRegex = /^assets\/.+\.svg$/;
90134
if (!imageURLRegex.test(imageURL)) return null;
@@ -151,6 +195,25 @@ export default class DrawioPlugin extends Plugin {
151195
fetchPost("/api/file/putFile", formData, callback);
152196
}
153197

198+
public updateAttrLabel(imageInfo: DrawioImageInfo, blockElement: HTMLElement) {
199+
if (!imageInfo) return;
200+
201+
const attrElement = blockElement.querySelector(".protyle-attr") as HTMLDivElement;
202+
if (attrElement) {
203+
const pageCount = (imageInfo.data.match(/name=&quot;/g) || []).length;
204+
const labelHTML = `<span>draw.io${pageCount > 1 ? `:${pageCount}` : ''}</span>`;
205+
let labelElement = attrElement.querySelector(".label--embed-drawio") as HTMLDivElement;
206+
if (labelElement) {
207+
labelElement.innerHTML = labelHTML;
208+
} else {
209+
labelElement = document.createElement("div");
210+
labelElement.classList.add("label--embed-drawio");
211+
labelElement.innerHTML = labelHTML;
212+
attrElement.prepend(labelElement);
213+
}
214+
}
215+
}
216+
154217
private openMenuImageHandler({ detail }) {
155218
const selectedElement = detail.element;
156219
const imageElement = selectedElement.querySelector("img") as HTMLImageElement;
@@ -245,6 +308,10 @@ export default class DrawioPlugin extends Plugin {
245308
fetch(imageInfo.imageURL, { cache: 'reload' }).then(() => {
246309
document.querySelectorAll(`img[data-src='${imageInfo.imageURL}']`).forEach(imageElement => {
247310
(imageElement as HTMLImageElement).src = imageInfo.imageURL;
311+
const blockElement = imageElement.closest("div[data-type='NodeParagraph']") as HTMLElement;
312+
if (blockElement) {
313+
this.updateAttrLabel(imageInfo, blockElement);
314+
}
248315
});
249316
});
250317
});

0 commit comments

Comments
 (0)