Skip to content

Commit 7462afe

Browse files
Fix editor link padding clicks
1 parent cf762da commit 7462afe

2 files changed

Lines changed: 240 additions & 43 deletions

File tree

app/src/features/editor/extensions/link-interactions.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,80 @@ describe("Editor link interactions", () => {
228228
view.destroy();
229229
});
230230

231+
it("does not open markdown links when the pointer lands in link padding", async () => {
232+
const doc = "[Example](https://example.com)";
233+
const { view } = createView(doc);
234+
235+
await flush();
236+
237+
const link = view.dom.querySelector(".cm-md-link");
238+
expect(link).not.toBeNull();
239+
240+
const originalPosAtCoords = view.posAtCoords.bind(view);
241+
const originalPosAtDOM = view.posAtDOM.bind(view);
242+
const originalCoordsAtPos = view.coordsAtPos.bind(view);
243+
const createRangeSpy = vi.spyOn(document, "createRange").mockImplementation(
244+
() =>
245+
({
246+
getClientRects: () => [],
247+
setEnd: () => {},
248+
setStart: () => {},
249+
}) as unknown as Range,
250+
);
251+
Object.assign(view, {
252+
coordsAtPos: (pos: number, side?: -1 | 1) => {
253+
if (pos === doc.indexOf("Example") && side === 1) {
254+
return DOMRect.fromRect({
255+
height: 20,
256+
width: 0,
257+
x: 10,
258+
y: 10,
259+
});
260+
}
261+
262+
if (pos === doc.indexOf("Example") + "Example".length && side === -1) {
263+
return DOMRect.fromRect({
264+
height: 20,
265+
width: 0,
266+
x: 70,
267+
y: 10,
268+
});
269+
}
270+
271+
return originalCoordsAtPos(pos, side);
272+
},
273+
posAtCoords: () => doc.indexOf("Example"),
274+
posAtDOM: () => doc.indexOf("Example"),
275+
});
276+
277+
link?.dispatchEvent(
278+
new MouseEvent("mousedown", {
279+
bubbles: true,
280+
button: 0,
281+
clientX: 100,
282+
clientY: 20,
283+
}),
284+
);
285+
link?.dispatchEvent(
286+
new MouseEvent("click", {
287+
bubbles: true,
288+
button: 0,
289+
clientX: 100,
290+
clientY: 20,
291+
}),
292+
);
293+
294+
expect(openUrlMock).not.toHaveBeenCalled();
295+
296+
Object.assign(view, {
297+
coordsAtPos: originalCoordsAtPos,
298+
posAtCoords: originalPosAtCoords,
299+
posAtDOM: originalPosAtDOM,
300+
});
301+
createRangeSpy.mockRestore();
302+
view.destroy();
303+
});
304+
231305
it("opens plain external URLs on plain click", async () => {
232306
const { view } = createView("See https://github.com/nodeca/pica now");
233307

0 commit comments

Comments
 (0)