@contentful/field-editor-rich-text 6.6.4 → 6.6.5

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.
@@ -131,7 +131,20 @@ const withDeleteTable = (editor)=>{
131
131
  return applyOverride(editor, _table.withDeleteTable);
132
132
  };
133
133
  const withGetFragmentTable = (editor, _plugin)=>applyOverride(editor, _table.withGetFragmentTable);
134
- const withInsertFragmentTable = (editor, _plugin)=>applyOverride(editor, _table.withInsertFragmentTable);
134
+ const withInsertFragmentTable = (editor, _plugin)=>{
135
+ const { insertFragment } = editor;
136
+ editor.insertFragment = (fragment)=>{
137
+ const context = (0, _platejs.getEditorPlugin)(editor, _table.BaseTablePlugin);
138
+ _table.withInsertFragmentTable({
139
+ ...context,
140
+ tf: {
141
+ ...context.tf,
142
+ insertFragment
143
+ }
144
+ }).transforms.insertFragment(fragment);
145
+ };
146
+ return editor;
147
+ };
135
148
  const withInsertTextTable = (editor, _plugin)=>applyOverride(editor, _table.withInsertTextTable);
136
149
  const withSelectionTable = (editor)=>applyOverride(editor, _table.withTableCellSelection);
137
150
  const withSetFragmentDataTable = (editor)=>applyOverride(editor, _table.withSetFragmentDataTable);
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
+ const _richtexttypes = require("@contentful/rich-text-types");
5
6
  const _vitest = require("vitest");
6
7
  const _testutils = require("../../../test-utils");
7
8
  (0, _vitest.describe)('normalization', ()=>{
@@ -91,3 +92,121 @@ const _testutils = require("../../../test-utils");
91
92
  });
92
93
  });
93
94
  });
95
+ (0, _vitest.describe)('paste', ()=>{
96
+ const paragraph = (value)=>({
97
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
98
+ children: [
99
+ {
100
+ text: value
101
+ }
102
+ ]
103
+ });
104
+ const table = (rows)=>({
105
+ type: _richtexttypes.BLOCKS.TABLE,
106
+ children: rows.map((cells)=>({
107
+ type: _richtexttypes.BLOCKS.TABLE_ROW,
108
+ children: cells.map((value)=>({
109
+ type: _richtexttypes.BLOCKS.TABLE_CELL,
110
+ children: [
111
+ paragraph(value)
112
+ ]
113
+ }))
114
+ }))
115
+ });
116
+ _vitest.it.each([
117
+ {
118
+ name: 'rows',
119
+ cells: [
120
+ [
121
+ 'A1',
122
+ 'B1'
123
+ ],
124
+ [
125
+ 'A2',
126
+ 'B2'
127
+ ]
128
+ ]
129
+ },
130
+ {
131
+ name: 'columns',
132
+ cells: [
133
+ [
134
+ 'A1',
135
+ 'B1',
136
+ 'C1'
137
+ ]
138
+ ]
139
+ }
140
+ ])('expands table $name when pasting text and a table into a cell', ({ cells })=>{
141
+ const { editor } = (0, _testutils.createTestEditor)({});
142
+ editor.children = [
143
+ table([
144
+ [
145
+ 'Old A',
146
+ 'Old B'
147
+ ]
148
+ ]),
149
+ paragraph('Keep following paragraph')
150
+ ];
151
+ editor.tf.select({
152
+ path: [
153
+ 0,
154
+ 0,
155
+ 0,
156
+ 0,
157
+ 0
158
+ ],
159
+ offset: 2
160
+ });
161
+ editor.insertFragment([
162
+ paragraph('Before table'),
163
+ table(cells)
164
+ ]);
165
+ (0, _vitest.expect)(editor.children).toMatchObject([
166
+ table(cells),
167
+ paragraph('Keep following paragraph')
168
+ ]);
169
+ editor.insertText('!');
170
+ const lastRow = cells.length - 1;
171
+ const lastCol = cells[lastRow].length - 1;
172
+ (0, _vitest.expect)(editor.api.string([
173
+ 0,
174
+ lastRow,
175
+ lastCol
176
+ ])).toBe(`${cells[lastRow][lastCol]}!`);
177
+ });
178
+ (0, _vitest.it)('pastes a paragraph into a cell without expanding the table', ()=>{
179
+ const { editor } = (0, _testutils.createTestEditor)({});
180
+ editor.children = [
181
+ table([
182
+ [
183
+ 'Before after',
184
+ 'Keep cell'
185
+ ]
186
+ ]),
187
+ paragraph('Keep following paragraph')
188
+ ];
189
+ editor.tf.select({
190
+ path: [
191
+ 0,
192
+ 0,
193
+ 0,
194
+ 0,
195
+ 0
196
+ ],
197
+ offset: 7
198
+ });
199
+ editor.insertFragment([
200
+ paragraph('pasted ')
201
+ ]);
202
+ (0, _vitest.expect)(editor.children).toMatchObject([
203
+ table([
204
+ [
205
+ 'Before pasted after',
206
+ 'Keep cell'
207
+ ]
208
+ ]),
209
+ paragraph('Keep following paragraph')
210
+ ]);
211
+ });
212
+ });
@@ -1,6 +1,6 @@
1
1
  import * as table from '@platejs/table';
2
2
  import { onKeyDownTable as onNativeKeyDownTable } from '@platejs/table/react';
3
- import { KEYS, assignLegacyApi, assignLegacyTransforms } from 'platejs';
3
+ import { KEYS, assignLegacyApi, assignLegacyTransforms, getEditorPlugin as getSlateEditorPlugin } from 'platejs';
4
4
  import { getEditorPlugin } from 'platejs/react';
