Skip to content

Commit

Permalink
Improve the class name of showTooltip()
Browse files Browse the repository at this point in the history
  • Loading branch information
TCOTC committed Oct 10, 2024
1 parent 6db8829 commit f6295ca
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
14 changes: 11 additions & 3 deletions app/src/block/popover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const initBlockPopover = (app: App) => {
hasClosestByClassName(event.target, "av__calc--ashow") ||
hasClosestByClassName(event.target, "av__cell");
if (aElement) {
let tip = aElement.getAttribute("aria-label") || aElement.getAttribute("data-inline-memo-content");
let tooltipClass = "";
let tip = aElement.getAttribute("aria-label");
if (aElement.classList.contains("av__cell")) {
if (aElement.classList.contains("av__cell--header")) {
const textElement = aElement.querySelector(".av__celltext");
Expand All @@ -51,11 +52,18 @@ export const initBlockPopover = (app: App) => {
} else if (aElement.classList.contains("av__calc--ashow") && aElement.clientWidth + 2 < aElement.scrollWidth) {
tip = aElement.lastChild.textContent + " " + aElement.firstElementChild.textContent;
}
if (!tip) {
tip = aElement.getAttribute("data-inline-memo-content");
if (tip) {
tooltipClass = "memo"; // 为行级备注添加 class https://github.com/siyuan-note/siyuan/issues/6161
}
}
if (!tip) {
const href = aElement.getAttribute("data-href") || "";
// 链接地址强制换行 https://github.com/siyuan-note/siyuan/issues/11539
if (href) {
tip = `<span style="word-break: break-all">${href.substring(0, Constants.SIZE_TITLE)}</span>`;
tooltipClass = "href"; // 为超链接添加 class https://github.com/siyuan-note/siyuan/issues/11440#issuecomment-2119080691
}
const title = aElement.getAttribute("data-title");
if (tip && isLocalPath(href) && !aElement.classList.contains("b3-tooltips")) {
Expand All @@ -78,10 +86,10 @@ export const initBlockPopover = (app: App) => {
if (tip && !aElement.classList.contains("b3-tooltips")) {
// https://github.com/siyuan-note/siyuan/issues/11294
try {
showTooltip(decodeURIComponent(tip), aElement);
showTooltip(decodeURIComponent(tip), aElement, tooltipClass);
} catch (e) {
// https://ld246.com/article/1718235737991
showTooltip(tip, aElement);
showTooltip(tip, aElement, tooltipClass);
}
event.stopPropagation();
} else {
Expand Down
19 changes: 10 additions & 9 deletions app/src/dialog/tooltip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {isMobile} from "../util/functions";

export const showTooltip = (message: string, target: Element, error = false) => {
export const showTooltip = (message: string, target: Element, tooltipClass?: string) => {
if (isMobile()) {
return;
}
Expand All @@ -16,15 +16,16 @@ export const showTooltip = (message: string, target: Element, error = false) =>
} else {
messageElement.innerHTML = message;
}
if (error) {
messageElement.classList.add("tooltip--error");
} else {
messageElement.classList.remove("tooltip--error");
}
if (target.getAttribute("data-inline-memo-content")) {
messageElement.classList.add("tooltip--memo"); // 为行级备注添加 class https://github.com/siyuan-note/siyuan/issues/6161

if (tooltipClass) {
messageElement.classList.add("tooltip--" + tooltipClass);
} else {
messageElement.classList.remove("tooltip--memo");
const classesToRemove = Array.from(messageElement.classList).filter(className =>
className.startsWith("tooltip--")
);
classesToRemove.forEach(className => {
messageElement.classList.remove(className);
});
}

let left = targetRect.left;
Expand Down
4 changes: 2 additions & 2 deletions app/src/editor/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import {getAllEditor} from "../layout/getAll";
export const validateName = (name: string, targetElement?: HTMLElement) => {
if (/\r\n|\r|\n|\u2028|\u2029|\t|\//.test(name)) {
if (targetElement) {
showTooltip(window.siyuan.languages.fileNameRule, targetElement, true);
showTooltip(window.siyuan.languages.fileNameRule, targetElement, "error");
} else {
showMessage(window.siyuan.languages.fileNameRule);
}
return false;
}
if (name.length > Constants.SIZE_TITLE) {
if (targetElement) {
showTooltip(window.siyuan.languages["_kernel"]["106"], targetElement, true);
showTooltip(window.siyuan.languages["_kernel"]["106"], targetElement, "error");
} else {
showMessage(window.siyuan.languages["_kernel"]["106"]);
}
Expand Down

0 comments on commit f6295ca

Please sign in to comment.