2023-01-17 17:03:11 +01:00
|
|
|
export function input(node, { json, autofocus } = { json: false, autofocus: false }) {
|
2023-01-10 17:28:27 +01:00
|
|
|
const handleInput = () => {
|
|
|
|
if (json) {
|
|
|
|
try {
|
|
|
|
JSON.parse(node.value);
|
|
|
|
node.classList.remove('invalid');
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
node.classList.add('invalid');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleFocus = () => {
|
|
|
|
node.select();
|
|
|
|
};
|
|
|
|
|
|
|
|
node.addEventListener('focus', handleFocus);
|
|
|
|
node.addEventListener('input', handleInput);
|
|
|
|
|
2023-01-17 17:03:11 +01:00
|
|
|
if (autofocus) {
|
|
|
|
node.focus();
|
|
|
|
}
|
|
|
|
|
2023-01-10 17:28:27 +01:00
|
|
|
return {
|
|
|
|
destroy: () => {
|
|
|
|
node.removeEventListener('focus', handleFocus);
|
|
|
|
node.removeEventListener('input', handleInput);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|