2022-08-18 20:07:58 +08:00
|
|
|
import {fetchPost} from "../../util/fetch";
|
2023-10-17 11:37:05 +08:00
|
|
|
import {Constants} from "../../constants";
|
|
|
|
|
import {focusByRange, focusByWbr} from "../util/selection";
|
2022-08-18 20:07:58 +08:00
|
|
|
|
2022-08-18 23:44:45 +08:00
|
|
|
export const previewTemplate = (pathString: string, element: Element, parentId: string) => {
|
|
|
|
|
if (!pathString) {
|
2022-08-18 23:47:22 +08:00
|
|
|
element.innerHTML = "";
|
2022-08-18 23:44:45 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
fetchPost("/api/template/render", {
|
|
|
|
|
id: parentId,
|
2023-10-24 10:06:19 +08:00
|
|
|
path: pathString,
|
|
|
|
|
preview: true
|
2022-08-18 23:44:45 +08:00
|
|
|
}, (response) => {
|
|
|
|
|
element.innerHTML = `<div class="protyle-wysiwyg" style="padding: 8px">${response.data.content.replace(/contenteditable="true"/g, "")}</div>`;
|
2022-08-18 23:47:22 +08:00
|
|
|
});
|
|
|
|
|
};
|
2023-03-07 10:42:20 +08:00
|
|
|
|
|
|
|
|
const mergeElement = (a: Element, b: Element, after = true) => {
|
2023-06-09 10:43:59 +08:00
|
|
|
if (!a.getAttribute("data-type") || !b.getAttribute("data-type")) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-03-07 10:42:20 +08:00
|
|
|
a.setAttribute("data-type", a.getAttribute("data-type").replace("search-mark", "").trim());
|
|
|
|
|
b.setAttribute("data-type", b.getAttribute("data-type").replace("search-mark", "").trim());
|
2023-03-07 15:06:50 +08:00
|
|
|
const attributes = a.attributes;
|
|
|
|
|
let isMatch = true;
|
2023-03-07 10:42:20 +08:00
|
|
|
for (let i = 0; i < attributes.length; i++) {
|
|
|
|
|
if (b.getAttribute(attributes[i].name) !== attributes[i].value) {
|
2023-03-07 15:06:50 +08:00
|
|
|
isMatch = false;
|
2023-03-07 10:42:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isMatch) {
|
|
|
|
|
if (after) {
|
2023-03-07 15:06:50 +08:00
|
|
|
a.innerHTML = a.innerHTML + b.innerHTML;
|
2023-03-07 10:42:20 +08:00
|
|
|
} else {
|
2023-03-07 15:06:50 +08:00
|
|
|
a.innerHTML = b.innerHTML + a.innerHTML;
|
2023-03-07 10:42:20 +08:00
|
|
|
}
|
|
|
|
|
b.remove();
|
|
|
|
|
}
|
|
|
|
|
return isMatch;
|
2023-03-07 15:06:50 +08:00
|
|
|
};
|
2023-03-07 10:42:20 +08:00
|
|
|
|
|
|
|
|
export const removeSearchMark = (element: HTMLElement) => {
|
|
|
|
|
let previousElement = element.previousSibling as HTMLElement;
|
|
|
|
|
while (previousElement && previousElement.nodeType !== 3) {
|
|
|
|
|
if (!mergeElement(element, previousElement, false)) {
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
previousElement = element.previousSibling as HTMLElement;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let nextElement = element.nextSibling as HTMLElement;
|
|
|
|
|
while (nextElement && nextElement.nodeType !== 3) {
|
|
|
|
|
if (!mergeElement(element, nextElement)) {
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
nextElement = element.nextSibling as HTMLElement;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 17:04:49 +08:00
|
|
|
if ((element.getAttribute("data-type") || "").includes("search-mark")) {
|
2023-03-07 10:42:20 +08:00
|
|
|
element.setAttribute("data-type", element.getAttribute("data-type").replace("search-mark", "").trim());
|
|
|
|
|
}
|
2023-03-07 15:06:50 +08:00
|
|
|
};
|
2023-10-17 11:37:05 +08:00
|
|
|
|
|
|
|
|
export const removeInlineType = (linkElement: HTMLElement, range?: Range) => {
|
|
|
|
|
const types = linkElement.getAttribute("data-type").split(" ");
|
|
|
|
|
if (types.length === 1) {
|
|
|
|
|
const linkParentElement = linkElement.parentElement;
|
|
|
|
|
linkElement.outerHTML = linkElement.innerHTML.replace(Constants.ZWSP, "") + "<wbr>";
|
|
|
|
|
if (range) {
|
|
|
|
|
focusByWbr(linkParentElement, range);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
types.find((itemType, index) => {
|
|
|
|
|
if ("a" === itemType) {
|
|
|
|
|
types.splice(index, 1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
linkElement.setAttribute("data-type", types.join(" "));
|
|
|
|
|
linkElement.removeAttribute("data-href");
|
|
|
|
|
if (range) {
|
|
|
|
|
range.selectNodeContents(linkElement);
|
|
|
|
|
range.collapse(false);
|
|
|
|
|
focusByRange(range);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|