Files
client/app/renderer/js/chat-markdown-help.js
T

55 lines
3.4 KiB
JavaScript

// Settings > Chats > Markdown Help (official ts-chat-markdown-help view, client_ui/main.js module 71415):
// the dialect intro and one card per syntax entry with its source, a copy button and the example rendered by
// the same renderer the chat log uses, so every example shows exactly what TeamsPeach displays.
import { h, icon } from './dom.js';
import { t, hasTranslation } from './i18n.js';
import { tp } from './core.js';
import { renderMarkdownInto } from './markdown/render.js';
import { openExternalLink } from './external-links.js';
import { section } from './settings-ui.js';
import { MARKDOWN_HELP_IDS } from './chat-options.js';
/** Rendered-markdown interaction shared with the chat log: links go through the external-link guard, spoilers toggle. */
export function handleMarkdownClick(e) {
const link = e.target.closest('a');
if (link) { e.preventDefault(); if (link.dataset.href && !link.hasAttribute('data-invalid')) void openExternalLink(link.dataset.href); return; }
const spoiler = e.target.closest('.md-spoiler');
if (spoiler) { const revealed = spoiler.getAttribute('aria-expanded') !== 'true'; spoiler.setAttribute('aria-expanded', String(revealed)); spoiler.classList.toggle('revealed', revealed); }
}
export function handleMarkdownKey(e) {
if ((e.key === 'Enter' || e.key === ' ') && e.target.matches('.md-spoiler')) { e.preventDefault(); e.target.click(); }
}
function rendered(source) {
const content = h('span', { class: 'ts-chat-message-content ts-parsed-text-content offline-preview' });
renderMarkdownInto(content, source);
return content;
}
function card(id) {
const key = (part) => `settings.chat.markdown_help.syntax.${id}.${part}`;
const syntax = t(key('syntax'));
const copy = h('button', { type: 'button', class: 'icon-btn ts-chat-markdown-help-copy-button', 'aria-label': t('universal.copy_to_clipboard'), title: t('universal.copy_to_clipboard'),
onclick: () => { void tp.copyText(syntax); } }, icon('item-copy'));
return h('article', { class: 'ts-chat-markdown-help-card' },
h('h4', { class: 'ts-chat-markdown-help-card-title', text: t(key('name')) }),
hasTranslation(key('notes')) ? h('p', { class: 'ts-card-subtitle', text: t(key('notes')) }) : null,
h('div', { class: 'ts-chat-markdown-help-columns' },
h('div', { class: 'ts-chat-markdown-help-column' },
h('div', { class: 'ts-chat-markdown-help-label' }, h('span', { text: t('settings.chat.markdown_help.card.syntax') }), copy),
h('div', { class: 'ts-chat-markdown-help-syntax-wrapper' }, h('pre', { class: 'ts-chat-markdown-help-syntax' }, h('code', { text: syntax })))),
h('div', { class: 'ts-chat-markdown-help-column' },
h('div', { class: 'ts-chat-markdown-help-label', text: t('settings.chat.markdown_help.card.example') }),
h('div', { class: 'ts-chat-markdown-help-example' }, rendered(t(key('example')))))));
}
/** The two official sections of the help page, ready for showSubpage(). */
export function markdownHelpSections() {
const root = h('div', { class: 'ts-chat-markdown-help', onclick: handleMarkdownClick, onkeydown: handleMarkdownKey },
section(t('settings.chat.markdown_help.intro.title'), h('div', { class: 'ts-card-setting-container' }, rendered(t('settings.chat.markdown_help.intro.body')))),
section(t('settings.chat.markdown_help.syntax.title'), h('div', { class: 'ts-card-setting-container' },
h('div', { class: 'ts-chat-markdown-help-grid' }, MARKDOWN_HELP_IDS.map(card)))));
return root;
}