Skip to content

Commit 4bd6973

Browse files
Merge pull request #75 from valerybugakov/freelife2010/bug/selection-for-multiline
Freelife2010/bug/selection for multiline
2 parents 0ccc9e5 + 5ef7e86 commit 4bd6973

5 files changed

+26
-15
lines changed

src/CreateSelectable.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const createSelectable = (WrappedComponent: ComponentType<any>) =>
2424

2525
node: HTMLElement | null = null
2626

27-
bounds: TComputedBounds | null = null
27+
bounds: TComputedBounds[] | null = null
2828

2929
componentDidMount() {
3030
this.registerSelectable()

src/Selectable.types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type TSelectableItem = Component & {
2424
state: TSelectableItemState
2525
deselected: boolean
2626
node: Maybe<HTMLDivElement>
27-
bounds: Maybe<TComputedBounds>
27+
bounds: Maybe<TComputedBounds[]>
2828
}
2929

3030
export type TSelectableItemProps = TSelectableItemState & {

src/SelectableGroup.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
343343
return
344344
}
345345

346-
const selectboxBounds = getBoundsForNode(selectboxNode)
346+
const [selectboxBounds] = getBoundsForNode(selectboxNode)
347347

348348
this.selectItems({
349349
...selectboxBounds,
@@ -377,7 +377,8 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
377377
return null
378378
}
379379

380-
const isCollided = doObjectsCollide(selectboxBounds, item.bounds!, tolerance, this.props.delta)
380+
const { delta } = this.props
381+
const isCollided = doObjectsCollide(selectboxBounds, item.bounds!, tolerance, delta)
381382
const { isSelecting, isSelected } = item.state
382383

383384
if (isFromClick && isCollided) {
@@ -504,7 +505,7 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
504505
const evt = castTouchToMouseEvent(e)
505506

506507
if (!this.props.globalMouse && !isNodeInRoot(evt.target as any, this.selectableGroup!)) {
507-
const offsetData = getBoundsForNode(this.selectableGroup!)
508+
const [offsetData] = getBoundsForNode(this.selectableGroup!)
508509
const collides = doObjectsCollide(
509510
{
510511
top: offsetData.top,

src/utils/doObjectsCollide.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,30 @@ const areBoundsCollide = (
3030
)
3131
}
3232

33+
function toArray(value: any) {
34+
if (Array.isArray(value)) {
35+
return value
36+
}
37+
38+
return [value]
39+
}
40+
3341
/**
3442
* Given two objects containing "top", "left", "offsetWidth" and "offsetHeight"
3543
* properties, determine if they collide.
3644
*/
3745
export function doObjectsCollide(
38-
a: HTMLElement | TComputedBounds,
39-
b: HTMLElement | TComputedBounds,
46+
a: HTMLElement | TComputedBounds | TComputedBounds[],
47+
b: HTMLElement | TComputedBounds | TComputedBounds[],
4048
tolerance = 0,
4149
delta = 1
4250
) {
43-
const aObj = a instanceof HTMLElement ? getBoundsForNode(a) : a
44-
const bObj = b instanceof HTMLElement ? getBoundsForNode(b) : b
51+
const aBounds = a instanceof HTMLElement ? getBoundsForNode(a) : toArray(a)
52+
const bBounds = b instanceof HTMLElement ? getBoundsForNode(b) : toArray(b)
4553

46-
return areBoundsCollide(aObj, bObj, { tolerance, useOffsetSize: delta === 1 })
54+
for (let i = 0; i < aBounds.length; i++) {
55+
for (let j = 0; j < bBounds.length; j++) {
56+
return areBoundsCollide(aBounds[i], bBounds[j], { tolerance, useOffsetSize: delta === 1 })
57+
}
58+
}
4759
}

src/utils/getBoundsForNode.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,16 @@ export function getDocumentScroll() {
3434
export function getBoundsForNode(
3535
node: HTMLElement,
3636
containerScroll: TGetBoundsForNodeArgs = { scrollTop: 0, scrollLeft: 0 }
37-
) {
37+
): TComputedBounds[] {
3838
const { scrollTop, scrollLeft } = containerScroll
3939
const { documentScrollTop, documentScrollLeft } = getDocumentScroll()
4040

41-
const rect = node.getBoundingClientRect()
42-
43-
return {
41+
return Array.from(node.getClientRects()).map(rect => ({
4442
top: rect.top + documentScrollTop + scrollTop,
4543
left: rect.left + documentScrollLeft + scrollLeft,
4644
offsetWidth: node.offsetWidth,
4745
offsetHeight: node.offsetHeight,
4846
width: rect.width,
4947
height: rect.height
50-
}
48+
}))
5149
}

0 commit comments

Comments
 (0)