Skip to content

Commit

Permalink
feat: use #scrollIntoView to make elements visible
Browse files Browse the repository at this point in the history
* scroll auto-placed elements into view
* use centralized method in SearchPad

related to camunda/camunda-modeler#1249
  • Loading branch information
marstamm committed Apr 23, 2021
1 parent 0129993 commit 9171f4f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
9 changes: 7 additions & 2 deletions lib/features/auto-place/AutoPlace.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var LOW_PRIORITY = 100;
* @param {EventBus} eventBus
* @param {Modeling} modeling
*/
export default function AutoPlace(eventBus, modeling) {
export default function AutoPlace(eventBus, modeling, canvas) {

eventBus.on('autoPlace', LOW_PRIORITY, function(context) {
var shape = context.shape,
Expand All @@ -24,6 +24,10 @@ export default function AutoPlace(eventBus, modeling) {
return getNewShapePosition(source, shape);
});

eventBus.on('autoPlace.end', function(event) {
canvas.scrollToElement(event.shape);
});

/**
* Append shape to source at appropriate position.
*
Expand Down Expand Up @@ -59,7 +63,8 @@ export default function AutoPlace(eventBus, modeling) {

AutoPlace.$inject = [
'eventBus',
'modeling'
'modeling',
'canvas'
];

// helpers //////////
Expand Down
27 changes: 2 additions & 25 deletions lib/features/search-pad/SearchPad.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ SearchPad.prototype._preselect = function(node) {

this._resetOverlay(element);

this._centerViewbox(element);
this._canvas.scrollToElement(element, { top: 400 });

this._selection.select(element);

Expand All @@ -408,37 +408,14 @@ SearchPad.prototype._select = function(node) {

this._resetOverlay();

this._centerViewbox(element);
this._canvas.scrollToElement(element, { top: 400 });

this._selection.select(element);

this._eventBus.fire('searchPad.selected', element);
};


/**
* Center viewbox on the element middle point.
*
* @param {Element} element
*/
SearchPad.prototype._centerViewbox = function(element) {
var viewbox = this._canvas.viewbox();

var box = getBoundingBox(element);

var newViewbox = {
x: (box.x + box.width/2) - viewbox.outer.width/2,
y: (box.y + box.height/2) - viewbox.outer.height/2,
width: viewbox.outer.width,
height: viewbox.outer.height
};

this._canvas.viewbox(newViewbox);

this._canvas.zoom(viewbox.scale);
};


/**
* Reset overlay removes and, optionally, set
* overlay to a new element.
Expand Down
25 changes: 25 additions & 0 deletions test/spec/features/auto-place/AutoPlaceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ describe('features/auto-place', function() {
expect(selection.get()).to.eql([ newShape ]);
}));


it('should scroll into view', inject(function(autoPlace, canvas) {

// given
var container = canvas.getContainer();
container.style.width = '500px';
container.style.height = '500px';

canvas.viewbox({
x: -400,
y: -450,
width: 500,
height: 500
});

// when
autoPlace.append(shape, newShape);

// then
var newViewbox = canvas.viewbox();
expect(newViewbox.x).to.eql(-150);
expect(newViewbox.y).to.eql(-300);

}));

});


Expand Down
10 changes: 5 additions & 5 deletions test/spec/features/search-pad/SearchPadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ describe('features/searchPad', function() {
}));


it('select should center viewbox on an element', inject(function(searchPad, canvas) {
it('select should scroll element into view', inject(function(searchPad, canvas) {

// given
typeText(input_node, 'one');
Expand All @@ -314,8 +314,8 @@ describe('features/searchPad', function() {
container.style.height = '1000px';

canvas.viewbox({
x: 0,
y: 0,
x: 1000,
y: 1000,
width: 1000,
height: 1000
});
Expand All @@ -325,8 +325,8 @@ describe('features/searchPad', function() {

// then
var newViewbox = canvas.viewbox();
expect(newViewbox).to.have.property('x', -450);
expect(newViewbox).to.have.property('y', -460);
expect(newViewbox).to.have.property('x', -100);
expect(newViewbox).to.have.property('y', -400);
}));


Expand Down

0 comments on commit 9171f4f

Please sign in to comment.