update algh extract fragm

This commit is contained in:
Nikita (@Filolace) 2018-01-11 20:05:25 +03:00
commit fe5c99f620
4 changed files with 79 additions and 26 deletions

View file

@ -698,6 +698,30 @@ var Dom = function () {
return _this.isNodeEmpty(leaf);
});
}
/**
* Search for deepest node which responds regex
*
* @param {Element} node
* @return {Node}
*/
}, {
key: 'getBlockContainer',
value: function getBlockContainer(node) {
while (node) {
/**
* @regex tests node name for compliance 'div', 'h[1-6]', 'p'
*/
if (node.nodeType == 1 && /^(P|H[1-6]|DIV)$/i.test(node.nodeName)) {
return node;
}
node = node.parentNode;
}
}
}]);
return Dom;
@ -2440,23 +2464,24 @@ var Caret = function (_Module) {
key: 'extractFragmentFromCaretPosition',
value: function extractFragmentFromCaretPosition() {
var selection = _Selection2.default.get(),
range = new Range();
var selection = _Selection2.default.get();
var pluginsContent = this.Editor.BlockManager.currentBlock.pluginsContent,
if (selection.rangeCount) {
/**
* Second argument is true because we need to find last deepest text node
*/
lastNode = $.getDeepestNode(pluginsContent, true);
var selectRange = selection.getRangeAt(0),
blockElem = $.getBlockContainer(selectRange.endContainer);
range.setStart(selection.anchorNode, selection.getRangeAt(0).startOffset);
range.setEnd(lastNode, lastNode.length);
selectRange.deleteContents();
selection.removeAllRanges();
selection.addRange(range);
if (blockElem) {
return range.extractContents();
var range = selectRange.cloneRange(true);
range.selectNodeContents(blockElem);
range.setStart(selectRange.endContainer, selectRange.endOffset);
return range.extractContents();
}
}
}
}]);

File diff suppressed because one or more lines are too long

View file

@ -267,4 +267,28 @@ export default class Dom {
}
/**
* Search for deepest node which responds regex
*
* @param {Element} node
* @return {Node}
*/
static getBlockContainer(node) {
while (node) {
/**
* @regex tests node name for compliance 'div', 'h[1-6]', 'p'
*/
if (node.nodeType == 1 && /^(P|H[1-6]|DIV)$/i.test(node.nodeName)) {
return node;
}
node = node.parentNode;
}
}
};

View file

@ -121,23 +121,27 @@ export default class Caret extends Module {
*/
extractFragmentFromCaretPosition() {
let selection = Selection.get(),
range = new Range();
let selection = Selection.get();
let pluginsContent = this.Editor.BlockManager.currentBlock.pluginsContent,
/**
* Second argument is true because we need to find last deepest text node
*/
lastNode = $.getDeepestNode(pluginsContent, true);
if (selection.rangeCount) {
range.setStart(selection.anchorNode, selection.getRangeAt(0).startOffset);
range.setEnd(lastNode, lastNode.length);
let selectRange = selection.getRangeAt(0),
blockElem = $.getBlockContainer(selectRange.endContainer);
selection.removeAllRanges();
selection.addRange(range);
selectRange.deleteContents();
return range.extractContents();
if (blockElem) {
let range = selectRange.cloneRange(true);
range.selectNodeContents(blockElem);
range.setStart(selectRange.endContainer, selectRange.endOffset);
return range.extractContents();
}
}
}
}
}