Browse Source

Fix DnD issues regarding data type text (#9347)

* First dragged data should not contain new line character at the beginning
* Return any of "text", "Text" or "text/plain" in the method of getDataTransferText()
* Set only data of type "text" for IE on dragstart
* Clarify return of getDataTransferText() method
* Ignore generated data that is null
tags/8.1.0.beta1
Adam Wagner 7 years ago
parent
commit
df360681ae

+ 8
- 6
client/src/main/java/com/vaadin/client/connectors/grid/GridDragSourceConnector.java View File

@@ -212,12 +212,14 @@ public class GridDragSourceConnector extends DragSourceExtensionConnector {
getDraggedRows(dragStartEvent).forEach(row -> {
Map<String, String> rowDragData = getRowDragData(row);
rowDragData.forEach((type, data) -> {
if (dataMap.containsKey(type)) {
dataMap.put(type, data);
} else {
// Separate data with new line character when multiple rows
// are dragged
dataMap.put(type, dataMap.get(type) + "\n" + data);
if (!(data == null || data.isEmpty())) {
if (!dataMap.containsKey(type)) {
dataMap.put(type, data);
} else {
// Separate data with new line character when multiple rows
// are dragged
dataMap.put(type, dataMap.get(type) + "\n" + data);
}
}
});
});

+ 10
- 3
client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java View File

@@ -194,9 +194,16 @@ public class DragSourceExtensionConnector extends AbstractExtensionConnector {
// Always set something as the text data, or DnD won't work in FF !
dataMap.putIfAbsent(DragSourceState.DATA_TYPE_TEXT, "");

// Set data to the event's data transfer
dataMap.forEach((type, data) -> nativeEvent.getDataTransfer()
.setData(type, data));
if (!BrowserInfo.get().isIE11()) {
// Set data to the event's data transfer
dataMap.forEach((type, data) -> nativeEvent.getDataTransfer()
.setData(type, data));
} else {
// IE11 accepts only data with type "text"
nativeEvent.getDataTransfer()
.setData(DragSourceState.DATA_TYPE_TEXT,
dataMap.get(DragSourceState.DATA_TYPE_TEXT));
}

// Initiate firing server side dragstart event when there is a
// DragStartListener attached on the server side

+ 21
- 4
server/src/main/java/com/vaadin/ui/dnd/event/DropEvent.java View File

@@ -78,13 +78,30 @@ public class DropEvent<T extends AbstractComponent> extends Component.Event {
}

/**
* Get data of type {@code "text"} from the {@code DataTransfer} object.
* Get data of any of the types {@code "text"}, {@code "Text"} or {@code
* "text/plain"}.
* <p>
* IE 11 transfers data dropped from the desktop as {@code "Text"} while
* most other browsers transfer textual data as {@code "text/plain"}.
*
* @return Data of type {@code "text"} if exists in the {@code DataTransfer}
* object, otherwise {@literal null}.
* @return First existing data of types in order {@code "text"}, {@code
* "Text"} or {@code "text/plain"}, or {@code null} if none of them exist.
*/
public String getDataTransferText() {
return data.get(DragSourceState.DATA_TYPE_TEXT);
// Read data type "text"
String text = data.get(DragSourceState.DATA_TYPE_TEXT);

// IE stores data dragged from the desktop as "Text"
if (text == null) {
text = data.get(DragSourceState.DATA_TYPE_TEXT_IE);
}

// Browsers may store the key as "text/plain"
if (text == null) {
text = data.get(DragSourceState.DATA_TYPE_TEXT_PLAIN);
}

return text;
}

/**

+ 13
- 0
shared/src/main/java/com/vaadin/shared/ui/dnd/DragSourceState.java View File

@@ -45,6 +45,19 @@ public class DragSourceState extends SharedState {
*/
public static final String DATA_TYPE_TEXT = "text";

/**
* Data type {@code "Text"}. IE 11 stores data dragged from the desktop as
* "Text" with capital letter.
*/
public static final String DATA_TYPE_TEXT_IE = "Text";

/**
* Data type {@code "text/plain"} for reading data from {@code DataTransfer}
* object. Some browsers convert store data with {@code "text"} as {@code
* "text/plain"} when transferring data.
*/
public static final String DATA_TYPE_TEXT_PLAIN = "text/plain";

public static final String RESOURCE_DRAG_IMAGE = "drag-image";

/**

Loading…
Cancel
Save