Skip to content

Commit

Permalink
Merge pull request #3821 from udecode/block-selection/duration
Browse files Browse the repository at this point in the history
Add delay in addSelectedRow
  • Loading branch information
felixfeng33 authored Nov 28, 2024
2 parents 6c2b4d3 + 1f299f1 commit 8821d9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-keys-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-selection': patch
---

Add `delay` in addSelectedRow option
26 changes: 17 additions & 9 deletions packages/selection/src/react/BlockSelectionPlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import type { CSSProperties } from 'react';
import type React from 'react';

import {
type PluginConfig,
type TElement,
type TNodeEntry,
bindFirst,
getNodeEntries,
} from '@udecode/plate-common';
import type { PluginConfig, TElement, TNodeEntry } from '@udecode/plate-common';

import { bindFirst, getNodeEntries } from '@udecode/plate-common';
import { createTPlatePlugin } from '@udecode/plate-common/react';

import type { ChangedElements, PartialSelectionOptions } from '../internal';
Expand Down Expand Up @@ -52,10 +48,13 @@ export type BlockSelectionSelectors = {
};

export type BlockSelectionApi = {
addSelectedRow: (
id: string,
options?: { clear?: boolean; delay?: number }
) => void;
setSelectedIds: (
options: Partial<ChangedElements> & { ids?: string[] }
) => void;
addSelectedRow: (id: string, options?: { clear?: boolean }) => void;
focus: () => void;
getNodes: () => TNodeEntry[];
resetSelectedIds: () => void;
Expand Down Expand Up @@ -181,7 +180,7 @@ export const BlockSelectionPlugin = createTPlatePlugin<BlockSelectionConfig>({
)
.extendApi<Partial<BlockSelectionApi>>(({ api, getOptions, setOption }) => ({
addSelectedRow: (id, options = {}) => {
const { clear = true } = options;
const { clear = true, delay } = options;

const element = getSelectedDomNode(id);

Expand All @@ -194,6 +193,15 @@ export const BlockSelectionPlugin = createTPlatePlugin<BlockSelectionConfig>({
added: [element],
removed: [],
});

if (delay) {
setTimeout(() => {
api.blockSelection.setSelectedIds({
added: [],
removed: [element],
});
}, delay);
}
},

selectedAll: () => {
Expand Down

0 comments on commit 8821d9b

Please sign in to comment.