Files
terminal-lab/pxterm/scripts/archive/cjs_tools/patch_touch.cjs
2026-03-04 13:25:26 +08:00

94 lines
3.7 KiB
JavaScript

const fs = require('fs');
let code = fs.readFileSync('src/components/TerminalPanel.vue', 'utf8');
// Replace standard touch handlers to use JS custom scrolling with deep logging
let newStart = ` onTouchKeyboardTouchStart = (event: TouchEvent) => {
clearTouchScrollMomentum();
if (event.touches.length > 0) {
touchScrollLastY = event.touches[0]?.clientY || 0;
touchScrollLastTime = performance.now();
touchScrollVelocity = 0;
}
console.log("[Scroll] 👆 Touch START:", {
y: touchScrollLastY,
time: touchScrollLastTime.toFixed(1),
viewportScrollTop: viewportScroller?.scrollTop
});
if (!helperTextarea) return;
if (focusKeyboardTimerId !== null) {
window.clearTimeout(focusKeyboardTimerId);
focusKeyboardTimerId = null;
clearFocusKeyboardBlurRecover();
focusKeyboardInProgress = false;
}
helperTextarea.readOnly = true;
if (DEBUG_TOUCH_FOCUS) {
console.log("[TouchFocus] touchstart → readOnly=true (no blur)");
}
};`;
let newMove = ` onTouchKeyboardTouchMove = (event: TouchEvent) => {
const hasSelection = hasActiveNativeSelectionInTerminal();
if (hasSelection) {
lastTouchAction = "PASS_NATIVE";
event.stopImmediatePropagation();
return;
}
const now = performance.now();
const dt = now - touchScrollLastTime;
const currentY = event.touches[0]?.clientY || 0;
const dy = currentY - touchScrollLastY;
// Stop xterm from intercepting this manually
event.stopImmediatePropagation();
if (viewportScroller && dt > 0) {
const before = viewportScroller.scrollTop;
if (dy !== 0) {
// JS 强控滑动:完全跟手,零延迟
viewportScroller.scrollTop -= dy;
}
// 计算物理滑动速度,用于释放后的动量
const v = (-dy / dt) * 16;
if (touchScrollVelocity * v > 0) {
touchScrollVelocity = touchScrollVelocity * 0.5 + v * 0.5;
} else {
touchScrollVelocity = v;
}
// 限制最大动量
const touchMaxSpeed = 120; // 匹配原本常量
if (touchScrollVelocity > touchMaxSpeed) touchScrollVelocity = touchMaxSpeed;
else if (touchScrollVelocity < -touchMaxSpeed) touchScrollVelocity = -touchMaxSpeed;
console.log(\`[Scroll] 🖐️ MOVE | 跟手延迟Δt=\${dt.toFixed(1)}ms | 手指位移Δy=\${dy.toFixed(1)}px | 渲染视口=\${before}→\${viewportScroller.scrollTop} | 当前算得速度v=\${v.toFixed(2)}\`);
}
touchScrollLastY = currentY;
touchScrollLastTime = now;
};`;
let newEnd = ` onTouchKeyboardTouchEnd = () => {
console.log(\`[Scroll] 👇 Touch END | 释放时瞬时速度=\${touchScrollVelocity.toFixed(2)} | 最终 scrollTop: \${viewportScroller?.scrollTop}\`);
// 添加阈值,防止极轻微误触引发滚动
const threshold = 0.2;
if (Math.abs(touchScrollVelocity) > threshold) {
// 轻微乘子放大,提升顺滑手感
touchScrollVelocity *= 1.35;
console.log(\`[Scroll] 🚀 触发惯性滚动,起步速度=\${touchScrollVelocity.toFixed(2)}\`);
runTouchScrollMomentum();
} else {
console.log(\`[Scroll] 🛑 速度太小(\${touchScrollVelocity.toFixed(2)}),不触发惯性\`);
}
};`;
code = code.replace(/onTouchKeyboardTouchStart = \(event: TouchEvent\) => \{[\s\S]*?\}\;\n onTouchKeyboardTouchMove = \(event: TouchEvent\) => \{[\s\S]*?\}\;\n onTouchKeyboardTouchEnd = \(\) => \{[\s\S]*?\}\;/m,
newStart + '\n' + newMove + '\n' + newEnd);
fs.writeFileSync('src/components/TerminalPanel.vue', code);