aboutsummaryrefslogtreecommitdiff
path: root/build/js/ui/widgets.js
blob: c3a4fbcdc530549f6a9598438a62b4c1a4f7e9f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Small form controls shared by dialogs, the tool-options bar and panels.
import { el, clamp } from '../core/util.js';
import { icon } from './icons.js';
import { BLEND_MODES } from '../core/layer.js';

/** Slider + number box. onInput fires while dragging, onChange on release/commit. */
export function rangeField({ label, min = 0, max = 100, step = 1, value, unit = '', scale = 'linear', compact = false, onInput, onChange }) {
  const log = scale === 'log' && min > 0;
  const toSlider = (v) => (log ? (Math.log(v / min) / Math.log(max / min)) * 1000 : v);
  const fromSlider = (s) => {
    if (!log) return Number(s);
    const v = min * Math.pow(max / min, s / 1000);
    return roundTo(v, v < 10 ? 0.5 : 1);
  };
  const range = el('input', { type: 'range', min: log ? 0 : min, max: log ? 1000 : max, step: log ? 1 : step, value: toSlider(value) });
  const num = el('input', { type: 'number', min, max, step, value: fmt(value, step), class: 'num' });
  const set = (v, fire) => {
    v = clamp(Number(v), min, max);
    if (Number.isNaN(v)) return;
    range.value = toSlider(v);
    num.value = fmt(v, step);
    if (fire === 'input') onInput?.(v);
    if (fire === 'change') onChange?.(v);
  };
  range.addEventListener('input', () => set(fromSlider(range.value), 'input'));
  range.addEventListener('change', () => set(fromSlider(range.value), 'change'));
  num.addEventListener('change', () => { set(num.value, 'input'); set(num.value, 'change'); });
  num.addEventListener('keydown', (e) => e.stopPropagation());
  const wrap = el('label', { class: `field range-field${compact ? ' compact' : ''}` },
    label ? el('span', { class: 'field-label' }, label) : null,
    range,
    el('span', { class: 'num-wrap' }, num, unit ? el('span', { class: 'unit' }, unit) : null));
  wrap.setValue = (v) => set(v, null);
  return wrap;
}

function roundTo(v, q) { return Math.round(v / q) * q; }
function fmt(v, step) {
  const decimals = step < 1 ? Math.min(3, String(step).split('.')[1]?.length || 2) : 0;
  return Number(v).toFixed(decimals).replace(/\.0+$/, '');
}

export function selectField({ label, options, value, onChange, title }) {
  if (options === 'blend') options = BLEND_MODES.map((m) => ({ value: m.id, label: m.label }));
  const optionEls = () => options.map((o) => {
    const opt = typeof o === 'object' ? o : { value: o, label: String(o) };
    return el('option', { value: String(opt.value) }, opt.label);
  });
  const sel = el('select', { title: title || label || '' }, ...optionEls());
  sel.value = String(value);
  sel.addEventListener('change', () => {
    const o = options.find((x) => String(typeof x === 'object' ? x.value : x) === sel.value);
    onChange?.(typeof o === 'object' ? o.value : o);
  });
  sel.addEventListener('keydown', (e) => e.stopPropagation());
  const wrap = el('label', { class: 'field select-field' }, label ? el('span', { class: 'field-label' }, label) : null, sel);
  wrap.setValue = (v) => { sel.value = String(v); };
  /** Replace the option list, keeping the current value when it still exists. */
  wrap.setOptions = (opts) => {
    const cur = sel.value;
    options = opts;
    sel.replaceChildren(...optionEls());
    sel.value = cur;
  };
  wrap.select = sel;
  return wrap;
}

export function checkboxField({ label, value, onChange, title }) {
  const cb = el('input', { type: 'checkbox', checked: !!value });
  cb.addEventListener('change', () => onChange?.(cb.checked));
  const wrap = el('label', { class: 'field check-field', title: title || '' }, cb, el('span', {}, label));
  wrap.setValue = (v) => { cb.checked = !!v; };
  return wrap;
}

