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'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { getShapeUtils } from 'state/shape-utils'
|
2021-07-09 08:59:43 +00:00
|
|
|
import { deepClone, getBoundsFromPoints } from 'utils'
|
2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-06-29 12:00:59 +00:00
|
|
|
import tld from 'utils/tld'
|
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
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
update = (data: Data, point: number[]): void => {
|
2021-05-10 12:16:57 +00:00
|
|
|
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-07-09 08:59:43 +00:00
|
|
|
const selectedIds = [...snapshot.selectedIds]
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
for (const id in snapshot.shapeHitTests) {
|
2021-07-09 08:59:43 +00:00
|
|
|
if (selectedIds.includes(id)) continue
|
2021-06-09 16:10:00 +00:00
|
|
|
|
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-07-09 08:59:43 +00:00
|
|
|
if (!selectedIds.includes(selectId)) {
|
|
|
|
selectedIds.push(selectId)
|
2021-06-04 17:56:46 +00:00
|
|
|
}
|
2021-07-09 08:59:43 +00:00
|
|
|
} else if (selectedIds.includes(selectId)) {
|
|
|
|
selectedIds.splice(selectedIds.indexOf(selectId), 1)
|
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-29 12:00:59 +00:00
|
|
|
tld.getPageState(data).selectedIds = selectedIds
|
2021-06-09 16:10:00 +00:00
|
|
|
|
2021-05-11 10:13:07 +00:00
|
|
|
data.brush = brushBounds
|
2021-05-10 12:16:57 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
cancel = (data: Data): void => {
|
2021-05-10 12:16:57 +00:00
|
|
|
data.brush = undefined
|
2021-06-29 12:00:59 +00:00
|
|
|
tld.setSelectedIds(data, this.snapshot.selectedIds)
|
2021-05-10 12:16:57 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
complete = (data: Data): void => {
|
2021-05-10 12:16:57 +00:00
|
|
|
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.
|
|
|
|
*/
|
2021-06-21 21:35:28 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-05-22 15:45:24 +00:00
|
|
|
export function getBrushSnapshot(data: Data) {
|
2021-06-29 14:54:46 +00:00
|
|
|
const cData = data
|
2021-06-29 12:00:59 +00:00
|
|
|
const { selectedIds } = tld.getPageState(cData)
|
2021-06-09 16:10:00 +00:00
|
|
|
|
2021-06-29 12:00:59 +00:00
|
|
|
const shapesToTest = tld
|
|
|
|
.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(
|
2021-07-09 08:59:43 +00:00
|
|
|
(shape) =>
|
|
|
|
!(
|
|
|
|
selectedIds.includes(shape.id) || selectedIds.includes(shape.parentId)
|
|
|
|
)
|
2021-06-09 16:10:00 +00:00
|
|
|
)
|
2021-06-29 14:54:46 +00:00
|
|
|
.map(deepClone)
|
2021-06-09 16:10:00 +00:00
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
return {
|
2021-07-09 08:59:43 +00:00
|
|
|
selectedIds: [...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,
|
|
|
|
{
|
2021-06-29 14:54:46 +00:00
|
|
|
selectId: tld.getTopParentId(data, shape.id),
|
|
|
|
test: (bounds: Bounds) => {
|
|
|
|
return getShapeUtils(shape).hitTestBounds(shape, bounds)
|
|
|
|
},
|
2021-06-09 16:10:00 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
})
|
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>
|