@atlaskit/editor-plugin-selection
Version:
Selection plugin for @atlaskit/editor-core
142 lines (140 loc) • 7.88 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getSliceFromSelection = exports.getSelectionLocalIds = exports.getSelectionFragment = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _selection2 = require("@atlaskit/editor-common/selection");
var _editorJsonTransformer = require("@atlaskit/editor-json-transformer");
var _model = require("@atlaskit/editor-prosemirror/model");
var _state = require("@atlaskit/editor-prosemirror/state");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
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; } } }; }
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; } }
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; }
var listDepth = 3;
var selectionCoversAllListItems = function selectionCoversAllListItems($from, $to) {
// Block level lists
var listParents = ['bulletList', 'orderedList'];
if ($from.depth >= listDepth && $to.depth >= listDepth && $from.depth === $to.depth) {
var _greatGrandparentFrom, _greatGrandparentFrom2;
// Get grandparents (from)
var grandparentFrom = $from.node($from.depth - 1);
var greatGrandparentFrom = $from.node($from.depth - 2);
// Get grandparents (to)
var grandparentTo = $to.node($from.depth - 1);
var greatGrandparentTo = $to.node($from.depth - 2);
if (greatGrandparentTo.eq(greatGrandparentFrom) && listParents.includes(greatGrandparentFrom.type.name) && // Selection covers entire list
(_greatGrandparentFrom = greatGrandparentFrom.firstChild) !== null && _greatGrandparentFrom !== void 0 && _greatGrandparentFrom.eq(grandparentFrom) && (_greatGrandparentFrom2 = greatGrandparentFrom.lastChild) !== null && _greatGrandparentFrom2 !== void 0 && _greatGrandparentFrom2.eq(grandparentTo)) {
return true;
}
}
return false;
};
/**
* Get the slice of the document corresponding to the selection.
* This is similar to the prosemirror `selection.content()` - but
* does not include the parents (unless the result is inline)
*
* @param selection The selection to get the slice for.
* @returns The slice of the document corresponding to the selection.
*/
var getSliceFromSelection = exports.getSliceFromSelection = function getSliceFromSelection(selection) {
var from = selection.from,
to = selection.to;
if (from === to) {
return _model.Fragment.empty;
}
var frag = _model.Fragment.empty;
var sortedRanges = (0, _toConsumableArray2.default)(selection.ranges.slice()).sort(function (a, b) {
return a.$from.pos - b.$from.pos;
});
var _iterator = _createForOfIteratorHelper(sortedRanges),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var range = _step.value;
var $from = range.$from,
$to = range.$to;
var _to = $to.pos;
var depth =
// If we're in a text selection, and share the parent node across the anchor->head
// make the depth the parent node
selection instanceof _state.TextSelection && $from.parent.eq($to.parent) ? Math.max(0, $from.sharedDepth(_to) - 1) : $from.sharedDepth(_to);
var finalDepth = depth;
// For block-level lists (non-nested) specifically use the selection
if (selectionCoversAllListItems($from, $to)) {
finalDepth = $from.depth - listDepth;
}
var start = $from.start(finalDepth);
var node = $from.node(finalDepth);
var content = node.content.cut($from.pos - start, $to.pos - start);
frag = frag.append(content);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return frag;
};
var getSelectionFragment = exports.getSelectionFragment = function getSelectionFragment(api) {
return function () {
var _api$selection$shared;
var selection = api === null || api === void 0 || (_api$selection$shared = api.selection.sharedState) === null || _api$selection$shared === void 0 || (_api$selection$shared = _api$selection$shared.currentState()) === null || _api$selection$shared === void 0 ? void 0 : _api$selection$shared.selection;
if ((0, _platformFeatureFlags.fg)('platform_editor_renderer_selection_context')) {
return (0, _selection2.getFragmentsFromSelection)(selection);
} else {
var _api$core$sharedState;
var schema = api === null || api === void 0 || (_api$core$sharedState = api.core.sharedState.currentState()) === null || _api$core$sharedState === void 0 ? void 0 : _api$core$sharedState.schema;
if (!selection || !schema || selection.empty) {
return null;
}
var slice = getSliceFromSelection(selection);
var content = slice.content;
var fragment = [];
content.forEach(function (node) {
fragment.push((0, _editorJsonTransformer.nodeToJSON)(node));
});
return fragment;
}
};
};
var getSelectionLocalIds = exports.getSelectionLocalIds = function getSelectionLocalIds(api) {
return function () {
var _api$selection$shared2, _selection;
var selection = api === null || api === void 0 || (_api$selection$shared2 = api.selection.sharedState) === null || _api$selection$shared2 === void 0 || (_api$selection$shared2 = _api$selection$shared2.currentState()) === null || _api$selection$shared2 === void 0 ? void 0 : _api$selection$shared2.selection;
if ((_selection = selection) !== null && _selection !== void 0 && _selection.empty) {
// If we have an empty selection the current state might not be correct
// We have a hack here to retrieve the current selection - but not dispatch a transaction
api === null || api === void 0 || api.core.actions.execute(function (_ref) {
var tr = _ref.tr;
selection = tr.selection;
return null;
});
}
if ((0, _platformFeatureFlags.fg)('platform_editor_renderer_selection_context')) {
return (0, _selection2.getLocalIdsFromSelection)(selection);
} else {
if (!selection) {
return null;
}
if (selection instanceof _state.NodeSelection) {
return [selection.node.attrs.localId];
} else if (selection.empty) {
return [selection.$from.parent.attrs.localId];
}
var content = getSliceFromSelection(selection).content;
var ids = [];
content.forEach(function (node) {
var _node$attrs;
var localId = (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.localId;
if (localId) {
ids.push(localId);
}
});
return ids;
}
};
};