mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-02 13:10:28 +08:00
This commit is contained in:
parent
ef947c0259
commit
e09dbb3535
@ -241,7 +241,17 @@ export class Toolbar {
|
|||||||
this.range = setLastNodeRange(getContenteditableElement(nodeElement), this.range, false);
|
this.range = setLastNodeRange(getContenteditableElement(nodeElement), this.range, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
const rangeTypes = this.getCurrentType(this.range);
|
let rangeTypes: string[] = [];
|
||||||
|
this.range.cloneContents().childNodes.forEach((item: HTMLElement) => {
|
||||||
|
if (item.nodeType !== 3) {
|
||||||
|
rangeTypes = rangeTypes.concat((item.getAttribute("data-type") || "").split(" "));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (this.range.startContainer.nodeType === 3 && this.range.startContainer.parentElement.tagName === "SPAN" &&
|
||||||
|
this.range.startContainer.isSameNode(this.range.endContainer) &&
|
||||||
|
this.range.startOffset > -1 && this.range.endOffset <= this.range.startContainer.textContent.length) {
|
||||||
|
rangeTypes = rangeTypes.concat((this.range.startContainer.parentElement.getAttribute("data-type") || "").split(" "));
|
||||||
|
}
|
||||||
if (rangeTypes.length === 1) {
|
if (rangeTypes.length === 1) {
|
||||||
// https://github.com/siyuan-note/siyuan/issues/6501
|
// https://github.com/siyuan-note/siyuan/issues/6501
|
||||||
// https://github.com/siyuan-note/siyuan/issues/12877
|
// https://github.com/siyuan-note/siyuan/issues/12877
|
||||||
@ -305,10 +315,13 @@ export class Toolbar {
|
|||||||
contents = this.range.extractContents();
|
contents = this.range.extractContents();
|
||||||
}
|
}
|
||||||
this.mergeNode(contents.childNodes);
|
this.mergeNode(contents.childNodes);
|
||||||
contents.childNodes.forEach(item => {
|
contents.childNodes.forEach((item: HTMLElement) => {
|
||||||
if (item.nodeType === 3 && item.textContent === Constants.ZWSP) {
|
if (item.nodeType === 3 && item.textContent === Constants.ZWSP) {
|
||||||
item.remove();
|
item.remove();
|
||||||
}
|
}
|
||||||
|
if (item.nodeType === 1 && item.textContent === "" && item.tagName === "SPAN") {
|
||||||
|
item.remove();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
if (this.range.startContainer.nodeType !== 3) {
|
if (this.range.startContainer.nodeType !== 3) {
|
||||||
let emptyNode: Element = this.range.startContainer.childNodes[this.range.startOffset] as HTMLElement;
|
let emptyNode: Element = this.range.startContainer.childNodes[this.range.startOffset] as HTMLElement;
|
||||||
@ -466,40 +479,23 @@ export class Toolbar {
|
|||||||
}
|
}
|
||||||
contents.childNodes.forEach((item: HTMLElement) => {
|
contents.childNodes.forEach((item: HTMLElement) => {
|
||||||
let removeText = "";
|
let removeText = "";
|
||||||
if (item.nodeType === 3) {
|
if (item.nodeType === 3 && item.textContent) {
|
||||||
if (
|
// https://github.com/siyuan-note/siyuan/issues/14204
|
||||||
// 图片会有零宽空格,但图片不进行处理 https://github.com/siyuan-note/siyuan/issues/12840
|
while (item.textContent.endsWith("\n")) {
|
||||||
item.textContent !== Constants.ZWSP ||
|
item.textContent = item.textContent.substring(0, item.textContent.length - 1);
|
||||||
// tag 会有零宽空格 https://github.com/siyuan-note/siyuan/issues/12922
|
removeText += "\n";
|
||||||
(item.textContent === Constants.ZWSP && !rangeTypes.includes("img"))) {
|
|
||||||
// TODO ZWSP spin 后会在行内元素外 https://github.com/siyuan-note/siyuan/issues/13871
|
|
||||||
// if (item.textContent.startsWith(Constants.ZWSP) &&
|
|
||||||
// // https://github.com/siyuan-note/siyuan/issues/14639
|
|
||||||
// item.textContent.length > 1) {
|
|
||||||
// newNodes.push(document.createTextNode(Constants.ZWSP));
|
|
||||||
// item.textContent = item.textContent.substring(1);
|
|
||||||
// }
|
|
||||||
if (item.textContent) {
|
|
||||||
// https://github.com/siyuan-note/siyuan/issues/14204
|
|
||||||
while (item.textContent.endsWith("\n")) {
|
|
||||||
item.textContent = item.textContent.substring(0, item.textContent.length - 1);
|
|
||||||
removeText += "\n";
|
|
||||||
}
|
|
||||||
const inlineElement = document.createElement("span");
|
|
||||||
inlineElement.setAttribute("data-type", type);
|
|
||||||
inlineElement.textContent = item.textContent;
|
|
||||||
setFontStyle(inlineElement, textObj);
|
|
||||||
|
|
||||||
if (type === "text" && !inlineElement.getAttribute("style")) {
|
|
||||||
newNodes.push(item);
|
|
||||||
} else {
|
|
||||||
newNodes.push(inlineElement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
newNodes.push(item);
|
|
||||||
}
|
}
|
||||||
} else {
|
const inlineElement = document.createElement("span");
|
||||||
|
inlineElement.setAttribute("data-type", type);
|
||||||
|
inlineElement.textContent = item.textContent;
|
||||||
|
setFontStyle(inlineElement, textObj);
|
||||||
|
|
||||||
|
if (type === "text" && !inlineElement.getAttribute("style")) {
|
||||||
|
newNodes.push(item);
|
||||||
|
} else {
|
||||||
|
newNodes.push(inlineElement);
|
||||||
|
}
|
||||||
|
} else if (item.nodeType === 1) {
|
||||||
let types = (item.getAttribute("data-type") || "").split(" ");
|
let types = (item.getAttribute("data-type") || "").split(" ");
|
||||||
for (let i = 0; i < types.length; i++) {
|
for (let i = 0; i < types.length; i++) {
|
||||||
// "backslash", "virtual-block-ref", "search-mark" 只能单独存在
|
// "backslash", "virtual-block-ref", "search-mark" 只能单独存在
|
||||||
@ -574,7 +570,7 @@ export class Toolbar {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
types = [...new Set(types)];
|
types = [...new Set(types)];
|
||||||
if (item.tagName !== "BR" && item.tagName !== "IMG") {
|
if (item.tagName !== "BR" && item.tagName !== "IMG" && !types.includes("img")) {
|
||||||
item.setAttribute("data-type", types.join(" "));
|
item.setAttribute("data-type", types.join(" "));
|
||||||
setFontStyle(item, textObj);
|
setFontStyle(item, textObj);
|
||||||
if (types.includes("text") && !item.getAttribute("style")) {
|
if (types.includes("text") && !item.getAttribute("style")) {
|
||||||
@ -657,7 +653,9 @@ export class Toolbar {
|
|||||||
} else if (i === newNodes.length) {
|
} else if (i === newNodes.length) {
|
||||||
endContainer = currentNode;
|
endContainer = currentNode;
|
||||||
endOffset = previousElement.textContent.length;
|
endOffset = previousElement.textContent.length;
|
||||||
if (startContainer.isSameNode(previousElement)) {
|
if (!startContainer) {
|
||||||
|
startContainer = currentNode;
|
||||||
|
} else if (startContainer.isSameNode(previousElement)) {
|
||||||
startContainer = currentNode;
|
startContainer = currentNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -745,20 +743,21 @@ export class Toolbar {
|
|||||||
}
|
}
|
||||||
focusByRange(this.range);
|
focusByRange(this.range);
|
||||||
|
|
||||||
|
const showMenuElement = newNodes[0] as HTMLElement;
|
||||||
if (type === "inline-math") {
|
if (type === "inline-math") {
|
||||||
mathRender(nodeElement);
|
mathRender(nodeElement);
|
||||||
if (selectText === "") {
|
if (selectText === "" && showMenuElement.getAttribute("data-type") === "inline-math") {
|
||||||
protyle.toolbar.showRender(protyle, newNodes[0] as HTMLElement, undefined, html);
|
protyle.toolbar.showRender(protyle, showMenuElement, undefined, html);
|
||||||
}
|
}
|
||||||
} else if (type === "inline-memo") {
|
} else if (type === "inline-memo") {
|
||||||
const memoElement = newNodes[0] as HTMLElement;
|
if (!showMenuElement.getAttribute("data-inline-memo-content") &&
|
||||||
if (!memoElement.getAttribute("data-inline-memo-content")) {
|
showMenuElement.getAttribute("data-type") === "inline-memo") {
|
||||||
protyle.toolbar.showRender(protyle, memoElement, newNodes as Element[], html);
|
protyle.toolbar.showRender(protyle, showMenuElement, newNodes as Element[], html);
|
||||||
}
|
}
|
||||||
} else if (type === "a") {
|
} else if (type === "a") {
|
||||||
const aElement = newNodes[0] as HTMLElement;
|
if (showMenuElement.getAttribute("data-type") === "a" &&
|
||||||
if (aElement.textContent.replace(Constants.ZWSP, "") === "" || !aElement.getAttribute("data-href")) {
|
(showMenuElement.textContent.replace(Constants.ZWSP, "") === "" || !showMenuElement.getAttribute("data-href"))) {
|
||||||
linkMenu(protyle, aElement, aElement.getAttribute("data-href") ? true : false);
|
linkMenu(protyle, showMenuElement, showMenuElement.getAttribute("data-href") ? true : false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user