2021-06-03 21:40:27 +00:00
|
|
|
import { current } from 'immer'
|
2021-06-04 16:08:43 +00:00
|
|
|
import { Bounds, Data, ShapeType } from 'types'
|
2021-06-03 21:40:27 +00:00
|
|
|
import BaseSession from './base-session'
|
|
|
|
import { getShapeUtils } from 'lib/shape-utils'
|
2021-06-07 11:18:50 +00:00
|
|
|
import {
|
|
|
|
getBoundsFromPoints,
|
|
|
|
getPage,
|
2021-06-09 16:10:00 +00:00
|
|
|
getPageState,
|
2021-06-07 11:18:50 +00:00
|
|
|
getShapes,
|
2021-06-10 09:49:16 +00:00
|
|
|
getTopParentId,
|
2021-06-07 11:18:50 +00:00
|
|
|
setSelectedIds,
|
|
|
|
setToArray,
|
|
|
|
} from 'utils/utils'
|
2021-06-03 21:40:27 +00:00
|
|
|
import * as vec from 'utils/vec'
|
2021-06-06 10:50:01 +00:00
|
|
|
import state from 'state/state'
|
2021-05-10 12:16:57 +00:00
|
|
|
|
|
|
|
export default class BrushSession extends BaseSession {
|
|
|
|
origin: number[]
|
|
|
|
snapshot: BrushSnapshot
|
|
|
|
|
|
|
|
constructor(data: Data, point: number[]) {
|
|
|
|
super(data)
|
|
|
|
|
|
|
|
this.origin = vec.round(point)
|
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
this.snapshot = getBrushSnapshot(data)
|
2021-05-10 12:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update = (data: Data, point: number[]) => {
|
|
|
|
const { origin, snapshot } = this
|
|
|
|
|
2021-05-18 08:32:20 +00:00
|
|
|
const brushBounds = getBoundsFromPoints([origin, point])
|
2021-05-10 12:16:57 +00:00
|
|
|
|
2021-06-04 17:56:46 +00:00
|
|
|
const hits = new Set<string>([])
|
|
|
|
|
2021-06-09 16:10:00 +00:00
|
|
|
const selectedIds = new Set(snapshot.selectedIds)
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
for (let id in snapshot.shapeHitTests) {
|
2021-06-09 16:10:00 +00:00
|
|
|
if (selectedIds.has(id)) continue
|
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
const { test, selectId } = snapshot.shapeHitTests[id]
|
2021-06-04 17:56:46 +00:00
|
|
|
if (!hits.has(selectId)) {
|
|
|
|
if (test(brushBounds)) {
|
|
|
|
hits.add(selectId)
|
|
|
|
|
|
|
|
// When brushing a shape, select its top group parent.
|
2021-06-07 11:18:50 +00:00
|
|
|
if (!selectedIds.has(selectId)) {
|
|
|
|
selectedIds.add(selectId)
|
2021-06-04 17:56:46 +00:00
|
|
|
}
|
2021-06-07 11:18:50 +00:00
|
|
|
} else if (selectedIds.has(selectId)) {
|
|
|
|
selectedIds.delete(selectId)
|
2021-06-03 21:40:27 +00:00
|
|
|
}
|
2021-05-12 11:27:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-10 12:16:57 +00:00
|
|
|
|
2021-06-09 16:10:00 +00:00
|
|
|
getPageState(data).selectedIds = selectedIds
|
|
|
|
|
2021-05-11 10:13:07 +00:00
|
|
|
data.brush = brushBounds
|
2021-05-10 12:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel = (data: Data) => {
|
|
|
|
data.brush = undefined
|
2021-06-07 11:18:50 +00:00
|
|
|
setSelectedIds(data, this.snapshot.selectedIds)
|
2021-05-10 12:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
complete = (data: Data) => {
|
|
|
|
data.brush = undefined
|
|
|
|
}
|
2021-05-22 15:45:24 +00:00
|
|
|
}
|
2021-05-10 12:16:57 +00:00
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
/**
|
|
|
|
* Get a snapshot of the current selected ids, for each shape that is
|
|
|
|
* not already selected, the shape's id and a test to see whether the
|
|
|
|
* brush will intersect that shape. For tests, start broad -> fine.
|
|
|
|
*/
|
|
|
|
export function getBrushSnapshot(data: Data) {
|
2021-06-09 16:10:00 +00:00
|
|
|
const cData = current(data)
|
|
|
|
const { selectedIds } = getPageState(cData)
|
|
|
|
|
|
|
|
const shapesToTest = getShapes(cData)
|
2021-06-10 09:49:16 +00:00
|
|
|
.filter((shape) => shape.type !== ShapeType.Group && !shape.isHidden)
|
2021-06-09 16:10:00 +00:00
|
|
|
.filter(
|
|
|
|
(shape) => !(selectedIds.has(shape.id) || selectedIds.has(shape.parentId))
|
|
|
|
)
|
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
return {
|
2021-06-09 16:10:00 +00:00
|
|
|
selectedIds: setToArray(selectedIds),
|
2021-05-22 15:45:24 +00:00
|
|
|
shapeHitTests: Object.fromEntries(
|
2021-06-09 16:10:00 +00:00
|
|
|
shapesToTest.map((shape) => {
|
|
|
|
return [
|
|
|
|
shape.id,
|
|
|
|
{
|
|
|
|
selectId: getTopParentId(cData, shape.id),
|
|
|
|
test: (bounds: Bounds) =>
|
|
|
|
getShapeUtils(shape).hitTestBounds(shape, bounds),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
})
|
2021-05-22 15:45:24 +00:00
|
|
|
),
|
2021-05-10 12:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-22 15:45:24 +00:00
|
|
|
|
|
|
|
export type BrushSnapshot = ReturnType<typeof getBrushSnapshot>
|