Files
MajoorWaldi-ComfyUI-Majoor-…/js/utils/dom.js
T
MajoorWaldi dc72be0139 feat: enhance JS testing and linting setup
- Added coverage reporting for Vitest with configuration in vitest.config.mjs
- Updated package.json to include new scripts for JS coverage, linting, and formatting
- Modified run_quality_gate.py to allow skipping ESLint and Prettier checks
- Introduced .prettierrc for consistent code formatting
- Created .prettierignore to exclude specific files and directories from formatting
2026-03-28 14:14:28 +01:00

61 lines
1.7 KiB
JavaScript

/**
* DOM helpers
*/
/**
* Safely call Element.closest on a potentially non-Element target.
* Returns null if target is not suitable or any error occurs.
* @param {any} target
* @param {string} selector
* @returns {Element|null}
*/
export function safeClosest(target, selector) {
if (!selector) return null;
try {
if (!target) return null;
if (target instanceof Element && typeof target.closest === "function") {
return target.closest(selector);
}
// Text nodes or other non-Element targets
const parent = target?.parentElement;
if (parent && typeof parent.closest === "function") {
return parent.closest(selector);
}
} catch (e) {
console.debug?.(e);
}
return null;
}
/**
* Clipboard helper with safe fallback behavior.
* Returns true on success, false otherwise.
*/
export async function copyTextToClipboard(text) {
try {
if (!navigator?.clipboard?.writeText) return false;
await navigator.clipboard.writeText(String(text ?? ""));
return true;
} catch {
return false;
}
}
/**
* Build a reusable media error placeholder node.
* @param {string} iconClass
* @returns {HTMLDivElement}
*/
export function createMediaErrorPlaceholder(iconClass = "pi pi-image") {
const err = document.createElement("div");
err.className = "mjr-thumb-error";
const icon = document.createElement("i");
icon.className = String(iconClass || "pi pi-image");
icon.style.fontSize = "24px";
icon.style.opacity = "0.5";
err.appendChild(icon);
err.style.cssText =
"display:flex; align-items:center; justify-content:center; width:100%; height:100%; background:rgba(255,50,50,0.1);";
return err;
}