@contentful/field-editor-rich-text 6.3.8 → 6.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ const _platetestutils = require("@udecode/plate-test-utils");
6
+ const _vitest = require("vitest");
7
+ const _transforms = require("../transforms");
8
+ const paragraph = (text)=>({
9
+ type: 'paragraph',
10
+ data: {},
11
+ isVoid: false,
12
+ children: [
13
+ {
14
+ text
15
+ }
16
+ ]
17
+ });
18
+ const createEditor = (children)=>(0, _platetestutils.createEditor)('test-editor', {}, children);
19
+ (0, _vitest.describe)('setEditorValue', ()=>{
20
+ (0, _vitest.it)('preserves cursor position when incoming value has the same structure', ()=>{
21
+ const initial = [
22
+ paragraph('hello world')
23
+ ];
24
+ const editor = createEditor(initial);
25
+ (0, _transforms.select)(editor, {
26
+ path: [
27
+ 0,
28
+ 0
29
+ ],
30
+ offset: 5
31
+ });
32
+ (0, _vitest.expect)(editor.selection).toEqual({
33
+ anchor: {
34
+ path: [
35
+ 0,
36
+ 0
37
+ ],
38
+ offset: 5
39
+ },
40
+ focus: {
41
+ path: [
42
+ 0,
43
+ 0
44
+ ],
45
+ offset: 5
46
+ }
47
+ });
48
+ const incoming = [
49
+ paragraph('hello world')
50
+ ];
51
+ (0, _transforms.setEditorValue)(editor, incoming);
52
+ (0, _vitest.expect)(editor.selection).toEqual({
53
+ anchor: {
54
+ path: [
55
+ 0,
56
+ 0
57
+ ],
58
+ offset: 5
59
+ },
60
+ focus: {
61
+ path: [
62
+ 0,
63
+ 0
64
+ ],
65
+ offset: 5
66
+ }
67
+ });
68
+ });
69
+ (0, _vitest.it)('clamps cursor to end when incoming value is shorter than current selection', ()=>{
70
+ const initial = [
71
+ paragraph('hello world')
72
+ ];
73
+ const editor = createEditor(initial);
74
+ (0, _transforms.select)(editor, {
75
+ path: [
76
+ 0,
77
+ 0
78
+ ],
79
+ offset: 11
80
+ });
81
+ const incoming = [
82
+ paragraph('hi')
83
+ ];
84
+ (0, _transforms.setEditorValue)(editor, incoming);
85
+ (0, _vitest.expect)(editor.selection).toEqual({
86
+ anchor: {
87
+ path: [
88
+ 0,
89
+ 0
90
+ ],
91
+ offset: 2
92
+ },
93
+ focus: {
94
+ path: [
95
+ 0,
96
+ 0
97
+ ],
98
+ offset: 2
99
+ }
100
+ });
101
+ });
102
+ (0, _vitest.it)('moves cursor to end when there is no prior selection', ()=>{
103
+ const initial = [
104
+ paragraph('hello')
105
+ ];
106
+ const editor = createEditor(initial);
107
+ const incoming = [
108
+ paragraph('hello')
109
+ ];
110
+ (0, _transforms.setEditorValue)(editor, incoming);
111
+ (0, _vitest.expect)(editor.selection).toEqual({
112
+ anchor: {
113
+ path: [
114
+ 0,
115
+ 0
116
+ ],
117
+ offset: 5
118
+ },
119
+ focus: {
120
+ path: [
121
+ 0,
122
+ 0
123
+ ],
124
+ offset: 5
125
+ }
126
+ });
127
+ });
128
+ });
@@ -206,6 +206,7 @@ const deleteFragment = (editor, options)=>{
206
206
  return _platecommon.deleteFragment(editor, options);
207
207
  };
208
208
  const setEditorValue = (editor, nodes)=>{
209
+ const savedSelection = editor.selection;
209
210
  withoutNormalizing(editor, ()=>{
210
211
  const children = [
211
212
  ...editor.children
@@ -231,9 +232,24 @@ const setEditorValue = (editor, nodes)=>{
231
232
  });
232
233
  });
233
234
  }
234
- const point = (0, _queries.getEndPoint)(editor, []);
235
- if (point) {
236
- select(editor, point);
235
+ const endPoint = (0, _queries.getEndPoint)(editor, []);
236
+ if (savedSelection && endPoint) {
237
+ const clampPoint = (point)=>{
238
+ const entry = (0, _queries.getNodeEntry)(editor, point.path);
239
+ if (entry && (0, _queries.isText)(entry[0])) {
240
+ return {
241
+ path: point.path,
242
+ offset: Math.min(point.offset, entry[0].text.length)
243
+ };
244
+ }
245
+ return endPoint;
246
+ };
247
+ select(editor, {
248
+ anchor: clampPoint(savedSelection.anchor),
249
+ focus: clampPoint(savedSelection.focus)
250
+ });
251
+ } else if (endPoint) {
252
+ select(editor, endPoint);
237
253
  }
238
254
  });
239
255
  };
