@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,86 @@
1
+ import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
2
+ function isNodeContentEmpty(maybeNode) {
3
+ return (maybeNode === null || maybeNode === void 0 ? void 0 : maybeNode.content.size) === 0 || (maybeNode === null || maybeNode === void 0 ? void 0 : maybeNode.textContent) === '';
4
+ }
5
+ function findEmptySelectableParentNodePosition($pos, isValidPosition) {
6
+ var doc = $pos.doc;
7
+ if ($pos.pos + 1 > doc.content.size) {
8
+ return null;
9
+ }
10
+ if ($pos.nodeBefore !== null) {
11
+ if (isValidPosition($pos)) {
12
+ return $pos;
13
+ }
14
+
15
+ // We can not use `$pos.before()` because ProseMirror throws an error when depth is zero.
16
+ var currentPosIndex = $pos.index();
17
+ if (currentPosIndex === 0) {
18
+ return null;
19
+ }
20
+ var previousIndex = currentPosIndex - 1;
21
+ var $previousPos = $pos.doc.resolve($pos.posAtIndex(previousIndex));
22
+ if (isValidPosition($previousPos)) {
23
+ return $previousPos;
24
+ }
25
+ return null;
26
+ }
27
+ if (isValidPosition($pos)) {
28
+ return $pos;
29
+ }
30
+ var positionLevelUp = $pos.before();
31
+ var resolvedPositionLevelUp = doc.resolve(positionLevelUp);
32
+ return findEmptySelectableParentNodePosition(resolvedPositionLevelUp, isValidPosition);
33
+ }
34
+ var checkPositionNode = function checkPositionNode($pos) {
35
+ var maybeNode = $pos.nodeAfter;
36
+ if (!maybeNode || !maybeNode.isBlock) {
37
+ return false;
38
+ }
39
+ if (maybeNode.isAtom) {
40
+ return true;
41
+ }
42
+ return isNodeContentEmpty(maybeNode) && NodeSelection.isSelectable(maybeNode);
43
+ };
44
+ function findNextSelectionPosition(_ref) {
45
+ var $targetHead = _ref.$targetHead,
46
+ $anchor = _ref.$anchor,
47
+ doc = _ref.doc;
48
+ var direction = $anchor.pos < $targetHead.pos ? 'down' : 'up';
49
+ var maybeNextPosition = findEmptySelectableParentNodePosition($targetHead, checkPositionNode);
50
+ if (maybeNextPosition && maybeNextPosition.nodeAfter) {
51
+ var nodeAfter = maybeNextPosition.nodeAfter;
52
+ var pos = maybeNextPosition.pos;
53
+ var nextPositionToSelect = direction === 'down' ? Math.min(nodeAfter.nodeSize + pos, doc.content.size) : Math.max(pos, 0);
54
+ return doc.resolve(nextPositionToSelect);
55
+ }
56
+ return null;
57
+ }
58
+ export var onCreateSelectionBetween = function onCreateSelectionBetween(view, $anchor, $head) {
59
+ var _$head$parent, _$head$parent2;
60
+ if ($anchor.pos === $head.pos) {
61
+ return null;
62
+ }
63
+ if ($anchor.depth === $head.depth && $anchor.sameParent($head)) {
64
+ return null;
65
+ }
66
+
67
+ // If the head is targeting a paragraph on root, then let ProseMirror handle the text selection
68
+ if ($head.depth === 1 && ((_$head$parent = $head.parent) === null || _$head$parent === void 0 ? void 0 : _$head$parent.type.name) === 'paragraph') {
69
+ return null;
70
+ }
71
+
72
+ // If head is at the beginning of a non-empty textblock, let ProseMirror handle the text selection
73
+ if ((_$head$parent2 = $head.parent) !== null && _$head$parent2 !== void 0 && _$head$parent2.isTextblock && !isNodeContentEmpty($head.parent) && $head.parentOffset === 0) {
74
+ return null;
75
+ }
76
+ var $nextHeadPosition = findNextSelectionPosition({
77
+ $targetHead: $head,
78
+ $anchor: $anchor,
79
+ doc: view.state.doc
80
+ });
81
+ if (!$nextHeadPosition) {
82
+ return null;
83
+ }
84
+ var forcedTextSelection = TextSelection.create(view.state.doc, $anchor.pos, $nextHeadPosition.pos);
85
+ return forcedTextSelection;
86
+ };
@@ -0,0 +1,109 @@
1
+ import { TextSelection } from '@atlaskit/editor-prosemirror/state';
2
+ /*
3
+ * The way expand was built, no browser reconize selection on it.
4
+ * For instance, when a selection going to a "collapsed" expand
5
+ * the browser will try to send the cursor to inside the expand content (wrong),
6
+ * this behavior is caused because the expand content is never true hidden
7
+ * we just set the height to 1px.
8
+ *
9
+ * So, we need to capture a possible selection event
10
+ * when a collapsed exxpand is the next node in the common depth.
11
+ * If that is true, we create a new TextSelection and stop the event bubble
12
+ */
13
+ var isCollpasedExpand = function isCollpasedExpand(node) {
14
+ return Boolean(node && ['expand', 'nestedExpand'].includes(node.type.name) && !node.attrs.__expanded);
15
+ };
16
+
17
+ /**
18
+ * ED-18072 - Cannot shift + arrow past bodied extension if it is not empty
19
+ */
20
+ var isBodiedExtension = function isBodiedExtension(node) {
21
+ return Boolean(node && ['bodiedExtension'].includes(node.type.name));
22
+ };
23
+
24
+ /**
25
+ * ED-19861 - [Regression] keyboard selections within action items are unpredicatable
26
+ * Table was added to the list of problematic nodes because the desired behaviour when Shift+Up from outside the
27
+ * table is to select the table node itself, rather than the table cell content. Previously this behaviour was handled
28
+ * in `packages/editor/editor-core/src/plugins/selection/pm-plugins/events/create-selection-between.ts` but there was
29
+ * a bug in `create-selection-between` which after fixing the bug that code was no longer handling table selection
30
+ * correctly, so to fix that table was added here.
31
+ */
32
+ var isTable = function isTable(node) {
33
+ return Boolean(node && ['table'].includes(node.type.name));
34
+ };
35
+ var isProblematicNode = function isProblematicNode(node) {
36
+ return isCollpasedExpand(node) || isBodiedExtension(node) || isTable(node);
37
+ };
38
+ var findFixedProblematicNodePosition = function findFixedProblematicNodePosition(doc, $head, direction) {
39
+ if ($head.pos === 0 || $head.depth === 0) {
40
+ return null;
41
+ }
42
+ if (direction === 'up') {
43
+ var pos = $head.before();
44
+ var $posResolved = $head.doc.resolve(pos);
45
+ var maybeProblematicNode = $posResolved.nodeBefore;
46
+ if (maybeProblematicNode && isProblematicNode(maybeProblematicNode)) {
47
+ var nodeSize = maybeProblematicNode.nodeSize;
48
+ var nodeStartPosition = pos - nodeSize;
49
+
50
+ // ($head.pos - 1) will correspond to (nodeStartPosition + nodeSize) when we are at the start of the text node
51
+ var isAtEndOfProblematicNode = $head.pos - 1 === nodeStartPosition + nodeSize;
52
+ if (isAtEndOfProblematicNode) {
53
+ var startPosNode = Math.max(nodeStartPosition, 0);
54
+ var $startPosNode = $head.doc.resolve(Math.min(startPosNode, $head.doc.content.size));
55
+ return $startPosNode;
56
+ }
57
+ }
58
+ }
59
+ if (direction === 'down') {
60
+ var _pos = $head.after();
61
+ var _maybeProblematicNode = doc.nodeAt(_pos);
62
+ if (_maybeProblematicNode && isProblematicNode(_maybeProblematicNode) && $head.pos + 1 === _pos) {
63
+ var _nodeSize = _maybeProblematicNode.nodeSize;
64
+ var nodePosition = _pos + _nodeSize;
65
+ var _startPosNode = Math.max(nodePosition, 0);
66
+ var _$startPosNode = $head.doc.resolve(Math.min(_startPosNode, $head.doc.content.size));
67
+ return _$startPosNode;
68
+ }
69
+ }
70
+ return null;
71
+ };
72
+ export var onKeydown = function onKeydown(view, event) {
73
+ /*
74
+ * This workaround is needed for some specific situations.
75
+ * - expand collapse
76
+ * - bodied extension
77
+ */
78
+ if (!(event instanceof KeyboardEvent)) {
79
+ return false;
80
+ }
81
+ if (!event.shiftKey || event.ctrlKey || event.metaKey) {
82
+ return false;
83
+ }
84
+ if (!['ArrowUp', 'ArrowDown', 'ArrowRight', 'ArrowLeft', 'Home', 'End'].includes(event.key)) {
85
+ return false;
86
+ }
87
+ var _view$state = view.state,
88
+ doc = _view$state.doc,
89
+ _view$state$selection = _view$state.selection,
90
+ $head = _view$state$selection.$head,
91
+ $anchor = _view$state$selection.$anchor;
92
+ if (event.key === 'ArrowRight' && $head.nodeAfter || event.key === 'ArrowLeft' && $head.nodeBefore) {
93
+ return false;
94
+ }
95
+ var direction = ['ArrowLeft', 'ArrowUp', 'Home'].includes(event.key) ? 'up' : 'down';
96
+ var $fixedProblematicNodePosition = findFixedProblematicNodePosition(doc, $head, direction);
97
+ if ($fixedProblematicNodePosition) {
98
+ // an offset is used here so that left arrow selects the first character before the node (consistent with arrow right)
99
+ var headOffset = event.key === 'ArrowLeft' ? -1 : 0;
100
+ var head = $fixedProblematicNodePosition.pos + headOffset;
101
+ var forcedTextSelection = TextSelection.create(view.state.doc, $anchor.pos, head);
102
+ var tr = view.state.tr;
103
+ tr.setSelection(forcedTextSelection);
104
+ view.dispatch(tr);
105
+ event.preventDefault();
106
+ return true;
107
+ }
108
+ return false;
109
+ };
@@ -0,0 +1,40 @@
1
+ import { backspace, bindKeymapWithCommand, deleteKey, insertNewLine, moveDown, moveLeft, moveRight, moveUp } from '@atlaskit/editor-common/keymaps';
2
+ import { createParagraphNear } from '@atlaskit/editor-prosemirror/commands';
3
+ import { keymap } from '@atlaskit/editor-prosemirror/keymap';
4
+ import { arrow, deleteNode } from '../gap-cursor/actions';
5
+ import { Direction } from '../gap-cursor/direction';
6
+ import { GapCursorSelection } from '../gap-cursor/selection';
7
+ export default function keymapPlugin() {
8
+ var map = {};
9
+ bindKeymapWithCommand(insertNewLine.common, function (state, dispatch, view) {
10
+ var isInGapCursor = state.selection instanceof GapCursorSelection;
11
+ // Only operate in gap cursor
12
+ if (!isInGapCursor) {
13
+ return false;
14
+ }
15
+ return createParagraphNear(state, dispatch);
16
+ }, map);
17
+ bindKeymapWithCommand(moveLeft.common, function (state, dispatch, view) {
18
+ var endOfTextblock = view ? view.endOfTextblock.bind(view) : undefined;
19
+ return arrow(Direction.LEFT, endOfTextblock)(state, dispatch, view);
20
+ }, map);
21
+ bindKeymapWithCommand(moveRight.common, function (state, dispatch, view) {
22
+ var endOfTextblock = view ? view.endOfTextblock.bind(view) : undefined;
23
+ return arrow(Direction.RIGHT, endOfTextblock)(state, dispatch);
24
+ }, map);
25
+ bindKeymapWithCommand(moveUp.common, function (state, dispatch, view) {
26
+ var endOfTextblock = view ? view.endOfTextblock.bind(view) : undefined;
27
+ return arrow(Direction.UP, endOfTextblock)(state, dispatch);
28
+ }, map);
29
+ bindKeymapWithCommand(moveDown.common, function (state, dispatch, view) {
30
+ var endOfTextblock = view ? view.endOfTextblock.bind(view) : undefined;
31
+ return arrow(Direction.DOWN, endOfTextblock)(state, dispatch);
32
+ }, map);
33
+
34
+ // default PM's Backspace doesn't handle removing block nodes when cursor is after it
35
+ bindKeymapWithCommand(backspace.common, deleteNode(Direction.BACKWARD), map);
36
+
37
+ // handle Delete key (remove node after the cursor)
38
+ bindKeymapWithCommand(deleteKey.common, deleteNode(Direction.FORWARD), map);
39
+ return keymap(map);
40
+ }
@@ -0,0 +1,153 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import { GapCursorSelection, Side as GapCursorSide, hideCaretModifier, JSON_ID, setGapCursorAtPos, Side } from '@atlaskit/editor-common/selection';
3
+ import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
4
+ import { findPositionOfNodeBefore } from '@atlaskit/editor-prosemirror/utils';
5
+ import { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view';
6
+ import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
7
+ import { deleteNode } from '../gap-cursor/actions';
8
+ import { Direction } from '../gap-cursor/direction';
9
+ import { getLayoutModeFromTargetNode, isIgnoredClick } from '../gap-cursor/utils';
10
+ import { toDOM } from '../gap-cursor/utils/place-gap-cursor';
11
+ import { gapCursorPluginKey } from './gap-cursor-plugin-key';
12
+ var plugin = new SafePlugin({
13
+ key: gapCursorPluginKey,
14
+ state: {
15
+ init: function init() {
16
+ return {
17
+ selectionIsGapCursor: false,
18
+ displayGapCursor: true
19
+ };
20
+ },
21
+ apply: function apply(tr, pluginState, _oldState, newState) {
22
+ var _meta$displayGapCurso;
23
+ var meta = tr.getMeta(gapCursorPluginKey);
24
+ var selectionIsGapCursor = newState.selection instanceof GapCursorSelection;
25
+ return {
26
+ selectionIsGapCursor: selectionIsGapCursor,
27
+ // only attempt to hide gap cursor if selection is gap cursor
28
+ displayGapCursor: selectionIsGapCursor ? (_meta$displayGapCurso = meta === null || meta === void 0 ? void 0 : meta.displayGapCursor) !== null && _meta$displayGapCurso !== void 0 ? _meta$displayGapCurso : pluginState.displayGapCursor : true
29
+ };
30
+ }
31
+ },
32
+ view: function view(_view) {
33
+ /**
34
+ * If the selection is at the beginning of a document and is a NodeSelection,
35
+ * convert to a GapCursor selection. This is to stop users accidentally replacing
36
+ * the first node of a document by accident.
37
+ */
38
+ if (_view.state.selection.anchor === 0 && _view.state.selection instanceof NodeSelection) {
39
+ // This is required otherwise the dispatch doesn't trigger in the correct place
40
+ window.requestAnimationFrame(function () {
41
+ _view.dispatch(_view.state.tr.setSelection(new GapCursorSelection(_view.state.doc.resolve(0), GapCursorSide.LEFT)));
42
+ });
43
+ }
44
+ return {
45
+ update: function update(view) {
46
+ var _gapCursorPluginKey$g = gapCursorPluginKey.getState(view.state),
47
+ selectionIsGapCursor = _gapCursorPluginKey$g.selectionIsGapCursor;
48
+ /**
49
+ * Starting with prosemirror-view 1.19.4, cursor wrapper that previously was hiding cursor doesn't exist:
50
+ * https://github.com/ProseMirror/prosemirror-view/commit/4a56bc7b7e61e96ef879d1dae1014ede0fc09e43
51
+ *
52
+ * Because it was causing issues with RTL: https://github.com/ProseMirror/prosemirror/issues/948
53
+ *
54
+ * This is the work around which uses `caret-color: transparent` in order to hide regular caret,
55
+ * when gap cursor is visible.
56
+ *
57
+ * Browser support is pretty good: https://caniuse.com/#feat=css-caret-color
58
+ */
59
+ view.dom.classList.toggle(hideCaretModifier, selectionIsGapCursor);
60
+ }
61
+ };
62
+ },
63
+ props: {
64
+ decorations: function decorations(editorState) {
65
+ var doc = editorState.doc,
66
+ selection = editorState.selection;
67
+ var _gapCursorPluginKey$g2 = gapCursorPluginKey.getState(editorState),
68
+ displayGapCursor = _gapCursorPluginKey$g2.displayGapCursor;
69
+ if (selection instanceof GapCursorSelection && displayGapCursor) {
70
+ var $from = selection.$from,
71
+ side = selection.side;
72
+
73
+ // render decoration DOM node always to the left of the target node even if selection points to the right
74
+ // otherwise positioning of the right gap cursor is a nightmare when the target node has a nodeView with vertical margins
75
+ var position = selection.head;
76
+ var isRightCursor = side === Side.RIGHT;
77
+ if (isRightCursor && $from.nodeBefore) {
78
+ var nodeBeforeStart = findPositionOfNodeBefore(selection);
79
+ if (typeof nodeBeforeStart === 'number') {
80
+ position = nodeBeforeStart;
81
+ }
82
+ }
83
+ var node = isRightCursor ? $from.nodeBefore : $from.nodeAfter;
84
+ var layoutMode = node && getLayoutModeFromTargetNode(node);
85
+ return DecorationSet.create(doc, [Decoration.widget(position, toDOM, {
86
+ key: "".concat(JSON_ID, "-").concat(side, "-").concat(layoutMode),
87
+ side: layoutMode ? -1 : 0
88
+ })]);
89
+ }
90
+ return null;
91
+ },
92
+ // render gap cursor only when its valid
93
+ createSelectionBetween: function createSelectionBetween(view, $anchor, $head) {
94
+ if (view && view.state && view.state.selection instanceof CellSelection) {
95
+ // Do not show GapCursor when there is a CellSection happening
96
+ return null;
97
+ }
98
+ if ($anchor.pos === $head.pos && GapCursorSelection.valid($head)) {
99
+ return new GapCursorSelection($head);
100
+ }
101
+ return null;
102
+ },
103
+ handleClick: function handleClick(view, nodePos, event) {
104
+ var _$pos$parent;
105
+ var posAtCoords = view.posAtCoords({
106
+ left: event.clientX,
107
+ top: event.clientY
108
+ });
109
+ if (!posAtCoords || isIgnoredClick(event.target)) {
110
+ return false;
111
+ }
112
+ var isInsideTheTarget = posAtCoords.pos === posAtCoords.inside;
113
+ if (isInsideTheTarget) {
114
+ return false;
115
+ }
116
+ var leftSideOffsetX = 20;
117
+ var side = event.offsetX > leftSideOffsetX ? Side.RIGHT : Side.LEFT;
118
+ var $pos = view.state.doc.resolve(nodePos);
119
+ // In the new prosemirror-view posAtCoords is not returning a precise value for our media nodes
120
+ if (((_$pos$parent = $pos.parent) === null || _$pos$parent === void 0 ? void 0 : _$pos$parent.type.name) === 'mediaSingle') {
121
+ var $insidePos = view.state.doc.resolve(Math.max(posAtCoords.inside, 0));
122
+ // We don't have GapCursors problems when the node target is inside the root level
123
+ if ($insidePos.depth <= 1) {
124
+ return false;
125
+ }
126
+ var mediaGapCursor = !$pos.nodeBefore ? $pos.before() : $pos.after();
127
+ return setGapCursorAtPos(mediaGapCursor, side)(view.state, view.dispatch);
128
+ }
129
+ var docSize = view.state.doc.content.size;
130
+ var nodeInside = posAtCoords.inside < 0 || posAtCoords.inside > docSize ? null : view.state.doc.nodeAt(posAtCoords.inside);
131
+ if (nodeInside !== null && nodeInside !== void 0 && nodeInside.isAtom) {
132
+ return false;
133
+ }
134
+ return setGapCursorAtPos(nodePos, side)(view.state, view.dispatch);
135
+ },
136
+ handleDOMEvents: {
137
+ /**
138
+ * Android composition events aren't handled well by Prosemirror
139
+ * We've added a couple of beforeinput hooks to help PM out when trying to delete
140
+ * certain nodes. We can remove these when PM has better composition support.
141
+ * @see https://github.com/ProseMirror/prosemirror/issues/543
142
+ */
143
+ beforeinput: function beforeinput(view, event) {
144
+ if (event.inputType === 'deleteContentBackward' && view.state.selection instanceof GapCursorSelection) {
145
+ event.preventDefault();
146
+ return deleteNode(Direction.BACKWARD)(view.state, view.dispatch);
147
+ }
148
+ return false;
149
+ }
150
+ }
151
+ }
152
+ });
153
+ export default plugin;
@@ -0,0 +1,2 @@
1
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
2
+ export var gapCursorPluginKey = new PluginKey('gapCursorPlugin');
@@ -0,0 +1,10 @@
1
+ import { bindKeymapWithCommand, moveLeft, moveRight } from '@atlaskit/editor-common/keymaps';
2
+ import { keymap } from '@atlaskit/editor-prosemirror/keymap';
3
+ import { arrowLeft, arrowRight } from '../commands';
4
+ function keymapPlugin() {
5
+ var list = {};
6
+ bindKeymapWithCommand(moveRight.common, arrowRight, list);
7
+ bindKeymapWithCommand(moveLeft.common, arrowLeft, list);
8
+ return keymap(list);
9
+ }
10
+ export default keymapPlugin;
@@ -0,0 +1,98 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
3
+ import { SelectionActionTypes } from '../actions';
4
+ import { createPluginState, getPluginState } from '../plugin-factory';
5
+ import { selectionPluginKey } from '../types';
6
+ import { getAllSelectionAnalyticsPayload, getCellSelectionAnalyticsPayload, getDecorations, getNodeSelectionAnalyticsPayload, getRangeSelectionAnalyticsPayload, shouldRecalcDecorations } from '../utils';
7
+ import { onCreateSelectionBetween } from './events/create-selection-between';
8
+ import { onKeydown } from './events/keydown';
9
+ export var getInitialState = function getInitialState(state) {
10
+ return {
11
+ decorationSet: getDecorations(state.tr),
12
+ selection: state.selection
13
+ };
14
+ };
15
+ export var createPlugin = function createPlugin(dispatch, dispatchAnalyticsEvent) {
16
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
17
+ return new SafePlugin({
18
+ key: selectionPluginKey,
19
+ state: createPluginState(dispatch, getInitialState),
20
+ view: function view() {
21
+ return {
22
+ update: function update(editorView, oldEditorState) {
23
+ var state = editorView.state;
24
+ if (!shouldRecalcDecorations({
25
+ oldEditorState: oldEditorState,
26
+ newEditorState: state
27
+ })) {
28
+ return;
29
+ }
30
+ var analyticsPayload = getNodeSelectionAnalyticsPayload(state.selection) || getAllSelectionAnalyticsPayload(state.selection) ||
31
+ // We handle all range/cell selections except click and drag here, which is
32
+ // handled in mouseup handler below
33
+ !editorView.mouseDown && (getRangeSelectionAnalyticsPayload(state.selection, state.doc) || getCellSelectionAnalyticsPayload(state));
34
+
35
+ // We have to use dispatchAnalyticsEvent over any of the analytics plugin helpers
36
+ // as there were several issues caused by the fact that adding analytics through
37
+ // the plugin adds a new step to the transaction
38
+ // This causes prosemirror to run through some different code paths, eg. attempting
39
+ // to map selection
40
+ if (analyticsPayload) {
41
+ dispatchAnalyticsEvent(analyticsPayload);
42
+ }
43
+ }
44
+ };
45
+ },
46
+ appendTransaction: function appendTransaction(_transactions, oldEditorState, newEditorState) {
47
+ if (!shouldRecalcDecorations({
48
+ oldEditorState: oldEditorState,
49
+ newEditorState: newEditorState
50
+ })) {
51
+ return;
52
+ }
53
+ var tr = newEditorState.tr;
54
+ tr.setMeta(selectionPluginKey, {
55
+ type: SelectionActionTypes.SET_DECORATIONS,
56
+ selection: tr.selection,
57
+ decorationSet: getDecorations(tr)
58
+ });
59
+ return tr;
60
+ },
61
+ filterTransaction: function filterTransaction(tr, state) {
62
+ // Prevent single click selecting atom nodes on mobile (we want to select with long press gesture instead)
63
+ if (options.useLongPressSelection && tr.selectionSet && tr.selection instanceof NodeSelection && !tr.getMeta(selectionPluginKey)) {
64
+ return false;
65
+ }
66
+
67
+ // Prevent prosemirror's mutation observer overriding a node selection with a text selection
68
+ // for exact same range - this was cause of being unable to change dates in collab:
69
+ // https://product-fabric.atlassian.net/browse/ED-10645
70
+ if (state.selection instanceof NodeSelection && tr.selection instanceof TextSelection && state.selection.from === tr.selection.from && state.selection.to === tr.selection.to) {
71
+ return false;
72
+ }
73
+ return true;
74
+ },
75
+ props: {
76
+ createSelectionBetween: onCreateSelectionBetween,
77
+ decorations: function decorations(state) {
78
+ return getPluginState(state).decorationSet;
79
+ },
80
+ handleDOMEvents: {
81
+ keydown: onKeydown,
82
+ // We only want to fire analytics for a click and drag range/cell selection when
83
+ // the user has finished, otherwise we will get an event almost every time they move
84
+ // their mouse which is too much
85
+ mouseup: function mouseup(editorView, event) {
86
+ var mouseEvent = event;
87
+ if (!mouseEvent.shiftKey) {
88
+ var analyticsPayload = getRangeSelectionAnalyticsPayload(editorView.state.selection, editorView.state.doc) || getCellSelectionAnalyticsPayload(editorView.state);
89
+ if (analyticsPayload) {
90
+ dispatchAnalyticsEvent(analyticsPayload);
91
+ }
92
+ }
93
+ return false;
94
+ }
95
+ }
96
+ }
97
+ });
98
+ };
@@ -0,0 +1,19 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ import { SelectionActionTypes } from './actions';
5
+ export function reducer(pluginState, action) {
6
+ switch (action.type) {
7
+ case SelectionActionTypes.SET_DECORATIONS:
8
+ return _objectSpread(_objectSpread({}, pluginState), {}, {
9
+ decorationSet: action.decorationSet,
10
+ selection: action.selection
11
+ });
12
+ case SelectionActionTypes.SET_RELATIVE_SELECTION:
13
+ return _objectSpread(_objectSpread({}, pluginState), {}, {
14
+ selectionRelativeToNode: action.selectionRelativeToNode
15
+ });
16
+ default:
17
+ return pluginState;
18
+ }
19
+ }
@@ -0,0 +1,9 @@
1
+ import { RelativeSelectionPos } from '@atlaskit/editor-common/selection';
2
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
3
+ export var selectionPluginKey = new PluginKey('selection');
4
+ export { RelativeSelectionPos };
5
+ export var SelectionDirection = /*#__PURE__*/function (SelectionDirection) {
6
+ SelectionDirection[SelectionDirection["Before"] = -1] = "Before";
7
+ SelectionDirection[SelectionDirection["After"] = 1] = "After";
8
+ return SelectionDirection;
9
+ }({});