@atlaskit/editor-plugin-selection 0.1.0

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.
Files changed (97) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/LICENSE.md +13 -0
  3. package/README.md +30 -0
  4. package/dist/cjs/actions.js +11 -0
  5. package/dist/cjs/commands.js +257 -0
  6. package/dist/cjs/gap-cursor/actions.js +255 -0
  7. package/dist/cjs/gap-cursor/direction.js +23 -0
  8. package/dist/cjs/gap-cursor/selection.js +30 -0
  9. package/dist/cjs/gap-cursor/utils/is-ignored.js +12 -0
  10. package/dist/cjs/gap-cursor/utils/is-valid-target-node.js +12 -0
  11. package/dist/cjs/gap-cursor/utils/place-gap-cursor.js +103 -0
  12. package/dist/cjs/gap-cursor/utils.js +137 -0
  13. package/dist/cjs/gap-cursor-selection.js +37 -0
  14. package/dist/cjs/index.js +12 -0
  15. package/dist/cjs/plugin-factory.js +49 -0
  16. package/dist/cjs/plugin.js +75 -0
  17. package/dist/cjs/pm-plugins/events/create-selection-between.js +92 -0
  18. package/dist/cjs/pm-plugins/events/keydown.js +115 -0
  19. package/dist/cjs/pm-plugins/gap-cursor-keymap.js +46 -0
  20. package/dist/cjs/pm-plugins/gap-cursor-main.js +159 -0
  21. package/dist/cjs/pm-plugins/gap-cursor-plugin-key.js +8 -0
  22. package/dist/cjs/pm-plugins/keymap.js +16 -0
  23. package/dist/cjs/pm-plugins/selection-main.js +104 -0
  24. package/dist/cjs/reducer.js +26 -0
  25. package/dist/cjs/types.js +20 -0
  26. package/dist/cjs/utils.js +280 -0
  27. package/dist/es2019/actions.js +5 -0
  28. package/dist/es2019/commands.js +250 -0
  29. package/dist/es2019/gap-cursor/actions.js +256 -0
  30. package/dist/es2019/gap-cursor/direction.js +15 -0
  31. package/dist/es2019/gap-cursor/selection.js +1 -0
  32. package/dist/es2019/gap-cursor/utils/is-ignored.js +1 -0
  33. package/dist/es2019/gap-cursor/utils/is-valid-target-node.js +1 -0
  34. package/dist/es2019/gap-cursor/utils/place-gap-cursor.js +94 -0
  35. package/dist/es2019/gap-cursor/utils.js +124 -0
  36. package/dist/es2019/gap-cursor-selection.js +2 -0
  37. package/dist/es2019/index.js +1 -0
  38. package/dist/es2019/plugin-factory.js +43 -0
  39. package/dist/es2019/plugin.js +60 -0
  40. package/dist/es2019/pm-plugins/events/create-selection-between.js +89 -0
  41. package/dist/es2019/pm-plugins/events/keydown.js +111 -0
  42. package/dist/es2019/pm-plugins/gap-cursor-keymap.js +40 -0
  43. package/dist/es2019/pm-plugins/gap-cursor-main.js +157 -0
  44. package/dist/es2019/pm-plugins/gap-cursor-plugin-key.js +2 -0
  45. package/dist/es2019/pm-plugins/keymap.js +10 -0
  46. package/dist/es2019/pm-plugins/selection-main.js +97 -0
  47. package/dist/es2019/reducer.js +18 -0
  48. package/dist/es2019/types.js +9 -0
  49. package/dist/es2019/utils.js +233 -0
  50. package/dist/esm/actions.js +5 -0
  51. package/dist/esm/commands.js +251 -0
  52. package/dist/esm/gap-cursor/actions.js +249 -0
  53. package/dist/esm/gap-cursor/direction.js +15 -0
  54. package/dist/esm/gap-cursor/selection.js +1 -0
  55. package/dist/esm/gap-cursor/utils/is-ignored.js +1 -0
  56. package/dist/esm/gap-cursor/utils/is-valid-target-node.js +1 -0
  57. package/dist/esm/gap-cursor/utils/place-gap-cursor.js +97 -0
  58. package/dist/esm/gap-cursor/utils.js +128 -0
  59. package/dist/esm/gap-cursor-selection.js +2 -0
  60. package/dist/esm/index.js +1 -0
  61. package/dist/esm/plugin-factory.js +43 -0
  62. package/dist/esm/plugin.js +68 -0
  63. package/dist/esm/pm-plugins/events/create-selection-between.js +86 -0
  64. package/dist/esm/pm-plugins/events/keydown.js +109 -0
  65. package/dist/esm/pm-plugins/gap-cursor-keymap.js +40 -0
  66. package/dist/esm/pm-plugins/gap-cursor-main.js +153 -0
  67. package/dist/esm/pm-plugins/gap-cursor-plugin-key.js +2 -0
  68. package/dist/esm/pm-plugins/keymap.js +10 -0
  69. package/dist/esm/pm-plugins/selection-main.js +98 -0
  70. package/dist/esm/reducer.js +19 -0
  71. package/dist/esm/types.js +9 -0
  72. package/dist/esm/utils.js +241 -0
  73. package/dist/types/actions.d.ts +17 -0
  74. package/dist/types/commands.d.ts +9 -0
  75. package/dist/types/gap-cursor/actions.d.ts +23 -0
  76. package/dist/types/gap-cursor/direction.d.ts +10 -0
  77. package/dist/types/gap-cursor/selection.d.ts +1 -0
  78. package/dist/types/gap-cursor/utils/is-ignored.d.ts +1 -0
  79. package/dist/types/gap-cursor/utils/is-valid-target-node.d.ts +1 -0
  80. package/dist/types/gap-cursor/utils/place-gap-cursor.d.ts +2 -0
  81. package/dist/types/gap-cursor/utils.d.ts +8 -0
  82. package/dist/types/gap-cursor-selection.d.ts +2 -0
  83. package/dist/types/index.d.ts +3 -0
  84. package/dist/types/plugin-factory.d.ts +2 -0
  85. package/dist/types/plugin.d.ts +13 -0
  86. package/dist/types/pm-plugins/events/create-selection-between.d.ts +4 -0
  87. package/dist/types/pm-plugins/events/keydown.d.ts +2 -0
  88. package/dist/types/pm-plugins/gap-cursor-keymap.d.ts +2 -0
  89. package/dist/types/pm-plugins/gap-cursor-main.d.ts +6 -0
  90. package/dist/types/pm-plugins/gap-cursor-plugin-key.d.ts +2 -0
  91. package/dist/types/pm-plugins/keymap.d.ts +3 -0
  92. package/dist/types/pm-plugins/selection-main.d.ts +7 -0
  93. package/dist/types/reducer.d.ts +3 -0
  94. package/dist/types/types.d.ts +20 -0
  95. package/dist/types/utils.d.ts +58 -0
  96. package/package.json +93 -0
  97. package/types/package.json +15 -0
