Skip to content

Commit

Permalink
feat(search-pad): reset selection and viewbox on escape without enter
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Jun 1, 2024
1 parent fc55baf commit 35ff51c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
17 changes: 17 additions & 0 deletions lib/features/search-pad/SearchPad.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export default function SearchPad(canvas, eventBus, overlays, selection, transla
this._open = false;
this._results = [];
this._eventMaps = [];
this._cachedSelection = null;
this._cachedViewbox = null;

this._canvas = canvas;
this._eventBus = eventBus;
Expand Down Expand Up @@ -328,6 +330,9 @@ SearchPad.prototype.open = function() {
return;
}

this._cachedSelection = this._selection.get();
this._cachedViewbox = this._canvas.viewbox();

this._bindEvents();

this._open = true;
Expand All @@ -348,6 +353,14 @@ SearchPad.prototype.close = function() {
return;
}

if (this._cachedSelection) {
this._selection.select(this._cachedSelection);
}

if (this._cachedViewbox) {
this._canvas.viewbox(this._cachedViewbox);
}

this._unbindEvents();

this._open = false;
Expand Down Expand Up @@ -406,6 +419,7 @@ SearchPad.prototype._preselect = function(node) {

this._resetOverlay(element);

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

this._selection.select(element);
Expand All @@ -423,6 +437,9 @@ SearchPad.prototype._select = function(node) {
var id = domAttr(node, SearchPad.RESULT_ID_ATTRIBUTE);
var element = this._results[id].element;

this._cachedSelection = null;
this._cachedViewbox = null;

this.close();

this._resetOverlay();
Expand Down
52 changes: 42 additions & 10 deletions test/spec/features/search-pad/SearchPadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,11 @@ describe('features/searchPad', function() {

describe('searching/selection', function() {

var element;

beforeEach(inject(function(searchPad) {
beforeEach(inject(function(selection, searchPad) {

// given
element = searchProvider.find('one')[0].element;
selection.select(elements.one.a);

searchPad.open();
}));

Expand Down Expand Up @@ -286,13 +285,30 @@ describe('features/searchPad', function() {
});


it('should reset selection on escape without enter', inject(function(selection) {

// given
selection.select(elements.one.a);

typeText(input_node, 'two');

expect(selection.isSelected(elements.one.a)).to.be.false;

// when
triggerKeyEvent(input_node, 'keyup', 'Escape');

// then
expect(selection.isSelected(elements.one.a)).to.be.true;
}));


it('should set overlay on an highlighted element', inject(function(overlays) {

// when
typeText(input_node, 'one');

// then
var overlay = overlays.get({ element: element });
var overlay = overlays.get({ element: elements.one.a });
expect(overlay).length(1);
}));

Expand All @@ -306,7 +322,7 @@ describe('features/searchPad', function() {
triggerKeyEvent(input_node, 'keyup', 'Enter');

// then
var overlay = overlays.get({ element: element });
var overlay = overlays.get({ element: elements.one.a });
expect(overlay).length(0);
}));

Expand Down Expand Up @@ -337,7 +353,7 @@ describe('features/searchPad', function() {
}));


it('select should keep zoom level', inject(function(canvas) {
it('select set zoom level to 1', inject(function(canvas) {

// given
canvas.zoom(0.4);
Expand All @@ -348,8 +364,24 @@ describe('features/searchPad', function() {
triggerKeyEvent(input_node, 'keyup', 'Enter');

// then
var newViewbox = canvas.viewbox();
expect(newViewbox).to.have.property('scale', 0.4);
expect(canvas.zoom()).to.equal(1);
}));


it('select reset viewbox on escape without enter', inject(function(canvas) {

// given
var viewbox = canvas.viewbox();

typeText(input_node, 'two');

expect(canvas.viewbox()).not.to.eql(viewbox);

// when
triggerKeyEvent(input_node, 'keyup', 'Escape');

// then
expect(canvas.viewbox()).to.eql(viewbox);
}));


Expand All @@ -362,7 +394,7 @@ describe('features/searchPad', function() {
triggerKeyEvent(input_node, 'keyup', 'Enter');

// then
expect(selection.isSelected(element)).to.be.true;
expect(selection.isSelected(elements.one.a)).to.be.true;
}));


Expand Down

0 comments on commit 35ff51c

Please sign in to comment.