Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make hosts sticky for valid attachments #368

Merged
merged 1 commit into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions assets/diagram-js.css
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ marker.djs-dragger text {
pointer-events: none;
}

.djs-element.attach-ok .djs-hit {
stroke-width: 60px !important;
}

/**
* all pointer events for hit shape
*/
Expand Down
35 changes: 32 additions & 3 deletions lib/features/attach-support/AttachSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ import { getNewAttachShapeDelta } from '../../util/AttachUtil';

import inherits from 'inherits';

import CommandInterceptor from '../../command/CommandInterceptor';


var LOW_PRIORITY = 251,
HIGH_PRIORITY = 1401;

import CommandInterceptor from '../../command/CommandInterceptor';
var MARKER_ATTACH = 'attach-ok';


/**
Expand All @@ -34,10 +37,11 @@ import CommandInterceptor from '../../command/CommandInterceptor';
*
* @param {didi.Injector} injector
* @param {EventBus} eventBus
* @param {Canvas} canvas
* @param {Rules} rules
* @param {Modeling} modeling
*/
export default function AttachSupport(injector, eventBus, rules, modeling) {
export default function AttachSupport(injector, eventBus, canvas, rules, modeling) {

CommandInterceptor.call(this, eventBus);

Expand Down Expand Up @@ -73,6 +77,30 @@ export default function AttachSupport(injector, eventBus, rules, modeling) {
});
});

// add attach-ok marker to current host
movePreview && eventBus.on('shape.move.start', function(event) {
var context = event.context,
shapes = context.shapes;

if (shapes.length !== 1) {
return;
}

var shape = shapes[0];

var host = shape.host;

if (host) {
canvas.addMarker(host, MARKER_ATTACH);

eventBus.once([
'shape.move.out',
'shape.move.cleanup'
], function() {
canvas.removeMarker(host, MARKER_ATTACH);
});
}
});

// add all attachers to move closure
this.preExecuted('elements.move', HIGH_PRIORITY, function(e) {
Expand All @@ -95,7 +123,7 @@ export default function AttachSupport(injector, eventBus, rules, modeling) {
attachers;

// we only support attachment / detachment of one element
if (shapes.length > 1) {
if (shapes.length !== 1) {
return;
}

Expand Down Expand Up @@ -267,6 +295,7 @@ inherits(AttachSupport, CommandInterceptor);
AttachSupport.$inject = [
'injector',
'eventBus',
'canvas',
'rules',
'modeling'
];
Expand Down
13 changes: 13 additions & 0 deletions test/spec/features/attach-support/AttachSupportSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,19 @@ describe('features/attach-support', function() {
}));


it('should add attachment marker on start', inject(function(move, dragging, elementRegistry) {
// given
var hostGfx = elementRegistry.getGraphics(host);

// when
move.start(canvasEvent({ x: 800, y: 100 }), attacher);
dragging.move(canvasEvent({ x: 750, y: 100 }));

// then
expect(svgClasses(hostGfx).has('attach-ok')).to.be.true;
}));


it('should remove attachment marker', inject(function(move, dragging, elementRegistry) {
// given
var hostGfx = elementRegistry.getGraphics(host);
Expand Down