@@ -0,0 +1,250 @@
1
+ import { isIgnored as isIgnoredByGapCursor } from '@atlaskit/editor-common/selection';
2
+ import { isEmptyParagraph, isNodeEmpty } from '@atlaskit/editor-common/utils';
3
+ import { NodeSelection, Selection, TextSelection } from '@atlaskit/editor-prosemirror/state';
4
+ import { SelectionActionTypes } from './actions';
5
+ import { GapCursorSelection, Side } from './gap-cursor-selection';
6
+ import { createCommand, getPluginState } from './plugin-factory';
7
+ import { RelativeSelectionPos, SelectionDirection, selectionPluginKey } from './types';
8
+ import { findFirstChildNodeToSelect, findLastChildNodeToSelect, findSelectableContainerAfter, findSelectableContainerBefore, findSelectableContainerParent, isSelectableContainerNode, isSelectionAtEndOfParentNode, isSelectionAtStartOfParentNode } from './utils';
9
+ export const selectNearNode = (selectionRelativeToNode, selection) => ({
10
+ tr
11
+ }) => {
12
+ tr.setMeta(selectionPluginKey, {
13
+ type: SelectionActionTypes.SET_RELATIVE_SELECTION,
14
+ selectionRelativeToNode
15
+ });
16
+ if (selection) {
17
+ return tr.setSelection(selection);
18
+ }
19
+ return tr;
20
+ };
21
+ export const setSelectionRelativeToNode = (selectionRelativeToNode, selection) => createCommand({
22
+ type: SelectionActionTypes.SET_RELATIVE_SELECTION,
23
+ selectionRelativeToNode
24
+ }, tr => {
25
+ return selectNearNode(selectionRelativeToNode, selection)({
26
+ tr
27
+ }) || tr;
28
+ });
29
+ export const arrowRight = (state, dispatch) => {
30
+ const {
31
+ selection
32
+ } = state;
33
+ if (selection instanceof GapCursorSelection) {
34
+ return arrowRightFromGapCursor(selection)(state, dispatch);
35
+ } else if (selection instanceof NodeSelection) {
36
+ return arrowRightFromNode(selection)(state, dispatch);
37
+ } else if (selection instanceof TextSelection) {
38
+ return arrowRightFromText(selection)(state, dispatch);
39
+ }
40
+ return false;
41
+ };
42
+ export const arrowLeft = (state, dispatch) => {
43
+ const {
44
+ selection
45
+ } = state;
46
+ if (selection instanceof GapCursorSelection) {
47
+ return arrowLeftFromGapCursor(selection)(state, dispatch);
48
+ } else if (selection instanceof NodeSelection) {
49
+ return arrowLeftFromNode(selection)(state, dispatch);
50
+ } else if (selection instanceof TextSelection) {
51
+ return arrowLeftFromText(selection)(state, dispatch);
52
+ }
53
+ return false;
54
+ };
55
+ const arrowRightFromGapCursor = selection => (state, dispatch) => {
56
+ const {
57
+ $from,
58
+ $to,
59
+ side
60
+ } = selection;
61
+ if (side === Side.LEFT) {
62
+ const selectableNode = findSelectableContainerAfter($to, state.doc);
63
+ if (selectableNode) {
64
+ return setSelectionRelativeToNode(RelativeSelectionPos.Start, NodeSelection.create(state.doc, selectableNode.pos))(state, dispatch);
65
+ }
66
+ } else if (side === Side.RIGHT && isSelectionAtEndOfParentNode($from, selection)) {
67
+ const selectableNode = findSelectableContainerParent(selection);
68
+ if (selectableNode) {
69
+ return setSelectionRelativeToNode(RelativeSelectionPos.End, NodeSelection.create(state.doc, selectableNode.pos))(state, dispatch);
70
+ }
71
+ }
72
+ return false;
73
+ };
74
+ const arrowLeftFromGapCursor = selection => (state, dispatch) => {
75
+ const {
76
+ $from,
77
+ side
78
+ } = selection;
79
+ const {
80
+ selectionRelativeToNode
81
+ } = getPluginState(state);
82
+ if (side === Side.RIGHT) {
83
+ const selectableNode = findSelectableContainerBefore($from, state.doc);
84
+ if (selectableNode) {
85
+ return setSelectionRelativeToNode(RelativeSelectionPos.End, NodeSelection.create(state.doc, selectableNode.pos))(state, dispatch);
86
+ }
87
+ } else if (side === Side.LEFT && isSelectionAtStartOfParentNode($from, selection)) {
88
+ if (selectionRelativeToNode === RelativeSelectionPos.Before) {
89
+ const $parent = state.doc.resolve(selection.$from.before(selection.$from.depth));
90
+ if ($parent) {
91
+ const selectableNode = findSelectableContainerBefore($parent, state.doc);
92
+ if (selectableNode && isIgnoredByGapCursor(selectableNode.node)) {
93
+ // selection is inside node without gap cursor preceeded by another node without gap cursor - set node selection for previous node
94
+ return setSelectionRelativeToNode(RelativeSelectionPos.End, NodeSelection.create(state.doc, selectableNode.pos))(state, dispatch);
95
+ }
96
+ }
97
+ // we don't return this as we want to reset the relative pos, but not block other plugins
98
+ // from responding to arrow left key
99
+ setSelectionRelativeToNode()(state, dispatch);
100
+ } else {
101
+ const selectableNode = findSelectableContainerParent(selection);
102
+ if (selectableNode) {
103
+ return setSelectionRelativeToNode(RelativeSelectionPos.Start, NodeSelection.create(state.doc, selectableNode.pos))(state, dispatch);
104
+ }
105
+ }
106
+ }
107
+ return false;
108
+ };
109
+ const arrowRightFromNode = selection => (state, dispatch) => {
110
+ const {
111
+ node,
112
+ from,
113
+ $to
114
+ } = selection;
115
+ const {
116
+ selectionRelativeToNode
117
+ } = getPluginState(state);
118
+ if (node.isAtom) {
119
+ if (isSelectionAtEndOfParentNode($to, selection)) {
120
+ // selection is for inline node that is the last child of its parent node - set text selection after it
121
+ return findAndSetTextSelection(RelativeSelectionPos.End, state.doc.resolve(from + 1), SelectionDirection.After)(state, dispatch);
122
+ }
123
+ return false;
124
+ } else if (selectionRelativeToNode === RelativeSelectionPos.Start) {
125
+ // selection is for container node - set selection inside it at the start
126
+ return setSelectionInsideAtNodeStart(RelativeSelectionPos.Inside, node, from)(state, dispatch);
127
+ } else if (isIgnoredByGapCursor(node) && (!selectionRelativeToNode || selectionRelativeToNode === RelativeSelectionPos.End)) {
128
+ const selectableNode = findSelectableContainerAfter($to, state.doc);
129
+ if (selectableNode && isIgnoredByGapCursor(selectableNode.node)) {
130
+ // selection is for node without gap cursor followed by another node without gap cursor - set node selection for next node
131
+ return setSelectionRelativeToNode(RelativeSelectionPos.Start, NodeSelection.create(state.doc, selectableNode.pos))(state, dispatch);
132
+ }
133
+ }
134
+ return false;
135
+ };
136
+ const arrowLeftFromNode = selection => (state, dispatch) => {
137
+ const {
138
+ node,
139
+ from,
140
+ to,
141
+ $from
142
+ } = selection;
143
+ const {
144
+ selectionRelativeToNode
145
+ } = getPluginState(state);
146
+ if (node.isAtom) {
147
+ if (isSelectionAtStartOfParentNode($from, selection)) {
148
+ // selection is for inline node that is the first child of its parent node - set text selection before it
149
+ return findAndSetTextSelection(RelativeSelectionPos.Start, state.doc.resolve(from), SelectionDirection.Before)(state, dispatch);
150
+ }
151
+ return false;
152
+ } else if (selectionRelativeToNode === RelativeSelectionPos.End) {
153
+ // selection is for container node - set selection inside it at the end
154
+ return setSelectionInsideAtNodeEnd(RelativeSelectionPos.Inside, node, from, to)(state, dispatch);
155
+ } else if (!selectionRelativeToNode || selectionRelativeToNode === RelativeSelectionPos.Inside) {
156
+ // selection is for container node - set selection inside it at the start
157
+ // (this is a special case when the user selects by clicking node)
158
+ return setSelectionInsideAtNodeStart(RelativeSelectionPos.Before, node, from)(state, dispatch);
159
+ } else if (isIgnoredByGapCursor(node) && selectionRelativeToNode === RelativeSelectionPos.Start) {
160
+ // selection is for node without gap cursor preceeded by another node without gap cursor - set node selection for previous node
161
+ const selectableNode = findSelectableContainerBefore($from, state.doc);
162
+ if (selectableNode && isIgnoredByGapCursor(selectableNode.node)) {
163
+ return setSelectionRelativeToNode(RelativeSelectionPos.End, NodeSelection.create(state.doc, selectableNode.pos))(state, dispatch);
164
+ }
165
+ }
166
+ return false;
167
+ };
168
+ const arrowRightFromText = selection => (state, dispatch) => {
169
+ if (isSelectionAtEndOfParentNode(selection.$to, selection)) {
170
+ const selectableNode = findSelectableContainerParent(selection);
171
+ if (selectableNode) {
172
+ return setSelectionRelativeToNode(RelativeSelectionPos.End, NodeSelection.create(state.doc, selectableNode.pos))(state, dispatch);
173
+ }
174
+ }
175
+ return false;
176
+ };
177
+ const arrowLeftFromText = selection => (state, dispatch) => {
178
+ const {
179
+ selectionRelativeToNode
180
+ } = getPluginState(state);
181
+ if (selectionRelativeToNode === RelativeSelectionPos.Before) {
182
+ const selectableNode = findSelectableContainerBefore(selection.$from, state.doc);
183
+ if (selectableNode && isIgnoredByGapCursor(selectableNode.node)) {
184
+ // selection is inside node without gap cursor preceeded by another node without gap cursor - set node selection for previous node
185
+ return setSelectionRelativeToNode(RelativeSelectionPos.End, NodeSelection.create(state.doc, selectableNode.pos))(state, dispatch);
186
+ }
187
+ // we don't return this as we want to reset the relative pos, but not block other plugins
188
+ // from responding to arrow left key
189
+ setSelectionRelativeToNode(undefined)(state, dispatch);
190
+ } else if (isSelectionAtStartOfParentNode(selection.$from, selection)) {
191
+ const selectableNode = findSelectableContainerParent(selection);
192
+ if (selectableNode) {
193
+ return setSelectionRelativeToNode(RelativeSelectionPos.Start, NodeSelection.create(state.doc, selectableNode.pos))(state, dispatch);
194
+ }
195
+ }
196
+ return false;
197
+ };
198
+ const findAndSetTextSelection = (selectionRelativeToNode, $pos, dir) => (state, dispatch) => {
199
+ const sel = Selection.findFrom($pos, dir, true);
200
+ if (sel) {
201
+ return setSelectionRelativeToNode(selectionRelativeToNode, sel)(state, dispatch);
202
+ }
203
+ return false;
204
+ };
205
+ const setSelectionInsideAtNodeStart = (selectionRelativeToNode, node, pos) => (state, dispatch) => {
206
+ if (isNodeEmpty(node)) {
207
+ return findAndSetTextSelection(selectionRelativeToNode, state.doc.resolve(pos), SelectionDirection.After)(state, dispatch);
208
+ }
209
+ const selectableNode = findFirstChildNodeToSelect(node);
210
+ if (selectableNode) {
211
+ const {
212
+ node: childNode,
213
+ pos: childPos
214
+ } = selectableNode;
215
+ const selectionPos = pos + childPos + 1;
216
+ if (childNode.isText || childNode.isAtom) {
217
+ return findAndSetTextSelection(selectionRelativeToNode, state.doc.resolve(selectionPos), SelectionDirection.Before)(state, dispatch);
218
+ } else if (isEmptyParagraph(childNode)) {
219
+ return findAndSetTextSelection(selectionRelativeToNode, state.doc.resolve(selectionPos + 1), SelectionDirection.Before)(state, dispatch);
220
+ } else if (!isIgnoredByGapCursor(node)) {
221
+ return setSelectionRelativeToNode(selectionRelativeToNode, new GapCursorSelection(state.doc.resolve(selectionPos), Side.LEFT))(state, dispatch);
222
+ } else if (isSelectableContainerNode(node)) {
223
+ return setSelectionRelativeToNode(selectionRelativeToNode, NodeSelection.create(state.doc, selectionPos))(state, dispatch);
224
+ }
225
+ }
226
+ return false;
227
+ };
228
+ export const setSelectionInsideAtNodeEnd = (selectionRelativeToNode, node, from, to) => (state, dispatch) => {
229
+ if (isNodeEmpty(node)) {
230
+ return findAndSetTextSelection(selectionRelativeToNode, state.doc.resolve(to), SelectionDirection.Before)(state, dispatch);
231
+ }
232
+ const selectableNode = findLastChildNodeToSelect(node);
233
+ if (selectableNode) {
234
+ const {
235
+ node: childNode,
236
+ pos: childPos
237
+ } = selectableNode;
238
+ const selectionPos = from + childPos + childNode.nodeSize;
239
+ if (childNode.isText || childNode.isAtom) {
240
+ return findAndSetTextSelection(selectionRelativeToNode, state.doc.resolve(selectionPos + 1), SelectionDirection.After)(state, dispatch);
241
+ } else if (isEmptyParagraph(childNode)) {
242
+ return findAndSetTextSelection(selectionRelativeToNode, state.doc.resolve(selectionPos), SelectionDirection.After)(state, dispatch);
243
+ } else if (!isIgnoredByGapCursor(node)) {
244
+ return setSelectionRelativeToNode(selectionRelativeToNode, new GapCursorSelection(state.doc.resolve(selectionPos + 1), Side.RIGHT))(state, dispatch);
245
+ } else if (isSelectableContainerNode(node)) {
246
+ return setSelectionRelativeToNode(selectionRelativeToNode, NodeSelection.create(state.doc, selectionPos))(state, dispatch);
247
+ }
248
+ }
249
+ return false;
250
+ };
@@ -0,0 +1,256 @@
1
+ import { atTheBeginningOfDoc, atTheEndOfDoc } from '@atlaskit/editor-common/selection';
2
+ import { isMediaNode, isNodeBeforeMediaNode, isPositionNearTableRow, ZERO_WIDTH_SPACE } from '@atlaskit/editor-common/utils';
3
+ import { NodeSelection, Selection, TextSelection } from '@atlaskit/editor-prosemirror/state';
4
+ import { findDomRefAtPos, findPositionOfNodeBefore, removeNodeBefore } from '@atlaskit/editor-prosemirror/utils';
5
+ import { gapCursorPluginKey } from '../pm-plugins/gap-cursor-plugin-key';
6
+ import { Direction, isBackward, isForward } from './direction';
7
+ import { GapCursorSelection, Side } from './selection';
8
+ import { isTextBlockNearPos } from './utils';
9
+ import { isValidTargetNode } from './utils/is-valid-target-node';
10
+ export const shouldSkipGapCursor = (direction, state, $pos) => {
11
+ var _$pos$nodeBefore;
12
+ const {
13
+ doc,
14
+ schema
15
+ } = state;
16
+ switch (direction) {
17
+ case Direction.UP:
18
+ if (atTheBeginningOfDoc(state)) {
19
+ return false;
20
+ }
21
+ return isPositionNearTableRow($pos, schema, 'before') || isTextBlockNearPos(doc, schema, $pos, -1) || isNodeBeforeMediaNode($pos, state);
22
+ case Direction.DOWN:
23
+ return atTheEndOfDoc(state) || isTextBlockNearPos(doc, schema, $pos, 1) || isPositionNearTableRow($pos, schema, 'after') || ((_$pos$nodeBefore = $pos.nodeBefore) === null || _$pos$nodeBefore === void 0 ? void 0 : _$pos$nodeBefore.type.name) === 'text' && !$pos.nodeAfter // end of a paragraph
24
+ ;
25
+
26
+ default:
27
+ return false;
28
+ }
29
+ };
30
+
31
+ // These cases should be handled using the handleMediaGapCursor function
32
+ function shouldHandleMediaGapCursor(dir, state) {
33
+ var _selection$$from$node;
34
+ const {
35
+ selection
36
+ } = state;
37
+ const upArrowFromGapCursorIntoMedia = selection instanceof GapCursorSelection && dir === Direction.UP && selection.$from.nodeBefore && isMediaNode(selection.$from.nodeBefore);
38
+ const downArrowFromGapCursorIntoMediaGroup = selection instanceof GapCursorSelection && dir === Direction.DOWN && ((_selection$$from$node = selection.$from.nodeAfter) === null || _selection$$from$node === void 0 ? void 0 : _selection$$from$node.type.name) === 'mediaGroup';
39
+ return upArrowFromGapCursorIntoMedia || downArrowFromGapCursorIntoMediaGroup;
40
+ }
41
+
42
+ // Handle media gap cursor for up/down arrow into media nodes
43
+ // Should check this case by using shouldHandleMediaGapCursor first
44
+ function handleMediaGapCursor(dir, state) {
45
+ const {
46
+ selection,
47
+ tr
48
+ } = state;
49
+ let $pos = isBackward(dir) ? selection.$from : selection.$to;
50
+ if (dir === Direction.UP && selection.$from.nodeBefore && isMediaNode(selection.$from.nodeBefore)) {
51
+ var _tr$doc$nodeAt;
52
+ const nodeBeforePos = findPositionOfNodeBefore(tr.selection);
53
+ if (nodeBeforePos && selection.side === 'right' && ((_tr$doc$nodeAt = tr.doc.nodeAt(nodeBeforePos)) === null || _tr$doc$nodeAt === void 0 ? void 0 : _tr$doc$nodeAt.type.name) === 'mediaSingle') {
54
+ tr.setSelection(new NodeSelection(tr.doc.resolve(nodeBeforePos))).scrollIntoView();
55
+ } else if (nodeBeforePos || nodeBeforePos === 0) {
56
+ tr.setSelection(new GapCursorSelection(tr.doc.resolve(nodeBeforePos), Side.LEFT)).scrollIntoView();
57
+ }
58
+ }
59
+ if (dir === Direction.DOWN && selection.$from.nodeAfter) {
60
+ const nodeAfterPos = selection.side === 'right' ? $pos.pos : $pos.pos + selection.$from.nodeAfter.nodeSize;
61
+ if (nodeAfterPos) {
62
+ tr.setSelection(new GapCursorSelection(tr.doc.resolve(nodeAfterPos), Side.LEFT)).scrollIntoView();
63
+ }
64
+ }
65
+ return tr;
66
+ }
67
+ export const arrow = (dir, endOfTextblock) => (state, dispatch, view) => {
68
+ const {
69
+ doc,
70
+ selection,
71
+ tr
72
+ } = state;
73
+ let $pos = isBackward(dir) ? selection.$from : selection.$to;
74
+ let mustMove = selection.empty;
75
+
76
+ // start from text selection
77
+ if (selection instanceof TextSelection) {
78
+ // if cursor is in the middle of a text node, do nothing
79
+ if (!endOfTextblock || !endOfTextblock(dir.toString())) {
80
+ return false;
81
+ }
82
+
83
+ // UP/DOWN jumps to the nearest texblock skipping gapcursor whenever possible
84
+ if (shouldSkipGapCursor(dir, state, $pos)) {
85
+ return false;
86
+ }
87
+
88
+ // otherwise resolve previous/next position
89
+ $pos = doc.resolve(isBackward(dir) ? $pos.before() : $pos.after());
90
+ mustMove = false;
91
+ }
92
+ if (selection instanceof NodeSelection) {
93
+ if (selection.node.isInline) {
94
+ return false;
95
+ }
96
+ if (dir === Direction.UP && !atTheBeginningOfDoc(state) && !isNodeBeforeMediaNode($pos, state) || dir === Direction.DOWN) {
97
+ // We dont add gap cursor on node selections going up and down
98
+ // Except we do if we're going up for a block node which is the
99
+ // first node in the document OR the node before is a media node
100
+ return false;
101
+ }
102
+ }
103
+
104
+ // Handle media gap cursor for up/down arrow into media nodes
105
+ if (shouldHandleMediaGapCursor(dir, state)) {
106
+ const updatedTr = handleMediaGapCursor(dir, state);
107
+ if (dispatch) {
108
+ dispatch(updatedTr);
109
+ }
110
+ return true;
111
+ }
112
+
113
+ // when jumping between block nodes at the same depth, we need to reverse cursor without changing ProseMirror position
114
+ if (selection instanceof GapCursorSelection &&
115
+ // next node allow gap cursor position
116
+ isValidTargetNode(isBackward(dir) ? $pos.nodeBefore : $pos.nodeAfter) && (
117
+ // gap cursor changes block node
118
+ isBackward(dir) && selection.side === Side.LEFT || isForward(dir) && selection.side === Side.RIGHT)) {
119
+ // reverse cursor position
120
+ if (dispatch) {
121
+ dispatch(tr.setSelection(new GapCursorSelection($pos, selection.side === Side.RIGHT ? Side.LEFT : Side.RIGHT)).scrollIntoView());
122
+ }
123
+ return true;
124
+ }
125
+ if (view) {
126
+ const domAtPos = view.domAtPos.bind(view);
127
+ const target = findDomRefAtPos($pos.pos, domAtPos);
128
+ if (target && target.textContent === ZERO_WIDTH_SPACE) {
129
+ return false;
130
+ }
131
+ }
132
+ const nextSelection = GapCursorSelection.findFrom($pos, isBackward(dir) ? -1 : 1, mustMove);
133
+ if (!nextSelection) {
134
+ return false;
135
+ }
136
+ if (!isValidTargetNode(isForward(dir) ? nextSelection.$from.nodeBefore : nextSelection.$from.nodeAfter)) {
137
+ // reverse cursor position
138
+ if (dispatch) {
139
+ dispatch(tr.setSelection(new GapCursorSelection(nextSelection.$from, isForward(dir) ? Side.LEFT : Side.RIGHT)).scrollIntoView());
140
+ }
141
+ return true;
142
+ }
143
+ if (dispatch) {
144
+ dispatch(tr.setSelection(nextSelection).scrollIntoView());
145
+ }
146
+ return true;
147
+ };
148
+ export const deleteNode = dir => (state, dispatch) => {
149
+ if (state.selection instanceof GapCursorSelection) {
150
+ const {
151
+ $from,
152
+ $anchor
153
+ } = state.selection;
154
+ let {
155
+ tr
156
+ } = state;
157
+ if (isBackward(dir)) {
158
+ if (state.selection.side === 'left') {
159
+ tr.setSelection(new GapCursorSelection($anchor, Side.RIGHT));
160
+ if (dispatch) {
161
+ dispatch(tr);
162
+ }
163
+ return true;
164
+ }
165
+ tr = removeNodeBefore(state.tr);
166
+ } else if ($from.nodeAfter) {
167
+ tr = tr.delete($from.pos, $from.pos + $from.nodeAfter.nodeSize);
168
+ }
169
+ if (dispatch) {
170
+ dispatch(tr.setSelection(Selection.near(tr.doc.resolve(tr.mapping.map(state.selection.$from.pos)))).scrollIntoView());
171
+ }
172
+ return true;
173
+ }
174
+ return false;
175
+ };
176
+
177
+ // This function captures clicks outside of the ProseMirror contentEditable area
178
+ // see also description of "handleClick" in gap-cursor pm-plugin
179
+ const captureCursorCoords = (event, editorRef, posAtCoords, tr) => {
180
+ const rect = editorRef.getBoundingClientRect();
181
+
182
+ // capture clicks before the first block element
183
+ if (event.clientY < rect.top) {
184
+ return {
185
+ position: 0,
186
+ side: Side.LEFT
187
+ };
188
+ }
189
+ if (rect.left > 0) {
190
+ // calculate start position of a node that is vertically at the same level
191
+ const coords = posAtCoords({
192
+ left: rect.left,
193
+ top: event.clientY
194
+ });
195
+ if (coords && coords.inside > -1) {
196
+ const $from = tr.doc.resolve(coords.inside);
197
+ const start = $from.before(1);
198
+ const side = event.clientX < rect.left ? Side.LEFT : Side.RIGHT;
199
+ let position;
200
+ if (side === Side.LEFT) {
201
+ position = start;
202
+ } else {
203
+ const node = tr.doc.nodeAt(start);
204
+ if (node) {
205
+ position = start + node.nodeSize;
206
+ }
207
+ }
208
+ return {
209
+ position,
210
+ side
211
+ };
212
+ }
213
+ }
214
+ return null;
215
+ };
216
+ export const setSelectionTopLevelBlocks = (tr, event, editorRef, posAtCoords, editorFocused) => {
217
+ const cursorCoords = captureCursorCoords(event, editorRef, posAtCoords, tr);
218
+ if (!cursorCoords) {
219
+ return;
220
+ }
221
+ const $pos = cursorCoords.position !== undefined ? tr.doc.resolve(cursorCoords.position) : null;
222
+ if ($pos === null) {
223
+ return;
224
+ }
225
+ const isGapCursorAllowed = cursorCoords.side === Side.LEFT ? isValidTargetNode($pos.nodeAfter) : isValidTargetNode($pos.nodeBefore);
226
+ if (isGapCursorAllowed && GapCursorSelection.valid($pos)) {
227
+ // this forces PM to re-render the decoration node if we change the side of the gap cursor, it doesn't do it by default
228
+ if (tr.selection instanceof GapCursorSelection) {
229
+ tr.setSelection(Selection.near($pos));
230
+ } else {
231
+ tr.setSelection(new GapCursorSelection($pos, cursorCoords.side));
232
+ }
233
+ }
234
+ // try to set text selection if the editor isnt focused
235
+ // if the editor is focused, we are most likely dragging a selection outside.
236
+ else if (editorFocused === false) {
237
+ const selectionTemp = Selection.findFrom($pos, cursorCoords.side === Side.LEFT ? 1 : -1, true);
238
+ if (selectionTemp) {
239
+ tr.setSelection(selectionTemp);
240
+ }
241
+ }
242
+ };
243
+ export const setCursorForTopLevelBlocks = (event, editorRef, posAtCoords, editorFocused) => (state, dispatch) => {
244
+ const {
245
+ tr
246
+ } = state;
247
+ setSelectionTopLevelBlocks(tr, event, editorRef, posAtCoords, editorFocused);
248
+ if (tr.selectionSet && dispatch) {
249
+ dispatch(tr);
250
+ return true;
251
+ }
252
+ return false;
253
+ };
254
+ export const hasGapCursorPlugin = state => {
255
+ return Boolean(gapCursorPluginKey.get(state));
256
+ };
@@ -0,0 +1,15 @@
1
+ export let Direction = /*#__PURE__*/function (Direction) {
2
+ Direction["UP"] = "up";
3
+ Direction["RIGHT"] = "right";
4
+ Direction["DOWN"] = "down";
5
+ Direction["LEFT"] = "left";
6
+ Direction["BACKWARD"] = "backward";
7
+ Direction["FORWARD"] = "forward";
8
+ return Direction;
9
+ }({});
10
+ export function isBackward(dir) {
11
+ return [Direction.UP, Direction.LEFT, Direction.BACKWARD].indexOf(dir) !== -1;
12
+ }
13
+ export function isForward(dir) {
14
+ return [Direction.RIGHT, Direction.DOWN, Direction.FORWARD].indexOf(dir) !== -1;
15
+ }
@@ -0,0 +1 @@
1
+ export { GapCursorSelection, JSON_ID, Side, GapBookmark } from '@atlaskit/editor-common/selection';
@@ -0,0 +1 @@
1
+ export { isIgnored } from '@atlaskit/editor-common/selection';
@@ -0,0 +1 @@
1
+ export { isValidTargetNode } from '@atlaskit/editor-common/selection';
@@ -0,0 +1,94 @@
1
+ import { Side } from '@atlaskit/editor-common/selection';
2
+ import { getComputedStyleForLayoutMode, getLayoutModeFromTargetNode, isLeftCursor } from '../utils';
3
+
4
+ /**
5
+ * We have a couple of nodes that require us to compute style
6
+ * on different elements, ideally all nodes should be able to
7
+ * compute the appropriate styles based on their wrapper.
8
+ */
9
+ const nestedCases = {
10
+ 'tableView-content-wrap': 'table',
11
+ 'mediaSingleView-content-wrap': '.rich-media-item',
12
+ 'bodiedExtensionView-content-wrap': '.extension-container',
13
+ 'embedCardView-content-wrap': '.rich-media-item',
14
+ 'datasourceView-content-wrap': '.datasourceView-content-inner-wrap'
15
+ };
16
+ const computeNestedStyle = dom => {
17
+ const foundKey = Object.keys(nestedCases).find(className => dom.classList.contains(className));
18
+ const nestedSelector = foundKey && nestedCases[foundKey];
19
+ if (nestedSelector) {
20
+ const nestedElement = dom.querySelector(nestedSelector);
21
+ if (nestedElement) {
22
+ return window.getComputedStyle(nestedElement);
23
+ }
24
+ }
25
+ };
26
+ const measureHeight = style => {
27
+ return measureValue(style, ['height', 'padding-top', 'padding-bottom', 'border-top-width', 'border-bottom-width']);
28
+ };
29
+ const measureWidth = style => {
30
+ return measureValue(style, ['width', 'padding-left', 'padding-right', 'border-left-width', 'border-right-width']);
31
+ };
32
+ const measureValue = (style, measureValues) => {
33
+ const [base, ...contentBoxValues] = measureValues;
34
+ const measures = [style.getPropertyValue(base)];
35
+ const boxSizing = style.getPropertyValue('box-sizing');
36
+ if (boxSizing === 'content-box') {
37
+ contentBoxValues.forEach(value => {
38
+ measures.push(style.getPropertyValue(value));
39
+ });
40
+ }
41
+ let result = 0;
42
+ for (let i = 0; i < measures.length; i++) {
43
+ result += parseFloat(measures[i]);
44
+ }
45
+ return result;
46
+ };
47
+ const mutateElementStyle = (element, style, side) => {
48
+ element.style.transform = style.getPropertyValue('transform');
49
+ if (isLeftCursor(side)) {
50
+ element.style.width = style.getPropertyValue('width');
51
+ element.style.marginLeft = style.getPropertyValue('margin-left');
52
+ } else {
53
+ const marginRight = parseFloat(style.getPropertyValue('margin-right'));
54
+ if (marginRight > 0) {
55
+ element.style.marginLeft = `-${Math.abs(marginRight)}px`;
56
+ } else {
57
+ element.style.paddingRight = `${Math.abs(marginRight)}px`;
58
+ }
59
+ }
60
+ };
61
+ export const toDOM = (view, getPos) => {
62
+ const selection = view.state.selection;
63
+ const {
64
+ $from,
65
+ side
66
+ } = selection;
67
+ const isRightCursor = side === Side.RIGHT;
68
+ const node = isRightCursor ? $from.nodeBefore : $from.nodeAfter;
69
+ const nodeStart = getPos();
70
+ // @ts-ignore - [unblock prosemirror bump] nodeStart can be undefined
71
+ const dom = view.nodeDOM(nodeStart);
72
+ const element = document.createElement('span');
73
+ element.className = `ProseMirror-gapcursor ${isRightCursor ? '-right' : '-left'}`;
74
+ element.appendChild(document.createElement('span'));
75
+ if (dom instanceof HTMLElement && element.firstChild) {
76
+ const style = computeNestedStyle(dom) || window.getComputedStyle(dom);
77
+ const gapCursor = element.firstChild;
78
+ gapCursor.style.height = `${measureHeight(style)}px`;
79
+ const layoutMode = node && getLayoutModeFromTargetNode(node);
80
+
81
+ // TODO remove this table specific piece. need to figure out margin collapsing logic
82
+ if (nodeStart !== 0 || layoutMode || (node === null || node === void 0 ? void 0 : node.type.name) === 'table') {
83
+ gapCursor.style.marginTop = style.getPropertyValue('margin-top');
84
+ }
85
+ if (layoutMode) {
86
+ gapCursor.setAttribute('layout', layoutMode);
87
+ const breakoutModeStyle = getComputedStyleForLayoutMode(dom, node, style);
88
+ gapCursor.style.width = `${measureWidth(breakoutModeStyle)}px`;
89
+ } else {
90
+ mutateElementStyle(gapCursor, style, selection.side);
91
+ }
92
+ }
93
+ return element;
94
+ };