@atlaskit/renderer
Version:
Renderer component
63 lines (62 loc) • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getSelectionContext = void 0;
var _state = require("@atlaskit/editor-prosemirror/state");
var _selection = require("@atlaskit/editor-common/selection");
var _steps = require("../steps");
var getSelectionContext = exports.getSelectionContext = function getSelectionContext(_ref) {
var doc = _ref.doc,
schema = _ref.schema;
if (!doc || !schema) {
return null;
}
var selection = document.getSelection();
if (!selection || selection.type !== 'Range' || selection.rangeCount !== 1) {
return null;
}
var range = selection.getRangeAt(0);
if (range.collapsed) {
return null;
}
var startNode = (0, _steps.findParent)(range.startContainer);
var endNode = (0, _steps.findParent)(range.endContainer);
if (!startNode || !endNode) {
return null;
}
var startPos = (0, _steps.getStartPos)(startNode);
var endPos = (0, _steps.getStartPos)(endNode);
if (startPos === null || endPos === null) {
return null;
}
var pos = (0, _steps.getPosFromRange)(range);
if (!pos) {
return null;
}
var from = Math.min(pos.from, pos.to);
var to = Math.max(pos.from, pos.to);
if (from === to) {
return null;
}
var pmSelection;
try {
pmSelection = _state.TextSelection.create(doc, from, to);
} catch (_unused) {
return null;
}
var startIndex = from - startPos;
var endIndex = to - endPos;
if (startIndex < 0 || endIndex < 0) {
return null;
}
var selectionFragment = (0, _selection.getFragmentsFromSelection)(pmSelection);
var localIds = (0, _selection.getLocalIdsFromSelection)(pmSelection);
return {
localIds: localIds,
selectionFragment: selectionFragment,
selectionMarkdown: null,
startIndex: startIndex,
endIndex: endIndex
};
};