File size: 1,683 Bytes
97420cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import paper from '@turbowarp/paper';
import {isBoundsItem, getRootItem} from './item';
import {hoverBounds, hoverItem} from './guides';
import {isGroupChild} from './group';
import {sortItemsByZIndex} from './math';

/**
 * @param {!MouseEvent} event mouse event
 * @param {?object} hitOptions hit options to use
 * @param {?boolean} subselect Whether items within groups can be hovered. If false, the
 *    entire group should be hovered.
 * @return {paper.Item} the hovered item or null if there is none
 */
const getHoveredItem = function (event, hitOptions, subselect) {
    const oldMatch = hitOptions.match;
    hitOptions.match = hitResult => {
        if (hitResult.item.data && hitResult.item.data.noHover) return false;
        return oldMatch ? oldMatch(hitResult) : true;
    };
    const hitResults = paper.project.hitTestAll(event.point, hitOptions);
    if (hitResults.length === 0) {
        return null;
    }

    // Get highest z-index result
    let hitResult;
    for (const result of hitResults) {
        if (!hitResult || sortItemsByZIndex(hitResult.item, result.item) < 0) {
            hitResult = result;
        }
    }
    const item = hitResult.item;
    // If the hovered item is already selected, then there should be no hovered item.
    if (!item || item.selected) {
        return null;
    }

    let hoverGuide;
    if (isBoundsItem(item)) {
        hoverGuide = hoverBounds(item);
    } else if (!subselect && isGroupChild(item)) {
        hoverGuide = hoverBounds(getRootItem(item));
    } else {
        hoverGuide = hoverItem(item);
    }
    hoverGuide.data.hitResult = hitResult;

    return hoverGuide;
};

export {
    getHoveredItem
};