Skip to content

Commit

Permalink
feat(lasso-tool): add to selection through SHIFT
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Jan 22, 2024
1 parent 2dd354f commit de2b47f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
45 changes: 38 additions & 7 deletions lib/features/lasso-tool/LassoTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
* @typedef {import('../mouse/Mouse').default} Mouse
* @typedef {import('../selection/Selection').default} Selection
* @typedef {import('../tool-manager/ToolManager').default} ToolManager
*
* @typedef {import('../../util/Types').Rect} Rect
*/

var LASSO_TOOL_CURSOR = 'crosshair';
Expand Down Expand Up @@ -117,7 +119,9 @@ export default function LassoTool(
return element;
});

self.select(elements, bbox);
var add = hasSecondaryModifier(event);

self.select(elements, bbox, add);
});

eventBus.on('lasso.start', function(event) {
Expand Down Expand Up @@ -169,18 +173,30 @@ LassoTool.$inject = [
'mouse'
];


/**
* Activate lasso.
*
* @param {MouseEvent} event
* @param {boolean} [autoActivate=false]
*/
LassoTool.prototype.activateLasso = function(event, autoActivate) {

this._dragging.init(event, 'lasso', {
autoActivate: autoActivate,
cursor: LASSO_TOOL_CURSOR,
data: {
context: {}
}
},
keepSelection: true
});
};

/**
* Activate selection.
*
* @param {MouseEvent} event
* @param {boolean} [autoActivate=false]
*/
LassoTool.prototype.activateSelection = function(event, autoActivate) {

this._dragging.init(event, 'lasso.selection', {
Expand All @@ -189,16 +205,27 @@ LassoTool.prototype.activateSelection = function(event, autoActivate) {
cursor: LASSO_TOOL_CURSOR,
data: {
context: {}
}
},
keepSelection: true
});
};

LassoTool.prototype.select = function(elements, bbox) {
/**
* Select elements within the given bounds.
*
* @param {Element[]} elements
* @param {Rect} bbox
* @param {boolean} [add=false]
*/
LassoTool.prototype.select = function(elements, bbox, add) {
var selectedElements = getEnclosedElements(elements, bbox);

this._selection.select(values(selectedElements));
this._selection.select(values(selectedElements), add);
};

/**
* Toggle the lasso tool.
*/
LassoTool.prototype.toggle = function() {
if (this.isActive()) {
return this._dragging.cancel();
Expand All @@ -209,14 +236,18 @@ LassoTool.prototype.toggle = function() {
this.activateSelection(mouseEvent, !!mouseEvent);
};

/**
* Check if the lasso tool is active.
*
* @returns {boolean}
*/
LassoTool.prototype.isActive = function() {
var context = this._dragging.context();

return context && /^lasso/.test(context.prefix);
};



function toBBox(event) {

var start = {
Expand Down
29 changes: 28 additions & 1 deletion test/spec/features/lasso-tool/LassoToolSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ describe('features/lasso-tool', function() {
height: 220
};


// when
lassoTool.select(elements, bbox);

Expand All @@ -113,6 +112,34 @@ describe('features/lasso-tool', function() {
expect(selectedElements[0]).to.equal(childShape2);
expect(selectedElements[1]).to.equal(childShape3);
}));


it('shoud add elements in bbox to selected elements', inject(function(lassoTool, selection) {

// given
selection.select([ childShape ]);

var elements = [ childShape2, childShape3 ];

var bbox = {
x: 175,
y: 0,
width: 120,
height: 220
};

// when
lassoTool.select(elements, bbox, true);

// then
var selectedElements = selection.get();

expect(selectedElements.length).to.equal(3);
expect(selectedElements[0]).to.equal(childShape);
expect(selectedElements[1]).to.equal(childShape2);
expect(selectedElements[2]).to.equal(childShape3);
}));

});


Expand Down

0 comments on commit de2b47f

Please sign in to comment.