Skip to content

Commit 5df05c7

Browse files
committed
fix: shortcut for tab
1 parent bdc57ef commit 5df05c7

File tree

9 files changed

+451
-13
lines changed

9 files changed

+451
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ The label of a draw.io image block can be configured in the plugin settings.
8181

8282
## Changelog
8383

84+
+ v0.5.5
85+
+ Fix: shortcut for Tab
8486
+ v0.5.4
8587
+ Feature: fullscreen support for shortcut in Tab
8688
+ v0.5.3

README_zh_CN.md

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

7171
## 更新日志
7272

73+
+ v0.5.5
74+
+ 修复缺陷:快捷键
7375
+ v0.5.4
7476
+ 新增功能:Tab支持快捷键全屏
7577
+ v0.5.3

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.5.4",
3+
"version": "0.5.5",
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.5.4",
5+
"version": "0.5.5",
66
"minAppVersion": "3.0.0",
77
"disabledInPublish": true,
88
"backends": [

src/index.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
}
5353
}
5454

55-
html[data-theme-mode="dark"] img[data-src^="assets/drawio-"] {
55+
html[data-theme-mode="dark"] img[src^="assets/drawio-"] {
5656
filter: invert(0.8) brightness(1.2);
5757
}
5858

src/index.ts

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import {
77
getAllEditor,
88
openTab,
99
getAllModels,
10+
Custom,
1011
} from "siyuan";
1112
import "@/index.scss";
1213
import PluginInfoString from '@/../plugin.json';
1314
import {
1415
getImageSizeFromBase64,
1516
locatePNGtEXt,
17+
insertPNGpHYs,
1618
replaceSubArray,
1719
arrayToBase64,
1820
base64ToArray,
@@ -22,6 +24,7 @@ import {
2224
dataURLToBlob,
2325
HTMLToElement,
2426
} from "./utils";
27+
import { matchHotKey } from "./utils/hotkey";
2528
import defaultImageContent from "@/default.json";
2629

2730
let PluginInfo = {
@@ -444,11 +447,52 @@ export default class DrawioPlugin extends Plugin {
444447
})
445448
}
446449

450+
private getActiveCustomTab(type: string): Custom {
451+
const allCustoms = getAllModels().custom;
452+
const activeTabElement = document.querySelector(".layout__wnd--active .item--focus");
453+
if (activeTabElement) {
454+
const tabId = activeTabElement.getAttribute("data-id");
455+
for (const custom of allCustoms as any[]) {
456+
if (custom.type == this.name + type && custom.tab.headElement?.getAttribute('data-id') == tabId) {
457+
return custom;
458+
};
459+
}
460+
}
461+
return null;
462+
}
463+
464+
private tabHotKeyEventHandler = (event: KeyboardEvent, custom?: Custom) => {
465+
// 自定义处理方式的快捷键
466+
const isFullscreenHotKey = matchHotKey(window.siyuan.config.keymap.editor.general.fullscreen.custom, event);
467+
const isCloseTabHotKey = matchHotKey(window.siyuan.config.keymap.general.closeTab.custom, event);
468+
if (isFullscreenHotKey || isCloseTabHotKey) {
469+
if (!custom) custom = this.getActiveCustomTab(this.EDIT_TAB_TYPE);
470+
if (custom) {
471+
event.preventDefault();
472+
event.stopPropagation();
473+
474+
if (isFullscreenHotKey) {
475+
if (document.fullscreenElement) {
476+
document.exitFullscreen();
477+
} else {
478+
custom.element.requestFullscreen();
479+
}
480+
}
481+
if (isCloseTabHotKey) {
482+
custom.tab.close();
483+
}
484+
}
485+
}
486+
};
487+
447488
private globalKeyDownHandler = (event: KeyboardEvent) => {
448489
// 如果是在代码编辑器里使用快捷键,则阻止冒泡 https://github.com/YuxinZhaozyx/siyuan-embed-tikz/issues/1
449490
if (document.activeElement.closest(".b3-dialog--open .drawio-edit-dialog")) {
450491
event.stopPropagation();
451492
}
493+
494+
// 快捷键
495+
this.tabHotKeyEventHandler(event);
452496
};
453497

454498
public setupEditTab() {
@@ -486,6 +530,7 @@ export default class DrawioPlugin extends Plugin {
486530
postMessage({
487531
action: 'export',
488532
format: `xml${imageInfo.format}`,
533+
// scale: 1,
489534
});
490535
}
491536

@@ -544,17 +589,8 @@ export default class DrawioPlugin extends Plugin {
544589
}
545590
};
546591

547-
const switchFullscreen = () => {
548-
if (document.fullscreenElement) {
549-
document.exitFullscreen();
550-
} else {
551-
this.element.requestFullscreen();
552-
}
553-
}
554592
const keydownEventHandleer = (event: KeyboardEvent) => {
555-
if (event.key.toLowerCase() === 'y' && (event.altKey || event.metaKey)) {
556-
switchFullscreen();
557-
}
593+
that.tabHotKeyEventHandler(event, this);
558594
};
559595

560596
window.addEventListener("message", messageEventHandler);
@@ -842,6 +878,13 @@ export default class DrawioPlugin extends Plugin {
842878
base64String = unicodeToBase64(svgContent);
843879
imageDataURL = `data:image/svg+xml;base64,${base64String}`;
844880
}
881+
// 设置PNG DPI
882+
// if (imageDataURL.startsWith('data:image/png')) {
883+
// let binaryArray = base64ToArray(imageDataURL.split(',').pop());
884+
// binaryArray = insertPNGpHYs(binaryArray, 96 * 2);
885+
// const base64String = arrayToBase64(binaryArray);
886+
// imageDataURL = `data:image/png;base64,${base64String}`;
887+
// }
845888
// 当图像为空时,使用默认的占位图
846889
const imageSize = getImageSizeFromBase64(imageDataURL);
847890
if (imageSize && imageSize.width <= 1 && imageSize.height <= 1) {

src/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ interface Window {
128128
mode: number;
129129
};
130130
lang: string;
131+
keymap: any;
131132
}
132133
};
133134
_sy_plugin_sample: {

0 commit comments

Comments
 (0)