Skip to content

Commit

Permalink
Merge pull request #3622 from udecode/refactor/selection
Browse files Browse the repository at this point in the history
Fix link
  • Loading branch information
zbeyens authored Oct 10, 2024
2 parents 67ec113 + 7291340 commit f2d14b1
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/cuddly-weeks-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@udecode/plate-link': patch
---

- Fix floating link insert
- Hide floating link edit when clicking outside
5 changes: 5 additions & 0 deletions .changeset/curly-dolls-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-resizable': patch
---

Hide ResizeHandle when read-only
5 changes: 5 additions & 0 deletions .changeset/rare-lemons-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-floating': patch
---

Add `getDOMSelectionBoundingClientRect`
3 changes: 2 additions & 1 deletion apps/www/content/docs/components/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ Use the [CLI](https://platejs.org/docs/components/cli) to install the latest ver
### October 10 #15.3

- `dropdown-menu`(`DropdownMenuContent`): prevent default on `onCloseAutoFocus`
- `floating-toolbar`(`FloatingToolbar`): remove portal, hide on click outside
- `floating-toolbar`(`FloatingToolbar`): remove portal, hide on click outside, hide when floating link open
- `turn-into-dropdown-menu`(`TurnIntoDropdownMenu`): add indent list items
- `table-dropdown-menu`(`TableDropdownMenu`): select in `insertTable`

### October 4 #15.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,23 @@ export function PlaygroundInsertDropdownMenu(props: DropdownMenuProps) {
break;
}
case CodeBlockPlugin.key: {
insertEmptyCodeBlock(editor);
insertEmptyCodeBlock(editor, {
insertNodesOptions: { select: true },
});

break;
}
case ImagePlugin.key: {
await insertMedia(editor, { type: ImagePlugin.key });
await insertMedia(editor, {
select: true,
type: ImagePlugin.key,
});

break;
}
case MediaEmbedPlugin.key: {
await insertMedia(editor, {
select: true,
type: MediaEmbedPlugin.key,
});

Expand All @@ -239,7 +245,7 @@ export function PlaygroundInsertDropdownMenu(props: DropdownMenuProps) {
break;
}
case TablePlugin.key: {
insertTable(editor);
insertTable(editor, {}, { select: true });

break;
}
Expand Down
5 changes: 5 additions & 0 deletions apps/www/src/registry/default/plate-ui/floating-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { cn, withRef } from '@udecode/cn';
import {
useComposedRef,
useEditorId,
useEditorRef,
useEventEditorSelectors,
} from '@udecode/plate-common/react';
import {
Expand All @@ -15,6 +16,7 @@ import {
useFloatingToolbar,
useFloatingToolbarState,
} from '@udecode/plate-floating';
import { LinkPlugin } from '@udecode/plate-link/react';

import { Toolbar } from './toolbar';

Expand All @@ -24,12 +26,15 @@ export const FloatingToolbar = withRef<
state?: FloatingToolbarState;
}
>(({ children, state, ...props }, componentRef) => {
const editor = useEditorRef();
const editorId = useEditorId();
const focusedEditorId = useEventEditorSelectors.focus();
const isFloatingLinkOpen = !!editor.useOption(LinkPlugin, 'mode');

const floatingToolbarState = useFloatingToolbarState({
editorId,
focusedEditorId,
hideToolbar: isFloatingLinkOpen,
...state,
floatingOptions: {
middleware: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function TableDropdownMenu(props: DropdownMenuProps) {
<DropdownMenuItem
className="min-w-[180px]"
onSelect={() => {
insertTable(editor);
insertTable(editor, {}, { select: true });
focusEditor(editor);
}}
>
Expand Down
16 changes: 16 additions & 0 deletions packages/floating/src/utils/getDOMSelectionBoundingClientRect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { ClientRectObject } from '@floating-ui/core';

import { getDefaultBoundingClientRect } from '../createVirtualElement';

/** Get bounding client rect of the editor selection */
export const getDOMSelectionBoundingClientRect = (): ClientRectObject => {
const domSelection = window.getSelection();

if (!domSelection || domSelection.rangeCount < 1) {
return getDefaultBoundingClientRect();
}

const domRange = domSelection.getRangeAt(0);

return domRange.getBoundingClientRect();
};
1 change: 1 addition & 0 deletions packages/floating/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

export * from './createVirtualRef';
export * from './getBoundingClientRect';
export * from './getDOMSelectionBoundingClientRect';
export * from './getRangeBoundingClientRect';
export * from './getSelectionBoundingClientRect';
export * from './makeClientRect';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import {
someNode,
} from '@udecode/plate-common';
import {
useComposedRef,
useEditorPlugin,
useEditorReadOnly,
useEditorVersion,
useHotkeys,
useOnClickOutside,
} from '@udecode/plate-common/react';
import {
getDOMSelectionBoundingClientRect,
getRangeBoundingClientRect,
getSelectionBoundingClientRect,
} from '@udecode/plate-floating';

import type { LinkFloatingToolbarState } from './useFloatingLinkInsert';
Expand All @@ -29,7 +31,8 @@ import { useVirtualFloatingLink } from './useVirtualFloatingLink';
export const useFloatingLinkEditState = ({
floatingOptions,
}: LinkFloatingToolbarState = {}) => {
const { editor, getOptions, type, useOption } = useEditorPlugin(LinkPlugin);
const { api, editor, getOptions, type, useOption } =
useEditorPlugin(LinkPlugin);

const { triggerFloatingLinkHotkeys } = getOptions();
const readOnly = useEditorReadOnly();
Expand All @@ -52,7 +55,7 @@ export const useFloatingLinkEditState = ({
});
}

return getSelectionBoundingClientRect(editor);
return getDOMSelectionBoundingClientRect();
}, [editor, type]);

const isOpen = open && mode === 'edit';
Expand Down Expand Up @@ -118,6 +121,10 @@ export const useFloatingLinkEdit = ({

useFloatingLinkEscape();

const clickOutsideRef = useOnClickOutside(() => {
api.floatingLink.hide();
});

return {
editButtonProps: {
onClick: () => {
Expand All @@ -130,7 +137,7 @@ export const useFloatingLinkEdit = ({
zIndex: 50,
},
},
ref: floating.refs.setFloating,
ref: useComposedRef(floating.refs.setFloating, clickOutsideRef),
unlinkButtonProps: {
onClick: () => {
unwrapLink(editor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@udecode/plate-common/react';
import {
type UseVirtualFloatingOptions,
getSelectionBoundingClientRect,
getDOMSelectionBoundingClientRect,
} from '@udecode/plate-floating';
import { useFocused } from 'slate-react';

Expand All @@ -36,7 +36,7 @@ export const useFloatingLinkInsertState = ({

const floating = useVirtualFloatingLink({
editorId: editor.id,
getBoundingClientRect: () => getSelectionBoundingClientRect(editor),
getBoundingClientRect: getDOMSelectionBoundingClientRect,
open: isOpen && mode === 'insert',
whileElementsMounted: () => () => {},
...floatingOptions,
Expand Down
5 changes: 5 additions & 0 deletions packages/resizable/src/components/ResizeHandle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createAtomStore,
createPrimitiveComponent,
} from '@udecode/plate-common/react';
import { useReadOnly } from 'slate-react';

import type { ResizeDirection, ResizeEvent } from '../types';

Expand Down Expand Up @@ -43,6 +44,7 @@ export const useResizeHandleState = ({
onResize: onResizeProp,
onTouchStart,
}: ResizeHandleOptions) => {
const readOnly = useReadOnly();
const onResizeStore = useResizeHandleStore().get.onResize();
const onResize = onResizeProp ?? onResizeStore;

Expand Down Expand Up @@ -110,6 +112,7 @@ export const useResizeHandleState = ({
initialSize,
isHorizontal,
isResizing,
readOnly,
setInitialPosition,
setInitialSize,
setIsResizing,
Expand All @@ -124,6 +127,7 @@ export const useResizeHandleState = ({
export const useResizeHandle = ({
isHorizontal,
isResizing,
readOnly,
setInitialPosition,
setInitialSize,
setIsResizing,
Expand Down Expand Up @@ -167,6 +171,7 @@ export const useResizeHandle = ({
};

return {
hidden: readOnly,
props: {
onMouseDown: handleMouseDown,
onMouseOut: handleMouseOut,
Expand Down

0 comments on commit f2d14b1

Please sign in to comment.