rob-revisited/ide/ui/js/bootstrap-utilities.js

109 lines
7.1 KiB
JavaScript

// The utilities the inspector edits (no DOM: node --test runs it). A group is a family of classes of which an element has at
// most one per breakpoint (mt-3 and mt-md-4 live together, mt-3 and mt-2 do not): the group knows which class a value is, and which
// value a class is. setGroup() gives the new class list; bootstrap-model.js turns it into the one edit on the class attribute.
import { BREAKPOINTS, classSet } from './bootstrap-catalog.js';
const BPS = ['', ...BREAKPOINTS];
const withBp = (prefix, bp, value) => `${prefix}${bp ? bp + '-' : ''}${value}`;
function group(id, label, values, make, bpAware = true, extra = {}) {
const lookup = new Map();
const known = classSet('5');
// only what Bootstrap has (offset-0 and flex-grow-md-1 do not exist)
for (const bp of bpAware ? BPS : ['']) for (const v of values) if (known.has(make(v, bp))) lookup.set(make(v, bp), { value: v, bp });
return { id, label, values, bpAware, make, lookup, ...extra };
}
/** the values a group offers at a breakpoint (the ones whose class exists) */
export function valuesAt(g, bp = '') {
const at = g.bpAware ? bp : '';
return g.values.filter((v) => g.lookup.has(g.make(v, at)));
}
const prefixed = (id, label, prefix, values, bpAware = true, extra = {}) => group(id, label, values, (v, bp) => withBp(prefix, bp, v), bpAware, extra);
const SPACES = ['0', '1', '2', '3', '4', '5', 'auto'];
const COLOR_NAMES = ['primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark'];
const colored = (id, label, prefix, extraNames = []) => prefixed(id, label, prefix, [...COLOR_NAMES, ...extraNames], false, { color: true });
export const GROUPS = {};
const add = (g) => { GROUPS[g.id] = g; return g; };
// spacing: one group per property and side (m, mt, ms... p, pt, ps...)
for (const prop of ['m', 'p']) for (const side of ['', 't', 'b', 's', 'e', 'x', 'y']) add(prefixed(`${prop}${side}`, `${prop === 'm' ? 'Margin' : 'Padding'}`, `${prop}${side}-`, prop === 'm' ? SPACES : SPACES.slice(0, 6)));
add(prefixed('gap', 'Gap', 'gap-', ['0', '1', '2', '3', '4', '5']));
add(prefixed('gx', 'Column gap', 'gx-', ['0', '1', '2', '3', '4', '5']));
add(prefixed('gy', 'Row gap', 'gy-', ['0', '1', '2', '3', '4', '5']));
add(prefixed('g', 'Gutter', 'g-', ['0', '1', '2', '3', '4', '5']));
add(prefixed('display', 'Display', 'd-', ['none', 'inline', 'inline-block', 'block', 'grid', 'table', 'flex', 'inline-flex']));
add(prefixed('direction', 'Direction', 'flex-', ['row', 'column', 'row-reverse', 'column-reverse']));
add(prefixed('justify', 'Justify', 'justify-content-', ['start', 'center', 'end', 'between', 'around', 'evenly']));
add(prefixed('items', 'Align items', 'align-items-', ['start', 'center', 'end', 'baseline', 'stretch']));
add(prefixed('self', 'Align self', 'align-self-', ['start', 'center', 'end', 'baseline', 'stretch']));
add(prefixed('wrap', 'Wrap', 'flex-', ['wrap', 'nowrap', 'wrap-reverse']));
add(prefixed('order', 'Order', 'order-', ['0', '1', '2', '3', '4', '5', 'first', 'last']));
add(prefixed('grow', 'Grow', 'flex-grow-', ['0', '1'], true));
add(group('col', 'Column', ['', 'auto', ...Array.from({ length: 12 }, (_, i) => String(i + 1))], (v, bp) => (bp ? (v === '' ? `col-${bp}` : `col-${bp}-${v}`) : (v === '' ? 'col' : `col-${v}`))));
add(group('offset', 'Offset', Array.from({ length: 12 }, (_, i) => String(i)), (v, bp) => withBp('offset-', bp, v)));
add(prefixed('align', 'Align', 'text-', ['start', 'center', 'end']));
add(prefixed('weight', 'Weight', 'fw-', ['bold', 'bolder', 'semibold', 'medium', 'normal', 'light', 'lighter'], false));
add(prefixed('size', 'Size', 'fs-', ['1', '2', '3', '4', '5', '6'], false));
add(prefixed('style', 'Style', 'fst-', ['italic', 'normal'], false));
add(prefixed('transform', 'Transform', 'text-', ['lowercase', 'uppercase', 'capitalize'], false));
add(colored('textColor', 'Text colour', 'text-', ['body', 'body-secondary', 'body-emphasis', 'muted', 'white', 'black']));
add(colored('bg', 'Background', 'bg-', ['body', 'body-secondary', 'body-tertiary', 'white', 'black', 'transparent']));
add(colored('textBg', 'Text on background', 'text-bg-'));
add(colored('borderColor', 'Border colour', 'border-', ['white', 'black']));
add(prefixed('borderWidth', 'Border width', 'border-', ['1', '2', '3', '4', '5'], false));
for (const side of ['', 'top', 'end', 'bottom', 'start']) add(group(`border${side}`, side ? `Border ${side}` : 'Border', ['on'], () => (side ? `border-${side}` : 'border'), false));
add(group('rounded', 'Rounded', ['', '0', '1', '2', '3', '4', '5', 'circle', 'pill'], (v) => (v === '' ? 'rounded' : `rounded-${v}`), false));
add(group('shadow', 'Shadow', ['', 'sm', 'lg', 'none'], (v) => (v === '' ? 'shadow' : `shadow-${v}`), false));
add(prefixed('position', 'Position', 'position-', ['static', 'relative', 'absolute', 'fixed', 'sticky'], false));
add(prefixed('width', 'Width', 'w-', ['25', '50', '75', '100', 'auto'], false));
add(prefixed('height', 'Height', 'h-', ['25', '50', '75', '100', 'auto'], false));
add(group('visibility', 'Visibility', ['visible', 'invisible', 'visually-hidden'], (v) => v, false));
add(prefixed('float', 'Float', 'float-', ['start', 'end', 'none']));
add(prefixed('overflow', 'Overflow', 'overflow-', ['auto', 'hidden', 'visible', 'scroll'], false));
/** the value a group has in these classes at a breakpoint ('' = every width), or null */
export function groupValue(g, classes, bp = '') {
for (const c of classes) {
const hit = g.lookup.get(c);
if (hit && hit.bp === (g.bpAware ? bp : '')) return hit.value;
}
return null;
}
/** every breakpoint at which the group has a value: {'': '3', md: '4'} */
export function groupValues(g, classes) {
const out = {};
for (const c of classes) { const hit = g.lookup.get(c); if (hit) out[hit.bp] = hit.value; }
return out;
}
/** the classes with the group's value at that breakpoint set (value null: removed), the other breakpoints kept; the new class takes the old one's place */
export function setGroup(g, classes, value, bp = '') {
const at = g.bpAware ? bp : '';
const next = value === null || value === undefined || value === false ? null : g.make(value, at);
const out = [];
let placed = false;
for (const c of classes) {
const hit = g.lookup.get(c);
if (!(hit && hit.bp === at)) out.push(c);
else if (next !== null && !placed) { out.push(next); placed = true; }
}
if (next !== null && !placed) out.push(next);
return out;
}
/** the groups in the order the inspector shows them */
export const SECTIONS = [
{ id: 'grid', label: 'Grid', groups: ['col', 'offset', 'order', 'g'] },
{ id: 'spacing', label: 'Spacing', groups: [] },
{ id: 'display', label: 'Display', groups: ['display', 'float', 'visibility', 'overflow'] },
{ id: 'flex', label: 'Flex', groups: ['direction', 'justify', 'items', 'self', 'wrap', 'grow', 'gap'] },
{ id: 'text', label: 'Text', groups: ['align', 'weight', 'size', 'style', 'transform', 'textColor'] },
{ id: 'colors', label: 'Colours', groups: ['bg', 'textBg'] },
{ id: 'borders', label: 'Borders', groups: ['border', 'bordertop', 'borderend', 'borderbottom', 'borderstart', 'borderColor', 'borderWidth', 'rounded', 'shadow'] },
{ id: 'position', label: 'Position and size', groups: ['position', 'width', 'height'] },
];