Skip to content

Commit 203dca9

Browse files
committed
Handle cloning of SvgElement to create a drag image
1 parent a5c262b commit 203dca9

File tree

4 files changed

+28
-30
lines changed

4 files changed

+28
-30
lines changed

lib/src/dnd/draggable.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,6 @@ class DraggableEvent {
382382
* A drag feedback image.
383383
*/
384384
class DragImage {
385-
/// A small transparent gif.
386-
static const String EMPTY = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
387-
388385
Element element;
389386
int x;
390387
int y;
@@ -404,7 +401,8 @@ class DragImage {
404401

405402
/**
406403
* Constructor to create a [DragImage] for the [draggable]. [draggable]
407-
* must be visible in the DOM.
404+
* must be visible in the DOM. This is used for browsers that do not natively
405+
* support creating such an image.
408406
*
409407
* [mousePosition] is the mouse coordinate relative to the whole document
410408
* (usually the event's page property).
@@ -419,11 +417,22 @@ class DragImage {
419417
if (draggable is ImageElement) {
420418
element = new ImageElement(src: draggable.src,
421419
width: draggable.width, height: draggable.height);
420+
} else if (draggable is svg.SvgSvgElement) {
421+
// Is a root <svg> element --> we can just clone it.
422+
element = draggable.clone(true);
423+
element.style.pointerEvents = 'none';
424+
} else if (draggable is svg.SvgElement) {
425+
// Is a child of an <svg> element --> wrap it in an <svg>.
426+
element = new svg.SvgElement.tag('svg')
427+
..attributes['width'] = draggable.getBoundingClientRect().width.toString()
428+
..attributes['height'] = draggable.getBoundingClientRect().height.toString()
429+
..append(draggable.clone(true));
430+
element.style.pointerEvents = 'none';
422431
} else {
423432
element = draggable.clone(true);
424-
element.attributes.remove('id');
425433
element.style.width = draggable.getComputedStyle().width;
426434
element.style.height = draggable.getComputedStyle().height;
435+
print('background: ${element.style.background}');
427436
}
428437

429438
// Add some transparency like browsers would do for native dragging.

test/svg/svg_test.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@
2020

2121
#drop-within-svg.dnd-over {
2222
fill: green;
23+
}
24+
25+
.draggable {
26+
cursor: move;
27+
}
28+
29+
.dnd-dragging {
30+
opacity: 0.5;
2331
}

test/svg/svg_test.dart

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,7 @@ main() {
2121
}
2222

2323
void installSvg() {
24-
var svgDragGroup = new DraggableGroup(
25-
dragImageFunction: (Element draggable) {
26-
var element = new svg.SvgElement.tag('svg')
27-
..attributes = {
28-
'width': '100',
29-
'height': '100'
30-
}
31-
..append(draggable.clone(true));
32-
return new DragImage(element, 0, 0);
33-
}
34-
)
24+
var svgDragGroup = new DraggableGroup()
3525
..install(query('#draggable-svg'));
3626

3727
var divDragGroup = new DraggableGroup()
@@ -45,17 +35,7 @@ void installSvg() {
4535
}
4636

4737
void installSvgWithin() {
48-
var svgDragGroup = new DraggableGroup(
49-
dragImageFunction: (Element draggable) {
50-
var element = new svg.SvgElement.tag('svg')
51-
..attributes = {
52-
'width': '100',
53-
'height': '100'
54-
}
55-
..append(draggable.clone(true));
56-
return new DragImage(element, 0, 0);
57-
}
58-
)
38+
var svgDragGroup = new DraggableGroup()
5939
..install(query('#drag-within-svg'));
6040

6141
new DropzoneGroup()

test/svg/svg_test.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h3>Draggable SVG</h3>
1717

1818
<div>
1919
<h3>Draggable Div</h3>
20-
<div id="draggable-div"></div>
20+
<div id="draggable-div" class="draggable"></div>
2121
</div>
2222

2323
<div>
@@ -35,8 +35,9 @@ <h3>Div Dropzone</h3>
3535
<h1>Dragging within an SVG</h1>
3636
<div>
3737
<svg width="400" height="420">
38-
<rect id="drag-within-svg" width="200" height="100" fill="red"/>
39-
<rect id="drop-within-svg" y="120" width="200" height="100" fill="grey"/>
38+
<rect id="drag-within-svg" class="draggable" width="100" height="100" fill="red" />
39+
<rect id="drop-within-svg" y="120" width="200" height="100" fill="grey" />
40+
<rect id="drop-within-svg-inner" y="160" x="10" width="180" height="40" fill="lightgrey" />
4041
</svg>
4142
</div>
4243

0 commit comments

Comments
 (0)