Skip to content

Commit

Permalink
fix: allow clicking on image while selecting text
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-ext committed Feb 29, 2024
1 parent 65624cc commit cdb79bb
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions app/desktop/components/panes/PaneLinkingProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type JSX, lazy, batch } from 'solid-js';

import { hasSelectionRange, isCtrlKeyPressed, isElementAltClicked } from '~/utils/interaction';
import { isCtrlKeyPressed, isElementAltClicked } from '~/utils/interaction';

import { openModal } from '~/com/globals/modals';

Expand Down Expand Up @@ -123,7 +123,7 @@ export const PaneLinkingProvider = (props: PaneLinkingProviderProps) => {
{...props}
// @ts-expect-error
to={null}
onClick={(ev) => !hasSelectionRange() && navigate(to, isCtrlKeyPressed(ev))}
onClick={(ev) => isValidClick(ev) && navigate(to, isCtrlKeyPressed(ev))}
onAuxClick={(ev) => ev.button === 1 && navigate(to, true)}
onKeyDown={(ev) => isEnterPressed(ev) && navigate(to, isCtrlKeyPressed(ev))}
/>
Expand All @@ -135,6 +135,21 @@ export const PaneLinkingProvider = (props: PaneLinkingProviderProps) => {
return <LinkingContext.Provider value={linkContext}>{props.children}</LinkingContext.Provider>;
};

const isValidClick = (ev: MouseEvent & { target: Element }) => {
const selection = window.getSelection();
if (selection === null || selection.type === 'Caret') {
return true;
}

const closest = ev.target.closest('img');
if (closest !== null) {
selection.removeAllRanges();
return true;
}

return false;
};

const isEnterPressed = (ev: KeyboardEvent) => {
const key = ev.key;
return key === 'Enter' || key === 'Space';
Expand Down

0 comments on commit cdb79bb

Please sign in to comment.