@atlaskit/editor-plugin-block-controls 8.0.7 → 8.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @atlaskit/editor-plugin-block-controls
2
2
 
3
+ ## 8.0.8
4
+
5
+ ### Patch Changes
6
+
7
+ - [`eb7609ee331ab`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/eb7609ee331ab) -
8
+ [ux] EDITOR-4264 Fix preserved selection mapping
9
+ - Updated dependencies
10
+
3
11
  ## 8.0.7
4
12
 
5
13
  ### Patch Changes
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.createSelectionPreservationPlugin = void 0;
8
8
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
- var _monitoring = require("@atlaskit/editor-common/monitoring");
10
9
  var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
11
10
  var _styles = require("@atlaskit/editor-common/styles");
12
11
  var _selection = require("../utils/selection");
@@ -59,11 +58,10 @@ var createSelectionPreservationPlugin = exports.createSelectionPreservationPlugi
59
58
  var meta = (0, _utils.getSelectionPreservationMeta)(tr);
60
59
  var newState = _objectSpread({}, pluginState);
61
60
  if ((meta === null || meta === void 0 ? void 0 : meta.type) === 'startPreserving') {
62
- newState.preservedSelection = (0, _selection.mapPreservedSelection)(tr.selection, tr);
61
+ newState.preservedSelection = (0, _selection.createPreservedSelection)(tr.doc.resolve(tr.selection.from), tr.doc.resolve(tr.selection.to));
63
62
  } else if ((meta === null || meta === void 0 ? void 0 : meta.type) === 'stopPreserving') {
64
63
  newState.preservedSelection = undefined;
65
- }
66
- if (newState.preservedSelection && tr.docChanged) {
64
+ } else if (newState.preservedSelection && tr.docChanged) {
67
65
  newState.preservedSelection = (0, _selection.mapPreservedSelection)(newState.preservedSelection, tr);
68
66
  }
