1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 15:02:51 +02:00

Collapse selection according to reverse (#3799)

* Rename test slate transforms delete unit-character inline-before to inline-afte

* Add missing tests for Transforms.delete, symmetric to existing tests with respect to options.reverse

* In Transforms.delete collapse selection according to options.reverse

* Add directionality to Editor#deleteFragment
This commit is contained in:
Krzysztof Mędrzycki
2021-03-31 23:38:02 +02:00
committed by GitHub
parent e2576108a6
commit 9fce1066ba
11 changed files with 132 additions and 27 deletions

View File

@@ -301,7 +301,8 @@ export const Editable = (props: EditableProps) => {
Range.isExpanded(selection) && Range.isExpanded(selection) &&
type.startsWith('delete') type.startsWith('delete')
) { ) {
Editor.deleteFragment(editor) const direction = type.endsWith('Backward') ? 'backward' : 'forward'
Editor.deleteFragment(editor, direction)
return return
} }
@@ -945,7 +946,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor) Editor.deleteFragment(editor, 'backward')
} else { } else {
Editor.deleteBackward(editor) Editor.deleteBackward(editor)
} }
@@ -957,7 +958,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor) Editor.deleteFragment(editor, 'forward')
} else { } else {
Editor.deleteForward(editor) Editor.deleteForward(editor)
} }
@@ -969,7 +970,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor) Editor.deleteFragment(editor, 'backward')
} else { } else {
Editor.deleteBackward(editor, { unit: 'line' }) Editor.deleteBackward(editor, { unit: 'line' })
} }
@@ -981,7 +982,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor) Editor.deleteFragment(editor, 'forward')
} else { } else {
Editor.deleteForward(editor, { unit: 'line' }) Editor.deleteForward(editor, { unit: 'line' })
} }
@@ -993,7 +994,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor) Editor.deleteFragment(editor, 'backward')
} else { } else {
Editor.deleteBackward(editor, { unit: 'word' }) Editor.deleteBackward(editor, { unit: 'word' })
} }
@@ -1005,7 +1006,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor) Editor.deleteFragment(editor, 'forward')
} else { } else {
Editor.deleteForward(editor, { unit: 'word' }) Editor.deleteForward(editor, { unit: 'word' })
} }

View File

@@ -127,11 +127,11 @@ export const createEditor = (): Editor => {
} }
}, },
deleteFragment: () => { deleteFragment: (direction?: 'forward' | 'backward') => {
const { selection } = editor const { selection } = editor
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Transforms.delete(editor) Transforms.delete(editor, { reverse: direction === 'backward' })
} }
}, },

View File

@@ -54,7 +54,7 @@ export interface BaseEditor {
apply: (operation: Operation) => void apply: (operation: Operation) => void
deleteBackward: (unit: 'character' | 'word' | 'line' | 'block') => void deleteBackward: (unit: 'character' | 'word' | 'line' | 'block') => void
deleteForward: (unit: 'character' | 'word' | 'line' | 'block') => void deleteForward: (unit: 'character' | 'word' | 'line' | 'block') => void
deleteFragment: () => void deleteFragment: (direction?: 'forward' | 'backward') => void
getFragment: () => Descendant[] getFragment: () => Descendant[]
insertBreak: () => void insertBreak: () => void
insertFragment: (fragment: Node[]) => void insertFragment: (fragment: Node[]) => void
@@ -435,8 +435,8 @@ export const Editor: EditorInterface = {
* Delete the content in the current selection. * Delete the content in the current selection.
*/ */
deleteFragment(editor: Editor): void { deleteFragment(editor: Editor, direction?: 'forward' | 'backward'): void {
editor.deleteFragment() editor.deleteFragment(direction)
}, },
/** /**

View File

@@ -207,7 +207,9 @@ export const TextTransforms: TextTransforms = {
}) })
} }
const point = endRef.unref() || startRef.unref() const point = reverse
? startRef.unref() || endRef.unref()
: endRef.unref() || startRef.unref()
if (options.at == null && point) { if (options.at == null && point) {
Transforms.select(editor, point) Transforms.select(editor, point)

View File

@@ -0,0 +1,37 @@
/** @jsx jsx */
import { Transforms } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Transforms.delete(editor)
}
export const input = (
<editor>
<block>
one
<inline>
two
<cursor />
</inline>
<text />
</block>
<block>
<text />
<inline>three</inline>
four
</block>
</editor>
)
export const output = (
<editor>
<block>
one
<inline>two</inline>
<text>
<cursor />
</text>
<inline>three</inline>
four
</block>
</editor>
)

View File

@@ -27,11 +27,10 @@ export const output = (
<block> <block>
one one
<inline>two</inline> <inline>two</inline>
<text /> <text>
<inline>
<cursor /> <cursor />
three </text>
</inline> <inline>three</inline>
four four
</block> </block>
</editor> </editor>

View File

@@ -25,9 +25,10 @@ export const output = (
<block> <block>
<text /> <text />
<inline void> <inline void>
<text /> <text>
</inline>
<cursor /> <cursor />
</text>
</inline>
word word
</block> </block>
</editor> </editor>

View File

@@ -0,0 +1,34 @@
/** @jsx jsx */
import { Transforms } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Transforms.delete(editor)
}
export const input = (
<editor>
<block>
word
<cursor />
</block>
<block>
<text />
<inline void>
<text />
</inline>
<text />
</block>
</editor>
)
export const output = (
<editor>
<block>
word
<cursor />
<inline void>
<text />
</inline>
<text />
</block>
</editor>
)

View File

@@ -0,0 +1,25 @@
/** @jsx jsx */
import { Transforms } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Transforms.delete(editor)
}
export const input = (
<editor>
<block>
one
<inline>two</inline>
<cursor />a
</block>
</editor>
)
export const output = (
<editor>
<block>
one
<inline>two</inline>
<cursor />
</block>
</editor>
)

View File

@@ -3,23 +3,26 @@ import { Transforms } from 'slate'
import { jsx } from '../../..' import { jsx } from '../../..'
export const run = editor => { export const run = editor => {
Transforms.delete(editor) Transforms.delete(editor, { unit: 'character' })
} }
export const input = ( export const input = (
<editor> <editor>
<block> <block>
one a<cursor />
<inline>two</inline> <inline>two</inline>
<cursor />a three
</block> </block>
</editor> </editor>
) )
export const output = ( export const output = (
<editor> <editor>
<block> <block>
one a
<inline>two</inline> <inline>
<cursor /> <cursor />
wo
</inline>
three
</block> </block>
</editor> </editor>
) )

View File

@@ -18,8 +18,11 @@ export const output = (
<editor> <editor>
<block> <block>
one one
<inline>tw</inline> <inline>
tw
<cursor /> <cursor />
</inline>
<text />
</block> </block>
</editor> </editor>
) )