♻️ rm hasClosestByMatchTag

This commit is contained in:
Vanessa 2025-02-09 12:04:40 +08:00
parent 9885e6eb9b
commit 2408f8a0ba
15 changed files with 50 additions and 70 deletions

View file

@ -1,6 +1,6 @@
import {getAllEditor, getAllModels} from "../../layout/getAll";
import {isWindow} from "../../util/functions";
import {hasClosestBlock, hasClosestByClassName, hasClosestByMatchTag} from "../../protyle/util/hasClosest";
import {hasClosestBlock, hasClosestByClassName, hasClosestByTag} from "../../protyle/util/hasClosest";
import {getColIndex} from "../../protyle/util/table";
const getRightBlock = (element: HTMLElement, x: number, y: number) => {
@ -194,7 +194,7 @@ export const windowMouseMove = (event: MouseEvent & { target: HTMLElement }, mou
const target = event.target as Element;
const blockElement = hasClosestByClassName(target, "table");
if (blockElement && blockElement.style.cursor !== "col-resize" && !hasClosestByClassName(blockElement, "protyle-wysiwyg__embed")) {
const cellElement = (hasClosestByMatchTag(target, "TH") || hasClosestByMatchTag(target, "TD")) as HTMLTableCellElement;
const cellElement = (hasClosestByTag(target, "TH") || hasClosestByTag(target, "TD")) as HTMLTableCellElement;
if (cellElement) {
const tableElement = blockElement.querySelector("table");
const tableHeight = blockElement.querySelector("table").clientHeight;

View file

@ -1,5 +1,5 @@
import {hasClosestByMatchTag} from "../protyle/util/hasClosest";
import {fetchPost} from "../util/fetch";
import {hasClosestByTag} from "../protyle/util/hasClosest";
export const publish = {
element: undefined as Element,
@ -153,7 +153,7 @@ export const publish = {
.querySelectorAll("input")
.forEach(input => {
input.addEventListener("change", () => {
const li = hasClosestByMatchTag(input, "LI");
const li = hasClosestByTag(input, "LI");
if (li) {
const index = parseInt(li.dataset.index);
const name = input.dataset.name as keyof Config.IPublishAuthAccount;
@ -170,7 +170,7 @@ export const publish = {
.querySelectorAll('.block__icon[data-action="remove"]')
.forEach(remove => {
remove.addEventListener("click", () => {
const li = hasClosestByMatchTag(remove, "LI");
const li = hasClosestByTag(remove, "LI");
if (li) {
const index = parseInt(li.dataset.index);
window.siyuan.config.publish.auth.accounts.splice(index, 1);

View file

@ -1,8 +1,7 @@
import {
hasClosestBlock,
hasClosestByAttribute,
hasClosestByClassName,
hasClosestByMatchTag,
hasClosestByClassName, hasClosestByTag,
hasTopClosestByClassName
} from "../protyle/util/hasClosest";
import {MenuItem} from "./Menu";
@ -725,7 +724,7 @@ export const contentMenu = (protyle: IProtyle, nodeElement: Element) => {
}).element);
} else {
// https://github.com/siyuan-note/siyuan/issues/9630
const inlineElement = hasClosestByMatchTag(range.startContainer, "SPAN");
const inlineElement = hasClosestByTag(range.startContainer, "SPAN");
if (inlineElement) {
const inlineTypes = protyle.toolbar.getCurrentType(range);
if (inlineTypes.includes("code") || inlineTypes.includes("kbd")) {
@ -817,7 +816,7 @@ export const contentMenu = (protyle: IProtyle, nodeElement: Element) => {
}
}).element);
if (nodeElement.classList.contains("table") && !protyle.disabled) {
const cellElement = hasClosestByMatchTag(range.startContainer, "TD") || hasClosestByMatchTag(range.startContainer, "TH");
const cellElement = hasClosestByTag(range.startContainer, "TD") || hasClosestByTag(range.startContainer, "TH");
if (cellElement) {
const tableMenus = tableMenu(protyle, nodeElement, cellElement as HTMLTableCellElement, range);
if (tableMenus.insertMenus.length > 0) {

View file

@ -1,4 +1,8 @@
import {hasClosestByClassName, hasClosestByTag, hasTopClosestByTag} from "../../protyle/util/hasClosest";
import {
hasClosestByClassName,
hasClosestByTag,
hasTopClosestByTag
} from "../../protyle/util/hasClosest";
import {escapeHtml} from "../../util/escape";
import {Model} from "../../layout/Model";
import {Constants} from "../../constants";

View file

@ -2,8 +2,7 @@ import {listIndent, listOutdent} from "../../protyle/wysiwyg/list";
import {
hasClosestBlock,
hasClosestByAttribute,
hasClosestByClassName,
hasClosestByMatchTag
hasClosestByClassName, hasClosestByTag,
} from "../../protyle/util/hasClosest";
import {moveToDown, moveToUp} from "../../protyle/wysiwyg/move";
import {Constants} from "../../constants";
@ -532,7 +531,7 @@ export const initKeyboardToolbar = () => {
}
return;
}
const buttonElement = hasClosestByMatchTag(target, "BUTTON");
const buttonElement = hasClosestByTag(target, "BUTTON");
if (!buttonElement || buttonElement.getAttribute("disabled")) {
return;
}

View file

@ -1,7 +1,6 @@
import {
hasClosestBlock,
hasClosestByClassName,
hasClosestByMatchTag,
hasClosestByTag,
hasTopClosestByClassName,
isInEmbedBlock
@ -1539,7 +1538,7 @@ export class Gutter {
if (!tableElement.contains(range.startContainer)) {
range = getEditorRange(tableElement.querySelector("th"));
}
const cellElement = hasClosestByMatchTag(range.startContainer, "TD") || hasClosestByMatchTag(range.startContainer, "TH");
const cellElement = hasClosestByTag(range.startContainer, "TD") || hasClosestByTag(range.startContainer, "TH");
if (cellElement) {
window.siyuan.menus.menu.append(new MenuItem({id: "separator_table", type: "separator"}).element);
window.siyuan.menus.menu.append(new MenuItem({

View file

@ -1,5 +1,5 @@
import {Constants} from "../../constants";
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName, hasClosestByMatchTag} from "../util/hasClosest";
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName, hasClosestByTag} from "../util/hasClosest";
import {
focusBlock,
focusByRange,
@ -63,7 +63,7 @@ export class Hint {
event.stopPropagation();
return;
}
const btnElement = hasClosestByMatchTag(eventTarget, "BUTTON");
const btnElement = hasClosestByTag(eventTarget, "BUTTON");
if (btnElement && !btnElement.classList.contains("emojis__item") && !btnElement.classList.contains("emojis__type")) {
this.fill(decodeURIComponent(btnElement.getAttribute("data-value")), protyle, false, this.source === "search" ? isNotCtrl(event) : isOnlyMeta(event));
event.preventDefault();

View file

@ -1,22 +1,3 @@
export const hasClosestByTag = (element: Node, nodeName: string) => {
if (!element || element.nodeType === 9) {
return false;
}
if (element.nodeType === 3) {
element = element.parentElement;
}
let e = element as HTMLElement;
let isClosest = false;
while (e && !isClosest && !e.classList.contains("b3-typography")) {
if (e.nodeName.indexOf(nodeName) === 0) {
isClosest = true;
} else {
e = e.parentElement;
}
}
return isClosest && e;
};
export const hasTopClosestByClassName = (element: Node, className: string, top = false) => {
let closest = hasClosestByClassName(element, className, top);
let parentClosest: boolean | HTMLElement = false;
@ -83,15 +64,7 @@ export const hasClosestByAttribute = (element: Node, attr: string, value: string
return isClosest && e;
};
export const hasClosestBlock = (element: Node) => {
const nodeElement = hasClosestByAttribute(element, "data-node-id", null);
if (nodeElement && nodeElement.tagName !== "BUTTON" && nodeElement.getAttribute("data-type")?.startsWith("Node")) {
return nodeElement;
}
return false;
};
export const hasClosestByMatchTag = (element: Node, nodeName: string) => {
export const hasClosestByTag = (element: Node, nodeName: string) => {
if (!element || element.nodeType === 9) {
return false;
}
@ -129,6 +102,14 @@ export const hasClosestByClassName = (element: Node, className: string, top = fa
return isClosest && e;
};
export const hasClosestBlock = (element: Node) => {
const nodeElement = hasClosestByAttribute(element, "data-node-id", null);
if (nodeElement && nodeElement.tagName !== "BUTTON" && nodeElement.getAttribute("data-type")?.startsWith("Node")) {
return nodeElement;
}
return false;
};
export const isInEmbedBlock = (element: Element) => {
const embedElement = hasTopClosestByAttribute(element, "data-type", "NodeBlockQueryEmbed");
if (embedElement) {

View file

@ -1,4 +1,4 @@
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName, hasClosestByMatchTag} from "./hasClosest";
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName, hasClosestByTag} from "./hasClosest";
import * as dayjs from "dayjs";
import {transaction, updateTransaction} from "../wysiwyg/transaction";
import {getContenteditableElement} from "../wysiwyg/getBlock";
@ -290,7 +290,7 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
fixTableRange(range);
let tableInlineHTML;
if (hasClosestByAttribute(range.startContainer, "data-type", "NodeTable") && !isBlock) {
if (hasClosestByMatchTag(range.startContainer, "TABLE")) {
if (hasClosestByTag(range.startContainer, "TABLE")) {
tableInlineHTML = protyle.lute.BlockDOM2InlineBlockDOM(html);
} else {
// https://github.com/siyuan-note/siyuan/issues/9411

View file

@ -5,7 +5,7 @@ import {
hasPreviousSibling,
isNotEditBlock
} from "../wysiwyg/getBlock";
import {hasClosestByAttribute, hasClosestByMatchTag} from "./hasClosest";
import {hasClosestByAttribute, hasClosestByTag} from "./hasClosest";
import {countBlockWord, countSelectWord} from "../../layout/status";
import {hideElements} from "../ui/hideElements";
@ -28,8 +28,8 @@ export const fixTableRange = (range: Range) => {
if (range.toString() !== "" && tableElement && range.commonAncestorContainer.nodeType !== 3) {
const parentTag = (range.commonAncestorContainer as Element).tagName;
if (parentTag !== "TH" && parentTag !== "TD") {
const startCellElement = hasClosestByMatchTag(range.startContainer, "TD") || hasClosestByMatchTag(range.startContainer, "TH");
const endCellElement = hasClosestByMatchTag(range.endContainer, "TD") || hasClosestByMatchTag(range.endContainer, "TH");
const startCellElement = hasClosestByTag(range.startContainer, "TD") || hasClosestByTag(range.startContainer, "TH");
const endCellElement = hasClosestByTag(range.endContainer, "TD") || hasClosestByTag(range.endContainer, "TH");
if (!startCellElement && !endCellElement) {
const cellElement = tableElement.querySelector("th") || tableElement.querySelector("td");
range.setStart(cellElement.firstChild, 0);
@ -48,7 +48,7 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range)
if (editElement) {
let position;
if (editElement.tagName === "TABLE") {
const cellElement = hasClosestByMatchTag(range.startContainer, "TD") || hasClosestByMatchTag(range.startContainer, "TH");
const cellElement = hasClosestByTag(range.startContainer, "TD") || hasClosestByTag(range.startContainer, "TH");
if (cellElement) {
position = getSelectionOffset(cellElement, nodeElement, range);
if (position.start !== 0 || position.end !== cellElement.textContent.length) {

View file

@ -1,6 +1,6 @@
import {updateTransaction} from "../wysiwyg/transaction";
import {getSelectionOffset, focusByWbr, focusByRange, focusBlock} from "./selection";
import {hasClosestBlock, hasClosestByClassName, hasClosestByMatchTag} from "./hasClosest";
import {hasClosestBlock, hasClosestByClassName, hasClosestByTag} from "./hasClosest";
import {matchHotKey} from "./hotKey";
import {isNotCtrl} from "./compatibility";
import {scrollCenter} from "../../util/highlightById";
@ -365,7 +365,7 @@ export const moveColumnToRight = (protyle: IProtyle, range: Range, cellElement:
};
export const fixTable = (protyle: IProtyle, event: KeyboardEvent, range: Range) => {
const cellElement = hasClosestByMatchTag(range.startContainer, "TD") || hasClosestByMatchTag(range.startContainer, "TH");
const cellElement = hasClosestByTag(range.startContainer, "TD") || hasClosestByTag(range.startContainer, "TH");
const nodeElement = hasClosestBlock(range.startContainer) as HTMLTableElement;
if (!cellElement || !nodeElement) {
return false;

View file

@ -3,7 +3,6 @@ import {fetchPost} from "../../util/fetch";
import {isMac, writeText} from "../util/compatibility";
import {focusBlock, getSelectionOffset, setFirstNodeRange, setLastNodeRange,} from "../util/selection";
import {getContenteditableElement, getNextBlock} from "./getBlock";
import {hasClosestByMatchTag} from "../util/hasClosest";
import {hideElements} from "../ui/hideElements";
import {countBlockWord} from "../../layout/status";
import {scrollCenter} from "../../util/highlightById";
@ -14,6 +13,7 @@ import * as dayjs from "dayjs";
import {net2LocalAssets} from "../breadcrumb/action";
import {processClonePHElement} from "../render/util";
import {copyTextByType} from "../toolbar/util";
import {hasClosestByTag} from "../util/hasClosest";
export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent, nodeElement?: HTMLElement) => {
if (matchHotKey(window.siyuan.config.keymap.editor.general.netImg2LocalAsset.custom, event)) {
@ -140,7 +140,7 @@ export const upSelect = (options: {
options.event.stopPropagation();
options.event.preventDefault();
} else {
const tdElement = hasClosestByMatchTag(options.range.startContainer, "TD") || hasClosestByMatchTag(options.range.startContainer, "TH");
const tdElement = hasClosestByTag(options.range.startContainer, "TD") || hasClosestByTag(options.range.startContainer, "TH");
const nodeEditableElement = (tdElement || getContenteditableElement(options.nodeElement) || options.nodeElement) as HTMLElement;
const startIndex = getSelectionOffset(nodeEditableElement, options.editorElement, options.range).start;
const innerText = nodeEditableElement.innerText;
@ -187,7 +187,7 @@ export const downSelect = (options: {
options.event.stopPropagation();
options.event.preventDefault();
} else {
const tdElement = hasClosestByMatchTag(options.range.startContainer, "TD") || hasClosestByMatchTag(options.range.startContainer, "TH");
const tdElement = hasClosestByTag(options.range.startContainer, "TD") || hasClosestByTag(options.range.startContainer, "TH");
const nodeEditableElement = (tdElement || getContenteditableElement(options.nodeElement) || options.nodeElement) as HTMLElement;
const endIndex = getSelectionOffset(nodeEditableElement, options.editorElement, options.range).end;
const innerText = nodeEditableElement.innerText;

View file

@ -2,8 +2,7 @@ import {getTextStar, paste, pasteText} from "../util/paste";
import {
hasClosestBlock,
hasClosestByAttribute,
hasClosestByClassName,
hasClosestByMatchTag,
hasClosestByClassName, hasClosestByTag,
hasTopClosestByClassName,
isInEmbedBlock,
} from "../util/hasClosest";
@ -421,7 +420,7 @@ export class WYSIWYG {
if (isEndOfBlock(range)) {
textPlain = textPlain.replace(/\n$/, "");
}
} else if (!hasClosestByMatchTag(range.startContainer, "CODE")) {
} else if (!hasClosestByTag(range.startContainer, "CODE")) {
textPlain = range.toString();
}
}
@ -721,7 +720,7 @@ export class WYSIWYG {
}
// table cell select
let tableBlockElement: HTMLElement | false;
const targetCellElemet = hasClosestByMatchTag(target, "TH") || hasClosestByMatchTag(target, "TD");
const targetCellElemet = hasClosestByTag(target, "TH") || hasClosestByTag(target, "TD");
if (targetCellElemet) {
target = targetCellElemet;
}
@ -814,7 +813,7 @@ export class WYSIWYG {
moveTarget.classList.add("fn__none");
const pointElement = document.elementFromPoint(moveEvent.clientX, moveEvent.clientY);
moveTarget.classList.remove("fn__none");
moveTarget = hasClosestByMatchTag(pointElement, "TH") || hasClosestByMatchTag(pointElement, "TD");
moveTarget = hasClosestByTag(pointElement, "TH") || hasClosestByTag(pointElement, "TD");
}
if (moveTarget && moveTarget.isSameNode(target)) {
tableBlockElement.querySelector(".table__select").removeAttribute("style");
@ -1634,7 +1633,7 @@ export class WYSIWYG {
if (nodeElement.classList.contains("av")) {
updateAVName(protyle, nodeElement);
} else if (nodeElement.classList.contains("table")) {
parentElement = hasClosestByMatchTag(range.startContainer, "TD") || hasClosestByMatchTag(range.startContainer, "TH");
parentElement = hasClosestByTag(range.startContainer, "TD") || hasClosestByTag(range.startContainer, "TH");
} else {
parentElement = getContenteditableElement(nodeElement);
}
@ -1655,7 +1654,7 @@ export class WYSIWYG {
html = tempElement.innerHTML;
// https://github.com/siyuan-note/siyuan/issues/10722
if (hasClosestByAttribute(range.startContainer, "data-type", "NodeCodeBlock") ||
hasClosestByMatchTag(range.startContainer, "CODE")) {
hasClosestByTag(range.startContainer, "CODE")) {
textPlain = tempElement.textContent.replace(Constants.ZWSP, "");
}
// https://github.com/siyuan-note/siyuan/issues/4321

View file

@ -14,8 +14,7 @@ import {
import {
hasClosestBlock,
hasClosestByAttribute,
hasClosestByClassName,
hasClosestByMatchTag,
hasClosestByClassName, hasClosestByTag,
hasTopClosestByAttribute,
isInEmbedBlock
} from "../util/hasClosest";
@ -636,7 +635,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
// 上下左右光标移动
if (!event.altKey && !event.shiftKey && isNotCtrl(event) && !event.isComposing && (event.key.indexOf("Arrow") > -1)) {
// 需使用 editabled否则代码块会把语言字数算入
const tdElement = hasClosestByMatchTag(range.startContainer, "TD") || hasClosestByMatchTag(range.startContainer, "TH");
const tdElement = hasClosestByTag(range.startContainer, "TD") || hasClosestByTag(range.startContainer, "TH");
const nodeEditableElement = (tdElement || getContenteditableElement(nodeElement) || nodeElement) as HTMLElement;
const position = getSelectionOffset(nodeEditableElement, protyle.wysiwyg.element, range);
if (nodeElement.classList.contains("code-block") && position.end === nodeEditableElement.innerText.length) {
@ -708,7 +707,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}
} else if (selectText === "" && (event.key === "ArrowDown" || event.key === "ArrowRight") && nodeElement.isSameNode(getLastBlock(protyle.wysiwyg.element.lastElementChild)) &&
// 表格无法右移动 https://ld246.com/article/1631434502215
!hasClosestByMatchTag(range.startContainer, "TD") && !hasClosestByMatchTag(range.startContainer, "TH")) {
!hasClosestByTag(range.startContainer, "TD") && !hasClosestByTag(range.startContainer, "TH")) {
// 页面按向下/右箭头丢失焦点 https://ld246.com/article/1629954026096
const lastEditElement = getContenteditableElement(nodeElement);
// 代码块需替换最后一个 /n https://github.com/siyuan-note/siyuan/issues/3221
@ -958,7 +957,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
return;
}
// https://github.com/siyuan-note/siyuan/issues/9690
const inlineElement = hasClosestByMatchTag(range.startContainer, "SPAN");
const inlineElement = hasClosestByTag(range.startContainer, "SPAN");
if (position.start === 2 && inlineElement &&
getSelectionOffset(inlineElement, protyle.wysiwyg.element, range).start === 1 &&
inlineElement.innerText.startsWith(Constants.ZWSP) &&

View file

@ -1,10 +1,10 @@
import {getIconByType} from "../editor/getIcon";
import {hasClosestByMatchTag, hasClosestByTag} from "../protyle/util/hasClosest";
import {isMobile} from "./functions";
import {mathRender} from "../protyle/render/mathRender";
import {unicode2Emoji} from "../emoji";
import {Constants} from "../constants";
import {escapeAriaLabel} from "./escape";
import {hasClosestByTag} from "../protyle/util/hasClosest";
export class Tree {
public element: HTMLElement;
@ -224,7 +224,7 @@ data-def-path="${item.defPath}">
}
if (target.classList.contains("b3-list-item__action") && this.click) {
// 移动端书签父节点删除按钮
const liElement = hasClosestByMatchTag(target, "LI");
const liElement = hasClosestByTag(target, "LI");
if (liElement) {
this.click(liElement, event);
}