5
5
  export * from '@platejs/table';
6
6
  export const ELEMENT_TABLE = KEYS.table;
@@ -33,7 +33,20 @@ export const withDeleteTable = (editor)=>{
33
33
  return applyOverride(editor, table.withDeleteTable);
34
34
  };
35
35
  export const withGetFragmentTable = (editor, _plugin)=>applyOverride(editor, table.withGetFragmentTable);
36
- export const withInsertFragmentTable = (editor, _plugin)=>applyOverride(editor, table.withInsertFragmentTable);
36
+ export const withInsertFragmentTable = (editor, _plugin)=>{
37
+ const { insertFragment } = editor;
38
+ editor.insertFragment = (fragment)=>{
39
+ const context = getSlateEditorPlugin(editor, table.BaseTablePlugin);
40
+ table.withInsertFragmentTable({
41
+ ...context,
42
+ tf: {
43
+ ...context.tf,
44
+ insertFragment
45
+ }
46
+ }).transforms.insertFragment(fragment);
47
+ };
48
+ return editor;
49
+ };
37
50
  export const withInsertTextTable = (editor, _plugin)=>applyOverride(editor, table.withInsertTextTable);
38
51
  export const withSelectionTable = (editor)=>applyOverride(editor, table.withTableCellSelection);
39
52
  export const withSetFragmentDataTable = (editor)=>applyOverride(editor, table.withSetFragmentDataTable);
@@ -1,5 +1,6 @@
1
- import { describe, it } from 'vitest';
2
- import { assertOutput, jsx } from '../../../test-utils';
1
+ import { BLOCKS } from '@contentful/rich-text-types';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { assertOutput, createTestEditor, jsx } from '../../../test-utils';
3
4
  describe('normalization', ()=>{
4
5
  describe('Table', ()=>{
5
6
  it('removes empty table nodes', ()=>{
@@ -87,3 +88,121 @@ describe('normalization', ()=>{
87
88
  });
88
89
  });
89
90
  });
91
+ describe('paste', ()=>{
92
+ const paragraph = (value)=>({
93
+ type: BLOCKS.PARAGRAPH,
94
+ children: [
95
+ {
96
+ text: value
97
+ }
98
+ ]
99
+ });
100
+ const table = (rows)=>({
101
+ type: BLOCKS.TABLE,
102
+ children: rows.map((cells)=>({
103
+ type: BLOCKS.TABLE_ROW,
104
+ children: cells.map((value)=>({
105
+ type: BLOCKS.TABLE_CELL,
106
+ children: [
107
+ paragraph(value)
108
+ ]
109
+ }))
110
+ }))
111
+ });
112
+ it.each([
113
+ {
114
+ name: 'rows',
115
+ cells: [
116
+ [
117
+ 'A1',
118
+ 'B1'
119
+ ],
120
+ [
121
+ 'A2',
122
+ 'B2'
123
+ ]
124
+ ]
125
+ },
126
+ {
127
+ name: 'columns',
128
+ cells: [
129
+ [
130
+ 'A1',
131
+ 'B1',
132
+ 'C1'
133
+ ]
134
+ ]
135
+ }
136
+ ])('expands table $name when pasting text and a table into a cell', ({ cells })=>{
137
+ const { editor } = createTestEditor({});
138
+ editor.children = [
139
+ table([
140
+ [
141
+ 'Old A',
142
+ 'Old B'
143
+ ]
144
+ ]),
145
+ paragraph('Keep following paragraph')
146
+ ];
147
+ editor.tf.select({
148
+ path: [
149
+ 0,
150
+ 0,
151
+ 0,
152
+ 0,
153
+ 0
154
+ ],
155
+ offset: 2
156
+ });
157
+ editor.insertFragment([
158
+ paragraph('Before table'),
159
+ table(cells)
160
+ ]);
161
+ expect(editor.children).toMatchObject([
162
+ table(cells),
163
+ paragraph('Keep following paragraph')
164
+ ]);
165
+ editor.insertText('!');
166
+ const lastRow = cells.length - 1;
167
+ const lastCol = cells[lastRow].length - 1;
168
+ expect(editor.api.string([
169
+ 0,
170
+ lastRow,
171
+ lastCol
172
+ ])).toBe(`${cells[lastRow][lastCol]}!`);
173
+ });
174
+ it('pastes a paragraph into a cell without expanding the table', ()=>{
175
+ const { editor } = createTestEditor({});
176
+ editor.children = [
177
+ table([
178
+ [
179
+ 'Before after',
180
+ 'Keep cell'
181
+ ]
182
+ ]),
183
+ paragraph('Keep following paragraph')
184
+ ];
185
+ editor.tf.select({
186
+ path: [
187
+ 0,
188
+ 0,
189
+ 0,
190
+ 0,
191
+ 0
192
+ ],
193
+ offset: 7
194
+ });
195
+ editor.insertFragment([
196
+ paragraph('pasted ')
197
+ ]);
198
+ expect(editor.children).toMatchObject([
199
+ table([
200
+ [
201
+ 'Before pasted after',
202
+ 'Keep cell'
203
+ ]
204
+ ]),
205
+ paragraph('Keep following paragraph')
206
+ ]);
207
+ });
208
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-rich-text",
3
- "version": "6.6.4",
3
+ "version": "6.6.5",
4
4
  "source": "./src/index.tsx",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -96,5 +96,5 @@
96
96
  "prettier --write"
97
97
  ]
98
98
  },
99
- "gitHead": "67b16bd52fc17ed8bd578d6f405e68dc49e2e5a4"
99
+ "gitHead": "de438e4f6ab157aab21f50a558d6eeb2babec69c"
100
100
  }