fix(desktop): 拖项目时项目名被选中,WebKit 只认带前缀的 user-select

上一条提交把项目行做成可拖动之后,长按项目名会拖出一段文字选区。行上本来
就写着 user-select: none,看起来该拦住——但那条声明在宿主 WebView 里从来
没生效过。

在真 WebKitGTK 2.52.3(Linux 宿主同一个引擎)上跑四组对照,结论是它根本不
认识无前缀的 user-select:

  只写 user-select:none          → computed [-webkit-user-select] = text
  只写 -webkit-user-select:none  → none
  两个都写                        → none
  什么都不写                      → text

第一组和第四组结果完全一致:只写标准属性等于没写,而且无前缀属性的 computed
值是空字符串。所以选区能直接在项目名的 span 上起锚点。mac 侧同理,WKWebView
到 Safari 17.4 才认无前缀写法,支持窗口比 Linux 更窄。

本仓库其实早踩过一次:styles.css 的 .mc-preview-line::before(代码预览行号)
是唯一两个前缀都写的地方——也是唯一一处用户会真去拖选、因而暴露过的地方。
其余十几处内联 userSelect 都在静态文字上,失效了也没人会去拖,一直没人发现。

改了两处有拖拽交互、会真实暴露的:

- 项目行样式补 WebkitUserSelect(React 会序列化成 -webkit-user-select)。
- filesdrawer 的 trackPointer。它注释写着"期间锁定光标与选区",但那行
  document.body.style.userSelect = "none" 在 Linux 上一直是空操作,拖抽屉
  边缘调宽时同样会选中文字。

侧栏另加了一道 body 级选区锁:行上那条只管住项目名自己,拖动时指针要纵向
扫过下方的会话行,而 SessionRow 没设 none,跨元素选区仍连得起来。按下即锁、
松手复原,并在卸载时兜底还原——body 样式是全局副作用,拖到一半热更新若不
还原,整个应用都选不中文字,且现场早已消失、极难回溯。

其余十几处静态元素没动:改了看不出区别,徒增改动面。真要根治该在 styles.css
抽一个带两个前缀的 .noselect 工具类逐处替换,那是另一件事。

验证:新增 SSR 守卫断言项目行两种写法都在;变异测试摘掉前缀后它精确失败、
其余 6 条不动。tsc --noEmit 无输出;vitest 267 passed;探针脚本没入库。

遗留:选区行为终究是 GUI 行为,SSR 守卫只能证明属性写进了 DOM,证明不了
引擎的实际取舍——那部分靠上面那组引擎级对照兜底,以及人工在实机确认。
This commit is contained in:
xiaomakuaiz committed 2026-07-26 14:44:53 +00:00
1 parent a3b456190a
commit 165f4d442f
3 files changed
+43

No files matched your search

+4
View File
@@ -275,12 +275,16 @@ export function FilesDrawer({
};
// 拖拽跟踪:mousedown 后接管 move/up,期间锁定光标与选区,松手时收尾
// WebKitGTK(Linux 宿主)与旧 WKWebView 都只认带前缀的写法,实测无前缀声明
// 会被整条丢弃(computed 值仍是 text),锁不住选区——两个都写。
const trackPointer = (cursor: string, onMove: (ev: MouseEvent) => void, onDone: () => void) => {
document.body.style.cursor = cursor;
document.body.style.userSelect = "none";
document.body.style.setProperty("-webkit-user-select", "none");
const onUp = () => {
document.body.style.cursor = "";
document.body.style.userSelect = "";
document.body.style.removeProperty("-webkit-user-select");
window.removeEventListener("mousemove", onMove);
window.removeEventListener("mouseup", onUp);
onDone();
+12
View File
@@ -115,4 +115,16 @@ describe("侧栏项目拖动排序接线", () => {
]);
expect(dragDirs(html)).toEqual([]);
});
// 别把这条当冗余删掉:WebKitGTK 2.52.3 实测只认带前缀的写法,无前缀声明
// 会被整条丢弃(computed 值仍是 text),表现是拖项目时项目名被选中。
it("项目行同时写两种 user-select,少了前缀那条在 WebKit 上等于没写", () => {
const html = render([
session("s1", "/work/a", "2026-07-20T10:00:00Z"),
session("s2", "/work/b", "2026-07-26T10:00:00Z"),
]);
const rowTag = html.match(/<div[^>]*data-project-dir="[^"]*"[^>]*>/)?.[0] ?? "";
expect(rowTag).toContain("user-select:none");
expect(rowTag).toContain("-webkit-user-select:none");
});
});
+27
View File
@@ -347,6 +347,10 @@ function ProjectGroup({
borderRadius: 8,
cursor: drag?.active ? "grabbing" : "pointer",
userSelect: "none",
// WKWebView 到 Safari 17.4 才认无前缀 user-select,mac 上少了这行
// 整条规则就是空的——项目名会被拖选中。参照 .mc-preview-line::before
// (styles.css)两个都写:那是本仓库唯一被真拖选过、因此踩到过的地方。
WebkitUserSelect: "none",
// 拖动中原地留一个淡影,跟落点指示线一起交代"从哪来、到哪去"
opacity: drag?.active ? 0.45 : undefined,
// 触屏/触控板上按住不放要走拖动,不能被浏览器的滚动手势吃掉
@@ -709,6 +713,13 @@ export function Sidebar({
const dragRef = useRef<{ dir: string; startY: number; startX: number; active: boolean; rows: { dir: string; mid: number }[]; from: number; to: number } | null>(null);
const draggedRef = useRef(false);
// 选区锁是全局副作用:拖动中途卸载(热更新、整页切换)若不还原,整个应用
// 都会选不中文字,且现场早已消失、极难回溯。
useEffect(() => () => {
document.body.style.userSelect = "";
document.body.style.removeProperty("-webkit-user-select");
}, []);
// 外部入口(桌宠提醒、通知跳转)真正打开另一个空间时同步主导航;
// 单纯点主导航不会因当前主视图没变而被 effect 立即弹回。
useEffect(() => {
@@ -779,7 +790,19 @@ export function Sidebar({
// 搜索态的列表是过滤过的,此时提交顺序会把没显示出来的项目冲掉——直接不给拖。
const reorderable = !norm && projectGroups.length > 1;
// 选区锁与 filesdrawer 的分栏拖拽同一套做法(见 trackPointer):按住期间
// 全局禁选,松手还原,不覆盖用户在别处已有的偏好。两个前缀都写的理由同
// 项目行样式——只写标准属性在 WKWebView 上等于没写。
const lockBodySelection = () => {
document.body.style.userSelect = "none";
document.body.style.setProperty("-webkit-user-select", "none");
};
const unlockBodySelection = () => {
document.body.style.userSelect = "";
document.body.style.removeProperty("-webkit-user-select");
};
const endDrag = () => {
unlockBodySelection();
dragRef.current = null;
setDragTo(null);
};
@@ -806,6 +829,10 @@ export function Sidebar({
if ((e.target as HTMLElement | null)?.closest("button")) return;
dragRef.current = { dir: group.dir, startX: e.clientX, startY: e.clientY, active: false, rows: [], from: index, to: index };
e.currentTarget.setPointerCapture(e.pointerId);
// 行上那条只管住项目名自己。指针接着要纵向扫过下方的会话行,而
// SessionRow 没设 none,从这里按下拖过去仍会连出跨元素选区,所以
// 按下即锁全局,松手复原。
lockBodySelection();
},
onPointerMove: (e) => {
const state = dragRef.current;