Files
breadcrumb-the-shire/web/js/components/app-copy-badge.js
fs c7b8fd516a feat: extend module platform with UI slots, runtime components, CLI tooling and {{userId}} search support
Completes the generic module platform that enables modules to contribute
UI elements, runtime JS components, and search resources without any
core hardcoding.

New generic UI slot types:
- topbar.right_item: module-contributed topbar buttons
- layout.body_end_template: module-contributed dialog/overlay templates
- layout.head_style: module-contributed global CSS
- runtime.component: declarative JS component registration with phase ordering

New infrastructure:
- ModuleAutoloader: PSR-4 autoloading for module-local PHP classes
- ModuleRuntimePageBuilder: symlinks module pages into runtime directory
- ModuleRuntimeAssetPublisher: publishes module CSS/JS to web/modules/
- ModulePermissionSynchronizer: syncs module permissions to DB
- CLI scripts: module-runtime-sync, module-build, module-migrate,
  module-permissions-sync, module-assets-sync
- {{userId}} placeholder in SearchDataService for user-scoped search queries
- Component runtime with phased initialization (early/default/late)
- AppContainer.protectExistingBindings() to prevent module→core overwrites
- Architecture tests: ModuleStructureContractTest, CoreTemplateIsolationTest,
  FrontendComponentRuntimeContractTest, AppContainerIsolationContractTest

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:19:56 +01:00

108 lines
3.0 KiB
JavaScript

/**
* Click-to-copy badge — copies text to clipboard with visual feedback.
*/
import { resolveHost } from '../core/app-dom.js';
const COPY_SELECTOR = '.badge[data-copy="true"]';
const COPIED_CLASS = 'is-copied';
const getCopyValue = (badge) => {
const explicit = badge.getAttribute('data-copy-value');
if (explicit !== null && explicit !== '') {
return explicit;
}
return (badge.textContent || '').trim();
};
const copyText = async (value) => {
if (!value) {
return false;
}
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(value);
return true;
}
const textarea = document.createElement('textarea');
textarea.value = value;
textarea.setAttribute('readonly', 'true');
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
document.body.appendChild(textarea);
textarea.select();
const ok = document.execCommand('copy');
document.body.removeChild(textarea);
return ok;
};
export function initBadgeCopy(root = document, options = {}) {
const selector = String(options.selector || COPY_SELECTOR).trim() || COPY_SELECTOR;
const host = resolveHost(root);
const badges = Array.from(host.querySelectorAll(selector));
if (!badges.length) {
return { destroy: () => {} };
}
const cleanupFns = [];
const timers = [];
const enhanceBadge = (badge) => {
if (badge.dataset.badgeCopyBound === '1') {
return;
}
badge.dataset.badgeCopyBound = '1';
if (!badge.querySelector('.badge-copy-icon')) {
const icon = document.createElement('span');
icon.className = 'badge-copy-icon';
icon.setAttribute('aria-hidden', 'true');
icon.innerHTML = '<i class="bi bi-copy"></i><i class="bi bi-check2"></i>';
badge.appendChild(icon);
}
badge.setAttribute('role', 'button');
badge.setAttribute('tabindex', '0');
if (!badge.hasAttribute('aria-label')) {
badge.setAttribute('aria-label', 'Copy to clipboard');
}
const handler = async (event) => {
event.preventDefault();
const value = getCopyValue(badge);
const ok = await copyText(value);
if (!ok) {
return;
}
badge.classList.add(COPIED_CLASS);
const timer = window.setTimeout(() => badge.classList.remove(COPIED_CLASS), 1200);
timers.push(timer);
};
const onKeyDown = (event) => {
if (event.key === 'Enter' || event.key === ' ') {
handler(event);
}
};
badge.addEventListener('click', handler);
badge.addEventListener('keydown', onKeyDown);
cleanupFns.push(() => {
badge.removeEventListener('click', handler);
badge.removeEventListener('keydown', onKeyDown);
delete badge.dataset.badgeCopyBound;
});
};
badges.forEach((badge) => {
if (badge instanceof HTMLElement) {
enhanceBadge(badge);
}
});
const destroy = () => {
cleanupFns.forEach((cleanup) => cleanup());
timers.forEach((timer) => window.clearTimeout(timer));
};
return { destroy };
}
export const initCopyBadge = initBadgeCopy;