var sel = window.getSelection();
var range = document.createRange();
let currentNode = this.anchorNode;
let isStartNode = true;
let lastPosition = this.anchorOffset;
let lastNode = this.anchorNode;
while(currentNode && currentNode.nodeType === 3) {
// 这里将@之后的所有东西都替换
range.setStart(currentNode, 0);
const endOffset = currentNode.nodeValue.length;
range.setEnd(currentNode, isStartNode ? this.anchorOffset : endOffset);
let str = range.toString();
let nodeValue = currentNode.nodeValue;
if (/@/.test(str)) {
if (isStartNode) {
let prevValue = nodeValue.substring(0, this.anchorOffset);
this.anchorOffset = prevValue.lastIndexOf('@');
} else {
this.anchorOffset = nodeValue.lastIndexOf('@');
}
this.anchorNode = currentNode;
break;
} else {
currentNode = currentNode.previousSibling;
isStartNode = false;
}
}
range.setStart(this.anchorNode, this.anchorOffset);
range.setEnd(lastNode, lastPosition);
range.deleteContents();
var img = document.createElement('img');
img.ondragstart = 'return false';
img.src = dataurl;
img.alt = `@${item.text}`;
img.title = item.text;
img.style.cssText = 'vertical-align: middle';
img.setAttribute('atid', item.id);
range.insertNode(img);
range.setStart(img.nextSibling, 0);
range.setEnd(img.nextSibling, 0);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
myInputElem.focus();
Last active
March 20, 2019 09:24
-
-
Save lirongfei123/46398ce58fbdfa1d95c76720a45c9507 to your computer and use it in GitHub Desktop.
js #js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "dd".charCodeAr(0).toString(16) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typeof props.value == 'string' && props.value.trim() === '' |
var selection = window.getSelection();
if(selection.rangeCount === 0) return;
e.preventDefault();
const range = selection.getRangeAt(0);
const selectString = range.toString().replace(/^\s*|\s*$/, '');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment