mylomen-server/static/js/event/impl/MutationObserverImpl.js

127 lines
4.9 KiB
JavaScript
Raw Normal View History

2024-11-05 22:43:34 +08:00
"use strict";
/**
* 使用样式事件
*/
define(function (require, exports, module) {
function handle(mutationList) {
for (let mutation of mutationList) {
switch (mutation.type) {
case "childList":
addNewP(mutation);
break;
case "characterData":
updateText(mutation);
break;
2024-11-06 14:07:30 +08:00
case "attributes":
//暂时忽略属性变化
break
2024-11-05 22:43:34 +08:00
default: {
other(mutation);
}
}
}
}
function other(mutation) {
console.log("mutation : ", mutation,
// 观察的变动类型attributes、characterData或者childList
"\ntype", mutation.type,
//发生变动的DOM节点。
"\ntarget", mutation.target,
"\ntargetParent", mutation.target.parentElement, mutation.target.parentNode,
//新增的DOM节点。
"\naddedNodes", mutation.addedNodes,
//删除的DOM节点。
"\nremovedNodes", mutation.removedNodes,
//前一个同级节点如果没有则返回null。
"\npreviousSibling", mutation.previousSibling,
//下一个同级节点如果没有则返回null。
"\nnextSibling", mutation.nextSibling,
//发生变动的属性。如果设置了attributeFilter则只返回预先指定的属性。
"\nattributeName", mutation.attributeName,
//变动前的值。这个属性只对attribute和characterData变动有效如果发生childList变动则返回null。
"\noldValue", mutation.oldValue);
}
function addNewP(mutation) {
2024-11-07 00:17:43 +08:00
//div 下新增p元素
2024-11-05 22:43:34 +08:00
let target = mutation.target;
2024-11-06 14:17:32 +08:00
// target.clone()
2024-11-05 22:43:34 +08:00
if (target.nodeName === "DIV" && mutation.previousSibling !== null) {
2024-11-07 00:17:43 +08:00
let utils = require("../../common/utils");
let ctx = require("../../common/ctx");
if (ctx.getCurRowNo() <= 0) {
return
}
2024-11-06 14:17:32 +08:00
let newParagraph = mutation.previousSibling.nextSibling;
2024-11-05 22:43:34 +08:00
console.log(
2024-11-06 14:07:30 +08:00
"addNewP target: ", target,
2024-11-05 22:43:34 +08:00
"\nnodeType: ", target.nodeType,
"\nnodeName: ", target.nodeName,
"\ndata: ", target.value,
2024-11-07 00:17:43 +08:00
"\n next: ", newParagraph,
"\ncurRowNo: ", ctx.getCurRowNo()
2024-11-05 22:43:34 +08:00
)
2024-11-06 14:17:32 +08:00
if (newParagraph !== undefined && newParagraph !== null) {
newParagraph.id = utils.Uuid(ctx.usn, ctx.docType);
newParagraph.setAttribute("data-order", ctx.incrementNumThenReturn());
2024-11-06 14:07:30 +08:00
2024-11-06 14:17:32 +08:00
// let newParagraph = document.getElementById(targetP.id);
2024-11-06 14:07:30 +08:00
//前置 span
let preSpan = newParagraph.querySelector("span[data-flag='span_pre']");
if (preSpan === undefined || preSpan === null) {
preSpan = document.createElement("span");
preSpan.setAttribute("contenteditable", "false")
preSpan.setAttribute("data-flag", "span_pre")
2024-11-07 00:17:43 +08:00
// newParagraph.appendChild(preSpan);
//添加到元素首位
newParagraph.insertBefore(preSpan, newParagraph.firstChild);
2024-11-06 14:17:32 +08:00
} else {
2024-11-07 00:17:43 +08:00
console.log("newParagraph preSpan exist ", newParagraph, " ", preSpan)
2024-11-06 14:07:30 +08:00
}
//内置span
let spanContent = newParagraph.querySelector("span[data-flag='span_content']");
if (spanContent === undefined || spanContent === null) {
spanContent = document.createElement("span");
spanContent.append(document.createElement("br"));
spanContent.setAttribute("data-flag", "span_content")
newParagraph.appendChild(spanContent);
2024-11-06 14:17:32 +08:00
} else {
console.log("newParagraph preSpan spanContent ", newParagraph)
2024-11-06 14:07:30 +08:00
}
utils.GetSelection().setPosition(spanContent, 0);
spanContent.focus();
2024-11-05 22:43:34 +08:00
}
}
}
function updateText(mutation) {
let target = mutation.target;
if (target.nodeType === 3 && target.nodeName === "#text") {
// console.log(
// "target: ", target,
// "\nnodeType: ", target.nodeType,
// "\nnodeName: ", target.nodeName,
// "\ndata: ", target.value,
// "\nparentNode: ", target.parentNode,
// "\nparentNode_parentNode: ", target.parentNode.parentNode,
// )
// let curSpan = target.parentNode;
// let curP = target.parentNode.parentNode;
} else {
other(mutation);
}
}
//导出
exports.handle = handle;
});