Files
MajoorWaldi-ComfyUI-Majoor-…/js/utils/dom.js
T
MajoorWaldi 2fa41a0e76 feat: add useGridState and useVirtualGrid composables for asset management
- Implemented useGridState to manage grid state, including asset selection and loading states.
- Created useVirtualGrid to centralize virtualization helpers for improved performance.
- Added createVueApp for isolated Vue 3 + Pinia application instances.
- Introduced Vite configuration for building the ComfyUI-Majoor-AssetsManager with external dependencies.
2026-04-03 11:04:55 +02:00

77 lines
2.2 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;
}
/**
* Safely escape a string for use inside CSS selectors.
* Falls back to conservative character escaping when CSS.escape is unavailable.
* @param {any} value
* @returns {string}
*/
export function safeEscapeSelector(value) {
const str = String(value ?? "");
try {
if (typeof CSS?.escape === "function") return CSS.escape(str);
} catch (e) {
console.debug?.(e);
}
return str.replace(/([!"#$%&'()*+,./:;<=>?@[\\\]^`{|}~])/g, "\\$1");
}
/**
* 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;
}