refactor: Behaved

This commit is contained in:
cha0s 2021-02-03 01:49:06 -06:00
parent ca6d9ada21
commit fad35e4c2f

View File

@ -3,18 +3,10 @@ import {join} from 'path';
import { import {
PropTypes, PropTypes,
React, React,
useEffect,
usePrevious,
useRef,
useState,
} from '@latus/react'; } from '@latus/react';
import {useJsonPatcher} from '@persea/json';
import { import {
Tab, JsonTabs,
Tabs, } from '@persea/json';
TabList,
TabPanel,
} from 'react-tabs';
import Expressions from '../behavior-components/expressions'; import Expressions from '../behavior-components/expressions';
@ -22,133 +14,25 @@ const Behaved = ({
entity, entity,
json, json,
path, path,
}) => { }) => (
const patch = useJsonPatcher(); <div className="behaved">
const editingRef = useRef(); <JsonTabs
useEffect(() => { createPanel={(expressions, key) => (
editingRef.current?.focus();
});
const [isEditing, setIsEditing] = useState(false);
const [previousKey, setPreviousKey] = useState(false);
const previousEditing = usePrevious(isEditing);
useEffect(() => {
setTimeout(() => {
if (false === previousEditing) {
editingRef.current?.select();
}
}, 10);
}, [editingRef, isEditing, previousEditing]);
const tabs = [];
const tabPanels = [];
Object.entries(json.params.routines).forEach(([key, expressions]) => {
const routinePath = join(path, 'params/routines', key);
const confirmEdit = () => {
if (isEditing && previousKey !== isEditing) {
patch({
op: 'move',
from: join(path, 'params/routines', previousKey),
path: join(path, 'params/routines', isEditing),
});
}
setIsEditing(false);
setPreviousKey(false);
};
tabs.push(
<Tab
key={key}
onDoubleClick={() => {
setPreviousKey(key);
setIsEditing(key);
}}
>
{
key === previousKey
? (
<input
type="text"
value={isEditing}
onBlur={confirmEdit}
onChange={({target: {value}}) => {
setPreviousKey(key);
setIsEditing(value);
}}
onKeyPress={({key}) => {
if ('Enter' === key) {
confirmEdit();
}
}}
ref={editingRef}
/>
)
: key
}
&nbsp;
<button
type="button"
className="routines__tab-remove"
onClick={() => {
patch({
op: 'remove',
path: routinePath,
});
}}
>
</button>
</Tab>,
);
tabPanels.push(
<TabPanel key={key}>
<Expressions <Expressions
context={entity.context} context={entity.context}
value={expressions} value={expressions}
path={routinePath} path={join(path, 'params/routines', key)}
/> />
</TabPanel>, )}
); defaultValue={{
}); type: 'expressions',
return ( expressions: [],
<div className="behaved"> }}
<div className="label"> path={join(path, 'params/routines')}
<div className="vertical">Routines</div> map={json.params.routines}
<Tabs> />
<div className="behaved__routines-tab-bar"> </div>
<button );
className="traits__add-trait"
onClick={() => {
let i = 1;
let name = 'untitled';
while (json.params.routines[name]) {
name = `untitled (${i++})`;
}
patch({
op: 'add',
path: join(path, 'params/routines', name),
value: {
type: 'expressions',
expressions: [],
},
});
setIsEditing(name);
setPreviousKey(name);
}}
type="button"
>
+
</button>
<TabList>
{tabs}
</TabList>
</div>
<div className="routines__tab-panes">
{tabPanels}
</div>
</Tabs>
</div>
</div>
);
};
Behaved.displayName = 'Behaved'; Behaved.displayName = 'Behaved';