-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move multi selection outline into separate feature
related to bpmn-io/bpmn-js#2135
- Loading branch information
1 parent
e662738
commit f0d5377
Showing
5 changed files
with
177 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
|
||
import { | ||
append as svgAppend, | ||
attr as svgAttr, | ||
create as svgCreate, | ||
classes as svgClasses, | ||
clear as svgClear | ||
} from 'tiny-svg'; | ||
|
||
import { assign } from 'min-dash'; | ||
|
||
import { getBBox } from 'lib/util/Elements'; | ||
|
||
var SELECTION_OUTLINE_PADDING = 6; | ||
|
||
/** | ||
* @typedef {import('../../model/Types').Element} Element | ||
* | ||
* @typedef {import('../../core/EventBus').default} EventBus | ||
* @typedef {import('../selection/Selection').default} Selection | ||
* @typedef {import('../../core/Canvas').default} Canvas | ||
*/ | ||
|
||
/** | ||
* @class | ||
* | ||
* A plugin that adds an outline to shapes and connections that may be activated and styled | ||
* via CSS classes. | ||
* | ||
* @param {EventBus} eventBus | ||
* @param {Canvas} canvas | ||
* @param {Selection} selection | ||
*/ | ||
export default function MultiSelectionOutline(eventBus, canvas, selection) { | ||
|
||
this._canvas = canvas; | ||
|
||
var self = this; | ||
|
||
|
||
|
||
eventBus.on('element.changed', function(event) { | ||
if (selection.isSelected(event.element)) { | ||
self._updateMultiSelectionOutline(selection.get()); | ||
} | ||
}); | ||
|
||
eventBus.on('selection.changed', function(event) { | ||
var newSelection = event.newSelection; | ||
|
||
self._updateMultiSelectionOutline(newSelection); | ||
}); | ||
} | ||
|
||
|
||
|
||
MultiSelectionOutline.prototype._updateMultiSelectionOutline = function(selection) { | ||
var layer = this._canvas.getLayer('selectionOutline'); | ||
|
||
svgClear(layer); | ||
|
||
var enabled = selection.length > 1; | ||
|
||
var container = this._canvas.getContainer(); | ||
|
||
svgClasses(container)[enabled ? 'add' : 'remove']('djs-multi-select'); | ||
|
||
if (!enabled) { | ||
return; | ||
} | ||
|
||
var bBox = addSelectionOutlinePadding(getBBox(selection)); | ||
|
||
var rect = svgCreate('rect'); | ||
|
||
svgAttr(rect, assign({ | ||
rx: 3 | ||
}, bBox)); | ||
|
||
svgClasses(rect).add('djs-selection-outline'); | ||
|
||
svgAppend(layer, rect); | ||
}; | ||
|
||
|
||
MultiSelectionOutline.$inject = [ 'eventBus', 'canvas', 'selection' ]; | ||
|
||
// helpers ////////// | ||
|
||
function addSelectionOutlinePadding(bBox) { | ||
return { | ||
x: bBox.x - SELECTION_OUTLINE_PADDING, | ||
y: bBox.y - SELECTION_OUTLINE_PADDING, | ||
width: bBox.width + SELECTION_OUTLINE_PADDING * 2, | ||
height: bBox.height + SELECTION_OUTLINE_PADDING * 2 | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import Outline from './Outline'; | ||
|
||
import MultiSelectionOutline from './MultiSelectionOutline'; | ||
|
||
/** | ||
* @type { import('didi').ModuleDeclaration } | ||
*/ | ||
export default { | ||
__init__: [ 'outline' ], | ||
outline: [ 'type', Outline ] | ||
__init__: [ 'outline', 'multiSelectionOutline' ], | ||
outline: [ 'type', Outline ], | ||
multiSelectionOutline: [ 'type', MultiSelectionOutline ] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
import { | ||
bootstrapDiagram, | ||
inject, | ||
} from 'test/TestHelper'; | ||
|
||
import selectionModule from 'lib/features/selection'; | ||
|
||
import { | ||
classes as domClasses, | ||
query as domQuery | ||
} from 'min-dom'; | ||
|
||
describe('features/outline/MultiSelectionOutline', function() { | ||
|
||
beforeEach(bootstrapDiagram({ modules: [ selectionModule ] })); | ||
|
||
describe('bootstrap', function() { | ||
|
||
beforeEach(bootstrapDiagram({ modules: [ selectionModule ] })); | ||
|
||
it('should bootstrap diagram with component', inject(function() { | ||
|
||
})); | ||
|
||
}); | ||
|
||
|
||
describe('multi selection outline', function() { | ||
|
||
var shape1, shape2, outline; | ||
|
||
beforeEach(inject(function(elementFactory, canvas, selection) { | ||
shape1 = elementFactory.createShape({ | ||
id: 'shape1', | ||
x: 100, y: 100, width: 100, height: 100 | ||
}); | ||
|
||
shape2 = elementFactory.createShape({ | ||
id: 'shape2', | ||
x: 300, y: 100, width: 100, height: 100 | ||
}); | ||
|
||
canvas.addShape(shape1); | ||
canvas.addShape(shape2); | ||
|
||
selection.select([ shape1, shape2 ]); | ||
|
||
outline = domQuery('.djs-selection-outline'); | ||
|
||
})); | ||
|
||
|
||
it('should show box', inject(function() { | ||
expect(outline).to.exist; | ||
})); | ||
|
||
|
||
it('should add djs-multi-select marker', inject(function(selection, canvas) { | ||
|
||
// when | ||
var element = canvas.getContainer(); | ||
|
||
// then | ||
expect(domClasses(element).has('djs-multi-select')).to.be.true; | ||
|
||
// but when | ||
selection.select(null); | ||
|
||
// then | ||
expect(domClasses(element).has('djs-multi-select')).to.be.false; | ||
})); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters