Skip to content

Commit

Permalink
fix adding mask duplication issue
Browse files Browse the repository at this point in the history
  • Loading branch information
hxhxhx88 committed Mar 4, 2024
1 parent c477696 commit ab9fa5f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
10 changes: 5 additions & 5 deletions app/frontend/src/common/yjs/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function readComponent(doc: Y.Doc, cid: ComponentId, info: YjsComponent):
}
const {topLeft, bottomRight} = rect;
const comp: RectangleComponent = {type: 'rectangle', topLeft, bottomRight};
return {id: cid, ...comp};
return {...comp, id: cid};
}
case 'polychain': {
const vs = verts.get(cid);
Expand All @@ -100,7 +100,7 @@ export function readComponent(doc: Y.Doc, cid: ComponentId, info: YjsComponent):
}
const {closed} = info;
const comp: PolychainComponent = {type: 'polychain', vertices: vs.toArray(), closed};
return {id: cid, ...comp};
return {...comp, id: cid};
}
case 'mask': {
const mask = masks.get(cid);
Expand All @@ -109,7 +109,7 @@ export function readComponent(doc: Y.Doc, cid: ComponentId, info: YjsComponent):
comps.delete(cid);
break;
}
return {id: cid, ...mask};
return {...mask, id: cid};
}
default:
console.warn('unexpected');
Expand All @@ -126,8 +126,8 @@ function writeComponent(doc: Y.Doc, comp: Component, eid: EntityId, sidx: SliceI
switch (comp.type) {
case 'mask':
doc.transact(() => {
const {id: cid, type} = comp;
masks.set(cid, comp);
const {id: cid, type, ...rest} = comp;
masks.set(cid, {type, ...rest});
comps.set(cid, {type, sidx, eid});
});
break;
Expand Down
6 changes: 5 additions & 1 deletion app/frontend/src/common/yjs/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ function useComponentsListener() {

useEffect(() => {
const fn = (e: Y.YMapEvent<ComponentYjs>) => {
console.log(e);
for (const [cid, cc] of e.changes.keys) {
switch (cc.action) {
case 'add': {
Expand All @@ -37,6 +36,11 @@ function useComponentsListener() {
break;
}

if (info.type === 'mask') {
// adding mask will be handled in `useMasksListener`
break;
}

// add component
const {sidx, eid} = info;
const component = readComponent(doc, cid, info);
Expand Down
10 changes: 5 additions & 5 deletions app/frontend/src/state/annotate/annotation-broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,9 @@ export function useUpdateSliceMasks(): Pick<StateManipulation, 'updateSliceMasks
]);

doc.transact(() => {
adds.forEach(({entityId: eid, component: c}) => {
masks.set(c.id, c);
comps.set(c.id, {type: 'mask', sidx, eid});
adds.forEach(({entityId: eid, component: {id, ...mask}}) => {
masks.set(id, mask);
comps.set(id, {type: 'mask', sidx, eid});
});
removes.forEach(({componentId: cid}) => {
deleteComponentFromDoc(doc, cid, 'mask');
Expand Down Expand Up @@ -874,9 +874,9 @@ function addComponentToDoc(doc: Y.Doc, input: AddComponentInput) {
break;
}
case 'mask': {
const {id: cid, type} = component;
const {id: cid, type, ...rest} = component;
doc.transact(() => {
masks.set(cid, component);
masks.set(cid, {type, ...rest});
comps.set(cid, {type, sidx, eid});
});
break;
Expand Down

0 comments on commit ab9fa5f

Please sign in to comment.