mirror of
https://github.com/garraflavatra/rolens.git
synced 2025-04-19 08:51:03 +00:00
28 lines
575 B
JavaScript
28 lines
575 B
JavaScript
|
export function input(node, { json } = { json: false }) {
|
||
|
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);
|
||
|
|
||
|
return {
|
||
|
destroy: () => {
|
||
|
node.removeEventListener('focus', handleFocus);
|
||
|
node.removeEventListener('input', handleInput);
|
||
|
},
|
||
|
};
|
||
|
}
|