MCPcopy Create free account
hub / github.com/atomicdata-dev/atomic-data-browser / ElementEdit

Function ElementEdit

data-browser/src/views/Element.tsx:49–219  ·  view source on GitHub ↗
({
  subject,
  deleteElement,
  index,
  setCurrent,
  setElementSubject: setElement,
  active,
  canDrag,
}: ElementEditProps)

Source from the content-addressed store, hash-verified

47
48/** An element is a section inside document, such as a Paragraph, Header or Image */
49export function ElementEdit({
50 subject,
51 deleteElement,
52 index,
53 setCurrent,
54 setElementSubject: setElement,
55 active,
56 canDrag,
57}: ElementEditProps): JSX.Element {
58 const resource = useResource(subject);
59 const [err, setErr] = useState(null);
60 const [text, setText] = useString(resource, properties.description, {
61 commit: true,
62 handleValidationError: setErr,
63 validate: false,
64 });
65 const [klass] = useArray(resource, properties.isA);
66 const ref = React.useRef(null);
67 const [canWrite, canWriteErr] = useCanWrite(resource);
68
69 /** If it is not a text element */
70 const isAResource =
71 klass.length > 0 && !klass.includes(classes.elements.paragraph);
72
73 function handleOnChange(e: React.ChangeEvent<HTMLTextAreaElement>) {
74 handleResize();
75 setErr(null);
76 setText(e.target.value);
77 }
78
79 /** Let the textarea grow */
80 function handleResize() {
81 if (ref.current) {
82 ref.current.style.height = '0';
83 ref.current.style.height = ref.current.scrollHeight + 'px';
84 }
85 }
86
87 /** Resize the text area when the text changes, or it is set to active */
88 React.useEffect((): void => {
89 handleResize();
90 }, [ref, text, active]);
91
92 /** Auto focus on select, move cursor to end */
93 React.useEffect(() => {
94 ref?.current?.focus();
95 text && ref?.current?.setSelectionRange(text?.length, text?.length);
96 }, [active]);
97
98 useHotkeys(
99 'backspace',
100 e => {
101 const isEmpty = text == '' || text == null;
102 if ((active && isEmpty) || (active && isAResource)) {
103 e.preventDefault();
104 deleteElement(index);
105 }
106 },

Callers

nothing calls this directly

Calls 7

useResourceFunction · 0.90
useStringFunction · 0.90
useArrayFunction · 0.90
useCanWriteFunction · 0.90
deleteElementFunction · 0.85
setElementFunction · 0.85
handleResizeFunction · 0.70

Tested by

no test coverage detected