@contentful/field-editor-rich-text 6.4.4 → 6.4.5-canary.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.
@@ -0,0 +1,110 @@
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 _platecommon = require("@udecode/plate-common");
12
+ const _slate = require("slate");
13
+ const _transforms = require("../internal/transforms");
14
+ const syncSelection = (editor)=>{
15
+ const selection = (0, _platecommon.getEditorWindow)(editor)?.getSelection();
16
+ if ((0, _platecommon.isEditorReadOnly)(editor) || (0, _platecommon.isComposing)(editor) || !selection || selection.rangeCount === 0 || !(0, _platecommon.hasEditorTarget)(editor, selection.anchorNode) || !(0, _platecommon.hasEditorTarget)(editor, selection.focusNode)) {
17
+ return;
18
+ }
19
+ if (selection.isCollapsed && editor.selection && (0, _platecommon.isCollapsed)(editor.selection) && selection.anchorNode?.nodeType === Node.TEXT_NODE && selection.anchorNode.parentElement?.hasAttribute('data-slate-string') && selection.anchorOffset === editor.selection.anchor.offset) {
20
+ const node = (0, _platecommon.getNode)(editor, editor.selection.anchor.path);
21
+ if (node && 'text' in node && node.text === selection.anchorNode.textContent && node === (0, _platecommon.toSlateNode)(editor, selection.anchorNode)) {
22
+ return editor.selection;
23
+ }
24
+ }
25
+ const range = (0, _platecommon.toSlateRange)(editor, selection, {
26
+ exactMatch: false,
27
+ suppressThrow: true
28
+ });
29
+ if (!range || (0, _platecommon.getVoidNode)(editor, {
30
+ at: range.anchor
31
+ }) || (0, _platecommon.getVoidNode)(editor, {
32
+ at: range.focus
33
+ })) {
34
+ return;
35
+ }
36
+ if (!editor.selection || !_slate.Range.equals(range, editor.selection)) {
37
+ (0, _transforms.select)(editor, range);
38
+ }
39
+ return range;
40
+ };
41
+ const createSelectionSyncPlugin = ()=>({
42
+ key: 'selectionSync',
43
+ handlers: {
44
+ onMouseUp: (editor)=>(event)=>{
45
+ if (event.button === 0 && (0, _platecommon.hasEditorEditableTarget)(editor, event.target)) {
46
+ syncSelection(editor);
47
+ }
48
+ },
49
+ onKeyDown: (editor)=>(event)=>{
50
+ if ((0, _platecommon.isEditorReadOnly)(editor) || event.nativeEvent.isComposing || (0, _platecommon.isComposing)(editor)) {
51
+ return;
52
+ }
53
+ if (!(0, _platecommon.isEditorFocused)(editor) && editor.selection && (0, _platecommon.getVoidNode)(editor, {
54
+ at: editor.selection.focus
55
+ })) {
56
+ return;
57
+ }
58
+ if ([
59
+ 'ArrowLeft',
60
+ 'ArrowRight',
61
+ 'Backspace',
62
+ 'Delete',
63
+ 'Enter'
64
+ ].includes(event.key)) {
65
+ syncSelection(editor);
66
+ return;
67
+ }
68
+ if (!(0, _platecommon.hasEditorEditableTarget)(editor, event.target) || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || event.key !== 'ArrowUp' && event.key !== 'ArrowDown') {
69
+ return;
70
+ }
71
+ const nativeSelection = (0, _platecommon.getEditorWindow)(editor)?.getSelection();
72
+ if (!nativeSelection?.isCollapsed || typeof nativeSelection.modify !== 'function') {
73
+ return;
74
+ }
75
+ const before = (0, _platecommon.toSlateRange)(editor, nativeSelection, {
76
+ exactMatch: true,
77
+ suppressThrow: true
78
+ });
79
+ if (!before || (0, _platecommon.getVoidNode)(editor, {
80
+ at: before.anchor
81
+ })) {
82
+ return;
83
+ }
84
+ nativeSelection.modify('move', event.key === 'ArrowUp' ? 'backward' : 'forward', 'line');
85
+ if (!syncSelection(editor)) {
86
+ const previous = (0, _platecommon.toDOMRange)(editor, before);
87
+ if (previous) {
88
+ nativeSelection.setBaseAndExtent(previous.startContainer, previous.startOffset, previous.endContainer, previous.endOffset);
89
+ }
90
+ return;
91
+ }
92
+ event.preventDefault();
93
+ return true;
94
+ },
95
+ onCompositionStart: (editor)=>()=>{
96
+ syncSelection(editor);
97
+ },
98
+ onDOMBeforeInput: (editor)=>(event)=>{
99
+ const input = 'nativeEvent' in event ? event.nativeEvent : event;
100
+ if (input.isComposing || ![
101
+ 'insertText',
102
+ 'insertParagraph',
103
+ 'insertLineBreak'
104
+ ].includes(input.inputType) && !input.inputType.startsWith('delete')) {
105
+ return;
106
+ }
107
+ syncSelection(editor);
108
+ }
109
+ }
110
+ });
@@ -20,6 +20,7 @@ const _Break = require("./Break");
20
20
  const _CharCounter = require("./CharCounter");
