scratch-paint/src/helper/hover.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-09-11 10:52:00 -04:00
import paper from 'paper';
2017-09-11 14:23:30 -04:00
import {isBoundsItem, getRootItem} from './item';
import {hoverBounds, hoverItem} from './guides';
import {isGroupChild} from './group';
2017-09-11 10:52:00 -04:00
/**
2017-09-11 14:23:30 -04:00
* @param {!MouseEvent} event mouse event
* @param {?object} hitOptions hit options to use
2017-09-21 10:36:26 -04:00
* @param {?boolean} subselect Whether items within groups can be hovered. If false, the
* entire group should be hovered.
2017-09-11 14:23:30 -04:00
* @return {paper.Item} the hovered item or null if there is none
2017-09-11 10:52:00 -04:00
*/
2017-09-21 10:36:26 -04:00
const getHoveredItem = function (event, hitOptions, subselect) {
2017-09-11 10:52:00 -04:00
const hitResults = paper.project.hitTestAll(event.point, hitOptions);
if (hitResults.length === 0) {
return null;
}
let hitResult;
for (const result of hitResults) {
2017-09-13 15:17:59 -04:00
if (!(result.item.data && result.item.data.noHover) && !result.item.selected) {
2017-09-11 10:52:00 -04:00
hitResult = result;
break;
}
}
if (!hitResult) {
return null;
}
2017-09-11 14:23:30 -04:00
if (isBoundsItem(hitResult.item)) {
return hoverBounds(hitResult.item);
2017-09-21 10:36:26 -04:00
} else if (!subselect && isGroupChild(hitResult.item)) {
2017-09-11 14:23:30 -04:00
return hoverBounds(getRootItem(hitResult.item));
2017-09-11 10:52:00 -04:00
}
2017-09-11 14:23:30 -04:00
return hoverItem(hitResult);
2017-09-11 10:52:00 -04:00
};
export {
2017-09-11 14:23:30 -04:00
getHoveredItem
2017-09-11 10:52:00 -04:00
};