69
67
  if (!(0, _utils.compareSelections)(newState.preservedSelection, pluginState.preservedSelection)) {
@@ -97,14 +95,15 @@ var createSelectionPreservationPlugin = exports.createSelectionPreservationPlugi
97
95
  if (selectionUnchanged || selectionInvalid) {
98
96
  return null;
99
97
  }
100
- try {
101
- return newState.tr.setSelection(preservedSel);
102
- } catch (error) {
103
- (0, _monitoring.logException)(error, {
104
- location: 'editor-plugin-block-controls/SelectionPreservationPlugin'
98
+ var newSelection = (0, _selection.createPreservedSelection)(newState.doc.resolve(preservedSel.from), newState.doc.resolve(preservedSel.to));
99
+
100
+ // If selection becomes invalid, stop preserving
101
+ if (!newSelection) {
102
+ return (0, _editorCommands.stopPreservingSelection)({
103
+ tr: newState.tr
105
104
  });
106
105
  }
107
- return null;
106
+ return newState.tr.setSelection(newSelection);
108
107
  },
109
108
  view: function view() {
110
109
  return {
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.setCursorPositionAtMovedNode = exports.selectNode = exports.rootTaskListDepth = exports.rootListDepth = exports.isNodeWithCodeBlock = exports.isHandleCorrelatedToSelection = exports.getSelection = exports.getInlineNodePos = void 0;
6
+ exports.setCursorPositionAtMovedNode = exports.selectNode = exports.rootTaskListDepth = exports.rootListDepth = exports.newGetSelection = exports.isNodeWithCodeBlock = exports.isHandleCorrelatedToSelection = exports.getSelection = exports.getInlineNodePos = void 0;
7
7
  var _selection2 = require("@atlaskit/editor-common/selection");
8
8
  var _toolbarFlagCheck = require("@atlaskit/editor-common/toolbar-flag-check");
9
9
  var _state = require("@atlaskit/editor-prosemirror/state");
@@ -81,7 +81,16 @@ var oldGetSelection = function oldGetSelection(tr, start) {
81
81
  return new _state.TextSelection(tr.doc.resolve(inlineNodePos), tr.doc.resolve(inlineNodeEndPos));
82
82
  }
83
83
  };
84
- var newGetSelection = function newGetSelection(doc, selectionEmpty, start) {
84
+
85
+ /**
86
+ * Gets the appropriate selection for the node at the given start position.
87
+ *
88
+ * @param doc The ProseMirror document.
89
+ * @param selectionEmpty Indicates if the current selection is empty.
90
+ * @param start The start position of the node.
91
+ * @returns The appropriate selection for the node.
92
+ */
93
+ var newGetSelection = exports.newGetSelection = function newGetSelection(doc, selectionEmpty, start) {
85
94
  var node = doc.nodeAt(start);
86
95
  var isNodeSelection = node && _state.NodeSelection.isSelectable(node);
87
96
  var nodeSize = node ? node.nodeSize : 1;
@@ -3,10 +3,12 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.mapPreservedSelection = exports.getSelectedSlicePosition = exports.getMultiSelectionIfPosInside = exports.expandSelectionHeadToNodeAtPos = exports.alignAnchorHeadInDirectionOfPos = void 0;
6
+ exports.mapPreservedSelection = exports.getSelectedSlicePosition = exports.getMultiSelectionIfPosInside = exports.expandSelectionHeadToNodeAtPos = exports.createPreservedSelection = exports.alignAnchorHeadInDirectionOfPos = void 0;
7
7
  var _selection = require("@atlaskit/editor-common/selection");
8
8
  var _state = require("@atlaskit/editor-prosemirror/state");
9
+ var _utils = require("@atlaskit/editor-tables/utils");
9
10
  var _main = require("../main");
11
+ var _getSelection = require("./getSelection");
10
12
  var getMultiSelectionIfPosInside = exports.getMultiSelectionIfPosInside = function getMultiSelectionIfPosInside(api, pos, tr) {
11
13
  var _api$blockControls, _pluginState$multiSel, _tr$getMeta;
12
14
  var pluginState = api === null || api === void 0 || (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 ? void 0 : _api$blockControls.sharedState.currentState();
@@ -91,38 +93,51 @@ var mapPreservedSelection = exports.mapPreservedSelection = function mapPreserve
91
93
  var _ref = (0, _main.getBlockControlsMeta)(tr) || {},
92
94
  preservedSelectionMapping = _ref.preservedSelectionMapping;
93
95
  var mapping = preservedSelectionMapping || tr.mapping;
94
- if (selection instanceof _state.TextSelection) {
95
- var from = mapping.map(selection.from);
96
- var to = mapping.map(selection.to);
97
- var isSelectionEmpty = from === to;
98
- var wasSelectionEmpty = selection.from === selection.to;
99
- if (isSelectionEmpty && !wasSelectionEmpty) {
100
- // If selection has become empty i.e. content has been deleted, stop preserving
101
- return undefined;
102
- }
103
- // expand the text selection range to block boundaries, so as document changes occur the
104
- // selection always includes whole nodes
105
- var expanded = (0, _selection.expandToBlockRange)(tr.doc.resolve(from), tr.doc.resolve(to));
96
+ var from = mapping.map(selection.from);
97
+ var to = mapping.map(selection.to);
98
+ var isSelectionEmpty = from === to;
99
+ var wasSelectionEmpty = selection.from === selection.to;
100
+ if (isSelectionEmpty && !wasSelectionEmpty) {
101
+ // If selection has become empty i.e. content has been deleted, stop preserving
102
+ return undefined;
103
+ }
104
+ return createPreservedSelection(tr.doc.resolve(from), tr.doc.resolve(to));
105
+ };
106
106
 
107
- // If after expanding the selection it is still empty, return a single cursor selection at 'from'
108
- var nodeAfter = expanded.$from.nodeAfter;
109
- var nodeBefore = expanded.$to.nodeBefore;
110
- var expandedSelectionEmpty = nodeAfter === nodeBefore && (nodeAfter === null || nodeAfter === void 0 ? void 0 : nodeAfter.content.size) === 0;
111
- if (isSelectionEmpty && expandedSelectionEmpty) {
112
- return _state.TextSelection.create(tr.doc, from);
113
- }
114
- var $from = expanded.$from,
115
- $to = expanded.$to;
107
+ /**
108
+ * Creates a preserved selection which is expanded to block boundaries.
109
+ *
110
+ * Will return the correct type of selection based on the nodes contained within the
111
+ * expanded selection range.
112
+ *
113
+ * If the selection becomes empty or invalid, it returns undefined.
114
+ *
115
+ * @param $from The resolved position of the start of the selection
116
+ * @param $to The resolved position of the end of the selection
117
+ * @returns A Selection or undefined if selection is invalid
118
+ */
119
+ var createPreservedSelection = exports.createPreservedSelection = function createPreservedSelection($from, $to) {
120
+ var _doc$nodeAt;
121
+ var doc = $from.doc;
122
+
123
+ // expand the selection range to block boundaries, so selection always includes whole nodes
124
+ var expanded = (0, _selection.expandToBlockRange)($from, $to);
125
+
126
+ // stop preserving if selection becomes invalid
127
+ if (expanded.$from.pos < 0 || expanded.$to.pos > doc.content.size || expanded.$from.pos >= expanded.$to.pos) {
128
+ return undefined;
129
+ }
116
130
 
117
- // stop preserving if preserved selection becomes invalid
118
- if ($from.pos < 0 || $to.pos > tr.doc.content.size || $from.pos >= $to.pos) {
119
- return undefined;
120
- }
121
- return new _state.TextSelection($from, $to);
131
+ // If multiple blocks selected, create TextSelection from start of first node to end of last node
132
+ if (expanded.range && (0, _selection.isMultiBlockRange)(expanded.range)) {
133
+ return new _state.TextSelection(expanded.$from, expanded.$to);
122
134
  }
123
- try {
124
- return selection.map(tr.doc, mapping);
125
- } catch (_unused) {
135
+ var nodeType = (_doc$nodeAt = doc.nodeAt(expanded.$from.pos)) === null || _doc$nodeAt === void 0 || (_doc$nodeAt = _doc$nodeAt.type) === null || _doc$nodeAt === void 0 ? void 0 : _doc$nodeAt.name;
136
+ if (!nodeType) {
126
137
  return undefined;
127
138
  }
139
+ if (nodeType === 'table') {
140
+ return (0, _utils.getTableSelectionClosesToPos)($from);
141
+ }
142
+ return (0, _getSelection.newGetSelection)(doc, false, expanded.$from.pos) || undefined;
128
143
  };
@@ -1,7 +1,6 @@
1
- import { logException } from '@atlaskit/editor-common/monitoring';
2
1
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
3
2
  import { DRAG_HANDLE_SELECTOR } from '@atlaskit/editor-common/styles';
4
- import { mapPreservedSelection } from '../utils/selection';
3
+ import { createPreservedSelection, mapPreservedSelection } from '../utils/selection';
5
4
  import { stopPreservingSelection } from './editor-commands';
6
5
  import { selectionPreservationPluginKey } from './plugin-key';
7
6
  import { compareSelections, getSelectionPreservationMeta, hasUserSelectionChange, isPreservedSelectionChanged, isSelectionWithinCodeBlock, syncDOMSelection } from './utils';
@@ -51,11 +50,10 @@ export const createSelectionPreservationPlugin = api => () => {
51
50
  ...pluginState
52
51
  };
53
52
  if ((meta === null || meta === void 0 ? void 0 : meta.type) === 'startPreserving') {
54
- newState.preservedSelection = mapPreservedSelection(tr.selection, tr);
53
+ newState.preservedSelection = createPreservedSelection(tr.doc.resolve(tr.selection.from), tr.doc.resolve(tr.selection.to));
55
54
  } else if ((meta === null || meta === void 0 ? void 0 : meta.type) === 'stopPreserving') {
56
55
  newState.preservedSelection = undefined;
57
- }
58
- if (newState.preservedSelection && tr.docChanged) {
56
+ } else if (newState.preservedSelection && tr.docChanged) {
59
57
  newState.preservedSelection = mapPreservedSelection(newState.preservedSelection, tr);
60
58
  }
61
59
  if (!compareSelections(newState.preservedSelection, pluginState.preservedSelection)) {
@@ -89,14 +87,15 @@ export const createSelectionPreservationPlugin = api => () => {
89
87
  if (selectionUnchanged || selectionInvalid) {
90
88
  return null;
91
89
  }
92
- try {
93
- return newState.tr.setSelection(preservedSel);
94
- } catch (error) {
95
- logException(error, {
96
- location: 'editor-plugin-block-controls/SelectionPreservationPlugin'
90
+ const newSelection = createPreservedSelection(newState.doc.resolve(preservedSel.from), newState.doc.resolve(preservedSel.to));
91
+
92
+ // If selection becomes invalid, stop preserving
93
+ if (!newSelection) {
94
+ return stopPreservingSelection({
95
+ tr: newState.tr
97
96
  });
98
97
  }
99
- return null;
98
+ return newState.tr.setSelection(newSelection);
100
99
  },
101
100
  view() {
102
101
  return {
@@ -76,7 +76,16 @@ const oldGetSelection = (tr, start) => {
76
76
  return new TextSelection(tr.doc.resolve(inlineNodePos), tr.doc.resolve(inlineNodeEndPos));
77
77
  }
78
78
  };
79
- const newGetSelection = (doc, selectionEmpty, start) => {
79
+
80
+ /**
81
+ * Gets the appropriate selection for the node at the given start position.
82
+ *
83
+ * @param doc The ProseMirror document.
84
+ * @param selectionEmpty Indicates if the current selection is empty.
85
+ * @param start The start position of the node.
86
+ * @returns The appropriate selection for the node.
87
+ */
88
+ export const newGetSelection = (doc, selectionEmpty, start) => {
80
89
  const node = doc.nodeAt(start);
81
90
  const isNodeSelection = node && NodeSelection.isSelectable(node);
82
91
  const nodeSize = node ? node.nodeSize : 1;
@@ -1,6 +1,8 @@
1
- import { expandToBlockRange } from '@atlaskit/editor-common/selection';
1
+ import { expandToBlockRange, isMultiBlockRange } from '@atlaskit/editor-common/selection';
2
2
  import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
3
+ import { getTableSelectionClosesToPos } from '@atlaskit/editor-tables/utils';
3
4
  import { getBlockControlsMeta, key } from '../main';
5
+ import { newGetSelection } from './getSelection';
4
6
  export const getMultiSelectionIfPosInside = (api, pos, tr) => {
5
7
  var _api$blockControls, _pluginState$multiSel, _tr$getMeta;
6
8
  const pluginState = api === null || api === void 0 ? void 0 : (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 ? void 0 : _api$blockControls.sharedState.currentState();
@@ -87,40 +89,53 @@ export const mapPreservedSelection = (selection, tr) => {
87
89
  preservedSelectionMapping
88
90
  } = getBlockControlsMeta(tr) || {};
89
91
  const mapping = preservedSelectionMapping || tr.mapping;
90
- if (selection instanceof TextSelection) {
91
- const from = mapping.map(selection.from);
92
- const to = mapping.map(selection.to);
93
- const isSelectionEmpty = from === to;
94
- const wasSelectionEmpty = selection.from === selection.to;
95
- if (isSelectionEmpty && !wasSelectionEmpty) {
96
- // If selection has become empty i.e. content has been deleted, stop preserving
97
- return undefined;
98
- }
99
- // expand the text selection range to block boundaries, so as document changes occur the
100
- // selection always includes whole nodes
101
- const expanded = expandToBlockRange(tr.doc.resolve(from), tr.doc.resolve(to));
92
+ const from = mapping.map(selection.from);
93
+ const to = mapping.map(selection.to);
94
+ const isSelectionEmpty = from === to;
95
+ const wasSelectionEmpty = selection.from === selection.to;
96
+ if (isSelectionEmpty && !wasSelectionEmpty) {
97
+ // If selection has become empty i.e. content has been deleted, stop preserving
98
+ return undefined;
99
+ }
100
+ return createPreservedSelection(tr.doc.resolve(from), tr.doc.resolve(to));
101
+ };
102
+
103
+ /**
104
+ * Creates a preserved selection which is expanded to block boundaries.
105
+ *
106
+ * Will return the correct type of selection based on the nodes contained within the
107
+ * expanded selection range.
108
+ *
109
+ * If the selection becomes empty or invalid, it returns undefined.
110
+ *
111
+ * @param $from The resolved position of the start of the selection
112
+ * @param $to The resolved position of the end of the selection
113
+ * @returns A Selection or undefined if selection is invalid
114
+ */
115
+ export const createPreservedSelection = ($from, $to) => {
116
+ var _doc$nodeAt, _doc$nodeAt$type;
117
+ const {
118
+ doc
119
+ } = $from;
102
120
 
103
- // If after expanding the selection it is still empty, return a single cursor selection at 'from'
104
- const nodeAfter = expanded.$from.nodeAfter;
105
- const nodeBefore = expanded.$to.nodeBefore;
106
- const expandedSelectionEmpty = nodeAfter === nodeBefore && (nodeAfter === null || nodeAfter === void 0 ? void 0 : nodeAfter.content.size) === 0;
107
- if (isSelectionEmpty && expandedSelectionEmpty) {
108
- return TextSelection.create(tr.doc, from);
109
- }
110
- const {
111
- $from,
112
- $to
113
- } = expanded;
121
+ // expand the selection range to block boundaries, so selection always includes whole nodes
122
+ const expanded = expandToBlockRange($from, $to);
114
123
 
115
- // stop preserving if preserved selection becomes invalid
116
- if ($from.pos < 0 || $to.pos > tr.doc.content.size || $from.pos >= $to.pos) {
117
- return undefined;
118
- }
119
- return new TextSelection($from, $to);
124
+ // stop preserving if selection becomes invalid
125
+ if (expanded.$from.pos < 0 || expanded.$to.pos > doc.content.size || expanded.$from.pos >= expanded.$to.pos) {
126
+ return undefined;
120
127
  }
121
- try {
122
- return selection.map(tr.doc, mapping);
123
- } catch {
128
+
129
+ // If multiple blocks selected, create TextSelection from start of first node to end of last node
130
+ if (expanded.range && isMultiBlockRange(expanded.range)) {
131
+ return new TextSelection(expanded.$from, expanded.$to);
132
+ }
133
+ const nodeType = (_doc$nodeAt = doc.nodeAt(expanded.$from.pos)) === null || _doc$nodeAt === void 0 ? void 0 : (_doc$nodeAt$type = _doc$nodeAt.type) === null || _doc$nodeAt$type === void 0 ? void 0 : _doc$nodeAt$type.name;
134
+ if (!nodeType) {
124
135
  return undefined;
125
136
  }
137
+ if (nodeType === 'table') {
138
+ return getTableSelectionClosesToPos($from);
139
+ }
140
+ return newGetSelection(doc, false, expanded.$from.pos) || undefined;
126
141
  };
@@ -1,10 +1,9 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
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
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 { logException } from '@atlaskit/editor-common/monitoring';
5
4
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
6
5
  import { DRAG_HANDLE_SELECTOR } from '@atlaskit/editor-common/styles';
7
- import { mapPreservedSelection } from '../utils/selection';
6
+ import { createPreservedSelection, mapPreservedSelection } from '../utils/selection';
8
7
  import { stopPreservingSelection } from './editor-commands';
9
8
  import { selectionPreservationPluginKey } from './plugin-key';
10
9
  import { compareSelections, getSelectionPreservationMeta, hasUserSelectionChange, isPreservedSelectionChanged, isSelectionWithinCodeBlock, syncDOMSelection } from './utils';
@@ -53,11 +52,10 @@ export var createSelectionPreservationPlugin = function createSelectionPreservat
53
52
  var meta = getSelectionPreservationMeta(tr);
54
53
  var newState = _objectSpread({}, pluginState);
55
54
  if ((meta === null || meta === void 0 ? void 0 : meta.type) === 'startPreserving') {
56
- newState.preservedSelection = mapPreservedSelection(tr.selection, tr);
55
+ newState.preservedSelection = createPreservedSelection(tr.doc.resolve(tr.selection.from), tr.doc.resolve(tr.selection.to));
57
56
  } else if ((meta === null || meta === void 0 ? void 0 : meta.type) === 'stopPreserving') {
58
57
  newState.preservedSelection = undefined;
59
- }
60
- if (newState.preservedSelection && tr.docChanged) {
58
+ } else if (newState.preservedSelection && tr.docChanged) {
61
59
  newState.preservedSelection = mapPreservedSelection(newState.preservedSelection, tr);
62
60
  }
63
61
  if (!compareSelections(newState.preservedSelection, pluginState.preservedSelection)) {
@@ -91,14 +89,15 @@ export var createSelectionPreservationPlugin = function createSelectionPreservat
91
89
  if (selectionUnchanged || selectionInvalid) {
92
90
  return null;
93
91
  }
94
- try {
95
- return newState.tr.setSelection(preservedSel);
96
- } catch (error) {
97
- logException(error, {
98
- location: 'editor-plugin-block-controls/SelectionPreservationPlugin'
92
+ var newSelection = createPreservedSelection(newState.doc.resolve(preservedSel.from), newState.doc.resolve(preservedSel.to));
93
+
94
+ // If selection becomes invalid, stop preserving
95
+ if (!newSelection) {
96
+ return stopPreservingSelection({
97
+ tr: newState.tr
99
98
  });
100
99
  }
101
- return null;
100
+ return newState.tr.setSelection(newSelection);
102
101
  },
103
102
  view: function view() {
104
103
  return {
@@ -75,7 +75,16 @@ var oldGetSelection = function oldGetSelection(tr, start) {
75
75
  return new TextSelection(tr.doc.resolve(inlineNodePos), tr.doc.resolve(inlineNodeEndPos));
76
76
  }
77
77
  };
78
- var newGetSelection = function newGetSelection(doc, selectionEmpty, start) {
78
+
79
+ /**
80
+ * Gets the appropriate selection for the node at the given start position.
81
+ *
82
+ * @param doc The ProseMirror document.
83
+ * @param selectionEmpty Indicates if the current selection is empty.
84
+ * @param start The start position of the node.
85
+ * @returns The appropriate selection for the node.
86
+ */
87
+ export var newGetSelection = function newGetSelection(doc, selectionEmpty, start) {
79
88
  var node = doc.nodeAt(start);
80
89
  var isNodeSelection = node && NodeSelection.isSelectable(node);
81
90
  var nodeSize = node ? node.nodeSize : 1;
@@ -1,6 +1,8 @@
1
- import { expandToBlockRange } from '@atlaskit/editor-common/selection';
1
+ import { expandToBlockRange, isMultiBlockRange } from '@atlaskit/editor-common/selection';
2
2
  import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
3
+ import { getTableSelectionClosesToPos } from '@atlaskit/editor-tables/utils';
3
4
  import { getBlockControlsMeta, key } from '../main';
5
+ import { newGetSelection } from './getSelection';
4
6
  export var getMultiSelectionIfPosInside = function getMultiSelectionIfPosInside(api, pos, tr) {
5
7
  var _api$blockControls, _pluginState$multiSel, _tr$getMeta;
6
8
  var pluginState = api === null || api === void 0 || (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 ? void 0 : _api$blockControls.sharedState.currentState();
@@ -85,38 +87,51 @@ export var mapPreservedSelection = function mapPreservedSelection(selection, tr)
85
87
  var _ref = getBlockControlsMeta(tr) || {},
86
88
  preservedSelectionMapping = _ref.preservedSelectionMapping;
87
89
  var mapping = preservedSelectionMapping || tr.mapping;
88
- if (selection instanceof TextSelection) {
89
- var from = mapping.map(selection.from);
90
- var to = mapping.map(selection.to);
91
- var isSelectionEmpty = from === to;
92
- var wasSelectionEmpty = selection.from === selection.to;
93
- if (isSelectionEmpty && !wasSelectionEmpty) {
94
- // If selection has become empty i.e. content has been deleted, stop preserving
95
- return undefined;
96
- }
97
- // expand the text selection range to block boundaries, so as document changes occur the
98
- // selection always includes whole nodes
99
- var expanded = expandToBlockRange(tr.doc.resolve(from), tr.doc.resolve(to));
90
+ var from = mapping.map(selection.from);
91
+ var to = mapping.map(selection.to);
92
+ var isSelectionEmpty = from === to;
93
+ var wasSelectionEmpty = selection.from === selection.to;
94
+ if (isSelectionEmpty && !wasSelectionEmpty) {
95
+ // If selection has become empty i.e. content has been deleted, stop preserving
96
+ return undefined;
97
+ }
98
+ return createPreservedSelection(tr.doc.resolve(from), tr.doc.resolve(to));
99
+ };
100
100
 
101
- // If after expanding the selection it is still empty, return a single cursor selection at 'from'
102
- var nodeAfter = expanded.$from.nodeAfter;
103
- var nodeBefore = expanded.$to.nodeBefore;
104
- var expandedSelectionEmpty = nodeAfter === nodeBefore && (nodeAfter === null || nodeAfter === void 0 ? void 0 : nodeAfter.content.size) === 0;
105
- if (isSelectionEmpty && expandedSelectionEmpty) {
106
- return TextSelection.create(tr.doc, from);
107
- }
108
- var $from = expanded.$from,
109
- $to = expanded.$to;
101
+ /**
102
+ * Creates a preserved selection which is expanded to block boundaries.
103
+ *
104
+ * Will return the correct type of selection based on the nodes contained within the
105
+ * expanded selection range.
106
+ *
107
+ * If the selection becomes empty or invalid, it returns undefined.
108
+ *
109
+ * @param $from The resolved position of the start of the selection
110
+ * @param $to The resolved position of the end of the selection
111
+ * @returns A Selection or undefined if selection is invalid
112
+ */
113
+ export var createPreservedSelection = function createPreservedSelection($from, $to) {
114
+ var _doc$nodeAt;
115
+ var doc = $from.doc;
116
+
117
+ // expand the selection range to block boundaries, so selection always includes whole nodes
118
+ var expanded = expandToBlockRange($from, $to);
119
+
120
+ // stop preserving if selection becomes invalid
121
+ if (expanded.$from.pos < 0 || expanded.$to.pos > doc.content.size || expanded.$from.pos >= expanded.$to.pos) {
122
+ return undefined;
123
+ }
110
124
 
111
- // stop preserving if preserved selection becomes invalid
112
- if ($from.pos < 0 || $to.pos > tr.doc.content.size || $from.pos >= $to.pos) {
113
- return undefined;
114
- }
115
- return new TextSelection($from, $to);
125
+ // If multiple blocks selected, create TextSelection from start of first node to end of last node
126
+ if (expanded.range && isMultiBlockRange(expanded.range)) {
127
+ return new TextSelection(expanded.$from, expanded.$to);
116
128
  }
117
- try {
118
- return selection.map(tr.doc, mapping);
119
- } catch (_unused) {
129
+ var nodeType = (_doc$nodeAt = doc.nodeAt(expanded.$from.pos)) === null || _doc$nodeAt === void 0 || (_doc$nodeAt = _doc$nodeAt.type) === null || _doc$nodeAt === void 0 ? void 0 : _doc$nodeAt.name;
130
+ if (!nodeType) {
120
131
  return undefined;
121
132
  }
133
+ if (nodeType === 'table') {
134
+ return getTableSelectionClosesToPos($from);
135
+ }
136
+ return newGetSelection(doc, false, expanded.$from.pos) || undefined;
122
137
  };
@@ -7,6 +7,15 @@ export declare const getInlineNodePos: (doc: PMNode, start: number, nodeSize: nu
7
7
  inlineNodePos: number;
8
8
  };
9
9
  export declare const isNodeWithCodeBlock: (tr: Transaction, start: number, nodeSize: number) => boolean;
10
+ /**
11
+ * Gets the appropriate selection for the node at the given start position.
12
+ *
13
+ * @param doc The ProseMirror document.
14
+ * @param selectionEmpty Indicates if the current selection is empty.
15
+ * @param start The start position of the node.
16
+ * @returns The appropriate selection for the node.
17
+ */
18
+ export declare const newGetSelection: (doc: PMNode, selectionEmpty: boolean, start: number) => false | NodeSelection | TextSelection;
10
19
  export declare const getSelection: (tr: Transaction, start: number, api?: ExtractInjectionAPI<BlockControlsPlugin>) => false | NodeSelection | TextSelection;
11
20
  export declare const selectNode: (tr: Transaction, start: number, nodeType: string, api?: ExtractInjectionAPI<BlockControlsPlugin>) => Transaction;
12
21
  export declare const setCursorPositionAtMovedNode: (tr: Transaction, start: number, api?: ExtractInjectionAPI<BlockControlsPlugin>) => Transaction;
@@ -1,5 +1,6 @@
1
1
  import type { ExtractInjectionAPI } from '@atlaskit/editor-common/types';
2
- import { type ReadonlyTransaction, type Selection, type Transaction } from '@atlaskit/editor-prosemirror/state';
2
+ import type { ResolvedPos } from '@atlaskit/editor-prosemirror/model';
3
+ import { NodeSelection, TextSelection, type ReadonlyTransaction, type Selection, type Transaction } from '@atlaskit/editor-prosemirror/state';
3
4
  import type { BlockControlsPlugin } from '../../blockControlsPluginType';
4
5
  export declare const getMultiSelectionIfPosInside: (api: ExtractInjectionAPI<BlockControlsPlugin>, pos: number, tr?: Transaction) => {
5
6
  anchor?: number;
@@ -38,3 +39,16 @@ export declare const alignAnchorHeadInDirectionOfPos: (selection: Selection, pos
38
39
  * @returns The mapped selection or undefined if mapping is not possible
39
40
  */
40
41
  export declare const mapPreservedSelection: (selection: Selection, tr: ReadonlyTransaction | Transaction) => Selection | undefined;
42
+ /**
43
+ * Creates a preserved selection which is expanded to block boundaries.
44
+ *
45
+ * Will return the correct type of selection based on the nodes contained within the
46
+ * expanded selection range.
47
+ *
48
+ * If the selection becomes empty or invalid, it returns undefined.
49
+ *
50
+ * @param $from The resolved position of the start of the selection
51
+ * @param $to The resolved position of the end of the selection
52
+ * @returns A Selection or undefined if selection is invalid
53
+ */
54
+ export declare const createPreservedSelection: ($from: ResolvedPos, $to: ResolvedPos) => NodeSelection | TextSelection | import("@atlaskit/editor-tables").CellSelection | undefined;
@@ -7,6 +7,15 @@ export declare const getInlineNodePos: (doc: PMNode, start: number, nodeSize: nu
7
7
  inlineNodePos: number;
8
8
  };
9
9
  export declare const isNodeWithCodeBlock: (tr: Transaction, start: number, nodeSize: number) => boolean;
10
+ /**
11
+ * Gets the appropriate selection for the node at the given start position.
12
+ *
13
+ * @param doc The ProseMirror document.
14
+ * @param selectionEmpty Indicates if the current selection is empty.
15
+ * @param start The start position of the node.
16
+ * @returns The appropriate selection for the node.
17
+ */
18
+ export declare const newGetSelection: (doc: PMNode, selectionEmpty: boolean, start: number) => false | NodeSelection | TextSelection;
10
19
  export declare const getSelection: (tr: Transaction, start: number, api?: ExtractInjectionAPI<BlockControlsPlugin>) => false | NodeSelection | TextSelection;
11
20
  export declare const selectNode: (tr: Transaction, start: number, nodeType: string, api?: ExtractInjectionAPI<BlockControlsPlugin>) => Transaction;
12
21
  export declare const setCursorPositionAtMovedNode: (tr: Transaction, start: number, api?: ExtractInjectionAPI<BlockControlsPlugin>) => Transaction;
@@ -1,5 +1,6 @@
1
1
  import type { ExtractInjectionAPI } from '@atlaskit/editor-common/types';
2
- import { type ReadonlyTransaction, type Selection, type Transaction } from '@atlaskit/editor-prosemirror/state';
2
+ import type { ResolvedPos } from '@atlaskit/editor-prosemirror/model';
3
+ import { NodeSelection, TextSelection, type ReadonlyTransaction, type Selection, type Transaction } from '@atlaskit/editor-prosemirror/state';
3
4
  import type { BlockControlsPlugin } from '../../blockControlsPluginType';
4
5
  export declare const getMultiSelectionIfPosInside: (api: ExtractInjectionAPI<BlockControlsPlugin>, pos: number, tr?: Transaction) => {
5
6
  anchor?: number;
@@ -38,3 +39,16 @@ export declare const alignAnchorHeadInDirectionOfPos: (selection: Selection, pos
38
39
  * @returns The mapped selection or undefined if mapping is not possible
39
40
  */
40
41
  export declare const mapPreservedSelection: (selection: Selection, tr: ReadonlyTransaction | Transaction) => Selection | undefined;
42
+ /**
43
+ * Creates a preserved selection which is expanded to block boundaries.
44
+ *
45
+ * Will return the correct type of selection based on the nodes contained within the
46
+ * expanded selection range.
47
+ *
48
+ * If the selection becomes empty or invalid, it returns undefined.
49
+ *
50
+ * @param $from The resolved position of the start of the selection
51
+ * @param $to The resolved position of the end of the selection
52
+ * @returns A Selection or undefined if selection is invalid
53
+ */
54
+ export declare const createPreservedSelection: ($from: ResolvedPos, $to: ResolvedPos) => NodeSelection | TextSelection | import("@atlaskit/editor-tables").CellSelection | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-block-controls",
3
- "version": "8.0.7",
3
+ "version": "8.0.8",
4
4
  "description": "Block controls plugin for @atlaskit/editor-core",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
@@ -54,7 +54,7 @@
54
54
  "@atlaskit/pragmatic-drag-and-drop-react-drop-indicator": "^3.2.0",
55
55
  "@atlaskit/primitives": "^17.0.0",
56
56
  "@atlaskit/theme": "^21.0.0",
57
- "@atlaskit/tmp-editor-statsig": "^16.13.0",
57
+ "@atlaskit/tmp-editor-statsig": "^16.14.0",
58
58
  "@atlaskit/tokens": "^9.1.0",
59
59
  "@atlaskit/tooltip": "^20.13.0",
60
60
  "@babel/runtime": "^7.0.0",