@atlaskit/editor-plugin-selection 3.1.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @atlaskit/editor-plugin-selection
2
2
 
3
+ ## 3.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [`12618b43355c6`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/12618b43355c6) -
8
+ Improve selection APIs by getting their fragment without the parent structure.
9
+
3
10
  ## 3.1.0
4
11
 
5
12
  ### Minor Changes
@@ -1,11 +1,59 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
4
  Object.defineProperty(exports, "__esModule", {
4
5
  value: true
5
6
  });
6
7
  exports.getSelectionLocalIds = exports.getSelectionFragment = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
7
9
  var _editorJsonTransformer = require("@atlaskit/editor-json-transformer");
10
+ var _model = require("@atlaskit/editor-prosemirror/model");
8
11
  var _state = require("@atlaskit/editor-prosemirror/state");
12
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
13
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
14
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
15
+ /**
16
+ * Get the slice of the document corresponding to the selection.
17
+ * This is similar to the prosemirror `selection.content()` - but
18
+ * does not include the parents (unless the result is inline)
19
+ *
20
+ * @param selection The selection to get the slice for.
21
+ * @returns The slice of the document corresponding to the selection.
22
+ */
23
+ var getSlice = function getSlice(selection) {
24
+ var from = selection.from,
25
+ to = selection.to;
26
+ if (from === to) {
27
+ return _model.Fragment.empty;
28
+ }
29
+ var frag = _model.Fragment.empty;
30
+ var sortedRanges = (0, _toConsumableArray2.default)(selection.ranges.slice()).sort(function (a, b) {
31
+ return a.$from.pos - b.$from.pos;
32
+ });
33
+ var _iterator = _createForOfIteratorHelper(sortedRanges),
34
+ _step;
35
+ try {
36
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
37
+ var range = _step.value;
38
+ var $from = range.$from,
39
+ $to = range.$to;
40
+ var _to = $to.pos;
41
+ var depth =
42
+ // If we're in a text selection, and share the parent node across the anchor->head
43
+ // make the depth the parent node
44
+ selection instanceof _state.TextSelection && $from.parent.eq($to.parent) ? Math.max(0, $from.sharedDepth(_to) - 1) : $from.sharedDepth(_to);
45
+ var start = $from.start(depth);
46
+ var node = $from.node(depth);
47
+ var content = node.content.cut($from.pos - start, $to.pos - start);
48
+ frag = frag.append(content);
49
+ }
50
+ } catch (err) {
51
+ _iterator.e(err);
52
+ } finally {
53
+ _iterator.f();
54
+ }
55
+ return frag;
56
+ };
9
57
  var getSelectionFragment = exports.getSelectionFragment = function getSelectionFragment(api) {
10
58
  return function () {
11
59
  var _api$selection$shared, _api$core$sharedState;
@@ -14,7 +62,8 @@ var getSelectionFragment = exports.getSelectionFragment = function getSelectionF
14
62
  if (!selection || !schema || selection.empty) {
15
63
  return null;
16
64
  }
17
- var content = selection === null || selection === void 0 ? void 0 : selection.content().content;
65
+ var slice = getSlice(selection);
66
+ var content = slice.content;
18
67
  var fragment = [];
19
68
  content.forEach(function (node) {
20
69
  fragment.push((0, _editorJsonTransformer.nodeToJSON)(node));
@@ -43,7 +92,7 @@ var getSelectionLocalIds = exports.getSelectionLocalIds = function getSelectionL
43
92
  } else if (selection.empty) {
44
93
  return [selection.$from.parent.attrs.localId];
45
94
  }
46
- var content = selection.content().content;
95
+ var content = getSlice(selection).content;
47
96
  var ids = [];
48
97
  content.forEach(function (node) {
49
98
  var _node$attrs;
@@ -1,5 +1,41 @@
1
1
  import { nodeToJSON } from '@atlaskit/editor-json-transformer';
2
- import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
2
+ import { Fragment } from '@atlaskit/editor-prosemirror/model';
3
+ import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
4
+ /**
5
+ * Get the slice of the document corresponding to the selection.
6
+ * This is similar to the prosemirror `selection.content()` - but
7
+ * does not include the parents (unless the result is inline)
8
+ *
9
+ * @param selection The selection to get the slice for.
10
+ * @returns The slice of the document corresponding to the selection.
11
+ */
12
+ const getSlice = selection => {
13
+ const {
14
+ from,
15
+ to
16
+ } = selection;
17
+ if (from === to) {
18
+ return Fragment.empty;
19
+ }
20
+ let frag = Fragment.empty;
21
+ const sortedRanges = [...selection.ranges.slice()].sort((a, b) => a.$from.pos - b.$from.pos);
22
+ for (const range of sortedRanges) {
23
+ const {
24
+ $from,
25
+ $to
26
+ } = range;
27
+ const to = $to.pos;
28
+ const depth =
29
+ // If we're in a text selection, and share the parent node across the anchor->head
30
+ // make the depth the parent node
31
+ selection instanceof TextSelection && $from.parent.eq($to.parent) ? Math.max(0, $from.sharedDepth(to) - 1) : $from.sharedDepth(to);
32
+ const start = $from.start(depth);
33
+ const node = $from.node(depth);
34
+ const content = node.content.cut($from.pos - start, $to.pos - start);
35
+ frag = frag.append(content);
36
+ }
37
+ return frag;
38
+ };
3
39
  export const getSelectionFragment = api => () => {
4
40
  var _api$selection$shared, _api$selection$shared2, _api$core$sharedState;
5
41
  const selection = api === null || api === void 0 ? void 0 : (_api$selection$shared = api.selection.sharedState) === null || _api$selection$shared === void 0 ? void 0 : (_api$selection$shared2 = _api$selection$shared.currentState()) === null || _api$selection$shared2 === void 0 ? void 0 : _api$selection$shared2.selection;
@@ -7,7 +43,8 @@ export const getSelectionFragment = api => () => {
7
43
  if (!selection || !schema || selection.empty) {
8
44
  return null;
9
45
  }
10
- const content = selection === null || selection === void 0 ? void 0 : selection.content().content;
46
+ const slice = getSlice(selection);
47
+ const content = slice.content;
11
48
  const fragment = [];
12
49
  content.forEach(node => {
13
50
  fragment.push(nodeToJSON(node));
@@ -35,7 +72,7 @@ export const getSelectionLocalIds = api => () => {
35
72
  } else if (selection.empty) {
36
73
  return [selection.$from.parent.attrs.localId];
37
74
  }
38
- const content = selection.content().content;
75
+ const content = getSlice(selection).content;
39
76
  const ids = [];
40
77
  content.forEach(node => {
41
78
  var _node$attrs;
@@ -1,5 +1,52 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
3
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
4
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
1
5
  import { nodeToJSON } from '@atlaskit/editor-json-transformer';
2
- import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
6
+ import { Fragment } from '@atlaskit/editor-prosemirror/model';
7
+ import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
8
+ /**
9
+ * Get the slice of the document corresponding to the selection.
10
+ * This is similar to the prosemirror `selection.content()` - but
11
+ * does not include the parents (unless the result is inline)
12
+ *
13
+ * @param selection The selection to get the slice for.
14
+ * @returns The slice of the document corresponding to the selection.
15
+ */
16
+ var getSlice = function getSlice(selection) {
17
+ var from = selection.from,
18
+ to = selection.to;
19
+ if (from === to) {
20
+ return Fragment.empty;
21
+ }
22
+ var frag = Fragment.empty;
23
+ var sortedRanges = _toConsumableArray(selection.ranges.slice()).sort(function (a, b) {
24
+ return a.$from.pos - b.$from.pos;
25
+ });
26
+ var _iterator = _createForOfIteratorHelper(sortedRanges),
27
+ _step;
28
+ try {
29
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
30
+ var range = _step.value;
31
+ var $from = range.$from,
32
+ $to = range.$to;
33
+ var _to = $to.pos;
34
+ var depth =
35
+ // If we're in a text selection, and share the parent node across the anchor->head
36
+ // make the depth the parent node
37
+ selection instanceof TextSelection && $from.parent.eq($to.parent) ? Math.max(0, $from.sharedDepth(_to) - 1) : $from.sharedDepth(_to);
38
+ var start = $from.start(depth);
39
+ var node = $from.node(depth);
40
+ var content = node.content.cut($from.pos - start, $to.pos - start);
41
+ frag = frag.append(content);
42
+ }
43
+ } catch (err) {
44
+ _iterator.e(err);
45
+ } finally {
46
+ _iterator.f();
47
+ }
48
+ return frag;
49
+ };
3
50
  export var getSelectionFragment = function getSelectionFragment(api) {
4
51
  return function () {
5
52
  var _api$selection$shared, _api$core$sharedState;
@@ -8,7 +55,8 @@ export var getSelectionFragment = function getSelectionFragment(api) {
8
55
  if (!selection || !schema || selection.empty) {
9
56
  return null;
10
57
  }
11
- var content = selection === null || selection === void 0 ? void 0 : selection.content().content;
58
+ var slice = getSlice(selection);
59
+ var content = slice.content;
12
60
  var fragment = [];
13
61
  content.forEach(function (node) {
14
62
  fragment.push(nodeToJSON(node));
@@ -37,7 +85,7 @@ export var getSelectionLocalIds = function getSelectionLocalIds(api) {
37
85
  } else if (selection.empty) {
38
86
  return [selection.$from.parent.attrs.localId];
39
87
  }
40
- var content = selection.content().content;
88
+ var content = getSlice(selection).content;
41
89
  var ids = [];
42
90
  content.forEach(function (node) {
43
91
  var _node$attrs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-selection",
3
- "version": "3.1.0",
3
+ "version": "3.1.1",
4
4
  "description": "Selection plugin for @atlaskit/editor-core",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"
@@ -19,7 +19,7 @@
19
19
  "singleton": true
20
20
  },
21
21
  "dependencies": {
22
- "@atlaskit/editor-json-transformer": "^8.26.0",
22
+ "@atlaskit/editor-json-transformer": "^8.27.0",
23
23
  "@atlaskit/editor-plugin-interaction": "^5.0.0",
24
24
  "@atlaskit/editor-prosemirror": "7.0.0",
25
25
  "@atlaskit/editor-shared-styles": "^3.6.0",
@@ -30,7 +30,7 @@
30
30
  "@babel/runtime": "^7.0.0"
31
31
  },
32
32
  "peerDependencies": {
33
- "@atlaskit/editor-common": "^107.26.0",
33
+ "@atlaskit/editor-common": "^107.27.0",
34
34
  "react": "^18.2.0"
35
35
  },
36
36
  "techstack": {