Skip to content

Commit 604d7f1

Browse files
committed
fix: shift + tap selection behaviour
1 parent 533c1ac commit 604d7f1

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/lib/editorManager.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import sidebarApps from "sidebarApps";
22
import { indentUnit } from "@codemirror/language";
33
import { search } from "@codemirror/search";
4-
import { Compartment, EditorState, Prec, StateEffect } from "@codemirror/state";
4+
import {
5+
Compartment,
6+
EditorSelection,
7+
EditorState,
8+
Prec,
9+
StateEffect,
10+
} from "@codemirror/state";
511
import { oneDark } from "@codemirror/theme-one-dark";
612
import {
713
EditorView,
@@ -172,6 +178,36 @@ async function EditorManager($header, $body) {
172178
},
173179
);
174180

181+
let shiftTapSelectionFix;
182+
{
183+
const pointerIdMap = new Map();
184+
shiftTapSelectionFix = EditorView.domEventHandlers({
185+
pointerup(event, view) {
186+
if (!(event.isTrusted && event.isPrimary)) return;
187+
if (!event.shiftKey && quickTools.$footer.dataset.shift == null) return;
188+
const { pointerId } = event;
189+
const tid = setTimeout(() => pointerIdMap.delete(pointerId), 1001);
190+
pointerIdMap.set(pointerId, [view.state.selection.main.anchor, tid]);
191+
},
192+
click(event, view) {
193+
const { pointerId } = event;
194+
if (!pointerIdMap.has(pointerId)) return false;
195+
const [anchor, tid] = pointerIdMap.get(pointerId);
196+
clearTimeout(tid);
197+
pointerIdMap.delete(pointerId);
198+
view.dispatch({
199+
selection: EditorSelection.range(
200+
anchor,
201+
view.state.selection.main.anchor,
202+
),
203+
userEvent: "select.extend",
204+
});
205+
event.preventDefault();
206+
return true;
207+
},
208+
});
209+
}
210+
175211
const touchSelectionUpdateExtension = EditorView.updateListener.of(
176212
(update) => {
177213
if (!touchSelectionController) return;
@@ -725,6 +761,7 @@ async function EditorManager($header, $body) {
725761
fixedHeightTheme,
726762
scrollPastEnd(),
727763
pointerCursorVisibilityExtension,
764+
shiftTapSelectionFix,
728765
touchSelectionUpdateExtension,
729766
search(),
730767
// Ensure read-only can be toggled later via compartment
@@ -1088,6 +1125,7 @@ async function EditorManager($header, $body) {
10881125
fixedHeightTheme,
10891126
scrollPastEnd(),
10901127
pointerCursorVisibilityExtension,
1128+
shiftTapSelectionFix,
10911129
touchSelectionUpdateExtension,
10921130
search(),
10931131
// Keep dynamic compartments across state swaps

0 commit comments

Comments
 (0)