Skip to content

Commit

Permalink
fix: delete array issues and note editor states
Browse files Browse the repository at this point in the history
  • Loading branch information
thesophiaxu committed Jan 11, 2022
1 parent eb0d4f7 commit 2edfecd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/unigraph-dev-backend/src/localUnigraphApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,11 @@ export function getLocalUnigraphAPI(
!items.includes(el._value?.uid) &&
!items.includes(el._value?._value?.uid)
) {
newValues.push(`<${el._index.uid}> <_value.#i> "${newValues.length.toString()}" .`);
if (el._index?.uid)
newValues.push(`<${el._index.uid}> <_value.#i> "${newValues.length.toString()}" .`);
} else {
if (relUid) {
toDel += `<${el._value.uid}> <unigraph.origin> <${+relUid}> .\n`;
if (el._value?.uid) toDel += `<${el._value.uid}> <unigraph.origin> <${+relUid}> .\n`;
if (el?._value?._value?.uid)
toDel += `<${el._value._value.uid}> <unigraph.origin> <${+relUid}> .\n`;
if (el?._value?._value?._value?.uid)
Expand Down
35 changes: 27 additions & 8 deletions packages/unigraph-dev-explorer/src/examples/notes/NoteBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,21 @@ export function DetailedNoteBlock({ data, isChildren, callbacks, options, isColl

const [isChildrenCollapsed, setIsChildrenCollapsed] = React.useState<any>({});

const newTextSinceDataUpdating = React.useRef<any>(false);
React.useEffect(() => {
if (newTextSinceDataUpdating.current !== false) {
if (textInput.current?.firstChild?.textContent !== undefined) {
textInput.current.firstChild.textContent = textInput.current.firstChild.textContent.slice(
newTextSinceDataUpdating.current.caret,
);
setCaret(document, textInput.current.firstChild, newTextSinceDataUpdating.current.offset);
}
newTextSinceDataUpdating.current = false;
inputDebounced.current(textInput.current.firstChild.textContent);
inputDebounced.current.flush();
}
}, [data]);

React.useEffect(() => {
const newNodes = _.unionBy(
[
Expand Down Expand Up @@ -562,21 +577,25 @@ export function DetailedNoteBlock({ data, isChildren, callbacks, options, isColl
textref.current = ev.currentTarget.textContent;
onNoteInput(inputDebounced, ev);
}}
onKeyDown={async (ev) => {
onKeyUp={(ev) => {
if (newTextSinceDataUpdating.current !== false) {
newTextSinceDataUpdating.current.offset += 1;
}
}}
onKeyDown={(ev) => {
const sel = document.getSelection();
const caret = _.min([sel?.anchorOffset, sel?.focusOffset]) as number;
switch (ev.keyCode) {
case 13: // enter
ev.preventDefault();
edited.current = false;
newTextSinceDataUpdating.current = { caret, offset: 0 };
inputDebounced.current.cancel();
setCommand(() =>
callbacks['split-child']?.bind(
null,
textref.current || data.get('text').as('primitive'),
caret,
),
);
callbacks['split-child']?.bind(
null,
textref.current || data.get('text').as('primitive'),
caret,
)();
break;

case 9: // tab
Expand Down

0 comments on commit 2edfecd

Please sign in to comment.