diff --git a/components/label/usePaperStore.ts b/components/label/usePaperStore.ts index 9c742f7..e56bcc5 100644 --- a/components/label/usePaperStore.ts +++ b/components/label/usePaperStore.ts @@ -805,6 +805,68 @@ export const usePaperStore = create((set) => ({ .updateSelectedPath(useBottomToolsStore.getState().activeImage, "ADD") } + const getPanHitTolerance = (baseTolerance = 8) => + usePaperStore.getState().paperScope + ? baseTolerance / useTopToolsStore.getState().scale + : 0 + + const createPanHitOptions = ( + tolerance: number, + match: (hit: paper.HitResult) => boolean + ) => ({ + segments: true, + stroke: true, + curves: true, + fill: true, + guides: false, + tolerance, + match, + }) + + const resolvePanHitResult = (projectPoint: paper.Point) => { + const group = usePaperStore.getState().group + if (!group) return null + + const supportHit = group.hitTest( + projectPoint, + createPanHitOptions(getPanHitTolerance(), (hit) => + ["pathCircle", "pathBuff"].includes(hit.item.data?.type || "") + ) + ) + if (supportHit) return supportHit + + return group.hitTest( + projectPoint, + createPanHitOptions(0, (hit) => + ["rectangle", "polygon", "brush", "point"].includes( + hit.item.data?.type || "" + ) + ) + ) + } + + const clearCurrentSelection = () => { + let shouldClearSupport = false + + usePaperStore + .getState() + .group!.getItems({ + data: { selected: true }, + }) + .forEach((item) => { + if (item.data.selected) { + item.data.selected = false + shouldClearSupport = true + } + }) + + if (shouldClearSupport) { + usePaperSupportStore.getState().clearAll() + } + + handleCancelSelect() + } + panTool.onMouseDown = (e: { event: MouseEvent; point: paper.Point }) => { if (startMiddleMousePan(e.event)) return if (pressCtrl) return @@ -825,16 +887,7 @@ export const usePaperStore = create((set) => ({ "local" ) - let hitResult = usePaperStore.getState().group?.hitTest(e.point, { - segments: true, - stroke: true, - curves: true, - fill: true, - guide: false, - tolerance: usePaperStore.getState().paperScope - ? 8 / useTopToolsStore.getState().scale - : 0, - }) + let hitResult = resolvePanHitResult(e.point) console.log("hitResult", hitResult) let lastTagDataId = -1 if (activePath?.data.id) { @@ -941,35 +994,16 @@ export const usePaperStore = create((set) => ({ if (pressShift) { } else { - // not press shift - let selectedItems = usePaperStore.getState().group!.getItems({ - data: { selected: true }, - }) - if (selectedItems && selectedItems.length) { - selectedItems.forEach((item) => { - if (item.data.selected) { - let itemHitResult = item.hitTest(point, { - segments: true, - stroke: true, - curves: true, - fill: true, - guide: false, - tolerance: usePaperStore.getState().paperScope - ? 8 / usePaperStore.getState().paperScope!.view.zoom - : 0, - }) + const selectedIds = + useObjectStore.getState().selectedPath[ + useBottomToolsStore.getState().activeImage + ] || [] + const hitId = hitResult?.item?.data?.id + const keepCurrentSelection = + hitId && selectedIds.length === 1 && selectedIds[0] === hitId - if (!itemHitResult) { - item.data.selected = false - usePaperSupportStore.getState().clearAll() - - handleCancelSelect() - } - } - // item.data.selected = false; - }) - } else { - handleCancelSelect() + if (!keepCurrentSelection) { + clearCurrentSelection() } }