Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prop enableSelectionOp and onDOMSelectionChangeThrottleTime #5772

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/lovely-penguins-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"slate-react": patch
"slate": patch
---

add prop enableSelectionOp and onDOMSelectionChangeThrottleTime
16 changes: 16 additions & 0 deletions docs/api/operations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type InsertNodeOperation = {
type: 'insert_node'
path: Path
node: Node
// default value is true
enableSelectionOp: boolean
}

// merge two `Node` objects
Expand All @@ -28,20 +30,26 @@ type MergeNodeOperation = {
path: Path
position: number
properties: Partial<Node>
// default value is true
enableSelectionOp: boolean
}

// move `Node` from one path to another
type MoveNodeOperation = {
type: 'move_node'
path: Path
newPath: Path
// default value is true
enableSelectionOp: boolean
}

// Remove a `Node`
type RemoveNodeOperation = {
type: 'remove_node'
path: Path
node: Node
// default value is true
enableSelectionOp: boolean
}

// Set properties of a `Node`
Expand All @@ -50,6 +58,8 @@ type SetNodeOperation = {
path: Path
properties: Partial<Node>
newProperties: Partial<Node>
// default value is true
enableSelectionOp: boolean
}

// Split a node into two separate `Node` objects
Expand All @@ -58,6 +68,8 @@ type SplitNodeOperation = {
path: Path
position: number
properties: Partial<Node>
// default value is true
enableSelectionOp: boolean
}

export type NodeOperation =
Expand All @@ -82,6 +94,8 @@ type InsertTextOperation = {
path: Path
offset: number
text: string
// default value is true
enableSelectionOp: boolean
}

// remove text from an existing `Text` node
Expand All @@ -90,6 +104,8 @@ type RemoveTextOperation = {
path: Path
offset: number
text: string
// default value is true
enableSelectionOp: boolean
}

