refactor: centralize shared utilities across backend and frontend
Backend: - Move SHA256 normalization logic from path_utils._normalize_header_sha256 into core/type_utils.normalize_sha256, adding support for 0x-prefixed hashes and unifying the normalization path used by extract_safetensors_header_sha256. - Add tests for the 0x prefix normalization cases. Frontend: - Introduce copyTextWithFeedback in web/resolver/utils/html_utils.js as a shared clipboard helper with configurable feedback text, CSS class, and duration, replacing repeated inline clipboard copy logic in globals.js, model_info_methods.js, and the notification utility. - Centralize showNotification on ResolverManagerDialog and remove the duplicate implementation from missingBrowserMethods. - Replace local const localStorage = safeStorage aliases in model_info_methods.js, missing_browser_methods.js, and options_methods.js with direct safeStorage usage.
This commit is contained in:
@@ -223,3 +223,44 @@ export function normalizePathIdentity(value = '') {
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies text to clipboard and handles time-based button visual feedback.
|
||||
* @param {string} text - The text content to copy.
|
||||
* @param {HTMLElement} button - The button to apply feedback to.
|
||||
* @param {Object} options - Customization parameters.
|
||||
*/
|
||||
export async function copyTextWithFeedback(text, button, options = {}) {
|
||||
if (!button) return;
|
||||
const successText = options.successText || 'Copied';
|
||||
const errorText = options.errorText || 'Failed';
|
||||
const duration = options.duration || 1200;
|
||||
const successClass = options.successClass || 'is-copied';
|
||||
const successHtml = options.successHtml || null;
|
||||
|
||||
const originalHtml = button.innerHTML;
|
||||
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
if (successClass) {
|
||||
button.classList.add(successClass);
|
||||
}
|
||||
if (successHtml) {
|
||||
button.innerHTML = successHtml;
|
||||
} else {
|
||||
button.textContent = successText;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Model Resolver: Copy failed:', err);
|
||||
button.textContent = errorText;
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
if (button.isConnected) {
|
||||
if (successClass) {
|
||||
button.classList.remove(successClass);
|
||||
}
|
||||
button.innerHTML = originalHtml;
|
||||
}
|
||||
}, duration);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user