TagSpaces is an offline, open source, document manager with tagging support
add CustomDragLayer for drag file preview
stanimir60b70f701f59
diff --git a/app/components/BoxDragPreview.tsx b/app/components/BoxDragPreview.tsxnew file mode 100644index 000000000..eff41c521--- /dev/null+++ b/app/components/BoxDragPreview.tsx@@ -0,0 +1,15 @@+import React, { memo } from 'react';++export interface Props {+ entries: Array<any>;+ path: string;+}++export const BoxDragPreview = memo((props: Props) => {+ const { entries, path } = props;+ return (+ <div style={{ display: 'inline-block' }}>+ {entries.length > 0 ? entries.map(entry => <h4>{entry.name}</h4>) : path}+ </div>+ );+});diff --git a/app/components/CustomDragLayer.tsx b/app/components/CustomDragLayer.tsxnew file mode 100644index 000000000..680162fad--- /dev/null+++ b/app/components/CustomDragLayer.tsx@@ -0,0 +1,74 @@+import React from 'react';+import { XYCoord, useDragLayer } from 'react-dnd';+import { BoxDragPreview } from './BoxDragPreview';+import DragItemTypes from '-/components/DragItemTypes';++const layerStyles: React.CSSProperties = {+ position: 'fixed',+ pointerEvents: 'none',+ zIndex: 100,+ left: 0,+ top: 0,+ width: '100%',+ height: '100%'+};++function getItemStyles(+ initialOffset: XYCoord | null,+ currentOffset: XYCoord | null+) {+ if (!initialOffset || !currentOffset) {+ return {+ display: 'none'+ };+ }++ const { x, y } = currentOffset;++ const transform = `translate(${x}px, ${y}px)`;+ return {+ transform,+ WebkitTransform: transform+ };+}++export interface CustomDragLayerProps {}++export const CustomDragLayer: React.FC<CustomDragLayerProps> = props => {+ const {+ itemType,+ isDragging,+ item,+ initialOffset,+ currentOffset+ } = useDragLayer(monitor => ({+ item: monitor.getItem(),+ itemType: monitor.getItemType(),+ initialOffset: monitor.getInitialSourceClientOffset(),+ currentOffset: monitor.getSourceClientOffset(),+ isDragging: monitor.isDragging()+ }));++ function renderItem() {+ switch (itemType) {+ case DragItemTypes.FILE:+ return (+ <BoxDragPreview entries={item.selectedEntries} path={item.path} />+ );++ default:+ return null;+ }+ }++ if (!isDragging) {+ return null;+ }+ return (+ <div style={layerStyles}>+ <div style={getItemStyles(initialOffset, currentOffset)}>+ {renderItem()}+ </div>+ </div>+ );+};diff --git a/app/components/FileSourceDnd.tsx b/app/components/FileSourceDnd.tsxindex 4966e22f0..3ed4069c9 100644--- a/app/components/FileSourceDnd.tsx+++ b/app/components/FileSourceDnd.tsx@@ -17,13 +17,14 @@*/import React from 'react';-import { DragSource, ConnectDragSource } from 'react-dnd';-+import { DragSource, ConnectDragSource, ConnectDragPreview } from 'react-dnd';+import { getEmptyImage } from 'react-dnd-html5-backend';import DragItemTypes from './DragItemTypes';interface Props {children: Array<Object>;connectDragSource: ConnectDragSource;+ connectDragPreview: ConnectDragPreview;}const boxSource = {@@ -36,29 +37,50 @@ const boxSource = {}/* endDrag(props, monitor) {- const item = monitor.getItem();- const dropResult = monitor.getDropResult();+ const item = monitor.getItem();+ const dropResult = monitor.getDropResult();++ // console.log('DropRESULT: ', dropResult);+ // console.log('item: ', item);+ if (dropResult && dropResult.tagGroupId && dropResult.tagGroupId !== item.sourceTagGroupId) {+ // console.log(`Dropped ${item.tagId} from ${item.sourceTagGroupId} into ${dropResult.tagGroupId}!`);+ props.moveTag(item.tagId, item.sourceTagGroupId, dropResult.tagGroupId);+ } else if (dropResult && dropResult.entryPath) {+ // console.log(`Dropped item: ${item.tag.title} onto file: ${dropResult.entryPath}!`);+ props.addTags([dropResult.entryPath], [item.tag]);+ }+ } */+};- // console.log('DropRESULT: ', dropResult);- // console.log('item: ', item);- if (dropResult && dropResult.tagGroupId && dropResult.tagGroupId !== item.sourceTagGroupId) {- // console.log(`Dropped ${item.tagId} from ${item.sourceTagGroupId} into ${dropResult.tagGroupId}!`);- props.moveTag(item.tagId, item.sourceTagGroupId, dropResult.tagGroupId);- } else if (dropResult && dropResult.entryPath) {- // console.log(`Dropped item: ${item.tag.title} onto file: ${dropResult.entryPath}!`);- props.addTags([dropResult.entryPath], [item.tag]);- }- } */+const FileSourceDnd = (props: 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.+ // if(props.children.props.selectedEntries && props.children.props.selectedEntries.length > 1) {+ 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 props.connectDragSource(<span>{props.children}</span>);};-const FileSourceDnd = (props: Props) =>- props.connectDragSource(<span>{props.children}</span>);+/**+ * 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()+});export default DragSource(DragItemTypes.FILE,boxSource,- (connect, monitor) => ({- connectDragSource: connect.dragSource(),- isDragging: monitor.isDragging()- })+ collect)(FileSourceDnd);diff --git a/app/containers/MainPage.tsx b/app/containers/MainPage.tsxindex 0fe183cbe..8aa31c95a 100644--- a/app/containers/MainPage.tsx+++ b/app/containers/MainPage.tsx@@ -85,6 +85,7 @@ import i18n from '../services/i18n';import { Pro } from '../pro';import LoadingLazy from '../components/LoadingLazy';import withDnDContext from '-/containers/withDnDContext';+import { CustomDragLayer } from '-/components/CustomDragLayer';const initialSplitSize = 44;const drawerWidth = 300;@@ -783,6 +784,7 @@ class MainPage extends Component<Props, State> {accepts={[FILE]}onDrop={this.handleFileDrop}>+ <CustomDragLayer /><SplitPanesplit="vertical"style={{