21
21
  const _CommandPalette = require("./CommandPalette");
22
22
  const _useCommands = require("./CommandPalette/useCommands");
23
+ const _createSelectionSyncPlugin = require("./createSelectionSyncPlugin");
23
24
  const _DeserializeDocx = require("./DeserializeDocx");
24
25
  const _DragAndDrop = require("./DragAndDrop");
25
26
  const _EmbeddedEntityBlock = require("./EmbeddedEntityBlock");
@@ -44,6 +45,7 @@ const _Voids = require("./Voids");
44
45
  const getPlugins = (sdk, onAction, restrictedMarks, withCharValidation)=>[
45
46
  (0, _DeserializeDocx.createDeserializeDocxPlugin)(),
46
47
  (0, _Tracking.createTrackingPlugin)(onAction),
48
+ (0, _createSelectionSyncPlugin.createSelectionSyncPlugin)(),
47
49
  (0, _DragAndDrop.createDragAndDropPlugin)(),
48
50
  ...Object.values((0, _useCommands.isCommandPromptPluginEnabled)(sdk)).some(Boolean) ? [
49
51
  (0, _CommandPalette.createCommandPalettePlugin)()
@@ -0,0 +1,100 @@
1
+ import { getEditorWindow, getNode, getVoidNode, hasEditorEditableTarget, hasEditorTarget, isCollapsed, isComposing, isEditorFocused, isEditorReadOnly, toDOMRange, toSlateNode, toSlateRange } from '@udecode/plate-common';
2
+ import { Range } from 'slate';
3
+ import { select } from '../internal/transforms';
4
+ const syncSelection = (editor)=>{
5
+ const selection = getEditorWindow(editor)?.getSelection();
6
+ if (isEditorReadOnly(editor) || isComposing(editor) || !selection || selection.rangeCount === 0 || !hasEditorTarget(editor, selection.anchorNode) || !hasEditorTarget(editor, selection.focusNode)) {
7
+ return;
8
+ }
9
+ if (selection.isCollapsed && editor.selection && isCollapsed(editor.selection) && selection.anchorNode?.nodeType === Node.TEXT_NODE && selection.anchorNode.parentElement?.hasAttribute('data-slate-string') && selection.anchorOffset === editor.selection.anchor.offset) {
10
+ const node = getNode(editor, editor.selection.anchor.path);
11
+ if (node && 'text' in node && node.text === selection.anchorNode.textContent && node === toSlateNode(editor, selection.anchorNode)) {
12
+ return editor.selection;
13
+ }
14
+ }
15
+ const range = toSlateRange(editor, selection, {
16
+ exactMatch: false,
17
+ suppressThrow: true
18
+ });
19
+ if (!range || getVoidNode(editor, {
20
+ at: range.anchor
21
+ }) || getVoidNode(editor, {
22
+ at: range.focus
23
+ })) {
24
+ return;
25
+ }
26
+ if (!editor.selection || !Range.equals(range, editor.selection)) {
27
+ select(editor, range);
28
+ }
29
+ return range;
30
+ };
31
+ export const createSelectionSyncPlugin = ()=>({
32
+ key: 'selectionSync',
33
+ handlers: {
34
+ onMouseUp: (editor)=>(event)=>{
35
+ if (event.button === 0 && hasEditorEditableTarget(editor, event.target)) {
36
+ syncSelection(editor);
37
+ }
38
+ },
39
+ onKeyDown: (editor)=>(event)=>{
40
+ if (isEditorReadOnly(editor) || event.nativeEvent.isComposing || isComposing(editor)) {
41
+ return;
42
+ }
43
+ if (!isEditorFocused(editor) && editor.selection && getVoidNode(editor, {
44
+ at: editor.selection.focus
45
+ })) {
46
+ return;
47
+ }
48
+ if ([
49
+ 'ArrowLeft',
50
+ 'ArrowRight',
51
+ 'Backspace',
52
+ 'Delete',
53
+ 'Enter'
54
+ ].includes(event.key)) {
55
+ syncSelection(editor);
56
+ return;
57
+ }
58
+ if (!hasEditorEditableTarget(editor, event.target) || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || event.key !== 'ArrowUp' && event.key !== 'ArrowDown') {
59
+ return;
60
+ }
61
+ const nativeSelection = getEditorWindow(editor)?.getSelection();
62
+ if (!nativeSelection?.isCollapsed || typeof nativeSelection.modify !== 'function') {
63
+ return;
64
+ }
65
+ const before = toSlateRange(editor, nativeSelection, {
66
+ exactMatch: true,
67
+ suppressThrow: true
68
+ });
69
+ if (!before || getVoidNode(editor, {
70
+ at: before.anchor
71
+ })) {
72
+ return;
73
+ }
74
+ nativeSelection.modify('move', event.key === 'ArrowUp' ? 'backward' : 'forward', 'line');
75
+ if (!syncSelection(editor)) {
76
+ const previous = toDOMRange(editor, before);
77
+ if (previous) {
78
+ nativeSelection.setBaseAndExtent(previous.startContainer, previous.startOffset, previous.endContainer, previous.endOffset);
79
+ }
80
+ return;
81
+ }
82
+ event.preventDefault();
83
+ return true;
84
+ },
85
+ onCompositionStart: (editor)=>()=>{
86
+ syncSelection(editor);
87
+ },
88
+ onDOMBeforeInput: (editor)=>(event)=>{
89
+ const input = 'nativeEvent' in event ? event.nativeEvent : event;
90
+ if (input.isComposing || ![
91
+ 'insertText',
92
+ 'insertParagraph',
93
+ 'insertLineBreak'
94
+ ].includes(input.inputType) && !input.inputType.startsWith('delete')) {
95
+ return;
96
+ }
97
+ syncSelection(editor);
98
+ }
99
+ }
100
+ });
@@ -2,6 +2,7 @@ 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';
5
6
  import { createDeserializeDocxPlugin } from './DeserializeDocx';
6
7
  import { createDragAndDropPlugin } from './DragAndDrop';
7
8
  import { createEmbeddedAssetBlockPlugin, createEmbeddedEntryBlockPlugin } from './EmbeddedEntityBlock';
@@ -26,6 +27,7 @@ import { createVoidsPlugin } from './Voids';
26
27
  export const getPlugins = (sdk, onAction, restrictedMarks, withCharValidation)=>[
27
28
  createDeserializeDocxPlugin(),
28
29
  createTrackingPlugin(onAction),
30
+ createSelectionSyncPlugin(),
29
31
  createDragAndDropPlugin(),
30
32
  ...Object.values(isCommandPromptPluginEnabled(sdk)).some(Boolean) ? [
31
33
  createCommandPalettePlugin()
@@ -0,0 +1,2 @@
1
+ import { PlatePlugin } from '../internal/types';
2
+ export declare const createSelectionSyncPlugin: () => PlatePlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-rich-text",
3
- "version": "6.4.4",
3
+ "version": "6.4.5-canary.1+c60db64c",
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": "515df2cbdfa9ce00c1d1843c757d50bef08f5446"
97
+ "gitHead": "c60db64c954542134e5ceff62981288fedae1d13"
98
98
  }