Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135import { h, clear, debounce, parseColor } from './util.js';import { icon } from './icons.js';import { iconButton, toast } from './ui.js';import { parseCSS, serializeCSS } from './cssparse.js';import { codeEditor } from './codeeditor.js';import { PRESENTATION } from './schema.js';import { makeEditor, textEditor } from './editors.js';
// CSS editor for <style> elements: a code view and a structured rules view// that stay in sync, with live "matches N elements" feedback.
export function cssPane ({ ctx }) { const root = h('div.drawer-pane'); let doc = null, styleEl = null; let sheet = { rules: [] }; let editor = null; let rulesBox = null; let selector = null;
const build = () => { clear(root); if (!doc) return; const styles = doc.elementsOfType('style'); if (!styles.length) { root.append(h('div.drawer-empty', h('div', 'This document has no stylesheet.'), h('button', { onclick: () => { const el = doc.createFromTemplate('style'); el.textContent = '/* styles */\n'; doc.insert(doc.ensureDefs(), el, 0); doc.select(el); show(doc, el); } }, icon('plus'), 'Add <style>'))); return; } if (!styleEl || !styles.includes(styleEl)) styleEl = styles[0]; selector = h('select', styles.map((s, i) => h('option', { value: i, selected: s === styleEl }, `style${s.id ? '#' + s.id : ''} (${i + 1}/${styles.length})`))); selector.addEventListener('change', () => { styleEl = styles[Number(selector.value)]; build(); }); const toolbar = h('div.pane-toolbar', h('span.title', 'CSS'), selector, h('span.spacer'), h('button.small.ghost', { title: 'Reformat the stylesheet', onclick: () => { editor.value = serializeCSS(parseCSS(editor.value)); commitText(editor.value); } }, 'format'), h('button.small.ghost', { onclick: () => addRule() }, icon('plus'), 'rule'), h('button.small.ghost', { onclick: () => addRule('@font-face') }, icon('plus'), '@font-face'), iconButton('select', 'Select <style> in tree', () => doc.select(styleEl)), ); editor = codeEditor({ value: styleEl.textContent, language: 'css', onChange: debounce(v => commitText(v), 250), onSave: v => commitText(v) }); rulesBox = h('div.css-rules'); root.append(toolbar, h('div.pane-split', h('div.left', editor.el), h('div.right', rulesBox))); sheet = parseCSS(styleEl.textContent); renderRules(); };
let selfChange = false; const commitText = (text) => { if (!styleEl) return; selfChange = true; doc.setText(styleEl, text, { key: `${doc.kid(styleEl)}:css` }); selfChange = false; sheet = parseCSS(text); renderRules(); }; const commitSheet = () => { const text = serializeCSS(sheet); editor.value = text; commitText(text); }; const addRule = (kind) => { if (kind === '@font-face') sheet.rules.push({ type: 'at', name: 'font-face', prelude: '', decls: [{ name: 'font-family', value: '"MyFont"' }, { name: 'src', value: 'url(font.woff2) format("woff2")' }] }); else { const sel = doc.primary?.nodeType === 1 && doc.primary !== doc.root ? (doc.primary.id ? `#${doc.primary.id}` : doc.primary.getAttribute('class') ? `.${doc.primary.getAttribute('class').split(/\s+/)[0]}` : doc.primary.localName) : 'rect'; sheet.rules.push({ type: 'rule', selector: sel, decls: [{ name: 'fill', value: '#4d8dff' }] }); } commitSheet(); };
const matchCount = (sel) => { try { return doc.root.querySelectorAll(sel).length + (doc.root.matches(sel) ? 1 : 0); } catch { return -1; } }; const highlightMatches = (sel, on) => { if (!on) { ctx.hoverMany?.(null); return; } try { ctx.hoverMany?.([...doc.root.querySelectorAll(sel)]); } catch { /* invalid selector */ } };
const renderRules = () => { if (!rulesBox) return; clear(rulesBox); const renderList = (rules, container) => { rules.forEach((r, i) => container.append(renderRule(r, i, rules))); }; const renderRule = (r, i, list) => { if (r.type === 'comment') return h('div.css-rule', h('div.comment', `/* ${r.text} */`)); if (r.type === 'garbage') return h('div.css-rule', h('div.comment', r.text)); const card = h('div.css-rule'); const remove = iconButton('x', 'Remove rule', () => { list.splice(i, 1); commitSheet(); }, 'small'); if (r.type === 'at') { const head = h('div.css-rule-head', h('span.at', `@${r.name}`), textEditor({ value: r.prelude, onCommit: v => { r.prelude = v; commitSheet(); }, mono: true, cls: 'selector', placeholder: 'prelude' }), remove); card.append(head); if (r.decls) card.append(renderDecls(r.decls)); else if (r.rules) { const nested = h('div.css-nested'); renderList(r.rules, nested); card.append(nested); } else if (r.raw != null) card.append(h('div.comment', r.raw.slice(0, 200))); return card; } const n = matchCount(r.selector); const matches = h('span.matches', { class: n === 0 ? 'zero' : '' }, n < 0 ? 'invalid' : `${n} match${n === 1 ? '' : 'es'}`); const selIn = textEditor({ value: r.selector, onCommit: v => { r.selector = v; commitSheet(); }, mono: true, cls: 'selector', placeholder: 'selector' }); const head = h('div.css-rule-head', selIn, matches, iconButton('select', 'Select matching elements', () => { try { doc.select([...doc.root.querySelectorAll(r.selector)]); } catch { toast('Invalid selector', { kind: 'error' }); } }, 'small'), remove); card.addEventListener('pointerenter', () => { card.classList.add('hover'); highlightMatches(r.selector, true); }); card.addEventListener('pointerleave', () => { card.classList.remove('hover'); highlightMatches(r.selector, false); }); card.append(head, renderDecls(r.decls)); return card; }; const renderDecls = (decls) => { const box = h('div.css-decls'); decls.forEach((d, j) => { const pd = PRESENTATION[d.name]; const type = pd && ['paint', 'color', 'enum', 'opacity', 'length', 'number', 'fontFamily', 'funciri'].includes(pd.type) ? pd.type : 'string'; const val = makeEditor({ type, def: pd || {}, value: d.value, ctx, name: d.name, onInput: v => { d.value = v; if (styleEl) { selfChange = true; doc.setText(styleEl, serializeCSS(sheet), { commit: false }); selfChange = false; } }, onCommit: v => { if (v === '' || v == null) decls.splice(j, 1); else d.value = v; commitSheet(); } }); const nameIn = textEditor({ value: d.name, onCommit: v => { if (!v) decls.splice(j, 1); else d.name = v; commitSheet(); }, mono: true, cls: 'name', placeholder: 'property' }); box.append(h('div.css-decl', nameIn, h('div.attr-value', val, d.important ? h('span.hint', { title: '!important' }, '!') : null), iconButton('x', 'Remove', () => { decls.splice(j, 1); commitSheet(); }, 'small'))); }); const newName = h('input', { type: 'text', placeholder: 'add property…', class: 'mono', style: { flex: 1, height: '20px', fontSize: '11px' } }); newName.addEventListener('keydown', e => { e.stopPropagation(); if (e.key === 'Enter' && newName.value.trim()) { const [n, v = ''] = newName.value.split(':'); decls.push({ name: n.trim(), value: v.trim() || (PRESENTATION[n.trim()]?.default ?? '') }); commitSheet(); } }); box.append(h('div.add-row', newName)); return box; }; renderList(sheet.rules, rulesBox); if (!sheet.rules.length) rulesBox.append(h('div.drawer-empty', 'No rules yet.')); };
const show = (d, el) => { doc = d; if (el && el.localName === 'style') styleEl = el; build(); }; const onDocChange = (d) => { if (!doc || !root.classList.contains('active')) return; if (d.kind === 'document' || d.kind === 'structure') { build(); return; } if (d.kind === 'text' && d.nodes?.includes(styleEl) && !selfChange) { editor.value = styleEl.textContent; sheet = parseCSS(styleEl.textContent); renderRules(); } else if (d.kind === 'attr') renderRules(); }; return { el: root, show, onDocChange, refresh: () => build(), focus: () => editor?.focus() };}