@kaitify/core 0.0.1-beta.25 → 0.0.1-beta.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/kaitify-core.es.js +132 -86
- package/lib/kaitify-core.umd.js +1 -1
- package/lib/model/Editor.d.ts +4 -0
- package/package.json +1 -1
package/lib/kaitify-core.es.js
CHANGED
|
@@ -3240,12 +3240,10 @@ const onComposition = async function(e) {
|
|
|
3240
3240
|
const parentElement = element2.parentElement;
|
|
3241
3241
|
const parentNode = this.findNode(parentElement);
|
|
3242
3242
|
if (parentNode.isText() && parentNode.textContent != element2.textContent) {
|
|
3243
|
-
const textContent = parentNode.textContent || "";
|
|
3244
3243
|
parentNode.textContent = element2.textContent || "";
|
|
3245
3244
|
if (this.isSelectionInTargetNode(parentNode)) {
|
|
3246
3245
|
updateSelection$1.apply(this);
|
|
3247
3246
|
}
|
|
3248
|
-
element2.textContent = textContent;
|
|
3249
3247
|
await this.updateView();
|
|
3250
3248
|
} else if (!parentNode.isText()) {
|
|
3251
3249
|
const index = Array.from(parentElement.childNodes).findIndex((item) => item === element2);
|
|
@@ -3311,30 +3309,6 @@ const onCut = function(e) {
|
|
|
3311
3309
|
event2.preventDefault();
|
|
3312
3310
|
}
|
|
3313
3311
|
};
|
|
3314
|
-
const isLegalDom = (editor, dom) => {
|
|
3315
|
-
let legal = true;
|
|
3316
|
-
if (dom.nodeType == 3) {
|
|
3317
|
-
if (dom.parentNode && dom.parentNode.childNodes.length == 1) {
|
|
3318
|
-
try {
|
|
3319
|
-
const node = editor.findNode(dom.parentNode);
|
|
3320
|
-
if (!node.isText()) {
|
|
3321
|
-
legal = false;
|
|
3322
|
-
}
|
|
3323
|
-
} catch (error2) {
|
|
3324
|
-
legal = false;
|
|
3325
|
-
}
|
|
3326
|
-
} else {
|
|
3327
|
-
legal = false;
|
|
3328
|
-
}
|
|
3329
|
-
} else if (dom.nodeType == 1) {
|
|
3330
|
-
try {
|
|
3331
|
-
editor.findNode(dom);
|
|
3332
|
-
} catch (error2) {
|
|
3333
|
-
legal = false;
|
|
3334
|
-
}
|
|
3335
|
-
}
|
|
3336
|
-
return legal;
|
|
3337
|
-
};
|
|
3338
3312
|
const removeDomObserve = (editor) => {
|
|
3339
3313
|
if (editor.domObserver) {
|
|
3340
3314
|
editor.domObserver.disconnect();
|
|
@@ -3351,80 +3325,107 @@ const setDomObserve = (editor) => {
|
|
|
3351
3325
|
if (editor.isComposition) {
|
|
3352
3326
|
return;
|
|
3353
3327
|
}
|
|
3354
|
-
|
|
3355
|
-
const illegalDoms = [];
|
|
3328
|
+
const updateRecords = [];
|
|
3356
3329
|
for (let i = 0; i < mutationList.length; i++) {
|
|
3357
3330
|
const mutationRecord = mutationList[i];
|
|
3358
3331
|
if (mutationRecord.type == "characterData") {
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
} else if (!parentNode.isText()) {
|
|
3372
|
-
const index = Array.from(parentElement.childNodes).findIndex((item) => item === mutationRecord.target);
|
|
3373
|
-
const node = editor.domParseNode(mutationRecord.target);
|
|
3374
|
-
parentNode.children.splice(index, 0, node);
|
|
3375
|
-
node.parent = parentNode;
|
|
3376
|
-
illegalDoms.push(mutationRecord.target);
|
|
3377
|
-
if (editor.selection.focused()) {
|
|
3378
|
-
editor.setSelectionAfter(node, "all");
|
|
3379
|
-
}
|
|
3380
|
-
hasUpdate = true;
|
|
3332
|
+
if (!updateRecords.find((item) => item.type === "update" && item.elm === mutationRecord.target)) {
|
|
3333
|
+
updateRecords.push({
|
|
3334
|
+
type: "update",
|
|
3335
|
+
elm: mutationRecord.target
|
|
3336
|
+
});
|
|
3337
|
+
}
|
|
3338
|
+
} else if (mutationRecord.type == "attributes") {
|
|
3339
|
+
if (mutationRecord.target != editor.$el && !updateRecords.find((item) => item.type === "update" && item.elm === mutationRecord.target)) {
|
|
3340
|
+
updateRecords.push({
|
|
3341
|
+
type: "update",
|
|
3342
|
+
elm: mutationRecord.target
|
|
3343
|
+
});
|
|
3381
3344
|
}
|
|
3382
3345
|
} else if (mutationRecord.type == "childList") {
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3346
|
+
if (mutationRecord.addedNodes.length > 0) {
|
|
3347
|
+
mutationRecord.addedNodes.forEach((addNode) => {
|
|
3348
|
+
if (!updateRecords.find((item) => item.type === "add" && item.elm === addNode)) {
|
|
3349
|
+
updateRecords.push({
|
|
3350
|
+
type: "add",
|
|
3351
|
+
elm: addNode
|
|
3352
|
+
});
|
|
3353
|
+
}
|
|
3354
|
+
});
|
|
3355
|
+
}
|
|
3356
|
+
if (mutationRecord.removedNodes.length > 0) {
|
|
3357
|
+
mutationRecord.removedNodes.forEach((removedNode) => {
|
|
3358
|
+
const recordIndex = updateRecords.findIndex((item) => item.type === "add" && item.elm === removedNode);
|
|
3359
|
+
if (recordIndex > -1) {
|
|
3360
|
+
updateRecords.splice(recordIndex, 1);
|
|
3361
|
+
} else if (!updateRecords.find((item) => item.type === "remove" && item.elm === removedNode)) {
|
|
3362
|
+
updateRecords.push({
|
|
3363
|
+
type: "remove",
|
|
3364
|
+
elm: removedNode
|
|
3365
|
+
});
|
|
3366
|
+
}
|
|
3367
|
+
});
|
|
3368
|
+
}
|
|
3369
|
+
}
|
|
3370
|
+
}
|
|
3371
|
+
if (updateRecords.length > 0) {
|
|
3372
|
+
updateRecords.forEach((record) => {
|
|
3373
|
+
if (record.type === "update") {
|
|
3374
|
+
const elm = record.elm.nodeType === 3 ? record.elm.parentNode : record.elm;
|
|
3375
|
+
const nodeKey = elm.getAttribute(NODE_MARK);
|
|
3376
|
+
const node = nodeKey ? KNode.searchByKey(nodeKey, editor.stackNodes) : null;
|
|
3377
|
+
if (node) {
|
|
3378
|
+
const newNode2 = editor.domParseNode(elm);
|
|
3379
|
+
editor.addNodeAfter(newNode2, node);
|
|
3380
|
+
if (editor.isSelectionInTargetNode(node, "start")) {
|
|
3381
|
+
editor.setSelectionAfter(newNode2, "start");
|
|
3382
|
+
}
|
|
3383
|
+
if (editor.isSelectionInTargetNode(node, "end")) {
|
|
3384
|
+
editor.setSelectionAfter(newNode2, "end");
|
|
3385
|
+
}
|
|
3386
|
+
const index = (node.parent ? node.parent.children : editor.stackNodes).findIndex((item) => item.isEqual(node));
|
|
3387
|
+
(node.parent ? node.parent.children : editor.stackNodes).splice(index, 1);
|
|
3388
|
+
}
|
|
3389
|
+
} else if (record.type === "add") {
|
|
3390
|
+
if (updateRecords.some((item) => item.type === "update" && (item.elm.nodeType === 3 ? item.elm.parentNode : item.elm).contains(record.elm))) {
|
|
3391
|
+
return;
|
|
3392
|
+
}
|
|
3393
|
+
const parentElement = record.elm.parentNode;
|
|
3394
|
+
const index = Array.from(parentElement.childNodes).findIndex((item) => item === record.elm);
|
|
3395
|
+
const node = editor.domParseNode(record.elm);
|
|
3386
3396
|
if (parentElement === editor.$el) {
|
|
3387
|
-
|
|
3388
|
-
const index = Array.from(parentElement.childNodes).findIndex((item) => item === el);
|
|
3389
|
-
const node = editor.domParseNode(el);
|
|
3390
|
-
editor.stackNodes.splice(index, 0, node);
|
|
3391
|
-
illegalDoms.push(el);
|
|
3392
|
-
if (editor.selection.focused()) {
|
|
3393
|
-
editor.setSelectionAfter(node, "all");
|
|
3394
|
-
}
|
|
3395
|
-
hasUpdate = true;
|
|
3396
|
-
});
|
|
3397
|
+
editor.stackNodes.splice(index, 0, node);
|
|
3397
3398
|
} else {
|
|
3398
|
-
const
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3399
|
+
const nodeKey = parentElement.getAttribute(NODE_MARK);
|
|
3400
|
+
const parentNode = nodeKey ? KNode.searchByKey(nodeKey, editor.stackNodes) : null;
|
|
3401
|
+
if (parentNode) editor.addNode(node, parentNode, index);
|
|
3402
|
+
}
|
|
3403
|
+
parentElement.removeChild(record.elm);
|
|
3404
|
+
} else if (record.type === "remove") {
|
|
3405
|
+
if (updateRecords.some((item) => item.type === "update" && (item.elm.nodeType === 3 ? item.elm.parentNode : item.elm).contains(record.elm))) {
|
|
3406
|
+
return;
|
|
3407
|
+
}
|
|
3408
|
+
const elm = record.elm.nodeType === 3 ? record.elm.parentNode : record.elm;
|
|
3409
|
+
const nodeKey = elm.getAttribute(NODE_MARK);
|
|
3410
|
+
const node = nodeKey ? KNode.searchByKey(nodeKey, editor.stackNodes) : null;
|
|
3411
|
+
if (node) {
|
|
3412
|
+
const startInNode = editor.isSelectionInTargetNode(node, "start");
|
|
3413
|
+
const endInNode = editor.isSelectionInTargetNode(node, "end");
|
|
3414
|
+
node.toEmpty();
|
|
3415
|
+
if (startInNode) {
|
|
3416
|
+
editor.updateSelectionRecently("start");
|
|
3417
|
+
}
|
|
3418
|
+
if (endInNode) {
|
|
3419
|
+
editor.updateSelectionRecently("end");
|
|
3420
|
+
}
|
|
3415
3421
|
}
|
|
3416
3422
|
}
|
|
3417
|
-
}
|
|
3418
|
-
}
|
|
3419
|
-
if (hasUpdate) {
|
|
3420
|
-
illegalDoms.forEach((item) => {
|
|
3421
|
-
item.remove();
|
|
3422
3423
|
});
|
|
3423
3424
|
editor.updateView();
|
|
3424
3425
|
}
|
|
3425
3426
|
});
|
|
3426
3427
|
editor.domObserver.observe(editor.$el, {
|
|
3427
|
-
attributes:
|
|
3428
|
+
attributes: true,
|
|
3428
3429
|
characterData: true,
|
|
3429
3430
|
characterDataOldValue: true,
|
|
3430
3431
|
childList: true,
|
|
@@ -38320,6 +38321,51 @@ class Editor {
|
|
|
38320
38321
|
}
|
|
38321
38322
|
return `<style>${styles2}</style><div class="kaitify">${this.$el.innerHTML}</div>`;
|
|
38322
38323
|
}
|
|
38324
|
+
/**
|
|
38325
|
+
* 判断光标是否完全在可视范围内
|
|
38326
|
+
*/
|
|
38327
|
+
isSelectionInView() {
|
|
38328
|
+
if (this.selection.focused()) {
|
|
38329
|
+
const focusDom = this.findDom(this.selection.end.node);
|
|
38330
|
+
const isInView = (scrollElement) => {
|
|
38331
|
+
const scrollHeight = element.getScrollHeight(scrollElement);
|
|
38332
|
+
const scrollWidth = element.getScrollWidth(scrollElement);
|
|
38333
|
+
if (scrollElement.clientHeight < scrollHeight || scrollElement.clientWidth < scrollWidth) {
|
|
38334
|
+
const selection = window.getSelection();
|
|
38335
|
+
const range = selection.getRangeAt(0);
|
|
38336
|
+
const rects = range.getClientRects();
|
|
38337
|
+
let target = range;
|
|
38338
|
+
if (rects.length == 0) {
|
|
38339
|
+
target = focusDom;
|
|
38340
|
+
}
|
|
38341
|
+
const childRect = target.getBoundingClientRect();
|
|
38342
|
+
const parentRect = scrollElement.getBoundingClientRect();
|
|
38343
|
+
if (scrollElement.clientHeight < scrollHeight) {
|
|
38344
|
+
if (childRect.top < parentRect.top || childRect.bottom > parentRect.bottom) {
|
|
38345
|
+
return false;
|
|
38346
|
+
}
|
|
38347
|
+
}
|
|
38348
|
+
if (scrollElement.clientWidth < scrollWidth) {
|
|
38349
|
+
if (childRect.left < parentRect.left || childRect.right > parentRect.right) {
|
|
38350
|
+
return false;
|
|
38351
|
+
}
|
|
38352
|
+
}
|
|
38353
|
+
}
|
|
38354
|
+
return true;
|
|
38355
|
+
};
|
|
38356
|
+
let inView = true;
|
|
38357
|
+
let dom = focusDom;
|
|
38358
|
+
while (element.isElement(dom) && dom != document.documentElement) {
|
|
38359
|
+
if (!isInView(dom)) {
|
|
38360
|
+
inView = false;
|
|
38361
|
+
break;
|
|
38362
|
+
}
|
|
38363
|
+
dom = dom.parentNode;
|
|
38364
|
+
}
|
|
38365
|
+
return inView;
|
|
38366
|
+
}
|
|
38367
|
+
return true;
|
|
38368
|
+
}
|
|
38323
38369
|
/**
|
|
38324
38370
|
* 配置编辑器,返回创建的编辑器
|
|
38325
38371
|
*/
|