Files
client/app/renderer/js/chat-settings.js
T
omar f618d8bce9
ci / linux-arm64-cross (push) Successful in 3m22s
ci / linux-x64 (push) Successful in 3m32s
chat: add safe hybrid Markdown editing with strict CSP
2026-11-02 11:32:31 +03:00

116 lines
6.9 KiB
JavaScript

// Settings > Chats in the official order (client_ui/main.js settings component near 4873300): Text Editing
// (rich text editor, toolbar visibility, diagram auto-load, Markdown Help), Options, Ignored Users. Preference
// semantics live in chat-options.js; schema and defaults in app/main/chat-prefs.js.
import { S } from './state.js';
import { tp } from './core.js';
import { h, icon } from './dom.js';
import { section, toggleRow, inlineRow } from './settings-ui.js';
import { t, onLanguageChange } from './i18n.js';
import { toast } from './toast.js';
import { showSubpage } from './settings-subpage.js';
import { markdownHelpSections } from './chat-markdown-help.js';
import { ignoredUsers, withIgnored, withoutIgnored } from './chat-options.js';
import { contactSettingsSection } from './contacts.js';
let refresh = () => {};
async function savePrefs(patch) {
const result = await tp.setPrefs(patch);
if (!result?.ok) throw new Error(result?.error?.message ?? result?.message ?? 'Not saved');
S.prefs = result.prefs;
refresh();
}
/** Adds (ignored=true) or removes a client UID from the ignore list; `name` is only the label shown in settings. */
export async function setIgnoredUser(uid, name, ignored) {
await savePrefs({ chatIgnoredUsers: ignored ? withIgnored(S.prefs, uid, name) : withoutIgnored(S.prefs, uid) });
}
export function initChatSettings(changed) {
const host = document.getElementById('settings-chat-markdown');
if (!host) return;
let view = 'root';
let helpEntry = null;
const save = async (patch) => {
try { await savePrefs(patch); } catch (error) { toast(() => t('chat.operation_failed', { error: error.message }), 'error'); render(); }
};
refresh = () => { changed(); if (view === 'root') render(); };
const toggle = (key, title, enabledInfo, disabledInfo = enabledInfo, fallback = true) => {
const value = S.prefs?.[key] ?? fallback;
const input = h('input', { type: 'checkbox', class: 'toggle', role: 'switch' });
input.checked = value;
input.addEventListener('change', () => save({ [key]: input.checked }));
return toggleRow(t(title), t(value ? enabledInfo : disabledInfo), input);
};
// ts-segmented-control rows (official Text Editing section).
const segmented = (key, label, choices) => {
const current = S.prefs?.[key] ?? choices[0][0];
const group = h('div', { class: 'segmented', role: 'radiogroup', 'aria-label': t(label) });
for (const [value, text] of choices) {
group.append(h('button', { type: 'button', class: 'segment', role: 'radio', 'aria-checked': String(value === current), onclick: () => { if (value !== current) save({ [key]: value }); } }, t(text)));
}
return group;
};
function ignoredList() {
const list = h('ul', { class: 'ts-chat-ignored-users' });
const cell = (glyph, title, subtitle, action, className = '') => h('li', { class: `tsv-item ${className}`.trim() },
h('div', { class: 'tsv-item-container tsv-item-multiline' },
h('div', { class: 'tsv-item-content' }, glyph,
h('div', { class: 'tsv-item-text' }, h('div', { class: 'tsv-item-title', text: title }), h('div', { class: 'tsv-item-subtitle', text: subtitle })))),
action);
const entries = ignoredUsers(S.prefs);
if (entries.length === 0) {
list.append(cell(icon('empty', 'tsv-icon-primary'), t('settings.chat.ignored_users.empty'), t('settings.chat.ignored_users.empty.info'), null, 'ts-chat-ignored-empty'));
}
for (const entry of entries) {
const name = entry.name || t('contactlist.name.fallback');
const remove = h('button', { type: 'button', class: 'icon-btn tsv-action-subtle', 'aria-label': t('settings.chat.ignored_users.remove', { name }), title: t('settings.chat.ignored_users.remove', { name }) }, icon('decline'));
remove.addEventListener('click', async () => {
remove.disabled = true;
try { await setIgnoredUser(entry.uid, entry.name, false); } catch (error) { remove.disabled = false; toast(() => t('chat.operation_failed', { error: error.message }), 'error'); }
});
list.append(cell(icon('contact-normal', 'tsv-icon-primary'), name, entry.uid, remove));
}
return list;
}
function openHelp() {
view = 'help';
showSubpage(host, markdownHelpSections(), () => { render(); helpEntry?.focus(); }).focus();
}
function render() {
view = 'root';
helpEntry = h('button', { type: 'button', class: 'icon-btn ts-chat-markdown-help-open', 'aria-label': t('settings.chat.text_edit.markdown_help') }, icon('navigation-forward'));
const help = h('div', { class: 'ts-card-setting-container' },
h('div', { class: 'ts-chat-markdown-help-entry', onclick: (e) => { e.preventDefault(); openHelp(); } },
h('div', { class: 'ts-chat-markdown-help-entry-text' },
h('span', { class: 'ts-setting-title', text: t('settings.chat.text_edit.markdown_help') }),
h('p', { class: 'tsv-font-subtitle', text: t('settings.chat.text_edit.markdown_help.info') })),
helpEntry));
host.replaceChildren(
contactSettingsSection(save),
section(t('settings.chat.text_edit'), [
// render@4879468: the subtitle shows the source enabled/disabled strings verbatim.
toggle('chatRichTextEditor', 'settings.chat.text_edit.rich_text', 'settings.chat.text_edit.rich_text.enabled', 'settings.chat.text_edit.rich_text.disabled'),
inlineRow(t('settings.chat.toolbar'), t('settings.chat.toolbar.info'),
segmented('chatToolbarVisibility', 'settings.chat.toolbar', [['inactive', 'settings.chat.text_edit.toolbar_visibility.inactive'], ['dynamic', 'settings.chat.text_edit.toolbar_visibility.dynamic'], ['always-visible', 'settings.chat.text_edit.toolbar_visibility.always_visible']])),
inlineRow(t('settings.chat.diagrams'), t('settings.chat.diagrams.info'),
segmented('chatMermaidAutoload', 'settings.chat.diagrams', [['never', 'settings.chat.text_edit.mermaid_autoload.never'], ['only-simple', 'settings.chat.text_edit.mermaid_autoload.only_simple']])),
help,
]),
section(t('settings.chat.options'), [
toggle('chatOpenIncoming', 'settings.chat.direct.auto_open', 'settings.chat.direct.auto_open.info.enabled', 'settings.chat.direct.auto_open.disabled'),
toggle('chatShowModerationEvents', 'settings.chat.filter.moderation', 'settings.chat.filter.moderation.enabled', 'settings.chat.filter.moderation.disabled'),
toggle('chatUse24hTime', 'settings.chat.timestamp.format.24', 'settings.chat.timestamp.format.24.enabled', 'settings.chat.timestamp.format.24.disabled'),
toggle('autoDownloadImages', 'settings.chat.auto_download_images', 'settings.chat.auto_download_images.info', undefined, false),
]),
section(t('settings.chat.ignored_users'), ignoredList()));
}
render();
onLanguageChange(() => { if (view === 'help') openHelp(); else render(); });
document.getElementById('settings-nav')?.addEventListener('click', (e) => { if (e.target.closest('[data-panel="chats"]')) render(); });
}