Skip to content

Commit

Permalink
fix(editor): more editor fixes, general responsiveness, and todo interop
Browse files Browse the repository at this point in the history
  • Loading branch information
thesophiaxu committed Mar 3, 2022
1 parent ce045a3 commit 3a8b5a6
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 16 deletions.
8 changes: 3 additions & 5 deletions packages/unigraph-dev-common/src/api/unigraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
};
sendEvent(connection, 'get_object', { uidOrName, options, id });
}),
addObject: (object, schema, id?: any, subIds?: any) =>
addObject: (object, schema, padding?: any, subIds?: any, id?: any) =>
new Promise((resolve, reject) => {
id = id || getRandomInt();
callbacks[id] = (response: any) => {
Expand All @@ -388,6 +388,7 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
object,
schema,
id,
padding,
subIds,
});
}),
Expand Down Expand Up @@ -438,16 +439,13 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
}

subResults[subId] = newObj;
setTimeout(() => {
// console.log(newObj);
subscriptions[subId](newObj);
}, 0);

// Record state changes
if (id) {
if (subFakeUpdates[subId] === undefined) subFakeUpdates[subId] = [id];
else subFakeUpdates[subId].push(id);
}
subscriptions[subId](newObj);
},
deleteRelation: (uid, relation) => {
sendEvent(connection, 'delete_relation', { uid, relation });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export function AutoDynamicView({

if (!callbacks) callbacks = {};

if (object?.constructor.name !== 'UnigraphObject') object = new UnigraphObject(object);

const shouldGetBacklinks =
finalOptions.shouldGetBacklinks || (!excludableTypes.includes(object?.type?.['unigraph.id']) && !inline);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { getRandomInt } from 'unigraph-dev-common/lib/utils/utils';
import { getRandomInt, UnigraphObject } from 'unigraph-dev-common/lib/utils/utils';
import { TabContext } from '../../../utils';
import { isStub } from '../utils';

Expand All @@ -14,7 +14,11 @@ export const useSubscriptionDelegate: (...args: any) => [() => any, number] = (
const [loadedObj, setLoadedObj] = React.useState<any>(false);
const [subsId, setSubsId] = React.useState(0);

const getObject = React.useCallback(() => (isObjectStub ? loadedObj : object), [loadedObj, object, isObjectStub]);
const getObject = React.useCallback(() => {
let obj = isObjectStub ? loadedObj : object;
if (obj?.constructor.name !== 'UnigraphObject' && obj?.uid) obj = new UnigraphObject(obj);
return obj;
}, [loadedObj, object, isObjectStub]);

React.useEffect(() => {
if (!isObjectStub) setLoadedObj(object);
Expand All @@ -23,7 +27,6 @@ export const useSubscriptionDelegate: (...args: any) => [() => any, number] = (
React.useEffect(() => {
const newSubs = getRandomInt();
if (isObjectStub && object?.uid !== uidRef.current) {
uidRef.current = object?.uid;
// console.log(tabContext);
// if (subsId) tabContext.unsubscribe(subsId);
let query = objectView?.query?.('QUERYFN_TEMPLATE');
Expand All @@ -43,6 +46,7 @@ export const useSubscriptionDelegate: (...args: any) => [() => any, number] = (
},
},
(newObjects: any[]) => {
uidRef.current = object?.uid;
setLoadedObj(newObjects[0]);
},
newSubs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const isStub = (object: any) =>
object.type &&
typeof object.type['unigraph.id'] === 'string' &&
typeof object.type['unigraph.id'].startsWith('$/') &&
(Object.keys(object).length === 3 || Object.keys(object).filter((el) => el.startsWith('_value')).length === 0));
Object.keys(object).filter((el) => el.startsWith('_value')).length === 0);

export function SubentityDropAcceptor({ uid, display }: any) {
const [{ isOver, canDrop }, dropSub] = useDrop(() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ export function DetailedNoteBlock({
componentId={componentId}
displayAs={displayAs}
pullText={(uid: boolean) =>
uid ? data?._value?.text?._value?._value?.uid : data.get('text')?.as('primitive')
new UnigraphObject(data).get('text')?.as('primitiveRef')?.[uid ? 'uid' : '_value.%']
}
pushText={(text: string) => {
return data._hide
Expand Down Expand Up @@ -968,7 +968,7 @@ export const ReferenceNoteView = ({ data, callbacks, noChildren }: any) => {
refinedPath
.map((el: any) => new UnigraphObject(el)?.get('text')?.as('primitive'))
.filter(Boolean)
.slice(0, noChildren ? undefined : -2),
.slice(0, noChildren ? undefined : -1),
),
);
}, []);
Expand Down
10 changes: 8 additions & 2 deletions packages/unigraph-dev-explorer/src/examples/notes/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,13 @@ export const convertChildToTodo = async (data: any, context: NoteEditorContext,
todoObj.uid = window.unigraph.leaseUid?.();
clearEmpties(todoObj);
console.log(todoObj);
const paddedObj = buildUnigraphEntity(JSON.parse(JSON.stringify(todoObj)), '$/schema/todo', schemas);
const paddedObj: any = buildUnigraphEntity(JSON.parse(JSON.stringify(todoObj)), '$/schema/todo', schemas);
paddedObj._value.uid = window.unigraph.leaseUid?.();
paddedObj._value.name.uid = window.unigraph.leaseUid?.();
paddedObj['unigraph.indexes'].name.uid = paddedObj._value.name.uid;
paddedObj._value.name._value.uid = window.unigraph.leaseUid?.();
paddedObj._value.name._value._value.uid = window.unigraph.leaseUid?.();

stubConverted._value = paddedObj;

addCommand(context.historyState.value, [
Expand All @@ -1104,7 +1110,7 @@ export const convertChildToTodo = async (data: any, context: NoteEditorContext,
focusedState.setValue({ ...focusedState.value, component: undefined });

stubConverted._value = { uid: todoObj.uid };
await window.unigraph.addObject(todoObj, '$/schema/todo', undefined, []);
await window.unigraph.addObject(paddedObj, '$/schema/todo', true, []);
// eslint-disable-next-line prefer-destructuring
await window.unigraph.updateObject(
data?._value?.uid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ const editorPlugin = {
.filter((el: any) => el?._value?._value?.type?.['unigraph.id'] === '$/schema/tag')
.map((tag: any) => `#${tag?._value?._value?._value?.name?.['_value.%']}`);
const name = new UnigraphObject(data).get('name')?.as('primitive');
return name === undefined ? undefined : `${name}${tags.length ? ` ${tags.join(' ')}` : ''}`;
return name === undefined
? undefined
: `${name}${tags.length ? ` ${tags.join(' ')}` : ''}${name.trim().length ? '' : ' '}`;
},
pushText: (subsId: any, data: any, text: string) => {
// console.log(data);
Expand Down

0 comments on commit 3a8b5a6

Please sign in to comment.