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

fix point transform for insert_text to account for affinity #4848

Merged
merged 2 commits into from
Feb 23, 2022
Merged
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
5 changes: 5 additions & 0 deletions .changeset/fast-doors-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate': minor
---

fix point transform for insert_text operations to account for affinity
6 changes: 5 additions & 1 deletion packages/slate/src/interfaces/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ export const Point: PointInterface = {
}

case 'insert_text': {
if (Path.equals(op.path, path) && op.offset <= offset) {
if (
Path.equals(op.path, path) &&
(op.offset < offset ||
(op.offset === offset && affinity === 'forward'))
) {
p.offset += op.text.length
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Point } from 'slate'

export const input = {
path: [0, 0],
offset: 0,
}

export const test = value => {
return Point.transform(
value,
{
type: 'insert_text',
path: [0, 0],
text: 'a',
offset: 1,
properties: {},
},
{ affinity: 'backward' }
)
}
export const output = {
path: [0, 0],
offset: 0,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Point } from 'slate'

export const input = {
path: [0, 0],
offset: 1,
}

export const test = value => {
return Point.transform(
value,
{
type: 'insert_text',
path: [0, 0],
text: 'a',
offset: 1,
properties: {},
},
{ affinity: 'backward' }
)
}
export const output = {
path: [0, 0],
offset: 1,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Point } from 'slate'

export const input = {
path: [0, 0],
offset: 1,
}

export const test = value => {
return Point.transform(
value,
{
type: 'insert_text',
path: [0, 0],
text: 'a',
offset: 0,
properties: {},
},
{ affinity: 'backward' }
)
}
export const output = {
path: [0, 0],
offset: 2,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Point } from 'slate'

export const input = {
path: [0, 0],
offset: 0,
}

export const test = value => {
return Point.transform(
value,
{
type: 'insert_text',
path: [0, 0],
text: 'a',
offset: 1,
properties: {},
},
{ affinity: 'forward' }
)
}
export const output = {
path: [0, 0],
offset: 0,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Point } from 'slate'

export const input = {
path: [0, 0],
offset: 1,
}

export const test = value => {
return Point.transform(
value,
{
type: 'insert_text',
path: [0, 0],
text: 'a',
offset: 1,
properties: {},
},
{ affinity: 'forward' }
)
}
export const output = {
path: [0, 0],
offset: 2,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Point } from 'slate'

export const input = {
path: [0, 0],
offset: 1,
}

export const test = value => {
return Point.transform(
value,
{
type: 'insert_text',
path: [0, 0],
text: 'a',
offset: 0,
properties: {},
},
{ affinity: 'forward' }
)
}
export const output = {
path: [0, 0],
offset: 2,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Range } from 'slate'

/**
* If a collapsed Range is transformed with affinity outward by an insert_text operation, it should expand.
*/

export const input = {
anchor: {
path: [0, 0],
offset: 1,
},
focus: {
path: [0, 0],
offset: 1,
},
}
export const test = value => {
return Range.transform(
value,
{
type: 'insert_text',
path: [0, 0],
text: 'a',
offset: 1,
properties: {},
},
{ affinity: 'outward' }
)
}
export const output = {
anchor: {
path: [0, 0],
offset: 1,
},
focus: {
path: [0, 0],
offset: 2,
},
}