@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,159 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
8
+ var _selection = require("@atlaskit/editor-common/selection");
9
+ var _state = require("@atlaskit/editor-prosemirror/state");
10
+ var _utils = require("@atlaskit/editor-prosemirror/utils");
11
+ var _view2 = require("@atlaskit/editor-prosemirror/view");
12
+ var _cellSelection = require("@atlaskit/editor-tables/cell-selection");
13
+ var _actions = require("../gap-cursor/actions");
14
+ var _direction = require("../gap-cursor/direction");
15
+ var _utils2 = require("../gap-cursor/utils");
16
+ var _placeGapCursor = require("../gap-cursor/utils/place-gap-cursor");
17
+ var _gapCursorPluginKey = require("./gap-cursor-plugin-key");
18
+ var plugin = new _safePlugin.SafePlugin({
19
+ key: _gapCursorPluginKey.gapCursorPluginKey,
20
+ state: {
21
+ init: function init() {
22
+ return {
23
+ selectionIsGapCursor: false,
24
+ displayGapCursor: true
25
+ };
26
+ },
27
+ apply: function apply(tr, pluginState, _oldState, newState) {
28
+ var _meta$displayGapCurso;
29
+ var meta = tr.getMeta(_gapCursorPluginKey.gapCursorPluginKey);
30
+ var selectionIsGapCursor = newState.selection instanceof _selection.GapCursorSelection;
31
+ return {
32
+ selectionIsGapCursor: selectionIsGapCursor,
33
+ // only attempt to hide gap cursor if selection is gap cursor
34
+ displayGapCursor: selectionIsGapCursor ? (_meta$displayGapCurso = meta === null || meta === void 0 ? void 0 : meta.displayGapCursor) !== null && _meta$displayGapCurso !== void 0 ? _meta$displayGapCurso : pluginState.displayGapCursor : true
35
+ };
36
+ }
37
+ },
38
+ view: function view(_view) {
39
+ /**
40
+ * If the selection is at the beginning of a document and is a NodeSelection,
41
+ * convert to a GapCursor selection. This is to stop users accidentally replacing
42
+ * the first node of a document by accident.
43
+ */
44
+ if (_view.state.selection.anchor === 0 && _view.state.selection instanceof _state.NodeSelection) {
45
+ // This is required otherwise the dispatch doesn't trigger in the correct place
46
+ window.requestAnimationFrame(function () {
47
+ _view.dispatch(_view.state.tr.setSelection(new _selection.GapCursorSelection(_view.state.doc.resolve(0), _selection.Side.LEFT)));
48
+ });
49
+ }
50
+ return {
51
+ update: function update(view) {
52
+ var _gapCursorPluginKey$g = _gapCursorPluginKey.gapCursorPluginKey.getState(view.state),
53
+ selectionIsGapCursor = _gapCursorPluginKey$g.selectionIsGapCursor;
54
+ /**
55
+ * Starting with prosemirror-view 1.19.4, cursor wrapper that previously was hiding cursor doesn't exist:
56
+ * https://github.com/ProseMirror/prosemirror-view/commit/4a56bc7b7e61e96ef879d1dae1014ede0fc09e43
57
+ *
58
+ * Because it was causing issues with RTL: https://github.com/ProseMirror/prosemirror/issues/948
59
+ *
60
+ * This is the work around which uses `caret-color: transparent` in order to hide regular caret,
61
+ * when gap cursor is visible.
62
+ *
63
+ * Browser support is pretty good: https://caniuse.com/#feat=css-caret-color
64
+ */
65
+ view.dom.classList.toggle(_selection.hideCaretModifier, selectionIsGapCursor);
66
+ }
67
+ };
68
+ },
69
+ props: {
70
+ decorations: function decorations(editorState) {
71
+ var doc = editorState.doc,
72
+ selection = editorState.selection;
73
+ var _gapCursorPluginKey$g2 = _gapCursorPluginKey.gapCursorPluginKey.getState(editorState),
74
+ displayGapCursor = _gapCursorPluginKey$g2.displayGapCursor;
75
+ if (selection instanceof _selection.GapCursorSelection && displayGapCursor) {
76
+ var $from = selection.$from,
77
+ side = selection.side;
78
+
79
+ // render decoration DOM node always to the left of the target node even if selection points to the right
80
+ // otherwise positioning of the right gap cursor is a nightmare when the target node has a nodeView with vertical margins
81
+ var position = selection.head;
82
+ var isRightCursor = side === _selection.Side.RIGHT;
83
+ if (isRightCursor && $from.nodeBefore) {
84
+ var nodeBeforeStart = (0, _utils.findPositionOfNodeBefore)(selection);
85
+ if (typeof nodeBeforeStart === 'number') {
86
+ position = nodeBeforeStart;
87
+ }
88
+ }
89
+ var node = isRightCursor ? $from.nodeBefore : $from.nodeAfter;
90
+ var layoutMode = node && (0, _utils2.getLayoutModeFromTargetNode)(node);
91
+ return _view2.DecorationSet.create(doc, [_view2.Decoration.widget(position, _placeGapCursor.toDOM, {
92
+ key: "".concat(_selection.JSON_ID, "-").concat(side, "-").concat(layoutMode),
93
+ side: layoutMode ? -1 : 0
94
+ })]);
95
+ }
96
+ return null;
97
+ },
98
+ // render gap cursor only when its valid
99
+ createSelectionBetween: function createSelectionBetween(view, $anchor, $head) {
100
+ if (view && view.state && view.state.selection instanceof _cellSelection.CellSelection) {
101
+ // Do not show GapCursor when there is a CellSection happening
102
+ return null;
103
+ }
104
+ if ($anchor.pos === $head.pos && _selection.GapCursorSelection.valid($head)) {
105
+ return new _selection.GapCursorSelection($head);
106
+ }
107
+ return null;
108
+ },
109
+ handleClick: function handleClick(view, nodePos, event) {
110
+ var _$pos$parent;
111
+ var posAtCoords = view.posAtCoords({
112
+ left: event.clientX,
113
+ top: event.clientY
114
+ });
115
+ if (!posAtCoords || (0, _utils2.isIgnoredClick)(event.target)) {
116
+ return false;
117
+ }
118
+ var isInsideTheTarget = posAtCoords.pos === posAtCoords.inside;
119
+ if (isInsideTheTarget) {
120
+ return false;
121
+ }
122
+ var leftSideOffsetX = 20;
123
+ var side = event.offsetX > leftSideOffsetX ? _selection.Side.RIGHT : _selection.Side.LEFT;
124
+ var $pos = view.state.doc.resolve(nodePos);
125
+ // In the new prosemirror-view posAtCoords is not returning a precise value for our media nodes
126
+ if (((_$pos$parent = $pos.parent) === null || _$pos$parent === void 0 ? void 0 : _$pos$parent.type.name) === 'mediaSingle') {
127
+ var $insidePos = view.state.doc.resolve(Math.max(posAtCoords.inside, 0));
128
+ // We don't have GapCursors problems when the node target is inside the root level
129
+ if ($insidePos.depth <= 1) {
130
+ return false;
131
+ }
132
+ var mediaGapCursor = !$pos.nodeBefore ? $pos.before() : $pos.after();
133
+ return (0, _selection.setGapCursorAtPos)(mediaGapCursor, side)(view.state, view.dispatch);
134
+ }
135
+ var docSize = view.state.doc.content.size;
136
+ var nodeInside = posAtCoords.inside < 0 || posAtCoords.inside > docSize ? null : view.state.doc.nodeAt(posAtCoords.inside);
137
+ if (nodeInside !== null && nodeInside !== void 0 && nodeInside.isAtom) {
138
+ return false;
139
+ }
140
+ return (0, _selection.setGapCursorAtPos)(nodePos, side)(view.state, view.dispatch);
141
+ },
142
+ handleDOMEvents: {
143
+ /**
144
+ * Android composition events aren't handled well by Prosemirror
145
+ * We've added a couple of beforeinput hooks to help PM out when trying to delete
146
+ * certain nodes. We can remove these when PM has better composition support.
147
+ * @see https://github.com/ProseMirror/prosemirror/issues/543
148
+ */
149
+ beforeinput: function beforeinput(view, event) {
150
+ if (event.inputType === 'deleteContentBackward' && view.state.selection instanceof _selection.GapCursorSelection) {
151
+ event.preventDefault();
152
+ return (0, _actions.deleteNode)(_direction.Direction.BACKWARD)(view.state, view.dispatch);
153
+ }
154
+ return false;
155
+ }
156
+ }
157
+ }
158
+ });
159
+ var _default = exports.default = plugin;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.gapCursorPluginKey = void 0;
7
+ var _state = require("@atlaskit/editor-prosemirror/state");
8
+ var gapCursorPluginKey = exports.gapCursorPluginKey = new _state.PluginKey('gapCursorPlugin');
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _keymaps = require("@atlaskit/editor-common/keymaps");
8
+ var _keymap = require("@atlaskit/editor-prosemirror/keymap");
9
+ var _commands = require("../commands");
10
+ function keymapPlugin() {
11
+ var list = {};
12
+ (0, _keymaps.bindKeymapWithCommand)(_keymaps.moveRight.common, _commands.arrowRight, list);
13
+ (0, _keymaps.bindKeymapWithCommand)(_keymaps.moveLeft.common, _commands.arrowLeft, list);
14
+ return (0, _keymap.keymap)(list);
15
+ }
16
+ var _default = exports.default = keymapPlugin;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getInitialState = exports.createPlugin = void 0;
7
+ var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
8
+ var _state = require("@atlaskit/editor-prosemirror/state");
9
+ var _actions = require("../actions");
10
+ var _pluginFactory = require("../plugin-factory");
11
+ var _types = require("../types");
12
+ var _utils = require("../utils");
13
+ var _createSelectionBetween = require("./events/create-selection-between");
14
+ var _keydown = require("./events/keydown");
15
+ var getInitialState = exports.getInitialState = function getInitialState(state) {
16
+ return {
17
+ decorationSet: (0, _utils.getDecorations)(state.tr),
18
+ selection: state.selection
19
+ };
20
+ };
21
+ var createPlugin = exports.createPlugin = function createPlugin(dispatch, dispatchAnalyticsEvent) {
22
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
23
+ return new _safePlugin.SafePlugin({
24
+ key: _types.selectionPluginKey,
25
+ state: (0, _pluginFactory.createPluginState)(dispatch, getInitialState),
26
+ view: function view() {
27
+ return {
28
+ update: function update(editorView, oldEditorState) {
29
+ var state = editorView.state;
30
+ if (!(0, _utils.shouldRecalcDecorations)({
31
+ oldEditorState: oldEditorState,
32
+ newEditorState: state
33
+ })) {
34
+ return;
35
+ }
36
+ var analyticsPayload = (0, _utils.getNodeSelectionAnalyticsPayload)(state.selection) || (0, _utils.getAllSelectionAnalyticsPayload)(state.selection) ||
37
+ // We handle all range/cell selections except click and drag here, which is
38
+ // handled in mouseup handler below
39
+ !editorView.mouseDown && ((0, _utils.getRangeSelectionAnalyticsPayload)(state.selection, state.doc) || (0, _utils.getCellSelectionAnalyticsPayload)(state));
40
+
41
+ // We have to use dispatchAnalyticsEvent over any of the analytics plugin helpers
42
+ // as there were several issues caused by the fact that adding analytics through
43
+ // the plugin adds a new step to the transaction
44
+ // This causes prosemirror to run through some different code paths, eg. attempting
45
+ // to map selection
46
+ if (analyticsPayload) {
47
+ dispatchAnalyticsEvent(analyticsPayload);
48
+ }
49
+ }
50
+ };
51
+ },
52
+ appendTransaction: function appendTransaction(_transactions, oldEditorState, newEditorState) {
53
+ if (!(0, _utils.shouldRecalcDecorations)({
54
+ oldEditorState: oldEditorState,
55
+ newEditorState: newEditorState
56
+ })) {
57
+ return;
58
+ }
59
+ var tr = newEditorState.tr;
60
+ tr.setMeta(_types.selectionPluginKey, {
61
+ type: _actions.SelectionActionTypes.SET_DECORATIONS,
62
+ selection: tr.selection,
63
+ decorationSet: (0, _utils.getDecorations)(tr)
64
+ });
65
+ return tr;
66
+ },
67
+ filterTransaction: function filterTransaction(tr, state) {
68
+ // Prevent single click selecting atom nodes on mobile (we want to select with long press gesture instead)
69
+ if (options.useLongPressSelection && tr.selectionSet && tr.selection instanceof _state.NodeSelection && !tr.getMeta(_types.selectionPluginKey)) {
70
+ return false;
71
+ }
72
+
73
+ // Prevent prosemirror's mutation observer overriding a node selection with a text selection
74
+ // for exact same range - this was cause of being unable to change dates in collab:
75
+ // https://product-fabric.atlassian.net/browse/ED-10645
76
+ if (state.selection instanceof _state.NodeSelection && tr.selection instanceof _state.TextSelection && state.selection.from === tr.selection.from && state.selection.to === tr.selection.to) {
77
+ return false;
78
+ }
79
+ return true;
80
+ },
81
+ props: {
82
+ createSelectionBetween: _createSelectionBetween.onCreateSelectionBetween,
83
+ decorations: function decorations(state) {
84
+ return (0, _pluginFactory.getPluginState)(state).decorationSet;
85
+ },
86
+ handleDOMEvents: {
87
+ keydown: _keydown.onKeydown,
88
+ // We only want to fire analytics for a click and drag range/cell selection when
89
+ // the user has finished, otherwise we will get an event almost every time they move
90
+ // their mouse which is too much
91
+ mouseup: function mouseup(editorView, event) {
92
+ var mouseEvent = event;
93
+ if (!mouseEvent.shiftKey) {
94
+ var analyticsPayload = (0, _utils.getRangeSelectionAnalyticsPayload)(editorView.state.selection, editorView.state.doc) || (0, _utils.getCellSelectionAnalyticsPayload)(editorView.state);
95
+ if (analyticsPayload) {
96
+ dispatchAnalyticsEvent(analyticsPayload);
97
+ }
98
+ }
99
+ return false;
100
+ }
101
+ }
102
+ }
103
+ });
104
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.reducer = reducer;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _actions = require("./actions");
10
+ 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; }
11
+ 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) { (0, _defineProperty2.default)(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; }
12
+ function reducer(pluginState, action) {
13
+ switch (action.type) {
14
+ case _actions.SelectionActionTypes.SET_DECORATIONS:
15
+ return _objectSpread(_objectSpread({}, pluginState), {}, {
16
+ decorationSet: action.decorationSet,
17
+ selection: action.selection
18
+ });
19
+ case _actions.SelectionActionTypes.SET_RELATIVE_SELECTION:
20
+ return _objectSpread(_objectSpread({}, pluginState), {}, {
21
+ selectionRelativeToNode: action.selectionRelativeToNode
22
+ });
23
+ default:
24
+ return pluginState;
25
+ }
26
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "RelativeSelectionPos", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _selection.RelativeSelectionPos;
10
+ }
11
+ });
12
+ exports.selectionPluginKey = exports.SelectionDirection = void 0;
13
+ var _selection = require("@atlaskit/editor-common/selection");
14
+ var _state = require("@atlaskit/editor-prosemirror/state");
15
+ var selectionPluginKey = exports.selectionPluginKey = new _state.PluginKey('selection');
16
+ var SelectionDirection = exports.SelectionDirection = /*#__PURE__*/function (SelectionDirection) {
17
+ SelectionDirection[SelectionDirection["Before"] = -1] = "Before";
18
+ SelectionDirection[SelectionDirection["After"] = 1] = "After";
19
+ return SelectionDirection;
20
+ }({});
@@ -0,0 +1,280 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "createSelectionClickHandler", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _selection.createSelectionClickHandler;
10
+ }
11
+ });
12
+ exports.findSelectableContainerParent = exports.findSelectableContainerBefore = exports.findSelectableContainerAfter = exports.findLastChildNodeToSelect = exports.findFirstChildNodeToSelect = void 0;
13
+ Object.defineProperty(exports, "getAllSelectionAnalyticsPayload", {
14
+ enumerable: true,
15
+ get: function get() {
16
+ return _selection.getAllSelectionAnalyticsPayload;
17
+ }
18
+ });
19
+ Object.defineProperty(exports, "getCellSelectionAnalyticsPayload", {
20
+ enumerable: true,
21
+ get: function get() {
22
+ return _selection.getCellSelectionAnalyticsPayload;
23
+ }
24
+ });
25
+ exports.getDecorations = void 0;
26
+ Object.defineProperty(exports, "getNodeSelectionAnalyticsPayload", {
27
+ enumerable: true,
28
+ get: function get() {
29
+ return _selection.getNodeSelectionAnalyticsPayload;
30
+ }
31
+ });
32
+ exports.getNodesToDecorateFromSelection = void 0;
33
+ Object.defineProperty(exports, "getRangeSelectionAnalyticsPayload", {
34
+ enumerable: true,
35
+ get: function get() {
36
+ return _selection.getRangeSelectionAnalyticsPayload;
37
+ }
38
+ });
39
+ exports.isSelectionAtStartOfParentNode = exports.isSelectionAtEndOfParentNode = exports.isSelectableContainerNode = exports.isSelectableChildNode = void 0;
40
+ exports.shouldRecalcDecorations = shouldRecalcDecorations;
41
+ var _selection = require("@atlaskit/editor-common/selection");
42
+ var _utils = require("@atlaskit/editor-common/utils");
43
+ var _state = require("@atlaskit/editor-prosemirror/state");
44
+ var _utils2 = require("@atlaskit/editor-prosemirror/utils");
45
+ var _view = require("@atlaskit/editor-prosemirror/view");
46
+ var _editorSharedStyles = require("@atlaskit/editor-shared-styles");
47
+ var _types = require("./types");
48
+ var getDecorations = exports.getDecorations = function getDecorations(tr) {
49
+ if (tr.selection instanceof _state.NodeSelection) {
50
+ return _view.DecorationSet.create(tr.doc, [_view.Decoration.node(tr.selection.from, tr.selection.to, {
51
+ class: _editorSharedStyles.akEditorSelectedNodeClassName
52
+ })]);
53
+ }
54
+ if (tr.selection instanceof _state.TextSelection || tr.selection instanceof _state.AllSelection) {
55
+ var decorations = getNodesToDecorateFromSelection(tr.selection, tr.doc).map(function (_ref) {
56
+ var node = _ref.node,
57
+ pos = _ref.pos;
58
+ return _view.Decoration.node(pos, pos + node.nodeSize, {
59
+ class: _editorSharedStyles.akEditorSelectedNodeClassName
60
+ });
61
+ });
62
+ return _view.DecorationSet.create(tr.doc, decorations);
63
+ }
64
+ return _view.DecorationSet.empty;
65
+ };
66
+ var topLevelBlockNodesThatHaveSelectionStyles = ['table', 'panel', 'expand', 'layoutSection', 'decisionList', 'decisionItem', 'codeBlock'];
67
+
68
+ /**
69
+ * Use `getNodesToDecorateFromSelection` to collect and return
70
+ * a list of nodes within the Selection that should have Selection
71
+ * decorations applied. This allows selection styles to be added to
72
+ * nested nodes. It will ignore text nodes as decorations are
73
+ * applied natively and also ignore nodes that don't completely
74
+ * sit within the given `Selection`.
75
+ */
76
+ var getNodesToDecorateFromSelection = exports.getNodesToDecorateFromSelection = function getNodesToDecorateFromSelection(selection, doc) {
77
+ var nodes = [];
78
+ if (selection.from !== selection.to) {
79
+ var from = selection.from,
80
+ to = selection.to;
81
+ doc.nodesBetween(from, to, function (node, pos) {
82
+ var withinSelection = from <= pos && pos + node.nodeSize <= to;
83
+ // The reason we need to check for these nodes is to stop
84
+ // traversing their children if they are within a selection -
85
+ // this is to prevent selection styles from being added to
86
+ // the children as well as the parent node.
87
+ // Example scenario is if an entire table has been selected
88
+ // we should not traverse its children so we can apply the
89
+ // selection styles to the table. But if an entire tableRow
90
+ // has been selected (but the parent table has not) we should
91
+ // traverse it as it could contain other nodes that need
92
+ // selection styles. I couldn’t see a clear way to differentiate
93
+ // without explicitly stating which nodes should be traversed
94
+ // and which shouldn’t.
95
+ var isTopLevelNodeThatHasSelectionStyles = topLevelBlockNodesThatHaveSelectionStyles.includes(node.type.name);
96
+ // If the node is a top-level block node and completely sits within
97
+ // the selection, we do not recurse it's children to prevent selection
98
+ // styles being added to its child nodes. The expected behaviour
99
+ // is that selection styles are only added to the parent.
100
+ if (node && withinSelection && isTopLevelNodeThatHasSelectionStyles) {
101
+ nodes.push({
102
+ node: node,
103
+ pos: pos
104
+ });
105
+ return false;
106
+ // Otherwise we recurse the children and return them so we can apply
107
+ // selection styles. Text is handled by the browser.
108
+ } else if (node && withinSelection && !node.isText) {
109
+ nodes.push({
110
+ node: node,
111
+ pos: pos
112
+ });
113
+ }
114
+ return true;
115
+ });
116
+ }
117
+ return nodes;
118
+ };
119
+ function shouldRecalcDecorations(_ref2) {
120
+ var oldEditorState = _ref2.oldEditorState,
121
+ newEditorState = _ref2.newEditorState;
122
+ var oldSelection = oldEditorState.selection;
123
+ var newSelection = newEditorState.selection;
124
+ var oldPluginState = _types.selectionPluginKey.getState(oldEditorState);
125
+ var newPluginState = _types.selectionPluginKey.getState(newEditorState);
126
+ if (!oldPluginState || !newPluginState) {
127
+ return false;
128
+ }
129
+
130
+ // If selection is unchanged, no need to recalculate
131
+ if (oldSelection.eq(newSelection)) {
132
+ // We need this special case for NodeSelection, as Prosemirror still thinks the
133
+ // selections are equal when the node has changed
134
+ if (oldSelection instanceof _state.NodeSelection && newSelection instanceof _state.NodeSelection) {
135
+ var oldDecorations = oldPluginState.decorationSet.find();
136
+ var newDecorations = newPluginState.decorationSet.find();
137
+ // There might not be old or new decorations if the node selection is for a text node
138
+ // This wouldn't have happened intentionally, but we need to handle this case regardless
139
+ if (oldDecorations.length > 0 && newDecorations.length > 0) {
140
+ return !oldDecorations[0].eq(newDecorations[0]);
141
+ }
142
+ return !(oldDecorations.length === 0 && newDecorations.length === 0);
143
+ }
144
+ return false;
145
+ }
146
+
147
+ // There's no point updating decorations if going from one standard TextSelection to another
148
+ if (oldSelection instanceof _state.TextSelection && newSelection instanceof _state.TextSelection && oldSelection.from === oldSelection.to && newSelection.from === newSelection.to) {
149
+ return false;
150
+ }
151
+ return true;
152
+ }
153
+ var isSelectableContainerNode = exports.isSelectableContainerNode = function isSelectableContainerNode(node) {
154
+ return !!(node && !node.isAtom && _state.NodeSelection.isSelectable(node));
155
+ };
156
+ var isSelectableChildNode = exports.isSelectableChildNode = function isSelectableChildNode(node) {
157
+ return !!(node && (node.isText || (0, _utils.isEmptyParagraph)(node) || _state.NodeSelection.isSelectable(node)));
158
+ };
159
+
160
+ /**
161
+ * Finds closest parent node that is a selectable block container node
162
+ * If it finds a parent that is not selectable but supports gap cursor, will
163
+ * return undefined
164
+ */
165
+ var findSelectableContainerParent = exports.findSelectableContainerParent = function findSelectableContainerParent(selection) {
166
+ var foundNodeThatSupportsGapCursor = false;
167
+ var selectableNode = (0, _utils2.findParentNode)(function (node) {
168
+ var isSelectable = isSelectableContainerNode(node);
169
+ if (!isSelectable && !(0, _selection.isIgnored)(node)) {
170
+ foundNodeThatSupportsGapCursor = true;
171
+ }
172
+ return isSelectable;
173
+ })(selection);
174
+ if (!foundNodeThatSupportsGapCursor) {
175
+ return selectableNode;
176
+ }
177
+ };
178
+
179
+ /**
180
+ * Finds node before that is a selectable block container node, starting
181
+ * from $pos.depth + 1 and working in
182
+ * If it finds a node that is not selectable but supports gap cursor, will
183
+ * return undefined
184
+ */
185
+ var findSelectableContainerBefore = exports.findSelectableContainerBefore = function findSelectableContainerBefore($pos, doc) {
186
+ // prosemirror just returns the same pos from Selection.findFrom when
187
+ // parent.inlineContent is true, so we move position back one here
188
+ // to counteract that
189
+ if ($pos.parent.inlineContent && isSelectableContainerNode($pos.parent)) {
190
+ $pos = doc.resolve($pos.start() - 1);
191
+ }
192
+ var selectionBefore = _state.Selection.findFrom($pos, -1);
193
+ if (selectionBefore) {
194
+ var $selectionBefore = doc.resolve(selectionBefore.from);
195
+ for (var i = $pos.depth + 1; i <= $selectionBefore.depth; i++) {
196
+ var node = $selectionBefore.node(i);
197
+ if (isSelectableContainerNode(node)) {
198
+ return {
199
+ node: node,
200
+ pos: $selectionBefore.start(i) - 1
201
+ };
202
+ }
203
+ if (i > $pos.depth + 1 && !(0, _selection.isIgnored)(node)) {
204
+ return;
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Stick to the default left selection behaviour,
210
+ * useful for mediaSingleWithCaption
211
+ */
212
+ if (selectionBefore instanceof _state.NodeSelection && _state.NodeSelection.isSelectable(selectionBefore.node)) {
213
+ return {
214
+ node: selectionBefore.node,
215
+ pos: selectionBefore.from
216
+ };
217
+ }
218
+ }
219
+ };
220
+
221
+ /**
222
+ * Finds node after that is a selectable block container node, starting
223
+ * from $pos.depth + 1 and working in
224
+ * If it finds a node that is not selectable but supports gap cursor, will
225
+ * return undefined
226
+ */
227
+ var findSelectableContainerAfter = exports.findSelectableContainerAfter = function findSelectableContainerAfter($pos, doc) {
228
+ var selectionAfter = _state.Selection.findFrom($pos, 1);
229
+ if (selectionAfter) {
230
+ var $selectionAfter = doc.resolve(selectionAfter.from);
231
+ for (var i = $pos.depth + 1; i <= $selectionAfter.depth; i++) {
232
+ var node = $selectionAfter.node(i);
233
+ if (isSelectableContainerNode(node)) {
234
+ return {
235
+ node: node,
236
+ pos: $selectionAfter.start(i) - 1
237
+ };
238
+ }
239
+ if (i > $pos.depth + 1 && !(0, _selection.isIgnored)(node)) {
240
+ return;
241
+ }
242
+ }
243
+ }
244
+ };
245
+
246
+ /**
247
+ * Finds first child node that is a selectable block container node OR that
248
+ * supports gap cursor
249
+ */
250
+ var findFirstChildNodeToSelect = exports.findFirstChildNodeToSelect = function findFirstChildNodeToSelect(parent) {
251
+ return (0, _utils2.flatten)(parent).find(function (child) {
252
+ return isSelectableChildNode(child.node) || !(0, _selection.isIgnored)(child.node);
253
+ });
254
+ };
255
+
256
+ /**
257
+ * Finds last child node that is a selectable block container node OR that
258
+ * supports gap cursor
259
+ */
260
+ var findLastChildNodeToSelect = exports.findLastChildNodeToSelect = function findLastChildNodeToSelect(parent) {
261
+ var child;
262
+ parent.descendants(function (node, pos) {
263
+ if (isSelectableChildNode(node) || !(0, _selection.isIgnored)(node)) {
264
+ child = {
265
+ node: node,
266
+ pos: pos
267
+ };
268
+ return false;
269
+ }
270
+ });
271
+ if (child) {
272
+ return child;
273
+ }
274
+ };
275
+ var isSelectionAtStartOfParentNode = exports.isSelectionAtStartOfParentNode = function isSelectionAtStartOfParentNode($pos, selection) {
276
+ return (0, _selection.isSelectionAtStartOfNode)($pos, findSelectableContainerParent(selection));
277
+ };
278
+ var isSelectionAtEndOfParentNode = exports.isSelectionAtEndOfParentNode = function isSelectionAtEndOfParentNode($pos, selection) {
279
+ return (0, _selection.isSelectionAtEndOfNode)($pos, findSelectableContainerParent(selection));
280
+ };
@@ -0,0 +1,5 @@
1
+ export let SelectionActionTypes = /*#__PURE__*/function (SelectionActionTypes) {
2
+ SelectionActionTypes["SET_DECORATIONS"] = "SET_DECORATIONS";
3
+ SelectionActionTypes["SET_RELATIVE_SELECTION"] = "SET_RELATIVE_SELECTION";
4
+ return SelectionActionTypes;
5
+ }({});