medium-draft
Version:
A medium like rich text editor built upon draft-js with an emphasis on eliminating mouse usage by adding relevant keyboard shortcuts
58 lines (55 loc) • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
/*
Returns the `boundingClientRect` of the passed selection.
*/
var getSelectionRect = exports.getSelectionRect = function getSelectionRect(selected) {
var _rect = selected.getRangeAt(0).getBoundingClientRect();
// selected.getRangeAt(0).getBoundingClientRect()
var rect = _rect && _rect.top ? _rect : selected.getRangeAt(0).getClientRects()[0];
if (!rect) {
if (selected.anchorNode && selected.anchorNode.getBoundingClientRect) {
rect = selected.anchorNode.getBoundingClientRect();
rect.isEmptyline = true;
} else {
return null;
}
}
return rect;
};
/*
Returns the native selection node.
*/
var getSelection = exports.getSelection = function getSelection(root) {
var t = null;
if (root.getSelection) {
t = root.getSelection();
} else if (root.document.getSelection) {
t = root.document.getSelection();
} else if (root.document.selection) {
t = root.document.selection.createRange().text;
}
return t;
};
/*
Recursively finds the DOM Element of the block where the cursor is currently present.
If not found, returns null.
*/
var getSelectedBlockNode = exports.getSelectedBlockNode = function getSelectedBlockNode(root) {
var selection = root.getSelection();
if (selection.rangeCount === 0) {
return null;
}
var node = selection.getRangeAt(0).startContainer;
// console.log(node);
do {
if (node.getAttribute && node.getAttribute('data-block') === 'true') {
return node;
}
node = node.parentNode;
// console.log(node);
} while (node !== null);
return null;
};