export type TextOperation = InsertTextOperation | RemoveTextOperation
Expand Down
1 change: 1 addition & 0 deletions docs/libraries/slate-react/editable.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type EditableProps = {
scrollSelectionIntoView?: (editor: ReactEditor, domRange: DOMRange) => void
as?: React.ElementType
disableDefaultStyles?: boolean
onDOMSelectionChangeThrottleTime?: number
} & React.TextareaHTMLAttributes<HTMLDivElement>
```
Expand Down
6 changes: 4 additions & 2 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export type EditableProps = {
style?: React.CSSProperties
renderElement?: (props: RenderElementProps) => JSX.Element
renderLeaf?: (props: RenderLeafProps) => JSX.Element
onDOMSelectionChangeThrottleTime?: number
renderPlaceholder?: (props: RenderPlaceholderProps) => JSX.Element
scrollSelectionIntoView?: (editor: ReactEditor, domRange: DOMRange) => void
as?: React.ElementType
Expand Down Expand Up @@ -153,6 +154,7 @@ export const Editable = forwardRef(
style: userStyle = {},
as: Component = 'div',
disableDefaultStyles = false,
onDOMSelectionChangeThrottleTime = 100,
...attributes
} = props
const editor = useSlate()
Expand Down Expand Up @@ -287,8 +289,8 @@ export const Editable = forwardRef(
Transforms.deselect(editor)
}
}
}, 100),
[editor, readOnly, state]
}, onDOMSelectionChangeThrottleTime),
[editor, onDOMSelectionChangeThrottleTime, readOnly, state]
)

const scheduleOnDOMSelectionChange = useMemo(
Expand Down
8 changes: 8 additions & 0 deletions packages/slate/src/interfaces/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type BaseInsertNodeOperation = {
type: 'insert_node'
path: Path
node: Node
enableSelectionOp?: boolean
}

export type InsertNodeOperation = ExtendedType<
Expand All @@ -17,6 +18,7 @@ export type BaseInsertTextOperation = {
path: Path
offset: number
text: string
enableSelectionOp?: boolean
}

export type InsertTextOperation = ExtendedType<
Expand All @@ -29,6 +31,7 @@ export type BaseMergeNodeOperation = {
path: Path
position: number
properties: Partial<Node>
enableSelectionOp?: boolean
}

export type MergeNodeOperation = ExtendedType<
Expand All @@ -40,6 +43,7 @@ export type BaseMoveNodeOperation = {
type: 'move_node'
path: Path
newPath: Path
enableSelectionOp?: boolean
}

export type MoveNodeOperation = ExtendedType<
Expand All @@ -51,6 +55,7 @@ export type BaseRemoveNodeOperation = {
type: 'remove_node'
path: Path
node: Node
enableSelectionOp?: boolean
}

export type RemoveNodeOperation = ExtendedType<
Expand All @@ -63,6 +68,7 @@ export type BaseRemoveTextOperation = {
path: Path
offset: number
text: string
enableSelectionOp?: boolean
}

export type RemoveTextOperation = ExtendedType<
Expand All @@ -75,6 +81,7 @@ export type BaseSetNodeOperation = {
path: Path
properties: Partial<Node>
newProperties: Partial<Node>
enableSelectionOp?: boolean
}

export type SetNodeOperation = ExtendedType<
Expand Down Expand Up @@ -109,6 +116,7 @@ export type BaseSplitNodeOperation = {
path: Path
position: number
properties: Partial<Node>
enableSelectionOp?: boolean
}

export type SplitNodeOperation = ExtendedType<
Expand Down
29 changes: 14 additions & 15 deletions packages/slate/src/interfaces/transforms/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface GeneralTransforms {
const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {
switch (op.type) {
case 'insert_node': {
const { path, node } = op
const { path, node, enableSelectionOp = true } = op
const parent = Node.parent(editor, path)
const index = path[path.length - 1]

Expand All @@ -37,7 +37,7 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {

parent.children.splice(index, 0, node)

if (selection) {
if (selection && enableSelectionOp) {
for (const [point, key] of Range.points(selection)) {
selection[key] = Point.transform(point, op)!
}
Expand All @@ -47,24 +47,23 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {
}

case 'insert_text': {
const { path, offset, text } = op
const { path, offset, text, enableSelectionOp = true } = op
if (text.length === 0) break
const node = Node.leaf(editor, path)
const before = node.text.slice(0, offset)
const after = node.text.slice(offset)
node.text = before + text + after

if (selection) {
if (selection && enableSelectionOp) {
for (const [point, key] of Range.points(selection)) {
selection[key] = Point.transform(point, op)!
}
}

break
}

case 'merge_node': {
const { path } = op
const { path, enableSelectionOp = true } = op
const node = Node.get(editor, path)
const prevPath = Path.previous(path)
const prev = Node.get(editor, prevPath)
Expand All @@ -85,7 +84,7 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {

parent.children.splice(index, 1)

if (selection) {
if (selection && enableSelectionOp) {
for (const [point, key] of Range.points(selection)) {
selection[key] = Point.transform(point, op)!
}
Expand All @@ -95,7 +94,7 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {
}

case 'move_node': {
const { path, newPath } = op
const { path, newPath, enableSelectionOp = true } = op

if (Path.isAncestor(path, newPath)) {
throw new Error(
Expand All @@ -120,7 +119,7 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {

newParent.children.splice(newIndex, 0, node)

if (selection) {
if (selection && enableSelectionOp) {
for (const [point, key] of Range.points(selection)) {
selection[key] = Point.transform(point, op)!
}
Expand All @@ -130,14 +129,14 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {
}

case 'remove_node': {
const { path } = op
const { path, enableSelectionOp = true } = op
const index = path[path.length - 1]
const parent = Node.parent(editor, path)
parent.children.splice(index, 1)

// Transform all the points in the value, but if the point was in the
// node that was removed we need to update the range or remove it.
if (selection) {
if (selection && enableSelectionOp) {
for (const [point, key] of Range.points(selection)) {
const result = Point.transform(point, op)

Expand Down Expand Up @@ -184,14 +183,14 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {
}

case 'remove_text': {
const { path, offset, text } = op
const { path, offset, text, enableSelectionOp = true } = op
if (text.length === 0) break
const node = Node.leaf(editor, path)
const before = node.text.slice(0, offset)
const after = node.text.slice(offset + text.length)
node.text = before + after

if (selection) {
if (selection && enableSelectionOp) {
for (const [point, key] of Range.points(selection)) {
selection[key] = Point.transform(point, op)!
}
Expand Down Expand Up @@ -270,7 +269,7 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {
}

case 'split_node': {
const { path, position, properties } = op
const { path, position, properties, enableSelectionOp = true } = op

if (path.length === 0) {
throw new Error(
Expand Down Expand Up @@ -304,7 +303,7 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {

parent.children.splice(index + 1, 0, newNode)

if (selection) {
if (selection && enableSelectionOp) {
for (const [point, key] of Range.points(selection)) {
selection[key] = Point.transform(point, op)!
}
Expand Down
Loading