Skip to content

Commit 57c0cc4

Browse files
authoredMar 14, 2024
Don't select URI cell when clicking the link (glideapps#923)
* Don't select URI cell when clicking the link * tweak story
1 parent 68de152 commit 57c0cc4

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed
 

‎packages/core/src/cells/uri-cell.tsx

+34-18
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ function getTextRect(
3838
return { x, y, width, height };
3939
}
4040

41+
function isOverLinkText(e: {
42+
readonly cell: UriCell;
43+
readonly posX: number;
44+
readonly posY: number;
45+
readonly bounds: Rectangle;
46+
readonly theme: FullTheme;
47+
}): boolean {
48+
const { cell, bounds, posX, posY, theme } = e;
49+
const txt = cell.displayData ?? cell.data;
50+
if (cell.hoverEffect !== true || cell.onClickUri === undefined) return false;
51+
52+
const m = getMeasuredTextCache(txt, theme.baseFontFull);
53+
if (m === undefined) return false;
54+
const textRect = getTextRect(m, bounds, theme, cell.contentAlign);
55+
return pointInRect(
56+
{
57+
x: textRect.x - 4,
58+
y: textRect.y - 4,
59+
width: textRect.width + 8,
60+
height: textRect.height + 8,
61+
},
62+
posX,
63+
posY
64+
);
65+
}
66+
4167
export const uriCellRenderer: InternalCellRenderer<UriCell> = {
4268
getAccessibilityString: c => c.data?.toString() ?? "",
4369
kind: GridCellKind.Uri,
@@ -82,26 +108,16 @@ export const uriCellRenderer: InternalCellRenderer<UriCell> = {
82108
ctx.fillStyle = isLinky ? theme.linkColor : theme.textDark;
83109
drawTextCell(a, txt, cell.contentAlign);
84110
},
111+
onSelect: e => {
112+
if (isOverLinkText(e)) {
113+
e.preventDefault();
114+
}
115+
},
85116
onClick: a => {
86-
const { cell, bounds, posX, posY, theme } = a;
87-
const txt = cell.displayData ?? cell.data;
88-
if (cell.hoverEffect !== true || cell.onClickUri === undefined) return;
89-
90-
const m = getMeasuredTextCache(txt, theme.baseFontFull);
91-
if (m === undefined) return;
92-
const textRect = getTextRect(m, bounds, theme, cell.contentAlign);
93-
const didClick = pointInRect(
94-
{
95-
x: textRect.x - 4,
96-
y: textRect.y - 4,
97-
width: textRect.width + 8,
98-
height: textRect.height + 8,
99-
},
100-
posX,
101-
posY
102-
);
117+
const { cell } = a;
118+
const didClick = isOverLinkText(a);
103119
if (didClick) {
104-
cell.onClickUri(a);
120+
cell.onClickUri?.(a);
105121
}
106122
return undefined;
107123
},

‎packages/core/src/data-editor/stories/utils.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,10 @@ function getColumnsForCellTypes(): GridColumnWithMockingInfo[] {
678678
kind: GridCellKind.Uri,
679679
data: url,
680680
allowOverlay: true,
681+
hoverEffect: true,
682+
onClickUri: () => {
683+
window.open(url, "_blank");
684+
},
681685
};
682686
},
683687
},

0 commit comments

Comments
 (0)
Failed to load comments.