Skip to content

TagSpaces is an offline, open source, document manager with tagging support

custom preview for DnD tags (#1156)

Stanimirc7b476a9ded3
diff --git a/app/components/CustomDragLayer.tsx b/app/components/CustomDragLayer.tsx
index b559b51b1..e4cf605c2 100644
--- a/app/components/CustomDragLayer.tsx
+++ b/app/components/CustomDragLayer.tsx
@@ -1,6 +1,7 @@
import React from 'react';
import { XYCoord, useDragLayer } from 'react-dnd';
-import { BoxDragPreview } from './BoxDragPreview';
+import { FilesDragPreview } from './FilesDragPreview';
+import { TagDragPreview } from './TagDragPreview';
import DragItemTypes from '-/components/DragItemTypes';
const layerStyles: React.CSSProperties = {
@@ -56,8 +57,10 @@ export const CustomDragLayer: React.FC<CustomDragLayerProps> = props => {
switch (itemType) {
case DragItemTypes.FILE:
return (
- <BoxDragPreview entries={item.selectedEntries} path={item.path} />
+ <FilesDragPreview entries={item.selectedEntries} path={item.path} />
);
+ case DragItemTypes.TAG:
+ return <TagDragPreview tag={item.tag} />;
default:
return null;
diff --git a/app/components/BoxDragPreview.tsx b/app/components/FilesDragPreview.tsx
similarity index 87%
rename from app/components/BoxDragPreview.tsx
rename to app/components/FilesDragPreview.tsx
index b6b9041d9..4d68364fe 100644
--- a/app/components/BoxDragPreview.tsx
+++ b/app/components/FilesDragPreview.tsx
@@ -7,7 +7,7 @@ export interface Props {
path: string;
}
-export const BoxDragPreview = memo((props: Props) => {
+export const FilesDragPreview = memo((props: Props) => {
const { entries, path } = props;
return (
<Chip
diff --git a/app/components/TagContainerDnd.tsx b/app/components/TagContainerDnd.tsx
index 47431f4e7..872cae03d 100644
--- a/app/components/TagContainerDnd.tsx
+++ b/app/components/TagContainerDnd.tsx
@@ -19,6 +19,7 @@
import React from 'react';
import { DragSource, ConnectDragPreview, ConnectDragSource } from 'react-dnd';
+import { getEmptyImage } from 'react-dnd-html5-backend';
import DragItemTypes from './DragItemTypes';
import TagContainer from './TagContainer';
import { TagGroup, Tag } from '../reducers/taglibrary';
@@ -84,37 +85,6 @@ interface Props {
selectedEntries: Array<FileSystemEntry>;
}
-// class TagContainerDnd extends React.PureComponent<Props> {
-// // componentDidMount() {
-// // const xml = '<svg version="1.1" height="25" width="100" xmlns="http://www.w3.org/2000/svg">' +
-// // '<g>' +
-// // '<rect x="0" y="0" rx="5" ry="5" width="100%" height="100%" style="fill:' + this.props.tag.color + ';opacity:0.5" />' +
-// // '<text x="50%" y="55%" font-family="Roboto,Arial,Helvetica,Sans" font-size="13" dominant-baseline="middle" text-anchor="middle" fill="' + this.props.tag.textcolor + '">' + this.props.tag.title + '</text>' +
-// // '</g>' +
-// // '</svg>';
-// // const img = new Image();
-// // img.src = `data:image/svg+xml;charset=utf-8,${xml.replace(/(\r\n|\n|\r)/gm, '')}`;
-// // img.onload = () => this.props.connectDragPreview(img, { anchorX: 1, anchorY: 0 });
-// // // this.props.connectDragPreview(getEmptyImage(), { captureDraggingState: true });
-// // }
-
-// shouldComponentUpdate(nextProps) {
-// if (this.props.tag.title !== nextProps.tag.title
-// || typeof this.props.key !== typeof nextProps.key
-// || (this.props.key && nextProps.key && this.props.key !== nextProps.key)
-// || this.props.tag.color !== nextProps.tag.color
-// || this.props.tag.textcolor !== nextProps.tag.textcolor
-// || (this.props.tagGroup && this.props.tagGroup ? (this.props.tagGroup.uuid !== nextProps.tagGroup.uuid) : false)
-// || this.props.isDragging !== nextProps.isDragging
-// || typeof this.props.entryPath !== typeof nextProps.entryPath
-// || (this.props.entryPath && this.props.entryPath !== nextProps.entryPath)
-// ) {
-// return true;
-// }
-// return false;
-// }
-
-// render() {
const TagContainerDnd = (props: Props) => {
const {
key,
@@ -130,6 +100,14 @@ const TagContainerDnd = (props: Props) => {
tagMode
} = props;
+ // Use empty image as a drag preview so browsers don't draw it
+ // and we can draw whatever we want on the custom drag layer instead.
+ props.connectDragPreview(getEmptyImage(), {
+ // IE fallback: specify that we'd rather screenshot the node
+ // when it already knows it's being dragged so we can hide it with CSS.
+ captureDraggingState: true
+ });
+
return connectDragSource(
<span>
<TagContainer
@@ -148,8 +126,21 @@ const TagContainerDnd = (props: Props) => {
);
};
-export default DragSource(DragItemTypes.TAG, boxSource, (connect, monitor) => ({
+/**
+ * Specifies which props to inject into your component.
+ */
+const collect = (connect, monitor) => ({
+ // Call this function inside render()
+ // to let React DnD handle the drag events:
connectDragSource: connect.dragSource(),
+ // You can ask the monitor about the current drag preview
connectDragPreview: connect.dragPreview(),
+ // You can ask the monitor about the current drag state:
isDragging: monitor.isDragging()
-}))(TagContainerDnd);
+});
+
+export default DragSource(
+ DragItemTypes.TAG,
+ boxSource,
+ collect
+)(TagContainerDnd);
diff --git a/app/components/TagDragPreview.tsx b/app/components/TagDragPreview.tsx
new file mode 100644
index 000000000..6a9055dbd
--- /dev/null
+++ b/app/components/TagDragPreview.tsx
@@ -0,0 +1,12 @@
+import React, { memo } from 'react';
+import TagContainer from '-/components/TagContainer';
+import { Tag } from '-/reducers/taglibrary';
+
+export interface Props {
+ tag: Tag;
+}
+
+export const TagDragPreview = memo((props: Props) => {
+ const { tag } = props;
+ return <TagContainer tag={tag} isDragging={true} />;
+});
diff --git a/app/components/TagDropContainer.tsx b/app/components/TagDropContainer.tsx
index 2bc34e67f..95dd98ef2 100644
--- a/app/components/TagDropContainer.tsx
+++ b/app/components/TagDropContainer.tsx
@@ -28,7 +28,7 @@ interface Props {
}
const boxTarget = {
- drop(props) {
+ drop(props, monitor) {
return {
selectedEntries: props.selectedEntries,
entryPath: props.entryPath
diff --git a/app/components/dialogs/AddRemoveTagsDialog.tsx b/app/components/dialogs/AddRemoveTagsDialog.tsx
index f92bf3699..e5ca9b561 100644
--- a/app/components/dialogs/AddRemoveTagsDialog.tsx
+++ b/app/components/dialogs/AddRemoveTagsDialog.tsx
@@ -84,11 +84,8 @@ class AddRemoveTagsDialog extends React.Component<Props, State> {
};
removeTags = () => {
- const {
- selectedEntries = [],
- removeTags,
- } = this.props;
- const { newlyAddedTags = [] } = this.state;
+ const { selectedEntries = [], removeTags } = this.props;
+ const { newlyAddedTags = [] } = this.state;
if (selectedEntries && selectedEntries.length > 0) {
const paths = [];
selectedEntries.map(entry => {
@@ -98,13 +95,10 @@ class AddRemoveTagsDialog extends React.Component<Props, State> {
removeTags(paths, newlyAddedTags);
}
this.onClose(true);
- }
+ };
removeAllTags = () => {
- const {
- selectedEntries = [],
- removeAllTags,
- } = this.props;
+ const { selectedEntries = [], removeAllTags } = this.props;
if (selectedEntries && selectedEntries.length > 0) {
const paths = [];
selectedEntries.map(entry => {
@@ -114,7 +108,7 @@ class AddRemoveTagsDialog extends React.Component<Props, State> {
removeAllTags(paths);
}
this.onClose(true);
- }
+ };
render() {
const {
@@ -126,7 +120,10 @@ class AddRemoveTagsDialog extends React.Component<Props, State> {
onClose
} = this.props;
const { newlyAddedTags = [] } = this.state;
- const disabledButtons = !newlyAddedTags || newlyAddedTags.length < 1 ||selectedEntries.length < 1;
+ const disabledButtons =
+ !newlyAddedTags ||
+ newlyAddedTags.length < 1 ||
+ selectedEntries.length < 1;
return (
<Dialog
@@ -161,7 +158,11 @@ class AddRemoveTagsDialog extends React.Component<Props, State> {
</List>
</DialogContent>
<DialogActions>
- <Button data-tid="cancel" onClick={() => this.onClose()} color="primary">
+ <Button
+ data-tid="cancel"
+ onClick={() => this.onClose()}
+ color="primary"
+ >
{i18n.t('core:cancel')}
</Button>
<Button