to:sync
This commit is contained in:
parent
795da149a7
commit
95e072ab18
@ -20,6 +20,9 @@ define(function (require, exports, module) {
|
|||||||
for (let i = 0; i < curS.rangeCount; i++) {
|
for (let i = 0; i < curS.rangeCount; i++) {
|
||||||
let curRange = curS.getRangeAt(i);
|
let curRange = curS.getRangeAt(i);
|
||||||
let curCommonEle = curRange.commonAncestorContainer;
|
let curCommonEle = curRange.commonAncestorContainer;
|
||||||
|
let curStartContainerEle = curRange.startContainer;
|
||||||
|
let curEndContainerEle = curRange.endContainer;
|
||||||
|
let taskMap = new Map;
|
||||||
let start = curRange.startOffset;
|
let start = curRange.startOffset;
|
||||||
let end = curRange.endOffset;
|
let end = curRange.endOffset;
|
||||||
if (start > end) {
|
if (start > end) {
|
||||||
@ -27,93 +30,97 @@ define(function (require, exports, module) {
|
|||||||
start = end;
|
start = end;
|
||||||
end = tmpS;
|
end = tmpS;
|
||||||
}
|
}
|
||||||
if (start === end) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// //一个元素节点,例如 <p> 和 <div>。
|
let tmpStart = getSpanContent(curStartContainerEle);
|
||||||
// let curComIsSpan = curCommonEle.nodeType === 1 && curCommonEle.nodeName === "SPAN";
|
let tmpEnd = getSpanContent(curEndContainerEle);
|
||||||
// let curComParentIsP = curCommonEle.parentNode.nodeType === 1 && curCommonEle.parentNode.nodeName === "P";
|
if (tmpStart.getAttribute("id") === tmpEnd.getAttribute("id")) {
|
||||||
// let curComParentIsDiv = curCommonEle.parentNode.nodeType === 1 && curCommonEle.parentNode.nodeName === "DIV";
|
taskMap.set(tmpStart.getAttribute("id"), tmpStart)
|
||||||
// let curComParentIsSpan = curCommonEle.parentNode.nodeType === 1 && (curCommonEle.parentNode.nodeName === 'SPAN"' || curCommonEle.parentNode.nodeName === 'SPAN');
|
} else {
|
||||||
// console.log(" 当前选区信息 : ", curRange,
|
let tmp = tmpStart;
|
||||||
// "\ncurCommonEle: ", curCommonEle,
|
if (parseInt(tmpStart.getAttribute("data-order")) > parseInt(tmpEnd.getAttribute("data-order"))) {
|
||||||
// "\ncurCommonEleParent: ", curCommonEle.parentNode, curCommonEle.parentNode.nodeType, curCommonEle.parentNode.nodeName,
|
tmpStart = tmpEnd;
|
||||||
// "\ncurCommonEleParentParent: ", curCommonEle.parentNode.parentNode, curCommonEle.parentNode.parentNode.nodeType, curCommonEle.parentNode.parentNode.nodeName,
|
tmpEnd = tmp;
|
||||||
//
|
}
|
||||||
// "\ncurComParentIsSpan: ", curComParentIsSpan,
|
taskMap.set(tmpStart.getAttribute("id"), tmpStart)
|
||||||
// "\ncurComParentIsP : ", curComParentIsP,
|
while (true) {
|
||||||
// "\ncurComParentIsDiv", curComParentIsDiv,
|
let nextTask = tmpStart.nextSibling;
|
||||||
// "\ncurComIsSpan", curComIsSpan,
|
taskMap.set(nextTask.getAttribute("id"), nextTask);
|
||||||
//
|
if (nextTask.getAttribute("id") === tmpEnd.getAttribute("id")) {
|
||||||
// "\nstyleK: ", styleK,
|
break
|
||||||
// "\nstyleV: ", styleV,
|
|
||||||
// "\ntart: ", start,
|
|
||||||
// "\nend: ", end);
|
|
||||||
|
|
||||||
let curSpanContent = curCommonEle;
|
|
||||||
if (curSpanContent.nodeName !== "SPAN" || curSpanContent.getAttribute("data-flag") !== "span_content") {
|
|
||||||
curSpanContent = curSpanContent.parentElement.closest("span[data-flag='span_content']");
|
|
||||||
}
|
|
||||||
console.log("curSpanContent : ", curSpanContent, curCommonEle);
|
|
||||||
if (curSpanContent.childNodes.length <= 0) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
let firstItem = curSpanContent.childNodes[0];
|
|
||||||
console.log("curSpanContent : ", curSpanContent, curSpanContent.childNodes, firstItem.nodeName, firstItem.nodeType);
|
|
||||||
let isEmptyStyle = curSpanContent.childNodes.length === 1 && firstItem.nodeName === "#text" && firstItem.nodeType === 3;
|
|
||||||
if (isEmptyStyle) {
|
|
||||||
let copySpan = curSpanContent.cloneNode();
|
|
||||||
copySpan.innerHTML = "";
|
|
||||||
for (let j = 0; j < curSpanContent.innerText.length; j++) {
|
|
||||||
// console.log(curStartP.innerText.charAt(j))
|
|
||||||
if (j >= start && j < end) {
|
|
||||||
let tmpSpan = document.createElement("span");
|
|
||||||
tmpSpan.innerText = curSpanContent.innerText.charAt(j);
|
|
||||||
applySpanStyleKV(tmpSpan, styleK, styleV);
|
|
||||||
copySpan.append(tmpSpan);
|
|
||||||
} else {
|
|
||||||
let tmpSpan = document.createElement("span");
|
|
||||||
tmpSpan.innerText = curSpanContent.innerText.charAt(j);
|
|
||||||
copySpan.append(tmpSpan);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (taskMap.size === 1 && start === end) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
curSpanContent.innerHTML = copySpan.innerHTML;
|
for (let [key, curSpanContent] of taskMap) {
|
||||||
|
console.log("curSpanContent : ", curSpanContent, "\n", curCommonEle, "\nsize:", taskMap.size);
|
||||||
|
console.dir(taskMap)
|
||||||
|
console.dir(curS)
|
||||||
|
console.dir(curRange)
|
||||||
|
|
||||||
//光标保持
|
if (curSpanContent.childNodes === undefined || curSpanContent.childNodes === null || curSpanContent.childNodes.length <= 0) {
|
||||||
let s = window.getSelection();
|
continue
|
||||||
if (s.rangeCount > 0) s.removeAllRanges();
|
}
|
||||||
let newR = document.createRange();
|
let firstItem = curSpanContent.childNodes[0];
|
||||||
//重新获取元素
|
console.log("curSpanContent : ", curSpanContent, curSpanContent.childNodes, firstItem.nodeName, firstItem.nodeType);
|
||||||
let tmpP = curSpanContent;
|
let isEmptyStyle = curSpanContent.childNodes.length === 1 && firstItem.nodeName === "#text" && firstItem.nodeType === 3;
|
||||||
let childrenSize = tmpP.childNodes.length;
|
if (isEmptyStyle) {
|
||||||
console.log("debug007: ", tmpP, start, end, childrenSize)
|
let copySpan = curSpanContent.cloneNode();
|
||||||
newR.setStart(tmpP, start);
|
copySpan.innerHTML = "";
|
||||||
newR.setEnd(tmpP, end);
|
for (let j = 0; j < curSpanContent.innerText.length; j++) {
|
||||||
//区域 添加 到选区
|
// console.log(curStartP.innerText.charAt(j))
|
||||||
s.addRange(newR);
|
if (j >= start && j < end) {
|
||||||
} else {
|
let tmpSpan = document.createElement("span");
|
||||||
let total = 0, effectNum = 0;
|
tmpSpan.innerText = curSpanContent.innerText.charAt(j);
|
||||||
for (let j = 0; j < curSpanContent.childNodes.length; j++) {
|
|
||||||
let tmpSpan = curSpanContent.childNodes[j];
|
|
||||||
if (curS.containsNode(tmpSpan, true)) {
|
|
||||||
total++;
|
|
||||||
if (spanContainsStyleKV(tmpSpan, styleK, styleV)) {
|
|
||||||
console.dir(tmpSpan);
|
|
||||||
} else {
|
|
||||||
effectNum++;
|
|
||||||
applySpanStyleKV(tmpSpan, styleK, styleV);
|
applySpanStyleKV(tmpSpan, styleK, styleV);
|
||||||
|
copySpan.append(tmpSpan);
|
||||||
|
} else {
|
||||||
|
let tmpSpan = document.createElement("span");
|
||||||
|
tmpSpan.innerText = curSpanContent.innerText.charAt(j);
|
||||||
|
copySpan.append(tmpSpan);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//如果没有设置任何一个 则取消
|
curSpanContent.innerHTML = copySpan.innerHTML;
|
||||||
if (effectNum === 0) {
|
|
||||||
|
//光标保持. for 单行
|
||||||
|
if (taskMap.size === 1) {
|
||||||
|
let s = window.getSelection();
|
||||||
|
if (s.rangeCount > 0) s.removeAllRanges();
|
||||||
|
let newR = document.createRange();
|
||||||
|
//重新获取元素
|
||||||
|
let tmpP = curSpanContent;
|
||||||
|
let childrenSize = tmpP.childNodes.length;
|
||||||
|
console.log("debug007: ", tmpP, start, end, childrenSize)
|
||||||
|
newR.setStart(tmpP, start);
|
||||||
|
newR.setEnd(tmpP, end);
|
||||||
|
//区域 添加 到选区
|
||||||
|
s.addRange(newR);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let total = 0, effectNum = 0;
|
||||||
for (let j = 0; j < curSpanContent.childNodes.length; j++) {
|
for (let j = 0; j < curSpanContent.childNodes.length; j++) {
|
||||||
let tmpSpan = curSpanContent.childNodes[j];
|
let tmpSpan = curSpanContent.childNodes[j];
|
||||||
if (curS.containsNode(tmpSpan, true)) {
|
if (curS.containsNode(tmpSpan, true)) {
|
||||||
removeSpanStyleKV(tmpSpan, styleK, styleV);
|
total++;
|
||||||
|
if (spanContainsStyleKV(tmpSpan, styleK, styleV)) {
|
||||||
|
console.dir(tmpSpan);
|
||||||
|
} else {
|
||||||
|
effectNum++;
|
||||||
|
applySpanStyleKV(tmpSpan, styleK, styleV);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//如果没有设置任何一个 则取消
|
||||||
|
if (effectNum === 0) {
|
||||||
|
for (let j = 0; j < curSpanContent.childNodes.length; j++) {
|
||||||
|
let tmpSpan = curSpanContent.childNodes[j];
|
||||||
|
if (curS.containsNode(tmpSpan, true)) {
|
||||||
|
removeSpanStyleKV(tmpSpan, styleK, styleV);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,6 +159,15 @@ define(function (require, exports, module) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function getSpanContent(item) {
|
||||||
|
let tmp = item;
|
||||||
|
if (item.nodeName !== "SPAN" || item.getAttribute("data-flag") !== "span_content") {
|
||||||
|
tmp = item.parentElement.closest("span[data-flag='span_content']");
|
||||||
|
}
|
||||||
|
return tmp
|
||||||
|
}
|
||||||
|
|
||||||
//导出
|
//导出
|
||||||
exports.handle = handle;
|
exports.handle = handle;
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user