Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve Trackpad Adjustment Editor Font Size https://ld246.com/article/1764296257377 (#17295)

Jeffrey Chen1ebaae7f26d8
diff --git a/app/src/boot/globalEvent/event.ts b/app/src/boot/globalEvent/event.ts
index 8653d087f4..b54f4c5008 100644
--- a/app/src/boot/globalEvent/event.ts
+++ b/app/src/boot/globalEvent/event.ts
@@ -200,6 +200,7 @@ export const initWindowEvent = (app: App) => {
window.siyuan.ctrlIsPressed = false;
window.siyuan.shiftIsPressed = false;
window.siyuan.altIsPressed = false;
+ window.siyuan.metaIsPressed = false;
document.body.classList.remove("body--shift-pressed");
/// #if BROWSER
setWebViewFocusable();
diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.ts
index ee0a559772..7577a6b903 100644
--- a/app/src/boot/globalEvent/keydown.ts
+++ b/app/src/boot/globalEvent/keydown.ts
@@ -3,6 +3,7 @@ import {
isMac,
isNotCtrl,
isOnlyMeta,
+ isPhysicalMetaKeyEvent,
updateHotkeyTip,
writeText
} from "../../protyle/util/compatibility";
@@ -1175,6 +1176,9 @@ const panelTreeKeydown = (app: App, event: KeyboardEvent) => {
let switchDialog: Dialog;
export const windowKeyDown = (app: App, event: KeyboardEvent) => {
+ if (isPhysicalMetaKeyEvent(event)) {
+ window.siyuan.metaIsPressed = true;
+ }
if (filterHotkey(event, app)) {
return;
}
diff --git a/app/src/boot/globalEvent/keyup.ts b/app/src/boot/globalEvent/keyup.ts
index 9c5991dc76..fee4281817 100644
--- a/app/src/boot/globalEvent/keyup.ts
+++ b/app/src/boot/globalEvent/keyup.ts
@@ -7,8 +7,12 @@ import {App} from "../../index";
import {Constants} from "../../constants";
import {matchHotKey} from "../../protyle/util/hotKey";
import {isWindow} from "../../util/functions";
+import {isPhysicalMetaKeyEvent} from "../../protyle/util/compatibility";
export const windowKeyUp = (app: App, event: KeyboardEvent) => {
+ if (isPhysicalMetaKeyEvent(event)) {
+ window.siyuan.metaIsPressed = false;
+ }
window.siyuan.ctrlIsPressed = false;
window.siyuan.shiftIsPressed = false;
window.siyuan.altIsPressed = false;
diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts
index c53baa6f39..3db34ccaa6 100644
--- a/app/src/boot/onGetConfig.ts
+++ b/app/src/boot/onGetConfig.ts
@@ -145,6 +145,7 @@ export const initWindow = async (app: App) => {
window.siyuan.altIsPressed = false;
window.siyuan.ctrlIsPressed = false;
window.siyuan.shiftIsPressed = false;
+ window.siyuan.metaIsPressed = false;
document.body.classList.remove("body--blur");
} else if (cmd === "blur") {
document.body.classList.add("body--blur");
diff --git a/app/src/index.ts b/app/src/index.ts
index 8865cba825..1609f2af7c 100644
--- a/app/src/index.ts
+++ b/app/src/index.ts
@@ -237,6 +237,7 @@ export class App {
closedTabs: [],
ctrlIsPressed: false,
altIsPressed: false,
+ metaIsPressed: false,
ws: mainWs,
};
diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts
index a1f026aaaf..383f4956d4 100644
--- a/app/src/mobile/index.ts
+++ b/app/src/mobile/index.ts
@@ -194,6 +194,7 @@ class App {
window.siyuan.ctrlIsPressed = false;
window.siyuan.shiftIsPressed = false;
window.siyuan.altIsPressed = false;
+ window.siyuan.metaIsPressed = false;
});
window.addEventListener("blur", () => {
setWebViewFocusable();
diff --git a/app/src/protyle/ui/initUI.ts b/app/src/protyle/ui/initUI.ts
index 745d5de0f5..57fa801c35 100644
--- a/app/src/protyle/ui/initUI.ts
+++ b/app/src/protyle/ui/initUI.ts
@@ -86,24 +86,55 @@ export const initUI = (protyle: IProtyle) => {
let wheelTimeout: number;
const wheelId = genUUID();
const isMacOS = isMac();
- protyle.contentElement.addEventListener("mousewheel", (event: WheelEvent) => {
- if (!window.siyuan.config.editor.fontSizeScrollZoom || (isMacOS && !event.metaKey) || (!isMacOS && !event.ctrlKey) || event.deltaX !== 0) {
+ const ZOOM_THRESHOLD = 20; // 累加 deltaY 达到阈值时改动一级字号,避免单次缩放触发多次事件导致跳级
+ const WHEEL_IDLE_MS = 200; // 单轮滚动的间隔时间,超过该时间后重置累加值
+ let accumDeltaY = 0;
+ let resetTimer: number;
+ let wheelActive = false; // 记录是否处于同一轮滚动中
+ let modifierOnset = false; // 记录同一轮滚动的首次滚动是否按下 Ctrl/Cmd
+ protyle.contentElement.addEventListener("wheel", (event: WheelEvent) => {
+ if (!window.siyuan.config.editor.fontSizeScrollZoom || event.shiftKey || event.deltaY === 0) {
+ // 用 event.shiftKey || event.deltaY === 0 检测横向滚动,因为触控板快速划动时 deltaX 可能很大,即使容器并不能发生横向滚动
+ return;
+ }
+ // 用「首轮是否带修饰键 + 空闲窗口」避免惯性或残留累加误触
+ // 浏览器无法区分触控板与鼠标滚轮 https://github.com/w3c/pointerevents/issues/596 ,浏览器将触控板双指捏合与张开映射为带 ctrlKey 的 wheel
+ // Mac 在鼠标不移动的情况下 metaKey 状态更新滞后,参考 https://github.com/tldraw/tldraw/issues/7981 用 keydown/keyup 维护 metaIsPressed 状态
+ const modifierPressed = isMacOS ? window.siyuan.metaIsPressed || event.ctrlKey : event.ctrlKey;
+ if (!modifierPressed) {
+ accumDeltaY = 0;
+ }
+ clearTimeout(resetTimer);
+ resetTimer = window.setTimeout(() => {
+ wheelActive = false;
+ accumDeltaY = 0;
+ }, WHEEL_IDLE_MS);
+ if (!wheelActive) {
+ wheelActive = true;
+ modifierOnset = modifierPressed;
+ }
+ if (!modifierPressed || !modifierOnset) {
+ // 触控板惯性会在松手后仍产生滚轮事件,避免划动后再按下 Ctrl/Cmd 会调整字号 https://ld246.com/article/1764296257377
+ // 要在单轮滚动的最后一个滚动事件触发之后等待 WHEEL_IDLE_MS 时间间隔之后再按下 Ctrl/Cmd 滚动才能调整字号
return;
}
event.stopPropagation();
- if (event.deltaY < 0) {
- if (window.siyuan.config.editor.fontSize < 72) {
- window.siyuan.config.editor.fontSize++;
- } else {
- return;
- }
- } else if (event.deltaY > 0) {
- if (window.siyuan.config.editor.fontSize > 9) {
- window.siyuan.config.editor.fontSize--;
- } else {
- return;
- }
+ if (accumDeltaY !== 0 && Math.sign(event.deltaY) !== Math.sign(accumDeltaY)) {
+ accumDeltaY = 0;
+ }
+ accumDeltaY += event.deltaY;
+ let stepped = false;
+ if (accumDeltaY <= -ZOOM_THRESHOLD && window.siyuan.config.editor.fontSize < 72) {
+ window.siyuan.config.editor.fontSize++;
+ stepped = true;
+ } else if (accumDeltaY >= ZOOM_THRESHOLD && window.siyuan.config.editor.fontSize > 9) {
+ window.siyuan.config.editor.fontSize--;
+ stepped = true;
+ }
+ if (!stepped) {
+ return;
}
+ accumDeltaY = 0;
setInlineStyle();
clearTimeout(wheelTimeout);
showMessage(`${window.siyuan.languages.fontSize} ${window.siyuan.config.editor.fontSize}px<span class="fn__space"></span>
@@ -127,7 +158,7 @@ export const initUI = (protyle: IProtyle) => {
});
});
}, Constants.TIMEOUT_LOAD);
- }, {passive: true});
+ }, {passive: true}); // 调整字号有时候会滚动编辑器,目前使用 passive: true 没有解决方法
protyle.contentElement.addEventListener("click", (event: MouseEvent & { target: HTMLElement }) => {
hideElements(["hint", "util"], protyle);
// wysiwyg 元素下方点击无效果 https://github.com/siyuan-note/siyuan/issues/12009
diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts
index 7e0f1982ad..fa84265690 100644
--- a/app/src/protyle/util/compatibility.ts
+++ b/app/src/protyle/util/compatibility.ts
@@ -334,6 +334,13 @@ export const isNotCtrl = (event: KeyboardEvent | MouseEvent) => {
return false;
};
+export const isPhysicalMetaKeyEvent = (event: KeyboardEvent) =>
+ event.key === "Meta" ||
+ event.code === "MetaLeft" ||
+ event.code === "MetaRight" ||
+ event.code === "OSLeft" ||
+ event.code === "OSRight";
+
export const isHuawei = () => {
return window.siyuan.config.system.osPlatform.toLowerCase().indexOf("huawei") > -1;
};
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index dc372dab0f..235a242248 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -614,6 +614,7 @@ interface ISiyuan {
ctrlIsPressed?: boolean,
altIsPressed?: boolean,
shiftIsPressed?: boolean,
+ metaIsPressed?: boolean,
coordinates?: {
pageX: number,
pageY: number,
diff --git a/app/src/window/index.ts b/app/src/window/index.ts
index 2df8dfd7ba..683af2b643 100644
--- a/app/src/window/index.ts
+++ b/app/src/window/index.ts
@@ -181,6 +181,7 @@ class App {
closedTabs: [],
ctrlIsPressed: false,
altIsPressed: false,
+ metaIsPressed: false,
ws: mainWs,
};
fetchPost("/api/system/getConf", {}, async (response) => {