@contentful/field-editor-rich-text 6.6.1-canary.9 → 6.6.1

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.
package/README.md CHANGED
@@ -10,10 +10,6 @@ This package contains a React `RichTextEditor` component that is used as the def
10
10
  import { RichTextEditor } from '@contentful/field-editor-rich-text';
11
11
  ```
12
12
 
13
- Pass `withSelectionSync={true}` to opt in to synchronizing the browser caret before editing.
14
- It defaults to `false`. The host application can pass a feature flag's value through this prop;
15
- the editor does not need its own feature flag service.
16
-
17
13
  ## ⚠️ Important: Package Configuration
18
14
 
19
15
  Plate 53.3.14 pins `slate-react` to 0.126.4. To use the latest Slate React version tested by this package (0.127.1), update existing overrides in your application as shown below. Package-manager overrides in this repository are not inherited by applications that install the published package.
@@ -84,17 +84,13 @@ function _interop_require_wildcard(obj, nodeInterop) {
84
84
  return newObj;
85
85
  }
86
86
  const ConnectedRichTextEditor = /*#__PURE__*/ _react.memo(function ConnectedRichTextEditor(props) {
87
- const { sdk, onAction, restrictedMarks, withCharValidation, withSelectionSync } = props;
88
- const selectionSync = _react.useRef(false);
89
- selectionSync.current = withSelectionSync === true;
90
- const isSelectionSyncEnabled = _react.useCallback(()=>selectionSync.current, []);
87
+ const { sdk, onAction, restrictedMarks, withCharValidation } = props;
91
88
  const id = (0, _ContentfulEditorProvider.getContentfulEditorId)(sdk);
92
- const plugins = _react.useMemo(()=>(0, _plugins.getPlugins)(sdk, onAction ?? _noop.default, restrictedMarks, withCharValidation, isSelectionSyncEnabled), [
89
+ const plugins = _react.useMemo(()=>(0, _plugins.getPlugins)(sdk, onAction ?? _noop.default, restrictedMarks, withCharValidation), [
93
90
  sdk,
94
91
  onAction,
95
92
  restrictedMarks,
96
- withCharValidation,
97
- isSelectionSyncEnabled
93
+ withCharValidation
98
94
  ]);
99
95
  const initialValue = (0, _usedeepcompare.useDeepCompareMemo)(()=>{
100
96
  return (0, _toSlateDoc.toSlateDoc)(props.value);
@@ -224,8 +224,9 @@ function wrapLink(editor, { text, url, target, type, path }) {
224
224
  (0, _transforms.wrapNodes)(editor, link, {
225
225
  split: true
226
226
  });
227
- (0, _transforms.deleteText)(editor);
228
- (0, _transforms.insertText)(editor, text);
227
+ (0, _transforms.insertText)(editor, text, {
228
+ at: editor.selection ?? undefined
229
+ });
229
230
  (0, _transforms.collapseSelection)(editor, {
230
231
  edge: 'end'
231
232
  });
@@ -61,3 +61,223 @@ const _createTestEditor = require("../../test-utils/createTestEditor");
61
61
  }
62
62
  ]);
63
63
  });
64
+ (0, _vitest.it)('preserves formatting and can undo and redo converting selected text', ()=>{
65
+ const { editor } = (0, _createTestEditor.createTestEditor)({});
66
+ const original = [
67
+ {
68
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
69
+ children: [
70
+ {
71
+ text: 'Before bold After',
72
+ bold: true
73
+ }
74
+ ]
75
+ }
76
+ ];
77
+ editor.children = original;
78
+ editor.tf.select({
79
+ anchor: {
80
+ path: [
81
+ 0,
82
+ 0
83
+ ],
84
+ offset: 7
85
+ },
86
+ focus: {
87
+ path: [
88
+ 0,
89
+ 0
90
+ ],
91
+ offset: 11
92
+ }
93
+ });
94
+ editor.tf.withoutNormalizing(()=>{
95
+ (0, _editor.wrapLink)(editor, {
96
+ type: _richtexttypes.INLINES.HYPERLINK,
97
+ url: 'https://example.com',
98
+ text: 'bold'
99
+ });
100
+ });
101
+ const linked = [
102
+ {
103
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
104
+ children: [
105
+ {
106
+ text: 'Before ',
107
+ bold: true
108
+ },
109
+ {
110
+ type: _richtexttypes.INLINES.HYPERLINK,
111
+ data: {
112
+ uri: 'https://example.com'
113
+ },
114
+ children: [
115
+ {
116
+ text: 'bold',
117
+ bold: true
118
+ }
119
+ ]
120
+ },
121
+ {
122
+ text: ' After',
123
+ bold: true
124
+ }
125
+ ]
126
+ }
127
+ ];
128
+ (0, _vitest.expect)(editor.children).toEqual(linked);
129
+ editor.tf.undo();
130
+ (0, _vitest.expect)(editor.children).toEqual(original);
131
+ editor.tf.redo();
132
+ (0, _vitest.expect)(editor.children).toEqual(linked);
133
+ });
134
+ const linkOptions = [
135
+ {
136
+ type: _richtexttypes.INLINES.HYPERLINK,
137
+ url: 'https://example.com'
138
+ },
139
+ {
140
+ type: _richtexttypes.INLINES.ENTRY_HYPERLINK,
141
+ target: {
142
+ sys: {
143
+ id: 'entry-id',
144
+ type: 'Link',
145
+ linkType: 'Entry'
146
+ }
147
+ }
148
+ },
149
+ {
150
+ type: _richtexttypes.INLINES.ASSET_HYPERLINK,
151
+ target: {
152
+ sys: {
153
+ id: 'asset-id',
154
+ type: 'Link',
155
+ linkType: 'Asset'
156
+ }
157
+ }
158
+ }
159
+ ];
160
+ _vitest.describe.each(linkOptions)('$type', (options)=>{
161
+ _vitest.it.each([
162
+ {
163
+ name: 'a forward selection',
164
+ backward: false,
165
+ text: 'My cool website'
166
+ },
167
+ {
168
+ name: 'a backward selection',
169
+ backward: true,
170
+ text: 'My cool website'
171
+ },
172
+ {
173
+ name: 'a changed link label',
174
+ backward: false,
175
+ text: 'New label'
176
+ }
177
+ ])('keeps $name linked without changing surrounding content', ({ backward, text })=>{
178
+ const { editor } = (0, _createTestEditor.createTestEditor)({});
179
+ const existingLink = {
180
+ type: _richtexttypes.INLINES.HYPERLINK,
181
+ data: {
182
+ uri: 'https://existing.example'
183
+ },
184
+ children: [
185
+ {
186
+ text: 'Existing link',
187
+ data: {}
188
+ }
189
+ ],
190
+ isVoid: false
191
+ };
192
+ editor.children = [
193
+ {
194
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
195
+ children: [
196
+ {
197
+ text: 'Before My cool website After',
198
+ data: {}
199
+ },
200
+ existingLink,
201
+ {
202
+ text: '',
203
+ data: {}
204
+ }
205
+ ],
206
+ data: {},
207
+ isVoid: false
208
+ }
209
+ ];
210
+ const start = {
211
+ path: [
212
+ 0,
213
+ 0
214
+ ],
215
+ offset: 7
216
+ };
217
+ const end = {
218
+ path: [
219
+ 0,
220
+ 0
221
+ ],
222
+ offset: 22
223
+ };
224
+ editor.tf.select(backward ? {
225
+ anchor: end,
226
+ focus: start
227
+ } : {
228
+ anchor: start,
229
+ focus: end
230
+ });
231
+ editor.tf.withoutNormalizing(()=>(0, _editor.wrapLink)(editor, {
232
+ ...options,
233
+ text
234
+ }));
235
+ (0, _vitest.expect)(editor.children[0].children).toEqual([
236
+ {
237
+ text: 'Before ',
238
+ data: {}
239
+ },
240
+ {
241
+ type: options.type,
242
+ data: options.url ? {
243
+ uri: options.url
244
+ } : {
245
+ target: options.target
246
+ },
247
+ children: [
248
+ {
249
+ text,
250
+ data: {}
251
+ }
252
+ ]
253
+ },
254
+ {
255
+ text: ' After',
256
+ data: {}
257
+ },
258
+ existingLink,
259
+ {
260
+ text: '',
261
+ data: {}
262
+ }
263
+ ]);
264
+ (0, _vitest.expect)(editor.selection).toEqual({
265
+ anchor: {
266
+ path: [
267
+ 0,
268
+ 1,
269
+ 0
270
+ ],
271
+ offset: text.length
272
+ },
273
+ focus: {
274
+ path: [
275
+ 0,
276
+ 1,
277
+ 0
278
+ ],
279
+ offset: text.length
280
+ }
281
+ });
282
+ });
283
+ });
@@ -12,7 +12,6 @@ const _Break = require("./Break");
12
12
  const _CharCounter = require("./CharCounter");
13
13
  const _CommandPalette = require("./CommandPalette");
14
14
  const _useCommands = require("./CommandPalette/useCommands");
15
- const _createSelectionSyncPlugin = require("./createSelectionSyncPlugin");
16
15
  const _DeserializeDocx = require("./DeserializeDocx");
17
16
  const _DragAndDrop = require("./DragAndDrop");
18
17
  const _EmbeddedEntityBlock = require("./EmbeddedEntityBlock");
@@ -34,10 +33,9 @@ const _Text = require("./Text");
34
33
  const _Tracking = require("./Tracking");
35
34
  const _TrailingParagraph = require("./TrailingParagraph");
36
35
  const _Voids = require("./Voids");
37
- const getPlugins = (sdk, onAction, restrictedMarks, withCharValidation, isSelectionSyncEnabled = ()=>false)=>[
36
+ const getPlugins = (sdk, onAction, restrictedMarks, withCharValidation)=>[
38
37
  (0, _DeserializeDocx.createDeserializeDocxPlugin)(),
39
38
  (0, _Tracking.createTrackingPlugin)(onAction),
40
- (0, _createSelectionSyncPlugin.createSelectionSyncPlugin)(isSelectionSyncEnabled),
41
39
  (0, _DragAndDrop.createDragAndDropPlugin)(),
42
40
  ...Object.values((0, _useCommands.isCommandPromptPluginEnabled)(sdk)).some(Boolean) ? [
43
41
  (0, _CommandPalette.createCommandPalettePlugin)()
@@ -20,17 +20,13 @@ import { SyncEditorChanges } from './SyncEditorChanges';
20
20
  import Toolbar from './Toolbar';
21
21
  import StickyToolbarWrapper from './Toolbar/components/StickyToolbarWrapper';
22
22
  export const ConnectedRichTextEditor = /*#__PURE__*/ React.memo(function ConnectedRichTextEditor(props) {
23
- const { sdk, onAction, restrictedMarks, withCharValidation, withSelectionSync } = props;
24
- const selectionSync = React.useRef(false);
25
- selectionSync.current = withSelectionSync === true;
26
- const isSelectionSyncEnabled = React.useCallback(()=>selectionSync.current, []);
23
+ const { sdk, onAction, restrictedMarks, withCharValidation } = props;
27
24
  const id = getContentfulEditorId(sdk);
28
- const plugins = React.useMemo(()=>getPlugins(sdk, onAction ?? noop, restrictedMarks, withCharValidation, isSelectionSyncEnabled), [
25
+ const plugins = React.useMemo(()=>getPlugins(sdk, onAction ?? noop, restrictedMarks, withCharValidation), [
29
26
  sdk,
30
27
  onAction,
31
28
  restrictedMarks,
32
- withCharValidation,
33
- isSelectionSyncEnabled
29
+ withCharValidation
34
30
  ]);
35
31
  const initialValue = useDeepCompareMemo(()=>{
36
32
  return toSlateDoc(props.value);
@@ -1,7 +1,7 @@
1
1
  import { BLOCKS, INLINES } from '@contentful/rich-text-types';
2
2
  import { focusEditor } from '../internal/misc';
3
3
  import { getText, getNodeEntries, getNodeEntry, getNodeDescendants, isText, isRangeCollapsed as isSelectionCollapsed, isEditor, isElement, getPathLevels, isCommonPath } from '../internal/queries';
4
- import { setNodes, select, insertNodes, toggleNodeType, moveSelection, unwrapNodes, insertText, wrapNodes, deleteText, collapseSelection } from '../internal/transforms';
4
+ import { setNodes, select, insertNodes, toggleNodeType, moveSelection, unwrapNodes, insertText, wrapNodes, collapseSelection } from '../internal/transforms';
5
5
  import { IS_SAFARI } from './environment';
6
6
  export const LINK_TYPES = [
7
7
  INLINES.HYPERLINK,
@@ -146,8 +146,9 @@ export function wrapLink(editor, { text, url, target, type, path }) {
146
146
  wrapNodes(editor, link, {
147
147
  split: true
148
148
  });
149
- deleteText(editor);
150
- insertText(editor, text);
149
+ insertText(editor, text, {
150
+ at: editor.selection ?? undefined
151
+ });
151
152
  collapseSelection(editor, {
152
153
  edge: 'end'
153
154
  });
@@ -1,5 +1,5 @@
1
1
  import { BLOCKS, INLINES } from '@contentful/rich-text-types';
2
- import { expect, it } from 'vitest';
2
+ import { describe, expect, it } from 'vitest';
3
3
  import { wrapLink } from '../../helpers/editor';
4
4
  import { createTestEditor } from '../../test-utils/createTestEditor';
5
5
  it('converts selected text into a hyperlink', ()=>{
@@ -57,3 +57,223 @@ it('converts selected text into a hyperlink', ()=>{
57
57
  }
58
58
  ]);
59
59
  });
60
+ it('preserves formatting and can undo and redo converting selected text', ()=>{
61
+ const { editor } = createTestEditor({});
62
+ const original = [
63
+ {
64
+ type: BLOCKS.PARAGRAPH,
65
+ children: [
66
+ {
67
+ text: 'Before bold After',
68
+ bold: true
69
+ }
70
+ ]
71
+ }
72
+ ];
73
+ editor.children = original;
74
+ editor.tf.select({
75
+ anchor: {
76
+ path: [
77
+ 0,
78
+ 0
79
+ ],
80
+ offset: 7
81
+ },
82
+ focus: {
83
+ path: [
84
+ 0,
85
+ 0
86
+ ],
87
+ offset: 11
88
+ }
89
+ });
90
+ editor.tf.withoutNormalizing(()=>{
91
+ wrapLink(editor, {
92
+ type: INLINES.HYPERLINK,
93
+ url: 'https://example.com',
94
+ text: 'bold'
95
+ });
96
+ });
97
+ const linked = [
98
+ {
99
+ type: BLOCKS.PARAGRAPH,
100
+ children: [
101
+ {
102
+ text: 'Before ',
103
+ bold: true
104
+ },
105
+ {
106
+ type: INLINES.HYPERLINK,
107
+ data: {
108
+ uri: 'https://example.com'
109
+ },
110
+ children: [
111
+ {
112
+ text: 'bold',
113
+ bold: true
114
+ }
115
+ ]
116
+ },
117
+ {
118
+ text: ' After',
119
+ bold: true
120
+ }
121
+ ]
122
+ }
123
+ ];
124
+ expect(editor.children).toEqual(linked);
125
+ editor.tf.undo();
126
+ expect(editor.children).toEqual(original);
127
+ editor.tf.redo();
128
+ expect(editor.children).toEqual(linked);
129
+ });
130
+ const linkOptions = [
131
+ {
132
+ type: INLINES.HYPERLINK,
133
+ url: 'https://example.com'
134
+ },
135
+ {
136
+ type: INLINES.ENTRY_HYPERLINK,
137
+ target: {
138
+ sys: {
139
+ id: 'entry-id',
140
+ type: 'Link',
141
+ linkType: 'Entry'
142
+ }
143
+ }
144
+ },
145
+ {
146
+ type: INLINES.ASSET_HYPERLINK,
147
+ target: {
148
+ sys: {
149
+ id: 'asset-id',
150
+ type: 'Link',
151
+ linkType: 'Asset'
152
+ }
153
+ }
154
+ }
155
+ ];
156
+ describe.each(linkOptions)('$type', (options)=>{
157
+ it.each([
158
+ {
159
+ name: 'a forward selection',
160
+ backward: false,
161
+ text: 'My cool website'
162
+ },
163
+ {
164
+ name: 'a backward selection',
165
+ backward: true,
166
+ text: 'My cool website'
167
+ },
168
+ {
169
+ name: 'a changed link label',
170
+ backward: false,
171
+ text: 'New label'
172
+ }
173
+ ])('keeps $name linked without changing surrounding content', ({ backward, text })=>{
174
+ const { editor } = createTestEditor({});
175
+ const existingLink = {
176
+ type: INLINES.HYPERLINK,
177
+ data: {
178
+ uri: 'https://existing.example'
179
+ },
180
+ children: [
181
+ {
182
+ text: 'Existing link',
183
+ data: {}
184
+ }
185
+ ],
186
+ isVoid: false
187
+ };
188
+ editor.children = [
189
+ {
190
+ type: BLOCKS.PARAGRAPH,
191
+ children: [
192
+ {
193
+ text: 'Before My cool website After',
194
+ data: {}
195
+ },
196
+ existingLink,
197
+ {
198
+ text: '',
199
+ data: {}
200
+ }
201
+ ],
202
+ data: {},
203
+ isVoid: false
204
+ }
205
+ ];
206
+ const start = {
207
+ path: [
208
+ 0,
209
+ 0
210
+ ],
211
+ offset: 7
212
+ };
213
+ const end = {
214
+ path: [
215
+ 0,
216
+ 0
217
+ ],
218
+ offset: 22
219
+ };
220
+ editor.tf.select(backward ? {
221
+ anchor: end,
222
+ focus: start
223
+ } : {
224
+ anchor: start,
225
+ focus: end
226
+ });
227
+ editor.tf.withoutNormalizing(()=>wrapLink(editor, {
228
+ ...options,
229
+ text
230
+ }));
231
+ expect(editor.children[0].children).toEqual([
232
+ {
233
+ text: 'Before ',
234
+ data: {}
235
+ },
236
+ {
237
+ type: options.type,
238
+ data: options.url ? {
239
+ uri: options.url
240
+ } : {
241
+ target: options.target
242
+ },
243
+ children: [
244
+ {
245
+ text,
246
+ data: {}
247
+ }
248
+ ]
249
+ },
250
+ {
251
+ text: ' After',
252
+ data: {}
253
+ },
254
+ existingLink,
255
+ {
256
+ text: '',
257
+ data: {}
258
+ }
259
+ ]);
260
+ expect(editor.selection).toEqual({
261
+ anchor: {
262
+ path: [
263
+ 0,
264
+ 1,
265
+ 0
266
+ ],
267
+ offset: text.length
268
+ },
269
+ focus: {
270
+ path: [
271
+ 0,
272
+ 1,
273
+ 0
274
+ ],
275
+ offset: text.length
276
+ }
277
+ });
278
+ });
279
+ });
@@ -2,7 +2,6 @@ import { createSoftBreakPlugin, createExitBreakPlugin, createResetNodePlugin } f
2
2
  import { createCharCounterPlugin } from './CharCounter';
3
3
  import { createCommandPalettePlugin } from './CommandPalette';
4
4
  import { isCommandPromptPluginEnabled } from './CommandPalette/useCommands';
5
- import { createSelectionSyncPlugin } from './createSelectionSyncPlugin';
6
5
  import { createDeserializeDocxPlugin } from './DeserializeDocx';
7
6
  import { createDragAndDropPlugin } from './DragAndDrop';
8
7
  import { createEmbeddedAssetBlockPlugin, createEmbeddedEntryBlockPlugin } from './EmbeddedEntityBlock';
@@ -24,10 +23,9 @@ import { createTextPlugin } from './Text';
24
23
  import { createTrackingPlugin } from './Tracking';
25
24
  import { createTrailingParagraphPlugin } from './TrailingParagraph';
26
25
  import { createVoidsPlugin } from './Voids';
27
- export const getPlugins = (sdk, onAction, restrictedMarks, withCharValidation, isSelectionSyncEnabled = ()=>false)=>[
26
+ export const getPlugins = (sdk, onAction, restrictedMarks, withCharValidation)=>[
28
27
  createDeserializeDocxPlugin(),
29
28
  createTrackingPlugin(onAction),
30
- createSelectionSyncPlugin(isSelectionSyncEnabled),
31
29
  createDragAndDropPlugin(),
32
30
  ...Object.values(isCommandPromptPluginEnabled(sdk)).some(Boolean) ? [
33
31
  createCommandPalettePlugin()
@@ -19,8 +19,6 @@ type RichTextProps = {
19
19
  */
20
20
  onChange?: (doc: Contentful.Document) => unknown;
21
21
  withCharValidation?: boolean;
22
- /** Opt in to synchronizing Slate's selection with the browser caret. Defaults to false. */
23
- withSelectionSync?: boolean;
24
22
  queryClient?: QueryClient;
25
23
  };
26
24
  type ConnectedRichTextProps = {
@@ -36,7 +34,6 @@ type ConnectedRichTextProps = {
36
34
  actionsDisabled?: boolean;
37
35
  stickyToolbarOffset?: number;
38
36
  withCharValidation?: boolean;
39
- withSelectionSync?: boolean;
40
37
  queryClient?: QueryClient;
41
38
  };
42
39
  export declare const ConnectedRichTextEditor: React.NamedExoticComponent<ConnectedRichTextProps>;
@@ -1,4 +1,4 @@
1
1
  import { FieldAppSDK } from '@contentful/app-sdk';
2
2
  import { PlatePlugin } from '../internal/types';
3
3
  import { RichTextTrackingActionHandler } from './Tracking';
4
- export declare const getPlugins: (sdk: FieldAppSDK, onAction: RichTextTrackingActionHandler, restrictedMarks?: string[], withCharValidation?: boolean, isSelectionSyncEnabled?: () => boolean) => PlatePlugin[];
4
+ export declare const getPlugins: (sdk: FieldAppSDK, onAction: RichTextTrackingActionHandler, restrictedMarks?: string[], withCharValidation?: boolean) => PlatePlugin[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-rich-text",
3
- "version": "6.6.1-canary.9+37493b22",
3
+ "version": "6.6.1",
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": "37493b22df332d9464fad83715ff6aabdec23cf8"
99
+ "gitHead": "de96ffd473521caf9b0a67c0680e3fdd5d44b808"
100
100
  }
@@ -1,89 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "createSelectionSyncPlugin", {
6
- enumerable: true,
7
- get: function() {
8
- return createSelectionSyncPlugin;
9
- }
10
- });
11
- const _ishotkey = /*#__PURE__*/ _interop_require_default(require("is-hotkey"));
12
- const _plate = require("../internal/plate");
13
- const _pluginAdapter = require("../internal/pluginAdapter");
14
- function _interop_require_default(obj) {
15
- return obj && obj.__esModule ? obj : {
16
- default: obj
17
- };
18
- }
19
- const caretKeys = [
20
- 'ArrowLeft',
21
- 'ArrowRight',
22
- 'Backspace',
23
- 'Delete',
24
- 'Enter'
25
- ];
26
- const hasMatchingCaret = (editor, selection, anchorNode)=>{
27
- const caret = editor.selection;
28
- if (selection.rangeCount !== 1 || !selection.isCollapsed || !caret || !(0, _plate.isCollapsed)(caret) || anchorNode?.nodeType !== Node.TEXT_NODE || !anchorNode.parentElement?.hasAttribute('data-slate-string') || selection.anchorOffset !== caret.anchor.offset) return false;
29
- const node = editor.api.node(caret.anchor.path)?.[0];
30
- return node && 'text' in node && node.text === anchorNode.textContent && node === editor.api.toSlateNode(anchorNode);
31
- };
32
- const isEditorShortcut = (editor, event)=>{
33
- if (!event.metaKey && !event.ctrlKey) return false;
34
- return (0, _pluginAdapter.getContentfulPlugins)(editor).some(({ handlers, options })=>{
35
- const { hotkey } = options ?? {};
36
- return handlers?.onKeyDown && hotkey && (0, _ishotkey.default)(hotkey, event);
37
- });
38
- };
39
- const syncSelection = (editor)=>{
40
- const selection = editor.api.getWindow()?.getSelection();
41
- if (!selection) return;
42
- const { anchorNode, focusNode } = selection;
43
- if (!editor.api.hasTarget(anchorNode) || focusNode !== anchorNode && !editor.api.hasTarget(focusNode)) {
44
- return;
45
- }
46
- if (hasMatchingCaret(editor, selection, anchorNode)) return;
47
- const range = editor.api.toSlateRange(selection, {
48
- exactMatch: false,
49
- suppressThrow: true
50
- });
51
- if (!range || editor.api.void({
52
- at: range.anchor
53
- }) || editor.api.void({
54
- at: range.focus
55
- })) return;
56
- editor.tf.select(range);
57
- };
58
- const createSelectionSyncPlugin = (isEnabled)=>({
59
- key: 'selectionSync',
60
- handlers: {
61
- onMouseUp: (editor)=>()=>{
62
- if (!isEnabled()) return;
63
- if (!editor.api.isComposing()) syncSelection(editor);
64
- },
65
- onKeyDown: (editor)=>(event)=>{
66
- if (!isEnabled()) return;
67
- if (!caretKeys.includes(event.key) && !isEditorShortcut(editor, event)) return;
68
- if (!editor.api.isFocused() && editor.selection && editor.api.void({
69
- at: editor.selection.focus
70
- })) return;
71
- if (event.nativeEvent.isComposing || editor.api.isComposing()) return;
72
- syncSelection(editor);
73
- },
74
- onCompositionStart: (editor)=>()=>{
75
- if (!isEnabled()) return;
76
- if (!editor.api.isComposing()) syncSelection(editor);
77
- },
78
- onDOMBeforeInput: (editor)=>(event)=>{
79
- if (!isEnabled()) return;
80
- const input = 'nativeEvent' in event ? event.nativeEvent : event;
81
- const changesContent = [
82
- 'insertText',
83
- 'insertParagraph',
84
- 'insertLineBreak'
85
- ].includes(input.inputType) || input.inputType.startsWith('delete');
86
- if (changesContent && !input.isComposing && !editor.api.isComposing()) syncSelection(editor);
87
- }
88
- }
89
- });
@@ -1,74 +0,0 @@
1
- import isHotkey from 'is-hotkey';
2
- import { isCollapsed } from '../internal/plate';
3
- import { getContentfulPlugins } from '../internal/pluginAdapter';
4
- const caretKeys = [
5
- 'ArrowLeft',
6
- 'ArrowRight',
7
- 'Backspace',
8
- 'Delete',
9
- 'Enter'
10
- ];
11
- const hasMatchingCaret = (editor, selection, anchorNode)=>{
12
- const caret = editor.selection;
13
- if (selection.rangeCount !== 1 || !selection.isCollapsed || !caret || !isCollapsed(caret) || anchorNode?.nodeType !== Node.TEXT_NODE || !anchorNode.parentElement?.hasAttribute('data-slate-string') || selection.anchorOffset !== caret.anchor.offset) return false;
14
- const node = editor.api.node(caret.anchor.path)?.[0];
15
- return node && 'text' in node && node.text === anchorNode.textContent && node === editor.api.toSlateNode(anchorNode);
16
- };
17
- const isEditorShortcut = (editor, event)=>{
18
- if (!event.metaKey && !event.ctrlKey) return false;
19
- return getContentfulPlugins(editor).some(({ handlers, options })=>{
20
- const { hotkey } = options ?? {};
21
- return handlers?.onKeyDown && hotkey && isHotkey(hotkey, event);
22
- });
23
- };
24
- const syncSelection = (editor)=>{
25
- const selection = editor.api.getWindow()?.getSelection();
26
- if (!selection) return;
27
- const { anchorNode, focusNode } = selection;
28
- if (!editor.api.hasTarget(anchorNode) || focusNode !== anchorNode && !editor.api.hasTarget(focusNode)) {
29
- return;
30
- }
31
- if (hasMatchingCaret(editor, selection, anchorNode)) return;
32
- const range = editor.api.toSlateRange(selection, {
33
- exactMatch: false,
34
- suppressThrow: true
35
- });
36
- if (!range || editor.api.void({
37
- at: range.anchor
38
- }) || editor.api.void({
39
- at: range.focus
40
- })) return;
41
- editor.tf.select(range);
42
- };
43
- export const createSelectionSyncPlugin = (isEnabled)=>({
44
- key: 'selectionSync',
45
- handlers: {
46
- onMouseUp: (editor)=>()=>{
47
- if (!isEnabled()) return;
48
- if (!editor.api.isComposing()) syncSelection(editor);
49
- },
50
- onKeyDown: (editor)=>(event)=>{
51
- if (!isEnabled()) return;
52
- if (!caretKeys.includes(event.key) && !isEditorShortcut(editor, event)) return;
53
- if (!editor.api.isFocused() && editor.selection && editor.api.void({
54
- at: editor.selection.focus
55
- })) return;
56
- if (event.nativeEvent.isComposing || editor.api.isComposing()) return;
57
- syncSelection(editor);
58
- },
59
- onCompositionStart: (editor)=>()=>{
60
- if (!isEnabled()) return;
61
- if (!editor.api.isComposing()) syncSelection(editor);
62
- },
63
- onDOMBeforeInput: (editor)=>(event)=>{
64
- if (!isEnabled()) return;
65
- const input = 'nativeEvent' in event ? event.nativeEvent : event;
66
- const changesContent = [
67
- 'insertText',
68
- 'insertParagraph',
69
- 'insertLineBreak'
70
- ].includes(input.inputType) || input.inputType.startsWith('delete');
71
- if (changesContent && !input.isComposing && !editor.api.isComposing()) syncSelection(editor);
72
- }
73
- }
74
- });
@@ -1,2 +0,0 @@
1
- import { PlatePlugin } from '../internal/types';
2
- export declare const createSelectionSyncPlugin: (isEnabled: () => boolean) => PlatePlugin;