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 Feb 23, 2022
1 parent 4bc6f4f commit 4521209
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 1 deletion.
6 changes: 5 additions & 1 deletion 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,
}
24 changes: 24 additions & 0 deletions test/interfaces/Point/transform/backward-insert-text-at-point.tsx
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,
}
24 changes: 24 additions & 0 deletions test/interfaces/Point/transform/forward-insert-text-at-point.tsx
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,
}
39 changes: 39 additions & 0 deletions test/interfaces/Range/transform/outward-collapsed.tsx
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 4521209

Please sign in to comment.