Various UI improvements for config
This commit is contained in:
275
ui.js
275
ui.js
@@ -1,4 +1,4 @@
|
|||||||
import { h, render, Component } from 'preact';
|
import { h, render, Component, createRef } from 'preact';
|
||||||
import initModule from './libapi.mjs';
|
import initModule from './libapi.mjs';
|
||||||
|
|
||||||
/** @typedef {InstanceType<import('./libapi.mjs').Module['Context']>} Context */
|
/** @typedef {InstanceType<import('./libapi.mjs').Module['Context']>} Context */
|
||||||
@@ -47,108 +47,166 @@ const scheduleOp = (() => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {{
|
* @extends Component<{ config: Config }>
|
||||||
* config: Config,
|
|
||||||
* inProgress: boolean,
|
|
||||||
* }} params
|
|
||||||
* @returns {import('preact').VNode<any>}
|
|
||||||
*/
|
*/
|
||||||
function Config({ config, inProgress }) {
|
class ConfigComponent extends Component {
|
||||||
let { label, name } = config;
|
state = { inProgress: false };
|
||||||
let id = `config-${name}`;
|
|
||||||
if (config.type === 'window' || config.type === 'section') {
|
shouldComponentUpdate(
|
||||||
return h(
|
/** @type {ConfigComponent['props']} */ nextProps,
|
||||||
'fieldset',
|
/** @type {ConfigComponent['state']} */ nextState
|
||||||
{ id },
|
) {
|
||||||
h('legend', {}, label),
|
return !(this.state.inProgress && nextState.inProgress);
|
||||||
Object.values(config.children).map(config =>
|
|
||||||
h(Config, { key: config.name, config, inProgress })
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
let { value, readonly } = config;
|
|
||||||
let valueProp;
|
getValueProp() {
|
||||||
switch (config.type) {
|
switch (this.props.config.type) {
|
||||||
case 'toggle':
|
case 'toggle':
|
||||||
valueProp = 'checked';
|
return 'checked';
|
||||||
break;
|
case 'datetime':
|
||||||
case 'range':
|
return 'valueAsNumber';
|
||||||
case 'datetime':
|
default:
|
||||||
valueProp = 'valueAsNumber';
|
return 'value';
|
||||||
break;
|
|
||||||
default:
|
|
||||||
valueProp = 'value';
|
|
||||||
}
|
|
||||||
// We don't want to override current input's value while user is editing it.
|
|
||||||
if (document.activeElement?.id === id) {
|
|
||||||
// @ts-ignore
|
|
||||||
value = document.activeElement?.[valueProp];
|
|
||||||
}
|
|
||||||
let attrs = {
|
|
||||||
id,
|
|
||||||
[valueProp]: value,
|
|
||||||
readonly,
|
|
||||||
disabled: inProgress
|
|
||||||
};
|
|
||||||
let inputElem;
|
|
||||||
switch (config.type) {
|
|
||||||
case 'range': {
|
|
||||||
let { min, max, step } = config;
|
|
||||||
inputElem = h('input', { type: 'number', min, max, step, ...attrs });
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case 'text':
|
}
|
||||||
inputElem = readonly ? value : h('input', attrs);
|
|
||||||
break;
|
handleChange = async e => {
|
||||||
case 'toggle': {
|
this.setState({ inProgress: true });
|
||||||
inputElem = h('input', {
|
|
||||||
type: 'checkbox',
|
let value = e.target[this.getValueProp()];
|
||||||
...attrs
|
|
||||||
|
try {
|
||||||
|
/** @type {Promise<void>} */
|
||||||
|
let uiTimeout;
|
||||||
|
await scheduleOp(context => {
|
||||||
|
// This is terrible, yes... but some configs return too quickly before they're actually updated.
|
||||||
|
// We want to wait some time before updating the UI in that case, but not block subsequent ops.
|
||||||
|
uiTimeout = new Promise(resolve => setTimeout(resolve, 800));
|
||||||
|
return context.setConfigValue(this.props.config.name, value);
|
||||||
});
|
});
|
||||||
break;
|
await uiTimeout;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
}
|
}
|
||||||
case 'menu':
|
|
||||||
case 'radio': {
|
this.setState({ inProgress: false });
|
||||||
let { choices } = config;
|
};
|
||||||
inputElem = h(
|
|
||||||
'select',
|
render(
|
||||||
attrs,
|
/** @type {ConfigComponent['props']} */ { config },
|
||||||
choices.map(choice =>
|
/** @type {ConfigComponent['state']} */ { inProgress }
|
||||||
h(
|
) {
|
||||||
'option',
|
let { label, name } = config;
|
||||||
{
|
let id = `config-${name}`;
|
||||||
|
if (config.type === 'window' || config.type === 'section') {
|
||||||
|
return h(
|
||||||
|
'fieldset',
|
||||||
|
{ id },
|
||||||
|
h('legend', {}, label),
|
||||||
|
Object.values(config.children).map(config =>
|
||||||
|
h(ConfigComponent, { key: config.name, config })
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let { value, readonly } = config;
|
||||||
|
let valueProp = this.getValueProp();
|
||||||
|
let attrs = {
|
||||||
|
id,
|
||||||
|
[valueProp]: value,
|
||||||
|
readonly: readonly || inProgress,
|
||||||
|
onChange: this.handleChange
|
||||||
|
};
|
||||||
|
let inputElem;
|
||||||
|
switch (config.type) {
|
||||||
|
case 'range': {
|
||||||
|
let { min, max, step } = config;
|
||||||
|
inputElem = h(EditableInput, {
|
||||||
|
type: 'number',
|
||||||
|
min,
|
||||||
|
max,
|
||||||
|
step,
|
||||||
|
...attrs
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'text':
|
||||||
|
inputElem = readonly ? value : h(EditableInput, attrs);
|
||||||
|
break;
|
||||||
|
case 'toggle': {
|
||||||
|
inputElem = h('input', {
|
||||||
|
type: 'checkbox',
|
||||||
|
...attrs
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'menu':
|
||||||
|
case 'radio': {
|
||||||
|
let { choices } = config;
|
||||||
|
inputElem = h(
|
||||||
|
'select',
|
||||||
|
attrs,
|
||||||
|
choices.map(choice =>
|
||||||
|
h(Option, {
|
||||||
key: choice,
|
key: choice,
|
||||||
value: choice,
|
value: choice,
|
||||||
disabled: attrs.readonly && value !== choice
|
disabled: attrs.readonly && value !== choice
|
||||||
},
|
})
|
||||||
choice
|
|
||||||
)
|
)
|
||||||
)
|
);
|
||||||
);
|
break;
|
||||||
break;
|
}
|
||||||
}
|
case 'datetime': {
|
||||||
case 'datetime': {
|
inputElem = h(EditableInput, {
|
||||||
inputElem = h('input', {
|
type: 'datetime-local',
|
||||||
type: 'datetime-local',
|
...attrs
|
||||||
...attrs
|
});
|
||||||
});
|
break;
|
||||||
break;
|
}
|
||||||
}
|
default: {
|
||||||
default: {
|
inputElem = '(unimplemented)';
|
||||||
inputElem = '(unimplemented)';
|
break;
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
|
return h(
|
||||||
|
'div',
|
||||||
|
{ class: 'pure-control-group' },
|
||||||
|
h('label', { for: id }, (inProgress ? '⌛ ' : '') + label),
|
||||||
|
inputElem
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Special memoized option to work around https://github.com/preactjs/preact/issues/3171.
|
||||||
|
* @extends Component<{ value: string, disabled?: boolean }>
|
||||||
|
*/
|
||||||
|
class Option extends Component {
|
||||||
|
shouldComponentUpdate(/** @type {Option['props']} */ nextProps) {
|
||||||
|
return nextProps.value !== this.props.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return h('option', this.props, this.props.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper around <input /> that doesn't update it while it's in focus to allow editing.
|
||||||
|
*/
|
||||||
|
class EditableInput extends Component {
|
||||||
|
ref = createRef();
|
||||||
|
|
||||||
|
shouldComponentUpdate() {
|
||||||
|
return document.activeElement !== this.ref.current;
|
||||||
|
}
|
||||||
|
|
||||||
|
render(props) {
|
||||||
|
return h('input', Object.assign(props, { ref: this.ref }));
|
||||||
}
|
}
|
||||||
return h(
|
|
||||||
'div',
|
|
||||||
{ class: 'pure-control-group' },
|
|
||||||
h('label', { for: id }, label),
|
|
||||||
inputElem
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Settings extends Component {
|
class Settings extends Component {
|
||||||
state = { inProgress: false, config: 'Connecting...' };
|
state = { config: 'Connecting...' };
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
@@ -160,38 +218,6 @@ class Settings extends Component {
|
|||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
|
||||||
handleChange = async e => {
|
|
||||||
let name = e.target.id;
|
|
||||||
if (!name.startsWith('config-')) {
|
|
||||||
throw new Error('Unhandled input');
|
|
||||||
}
|
|
||||||
name = name.slice('config-'.length);
|
|
||||||
let value;
|
|
||||||
switch (e.target.type) {
|
|
||||||
case 'checkbox':
|
|
||||||
value = e.target.checked;
|
|
||||||
break;
|
|
||||||
case 'number':
|
|
||||||
case 'datetime-local':
|
|
||||||
value = e.target.valueAsNumber;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
value = e.target.value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({ inProgress: true });
|
|
||||||
|
|
||||||
try {
|
|
||||||
await scheduleOp(context => context.setConfigValue(name, value));
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.refreshConfig();
|
|
||||||
this.setState({ inProgress: false });
|
|
||||||
};
|
|
||||||
|
|
||||||
async refreshConfig() {
|
async refreshConfig() {
|
||||||
let config;
|
let config;
|
||||||
try {
|
try {
|
||||||
@@ -218,16 +244,13 @@ class Settings extends Component {
|
|||||||
? state.config
|
? state.config
|
||||||
: h(
|
: h(
|
||||||
'form',
|
'form',
|
||||||
{
|
{ class: 'pure-form pure-form-aligned' },
|
||||||
class: 'pure-form pure-form-aligned',
|
|
||||||
onchange: this.handleChange
|
|
||||||
},
|
|
||||||
h('input', {
|
h('input', {
|
||||||
type: 'button',
|
type: 'button',
|
||||||
value: 'Capture image',
|
value: 'Capture image',
|
||||||
onclick: this.handleCapture
|
onclick: this.handleCapture
|
||||||
}),
|
}),
|
||||||
h(Config, state)
|
h(ConfigComponent, state)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user