export function numberField({ label, value, min, max, step = 1, unit, onChange, width }) {
  const num = el('input', { type: 'number', value, min, max, step, class: 'num' });
  if (width) num.style.width = width;
  num.addEventListener('change', () => {
    let v = Number(num.value);
    if (Number.isNaN(v)) return;
    if (min != null) v = Math.max(min, v);
    if (max != null) v = Math.min(max, v);
    num.value = v;
    onChange?.(v);
  });
  num.addEventListener('keydown', (e) => e.stopPropagation());
  const wrap = el('label', { class: 'field number-field' }, label ? el('span', { class: 'field-label' }, label) : null, num, unit ? el('span', { class: 'unit' }, unit) : null);
  wrap.setValue = (v) => { num.value = v; };
  wrap.input = num;
  return wrap;
}

export function textField({ label, value, onChange, placeholder, list }) {
  const input = el('input', { type: 'text', value: value ?? '', placeholder: placeholder || '' });
  if (list) input.setAttribute('list', list);
  input.addEventListener('change', () => onChange?.(input.value));
  input.addEventListener('keydown', (e) => e.stopPropagation());
  const wrap = el('label', { class: 'field text-field' }, label ? el('span', { class: 'field-label' }, label) : null, input);
  wrap.setValue = (v) => { input.value = v; };
  wrap.input = input;
  return wrap;
}

/** Segmented icon/label buttons; one active value. */
export function buttonGroup({ options, value, onChange }) {
  const wrap = el('div', { class: 'btn-group' });
  const buttons = options.map((o) => {
    const b = el('button', { type: 'button', class: 'btn icon-btn', title: o.title || o.label || '', html: o.icon ? icon(o.icon, 16) : o.label });
    b.addEventListener('click', () => { wrap.setValue(o.value); onChange?.(o.value); });
    b.dataset.value = String(o.value);
    wrap.appendChild(b);
    return b;
  });
  wrap.setValue = (v) => buttons.forEach((b) => b.classList.toggle('active', b.dataset.value === String(v)));
  wrap.setValue(value);
  return wrap;
}

export function toggleButton({ label, title, value, onChange, style }) {
  const b = el('button', { type: 'button', class: 'btn toggle-btn', title: title || label });
  b.textContent = label;
  if (style) b.setAttribute('style', style);
  let v = !!value;
  const sync = () => b.classList.toggle('active', v);
  b.addEventListener('click', () => { v = !v; sync(); onChange?.(v); });
  sync();
  b.setValue = (nv) => { v = !!nv; sync(); };
  return b;
}

/** Native color input styled as a swatch. */
export function colorField({ label, value, onChange, onInput }) {
  const input = el('input', { type: 'color', value: (value || '#000000').slice(0, 7), class: 'color-input' });
  input.addEventListener('input', () => onInput?.(input.value));
  input.addEventListener('change', () => onChange?.(input.value));
  const wrap = el('label', { class: 'field color-field' }, label ? el('span', { class: 'field-label' }, label) : null, input);
  wrap.setValue = (v) => { input.value = (v || '#000000').slice(0, 7); };
  return wrap;
}

/** Build a control for a schema field (tool options and filter dialogs share this). */
export function fieldFor(f, value, onChange, { compact = false, onInput } = {}) {
  switch (f.type) {
    case 'select': return selectField({ label: f.label, options: f.options, value, onChange });
    case 'checkbox': return checkboxField({ label: f.label, value, onChange, title: f.title });
    case 'toggle': return toggleButton({ label: f.label, title: f.title, value, onChange, style: f.style });
    case 'buttons': return buttonGroup({ options: f.options, value, onChange });
    case 'number': return numberField({ label: f.label, value, min: f.min, max: f.max, step: f.step, unit: f.unit, onChange });
    case 'color': return colorField({ label: f.label, value, onChange, onInput: onInput || onChange });
    case 'text': return textField({ label: f.label, value, onChange });
    case 'font': return textField({ label: f.label, value, onChange, list: 'font-list' });
    default: return rangeField({
      label: f.label, min: f.min, max: f.max, step: f.step || 1, value, unit: f.unit, scale: f.scale, compact,
      onInput: onInput || onChange, onChange,
    });
  }
}