Skip to content

Commit

Permalink
fix(editor): more efficient add text below
Browse files Browse the repository at this point in the history
  • Loading branch information
thesophiaxu committed Feb 4, 2022
1 parent e30d673 commit 3c87eba
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
21 changes: 10 additions & 11 deletions packages/unigraph-dev-explorer/src/examples/notes/NoteBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ const getCallbacks = (callbacks: any, data: any, editorContext: any, elindex: an
},
'focus-last-dfs-node': focusLastDFSNode,
'focus-next-dfs-node': focusNextDFSNode,
'add-children': (its: string[], indexx?: number) =>
'add-children': (its: string[], indexx?: number, changeValue?: string | false) =>
indexx
? addChildren(data, editorContext, elindex + indexx, its)
: addChildren(data, editorContext, elindex, its),
? addChildren(data, editorContext, elindex + indexx, its, changeValue)
: addChildren(data, editorContext, elindex, its, changeValue),
'add-parent-backlinks': (childrenUids: string[]) => {
const parents = getParentsAndReferences(
data['~_value'],
Expand Down Expand Up @@ -520,15 +520,15 @@ export function DetailedNoteBlock({
if (!callbacks?.viewId) callbacks = { ...(callbacks || {}), viewId: getRandomInt() };
const [subentities, otherChildren] = getSubentities(data);
const [command, setCommand] = React.useState<() => any | undefined>();
const inputter = (text: string, eagarlyUpdate = false) => {
const inputter = (text: string) => {
if (data?._value?.children?.['_value[']) {
const deadLinks: any = [];
data._value.children['_value['].forEach((el: any) => {
if (el && el._key && !text.includes(el._key)) deadLinks.push(el.uid);
});
if (deadLinks.length) window.unigraph.deleteItemFromArray(data._value.children.uid, deadLinks, data.uid);
}
if (eagarlyUpdate) edited.current = true;

return window.unigraph.updateObject(
data.get('text')._value._value.uid,
{
Expand All @@ -538,7 +538,6 @@ export function DetailedNoteBlock({
false,
callbacks.subsId,
[],
eagarlyUpdate,
);
};
const textInput: any = React.useRef();
Expand Down Expand Up @@ -816,7 +815,6 @@ export function DetailedNoteBlock({
callbacks['split-child'](
getCurrentText() || data.get('text')?.as('primitive'),
textInput.current.selectionStart,
inputter,
);
},
indentChild: callbacks['indent-child'],
Expand Down Expand Up @@ -875,12 +873,13 @@ export function DetailedNoteBlock({
document.execCommand('insertText', false, lines[0]);

edited.current = true;
inputDebounced.current(getCurrentText());
inputDebounced.current.flush();

if (lines.length > 1) {
const newLines = lines.slice(1);
callbacks['add-children'](newLines);
callbacks['add-children'](newLines, undefined, getCurrentText());
} else {
inputDebounced.current(getCurrentText());
inputDebounced.current.flush();
}
}

Expand Down Expand Up @@ -973,7 +972,7 @@ export function DetailedNoteBlock({
edited.current = false;
inputDebounced.current.cancel();
const currentText = getCurrentText() || data.get('text').as('primitive');
callbacks['split-child']?.(currentText, caret, inputter);
callbacks['split-child']?.(currentText, caret);
// setCurrentText(currentText.slice(caret));
setCaretPostRender(0);
} else if (ev.ctrlKey || ev.metaKey) {
Expand Down
52 changes: 39 additions & 13 deletions packages/unigraph-dev-explorer/src/examples/notes/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,23 @@ export const getSubentities = (data: any) =>
(el: any) => el?._value?.type?.['unigraph.id'] === '$/schema/subentity',
);

export const addChild = (data: any, context: NoteEditorContext, index?: number) => {
export const addChild = (
data: any,
context: NoteEditorContext,
index?: number,
changeValue: false | string = false,
) => {
if (typeof index === 'undefined') index = (getSemanticChildren(data)?.['_value[']?.length || 0) - 1;
return addChildren(data, context, index, ['']);
return addChildren(data, context, index, [''], changeValue);
};

export const addChildren = (data: any, context: NoteEditorContext, index: number, children: string[]) => {
export const addChildren = (
data: any,
context: NoteEditorContext,
index: number,
children: string[],
changeValue?: false | string,
) => {
let uidMode = false;
if (children.filter((el) => el.startsWith('0x')).length === children.length) uidMode = true;
if (typeof index === 'undefined') index = (getSemanticChildren(data)?.['_value[']?.length || 0) - 1;
Expand Down Expand Up @@ -64,6 +75,29 @@ export const addChildren = (data: any, context: NoteEditorContext, index: number
? el._index['_value.#i'] + children.length
: el._index['_value.#i'],
},
...(changeValue && el._index?.['_value.#i'] === index
? {
_value: {
uid: el._value.uid,
_value: {
uid: el._value._value.uid,
_value: {
uid: el._value._value._value.uid,
text: {
uid: el?._value?._value?._value?.text?.uid,
_value: {
uid: el?._value?._value?._value?.text?._value?.uid,
_value: {
uid: el?._value?._value?._value?.text?._value?._value?.uid,
'_value.%': changeValue,
},
},
},
},
},
},
}
: {}),
})),
...children.map((el: string, i: number) => ({
_value: {
Expand Down Expand Up @@ -110,22 +144,14 @@ export const addChildren = (data: any, context: NoteEditorContext, index: number
focusUid(myUid);
};

export const splitChild = (
data: any,
context: NoteEditorContext,
index: number,
oldtext: string,
at: number,
inputter?: any,
) => {
export const splitChild = (data: any, context: NoteEditorContext, index: number, oldtext: string, at: number) => {
// console.log(JSON.stringify([data, index, at], null, 4))
console.log(getSubentities(data).sort(byElementIndex)[index]?._value?._value);
if (
oldtext.slice(at) === '' &&
!getSubentities(getSubentities(data).sort(byElementIndex)[index]?._value?._value).length
) {
inputter(oldtext, true);
addChild(data, context, index);
addChild(data, context, index, oldtext);
return;
}
const parents = getParents(data);
Expand Down

0 comments on commit 3c87eba

Please sign in to comment.