Skip to content

Commit

Permalink
sync paste
Browse files Browse the repository at this point in the history
  • Loading branch information
hxhxhx88 committed Feb 2, 2024
1 parent 6a5a1f0 commit 036e55a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
13 changes: 8 additions & 5 deletions app/frontend/src/component/panel/layer/Idle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,14 @@ const PasteLoaded: FC<{copying: {ecids: EntityComponentId[]; sliceIndex: SliceIn
'ctrl+v, meta+v',
useCallback(() => {
const cs = ecids.map(ec => ({...ec, newComponentId: uuidv4()}));
paste({
entityComponents: cs,
sourceSliceIndex: sliceIndex,
targetSliceIndex: sidx,
});
paste(
{
entityComponents: cs,
sourceSliceIndex: sliceIndex,
targetSliceIndex: sidx,
},
{broadcast: true}
);
}, [sliceIndex, ecids, sidx, paste])
);

Expand Down
13 changes: 8 additions & 5 deletions app/frontend/src/component/panel/menu/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ export function useEntityActions(): Action[] {
return;
}
const cs = clipboard.ecids.map(ec => ({...ec, newComponentId: uuidv4()}));
paste({
entityComponents: cs,
sourceSliceIndex: clipboard.sliceIndex,
targetSliceIndex: sliceIndex,
});
paste(
{
entityComponents: cs,
sourceSliceIndex: clipboard.sliceIndex,
targetSliceIndex: sliceIndex,
},
{broadcast: true}
);
},
},
];
Expand Down
10 changes: 8 additions & 2 deletions app/frontend/src/state/annotate/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type State = {
setAnnotation: (annotation: Annotation | undefined) => void;

commitDraftComponents: () => void;
paste: (input: PasteInput) => void;

// general
addComponent: (input: AddComponentInput, opts?: Options) => void;
Expand All @@ -53,6 +52,9 @@ export type State = {

// mask
updateSliceMasks: (input: UpdateSliceMasksInput, opts?: Options) => void;

// other
paste: (input: PasteInput, opts?: Options) => void;
};

export type SetEntityCategoryInput = {
Expand Down Expand Up @@ -441,7 +443,7 @@ export const useStore = create<State>()(
});
},

paste: (input: PasteInput) => {
paste: (input: PasteInput, opts = {}) => {
const {entityComponents: ecs, sourceSliceIndex: sidx0, targetSliceIndex: sidx1} = input;
set(s => {
ecs.forEach(({entityId: eid, componentId: cid0, newComponentId: cid1}) => {
Expand All @@ -451,6 +453,10 @@ export const useStore = create<State>()(
addComponent(s, sidx1, eid, c1);
}
});

if (opts.broadcast) {
syncAnnotation.paste(input);
}
});
},

Expand Down
59 changes: 57 additions & 2 deletions app/frontend/src/sync/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
UpdateSliceMasksInput,
} from 'state/annotate/annotation';
import {ComponentInfo, annoDoc} from './doc';
import {ComponentId, MaskComponent, PolychainComponent, RectangleComponent} from 'type/annotation';
import {ComponentDetail, ComponentId, MaskComponent, PolychainComponent, RectangleComponent} from 'type/annotation';
import {initialVertexBezier} from 'common/annotation';
import {newComponentAdapter} from 'common/adapter';

Expand Down Expand Up @@ -250,7 +250,62 @@ export const syncAnnotation: Omit<State, 'annotation' | 'setAnnotation'> = {
rects.set(componentId, {topLeft, bottomRight});
},
paste: (input: PasteInput) => {
console.error('not implemented', input);
const {entityComponents: ecs, sourceSliceIndex: sidx0, targetSliceIndex: sidx1} = input;

const comps = annoDoc.componentInfoMap();
const masks = annoDoc.maskMap();
const polys = annoDoc.polychainVerticesMap();
const rects = annoDoc.rectangleAnchorsMap();

annoDoc.transact(() => {
ecs.forEach(({entityId: eid, componentId: cid0, newComponentId: cid1}) => {
const comp = comps.get(cid0);
if (!comp) {
console.warn('component not found', cid0);
return;
}
if (comp.entityId !== eid || comp.sliceIndex !== sidx0) {
console.warn(
`expect component ${cid0} be at [${eid}, ${sidx0}] but got [${comp.entityId}, ${comp.sliceIndex}]`
);
return;
}

switch (comp.type) {
case 'mask': {
const c = masks.get(cid0);
if (!c) {
console.warn('mask not found', cid0);
return;
}
addComponent({sliceIndex: sidx1, entityId: eid, component: {...c, id: cid1}});
break;
}
case 'polychain': {
const vs = polys.get(cid0);
if (!vs) {
console.warn('polychain not found', cid0);
return;
}
const c: ComponentDetail = {type: 'polychain', vertices: vs.toArray(), closed: comp.closed};
addComponent({sliceIndex: sidx1, entityId: eid, component: {...c, id: cid1}});
break;
}
case 'rectangle': {
const rect = rects.get(cid0);
if (!rect) {
console.warn('rectangle not found', cid0);
return;
}
const c: ComponentDetail = {type: 'rectangle', ...rect};
addComponent({sliceIndex: sidx1, entityId: eid, component: {...c, id: cid1}});
break;
}
default:
console.error('not implemented', input);
}
});
});
},
translate: (input: TranslateInput) => {
const {entityComponents: ecs, offsetX: dx, offsetY: dy} = input;
Expand Down

0 comments on commit 036e55a

Please sign in to comment.