@@ -0,0 +1,125 @@
1
+ import { createEditor as createSlateEditor } from '@udecode/plate-test-utils';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { select } from '../transforms';
4
+ import { setEditorValue } from '../transforms';
5
+ const paragraph = (text)=>({
6
+ type: 'paragraph',
7
+ data: {},
8
+ isVoid: false,
9
+ children: [
10
+ {
11
+ text
12
+ }
13
+ ]
14
+ });
15
+ const createEditor = (children)=>createSlateEditor('test-editor', {}, children);
16
+ describe('setEditorValue', ()=>{
17
+ it('preserves cursor position when incoming value has the same structure', ()=>{
18
+ const initial = [
19
+ paragraph('hello world')
20
+ ];
21
+ const editor = createEditor(initial);
22
+ select(editor, {
23
+ path: [
24
+ 0,
25
+ 0
26
+ ],
27
+ offset: 5
28
+ });
29
+ expect(editor.selection).toEqual({
30
+ anchor: {
31
+ path: [
32
+ 0,
33
+ 0
34
+ ],
35
+ offset: 5
36
+ },
37
+ focus: {
38
+ path: [
39
+ 0,
40
+ 0
41
+ ],
42
+ offset: 5
43
+ }
44
+ });
45
+ const incoming = [
46
+ paragraph('hello world')
47
+ ];
48
+ setEditorValue(editor, incoming);
49
+ expect(editor.selection).toEqual({
50
+ anchor: {
51
+ path: [
52
+ 0,
53
+ 0
54
+ ],
55
+ offset: 5
56
+ },
57
+ focus: {
58
+ path: [
59
+ 0,
60
+ 0
61
+ ],
62
+ offset: 5
63
+ }
64
+ });
65
+ });
66
+ it('clamps cursor to end when incoming value is shorter than current selection', ()=>{
67
+ const initial = [
68
+ paragraph('hello world')
69
+ ];
70
+ const editor = createEditor(initial);
71
+ select(editor, {
72
+ path: [
73
+ 0,
74
+ 0
75
+ ],
76
+ offset: 11
77
+ });
78
+ const incoming = [
79
+ paragraph('hi')
80
+ ];
81
+ setEditorValue(editor, incoming);
82
+ expect(editor.selection).toEqual({
83
+ anchor: {
84
+ path: [
85
+ 0,
86
+ 0
87
+ ],
88
+ offset: 2
89
+ },
90
+ focus: {
91
+ path: [
92
+ 0,
93
+ 0
94
+ ],
95
+ offset: 2
96
+ }
97
+ });
98
+ });
99
+ it('moves cursor to end when there is no prior selection', ()=>{
100
+ const initial = [
101
+ paragraph('hello')
102
+ ];
103
+ const editor = createEditor(initial);
104
+ const incoming = [
105
+ paragraph('hello')
106
+ ];
107
+ setEditorValue(editor, incoming);
108
+ expect(editor.selection).toEqual({
109
+ anchor: {
110
+ path: [
111
+ 0,
112
+ 0
113
+ ],
114
+ offset: 5
115
+ },
116
+ focus: {
117
+ path: [
118
+ 0,
119
+ 0
120
+ ],
121
+ offset: 5
122
+ }
123
+ });
124
+ });
125
+ });
@@ -1,5 +1,5 @@
1
1
  import * as p from '@udecode/plate-common';
2
- import { getEndPoint, isNode } from './queries';
2
+ import { getEndPoint, getNodeEntry, isNode, isText } from './queries';
3
3
  export const normalize = (editor, options = {
4
4
  force: true
5
5
  })=>{
@@ -78,6 +78,7 @@ export const deleteFragment = (editor, options)=>{
78
78
  return p.deleteFragment(editor, options);
79
79
  };
80
80
  export const setEditorValue = (editor, nodes)=>{
81
+ const savedSelection = editor.selection;
81
82
  withoutNormalizing(editor, ()=>{
82
83
  const children = [
83
84
  ...editor.children
@@ -103,9 +104,24 @@ export const setEditorValue = (editor, nodes)=>{
103
104
  });
104
105
  });
105
106
  }
106
- const point = getEndPoint(editor, []);
107
- if (point) {
108
- select(editor, point);
107
+ const endPoint = getEndPoint(editor, []);
108
+ if (savedSelection && endPoint) {
109
+ const clampPoint = (point)=>{
110
+ const entry = getNodeEntry(editor, point.path);
111
+ if (entry && isText(entry[0])) {
112
+ return {
113
+ path: point.path,
114
+ offset: Math.min(point.offset, entry[0].text.length)
115
+ };
116
+ }
117
+ return endPoint;
118
+ };
119
+ select(editor, {
120
+ anchor: clampPoint(savedSelection.anchor),
121
+ focus: clampPoint(savedSelection.focus)
122
+ });
123
+ } else if (endPoint) {
124
+ select(editor, endPoint);
109
125
  }
110
126
  });
111
127
  };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-rich-text",
3
- "version": "6.3.8",
3
+ "version": "6.3.9",
4
4
  "source": "./src/index.tsx",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -94,5 +94,5 @@
94
94
  "publishConfig": {
95
95
  "registry": "https://npm.pkg.github.com/"
96
96
  },
97
- "gitHead": "5fb6c32d2590e72e56a53cb180a155f84d6505d9"
97
+ "gitHead": "fdd4cf7185af37fbee697ab03d6e65dbeb9b9e64"
98
98
  }