Skip to content

Commit

Permalink
fix point transform for insert_text to account for affinity (ianstorm…
Browse files Browse the repository at this point in the history
…taylor#4848)

* fix point transform for insert_text

* add changeset
  • Loading branch information
rockettomatooo authored and DougReeder committed Apr 3, 2022
1 parent ffca574 commit 07641bc
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 1 deletion.
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,
},
}

0 comments on commit 07641bc

Please sign in to comment.