Syntax Highlighting tool, Markdown guide, unread share badge, Open in menu
- Syntax Highlighting (/syntax): highlight.js tokens rendered with VS Code (Dark/Light Modern, One Dark Pro, Monokai, Dracula, GitHub) and Android Studio (Darcula, Dark, IntelliJ Light) colors; copy as rich text (VS Code clipboard HTML), download RTF, share. Format via Prettier/sql-formatter (lazy-loaded), the JSON printer, or built-in whitespace-only formatters for brace languages, Python and XML. - Markdown: Guide dialog, source next to rendered output, with Insert. - Pastes created by Share count as unread on the Pastebin nav badge. - Paste view: Open in menu built from registry opensPaste tools. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f4232ef9b5
commit
dc2b12a3c6
37 changed files with 3704 additions and 27 deletions
|
|
@ -157,3 +157,75 @@ export function tabIndent(ta, unit = ' ') {
|
|||
ta.setSelectionRange(lineStart, lineStart + out.join('\n').length);
|
||||
});
|
||||
}
|
||||
|
||||
/** A modal dialog. Returns the <dialog>; it removes itself when closed. */
|
||||
export function modal(title, body, { wide = false } = {}) {
|
||||
const dlg = el(`dialog.modal${wide ? '.wide' : ''}`, { 'aria-label': title },
|
||||
el('div.modal-head', {},
|
||||
el('h2', {}, title),
|
||||
el('button.icon-btn', { 'aria-label': 'Close', onclick: () => dlg.close() }, '✕'),
|
||||
),
|
||||
el('div.modal-body', {}, body),
|
||||
);
|
||||
dlg.addEventListener('click', (e) => {
|
||||
if (e.target === dlg) dlg.close(); // click on the backdrop
|
||||
});
|
||||
dlg.addEventListener('close', () => dlg.remove());
|
||||
document.body.append(dlg);
|
||||
dlg.showModal();
|
||||
return dlg;
|
||||
}
|
||||
|
||||
/** A button that opens a menu: dropdown('Open in', [{label, icon?, onSelect}]). */
|
||||
export function dropdown(label, items) {
|
||||
const menu = el('div.menu', { role: 'menu', hidden: true });
|
||||
const btn = el('button.btn', { 'aria-haspopup': 'menu', 'aria-expanded': 'false', onclick: (e) => {
|
||||
e.stopPropagation();
|
||||
toggle(menu.hidden);
|
||||
} }, label, el('span.caret', { 'aria-hidden': 'true' }, '▾'));
|
||||
for (const it of items) {
|
||||
menu.append(el('button.menu-item', { role: 'menuitem', onclick: () => { toggle(false); it.onSelect(); } },
|
||||
it.icon ? el('span.nav-icon', {}, it.icon) : null, it.label));
|
||||
}
|
||||
const outside = (e) => { if (!wrap.contains(e.target)) toggle(false); };
|
||||
const onKey = (e) => { if (e.key === 'Escape') { toggle(false); btn.focus(); } };
|
||||
function toggle(open) {
|
||||
menu.hidden = !open;
|
||||
btn.setAttribute('aria-expanded', String(open));
|
||||
if (open) {
|
||||
document.addEventListener('click', outside);
|
||||
document.addEventListener('keydown', onKey);
|
||||
menu.querySelector('button')?.focus();
|
||||
} else {
|
||||
document.removeEventListener('click', outside);
|
||||
document.removeEventListener('keydown', onKey);
|
||||
}
|
||||
}
|
||||
const wrap = el('div.dropdown', {}, btn, menu);
|
||||
return wrap;
|
||||
}
|
||||
|
||||
/** Copies HTML as rich text (with a plain-text alternative) for pasting into docs and chat apps. */
|
||||
export async function copyRich(html, text, what = 'Copied') {
|
||||
try {
|
||||
if (!window.ClipboardItem || !navigator.clipboard?.write) throw new Error('no async clipboard');
|
||||
await navigator.clipboard.write([new ClipboardItem({
|
||||
'text/html': new Blob([html], { type: 'text/html' }),
|
||||
'text/plain': new Blob([text], { type: 'text/plain' }),
|
||||
})]);
|
||||
} catch {
|
||||
// Older browsers / insecure origins: select a rendered copy and use the legacy copy command.
|
||||
const holder = el('div', { contenteditable: 'true', style: 'position:fixed;left:-9999px;top:0' });
|
||||
holder.innerHTML = html;
|
||||
document.body.append(holder);
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(holder);
|
||||
const sel = getSelection();
|
||||
sel.removeAllRanges();
|
||||
sel.addRange(range);
|
||||
document.execCommand('copy');
|
||||
sel.removeAllRanges();
|
||||
holder.remove();
|
||||
}
|
||||
toast(what);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue