TagSpaces is an offline, open source, document manager with tagging support
TSN689 Change the order of tags in taggroups with DnD (#1462)
Stanimir264c797fa9d5
diff --git a/app/components/TagContainerDnd.tsx b/app/components/TagContainerDnd.tsxindex 771a24ad3..95a545e25 100644--- a/app/components/TagContainerDnd.tsx+++ b/app/components/TagContainerDnd.tsx@@ -17,7 +17,13 @@*/import React from 'react';-import { DragSource, ConnectDragPreview, ConnectDragSource } from 'react-dnd';+import {+ DragSource,+ DropTarget,+ ConnectDragPreview,+ ConnectDragSource,+ ConnectDropTarget+} from 'react-dnd';import { getEmptyImage } from 'react-dnd-html5-backend';import DragItemTypes from './DragItemTypes';@@ -25,9 +31,11 @@ import TagContainer from './TagContainer';import { TS } from '-/tagspaces.namespace';const boxSource = {+ // Expected the drag source specification to only have some of the following keys: canDrag, beginDrag, isDragging, endDragbeginDrag(props) {// console.log('beginDrag', props);return {+ index: props.index,tagTitle: props.tag.title,tag: props.tag,sourceTagGroupId: props.tagGroup ? props.tagGroup.uuid : undefined@@ -71,8 +79,60 @@ const boxSource = {}};+const boxTarget = {+ hover(props, monitor) {+ const dragItem = monitor.getItem();+ if (props.tagGroup && dragItem.sourceTagGroupId === props.tagGroup.uuid) {+ // sort only tagGroups+ const dragIndex = dragItem.index;+ const hoverIndex = props.index;+ // Don't replace items with themselves+ if (dragIndex === hoverIndex) {+ return;+ }+ // Determine rectangle on screen+ const hoverBoundingRect =+ props.tagContainerRef && props.tagContainerRef.current+ ? props.tagContainerRef.current.getBoundingClientRect()+ : undefined; // findDOMNode(component).getBoundingClientRect(); // tagContainerRef.current.getBoundingClientRect();++ // Get vertical middle (bottom = right; top = left)+ const hoverMiddleY =+ (hoverBoundingRect.right - hoverBoundingRect.left) / 2;++ // Determine mouse position+ const clientOffset = monitor.getClientOffset();++ // Get pixels to the top+ const hoverClientY = clientOffset.x - hoverBoundingRect.left;++ // Only perform the move when the mouse has crossed half of the items height+ // When dragging downwards, only move when the cursor is below 50%+ // When dragging upwards, only move when the cursor is above 50%++ // Dragging downwards+ if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {+ return;+ }++ // Dragging upwards+ if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {+ return;+ }+ // Time to actually perform the action+ props.changeTagOrder(props.tagGroup.uuid, dragIndex, hoverIndex);+ // Note: we're mutating the monitor item here!+ // Generally it's better to avoid mutations,+ // but it's good here for the sake of performance+ // to avoid expensive index searches.+ dragItem.index = hoverIndex;+ }+ }+};+interface Props {tag: TS.Tag;+ index: number;tagGroup?: TS.TagGroup;handleTagMenu: (event: Object, tag: TS.Tag, param: any) => void;handleRemoveTag?: (event: Object, tag: TS.Tag) => void;@@ -82,10 +142,17 @@ interface Props {addTags?: (paths: Array<string>, tags: Array<TS.Tag>) => void;addTag?: (tag: TS.Tag, parentTagGroupUuid: TS.Uuid) => void;moveTag?: () => void;+ changeTagOrder?: (+ tagGroupUuid: TS.Uuid,+ fromIndex: number,+ toIndex: number+ ) => void;connectDragSource?: ConnectDragSource;+ connectDropTarget: ConnectDropTarget;connectDragPreview?: ConnectDragPreview;deleteIcon?: Object;selectedEntries: Array<TS.FileSystemEntry>;+ tagContainerRef?: string;}const TagContainerDnd = (props: Props) => {@@ -98,8 +165,10 @@ const TagContainerDnd = (props: Props) => {selectedEntries,isDragging,connectDragSource,+ connectDropTarget,addTags,- tagMode+ tagMode,+ tagContainerRef} = props;// Use empty image as a drag preview so browsers don't draw it@@ -110,27 +179,29 @@ const TagContainerDnd = (props: Props) => {captureDraggingState: true});- return connectDragSource(- <span>- <TagContainer- tag={tag}- tagGroup={tagGroup}- handleTagMenu={handleTagMenu}- deleteIcon={deleteIcon}- addTags={addTags}- tagMode={tagMode}- entryPath={entryPath}- isDragging={isDragging}- selectedEntries={selectedEntries}- />- </span>+ return connectDropTarget(+ connectDragSource(+ <span ref={tagContainerRef}>+ <TagContainer+ tag={tag}+ tagGroup={tagGroup}+ handleTagMenu={handleTagMenu}+ deleteIcon={deleteIcon}+ addTags={addTags}+ tagMode={tagMode}+ entryPath={entryPath}+ isDragging={isDragging}+ selectedEntries={selectedEntries}+ />+ </span>+ ));};/*** Specifies which props to inject into your component.*/-const collect = (connect, monitor) => ({+const collectSource = (connect, monitor) => ({// Call this function inside render()// to let React DnD handle the drag events:connectDragSource: connect.dragSource(),@@ -140,8 +211,14 @@ const collect = (connect, monitor) => ({isDragging: monitor.isDragging()});+const collectTarget = (connect, monitor) => ({+ connectDropTarget: connect.dropTarget(),+ isOver: monitor.isOver(),+ canDrop: monitor.canDrop()+});+export default DragSource(DragItemTypes.TAG,boxSource,- collect-)(TagContainerDnd);+ collectSource+)(DropTarget(DragItemTypes.TAG, boxTarget, collectTarget)(TagContainerDnd));diff --git a/app/components/TagLibrary.tsx b/app/components/TagLibrary.tsxindex ebe2e68c0..c62598509 100644--- a/app/components/TagLibrary.tsx+++ b/app/components/TagLibrary.tsx@@ -16,7 +16,7 @@**/-import React, { useCallback, useEffect, useState } from 'react';+import React, { useCallback, useEffect, useRef, useState } from 'react';import { bindActionCreators } from 'redux';import { connect } from 'react-redux';import classNames from 'classnames';@@ -88,6 +88,11 @@ interface Props {createTagGroup: () => void;addTag: () => void;moveTag: () => void;+ changeTagOrder: (+ tagGroupUuid: TS.Uuid,+ fromIndex: number,+ toIndex: number+ ) => void;editTagGroup: () => void;editTag: () => void;deleteTag: (tagTitle: string, parentTagGroupUuid: TS.Uuid) => void;@@ -103,6 +108,7 @@ interface Props {}const TagLibrary = (props: Props) => {+ const tagContainerRef = useRef<HTMLSpanElement>(null);const [tagGroupMenuAnchorEl,setTagGroupMenuAnchorEl@@ -309,7 +315,7 @@ const TagLibrary = (props: Props) => {data-tid={'tagGroupContainer_' + tagGroup.title}>{tagGroup.children &&- tagGroup.children.map((tag: TS.Tag) => {+ tagGroup.children.map((tag: TS.Tag, index) => {if (props.isReadOnlyMode) {return (<TagContainer@@ -326,11 +332,14 @@ const TagLibrary = (props: Props) => {return (<TagContainerDndkey={tagGroup.uuid + tag.title}+ tagContainerRef={tagContainerRef}+ index={index}tag={tag}tagGroup={tagGroup}handleTagMenu={handleTagMenuCallback}addTags={props.addTags}moveTag={props.moveTag}+ changeTagOrder={props.changeTagOrder}selectedEntries={props.selectedEntries}/>);@@ -521,6 +530,7 @@ function mapDispatchToProps(dispatch) {createTagGroup: TagLibraryActions.createTagGroup,editTag: TagLibraryActions.editTag,moveTag: TagLibraryActions.moveTag,+ changeTagOrder: TagLibraryActions.changeTagOrder,editTagGroup: TagLibraryActions.editTagGroup,deleteTag: TagLibraryActions.deleteTag,addTag: TagLibraryActions.addTag,diff --git a/app/reducers/taglibrary.ts b/app/reducers/taglibrary.tsindex 5decb3bce..167489b7a 100644--- a/app/reducers/taglibrary.ts+++ b/app/reducers/taglibrary.ts@@ -41,7 +41,8 @@ export const types = {MOVE_TAG_GROUP_UP: 'MOVE_TAG_GROUP_UP',MOVE_TAG_GROUP_DOWN: 'MOVE_TAG_GROUP_DOWN',EDIT_TAG_COLOR: 'EDIT_TAG_COLOR',- MOVE_TAG: 'MOVE_TAG'+ MOVE_TAG: 'MOVE_TAG',+ CHANGE_TAG_ORDER: 'CHANGE_TAG_ORDER'};export default (state: Array<TS.TagGroup> = defaultTagLibrary, action: any) => {@@ -358,6 +359,29 @@ export default (state: Array<TS.TagGroup> = defaultTagLibrary, action: any) => {}return state;}+ case types.CHANGE_TAG_ORDER: {+ let indexFromGroup = -1;+ state.forEach((tagGroup, index) => {+ if (tagGroup.uuid === action.tagGroupUuid) {+ indexFromGroup = index;+ }+ });++ if (indexFromGroup >= 0) {+ const newTagLibrary = [...state];+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#swapping_variables+ [+ newTagLibrary[indexFromGroup].children[action.fromIndex],+ newTagLibrary[indexFromGroup].children[action.toIndex]+ ] = [+ newTagLibrary[indexFromGroup].children[action.toIndex],+ newTagLibrary[indexFromGroup].children[action.fromIndex]+ ];++ return newTagLibrary;+ }+ return state;+ }case types.IMPORT_TAGGROUP: {const arr = action.replace ? [] : [...state];console.log(arr);@@ -648,6 +672,16 @@ export const actions = {fromTagGroupId: fromTagGroupUuid,toTagGroupId: toTagGroupUuid}),+ changeTagOrder: (+ tagGroupUuid: TS.Uuid,+ fromIndex: number,+ toIndex: number+ ) => ({+ type: types.CHANGE_TAG_ORDER,+ tagGroupUuid,+ fromIndex,+ toIndex+ }),importTagGroups: (entries: Array<TS.TagGroup>, replace: boolean = false) => ({type: types.IMPORT_TAGGROUP,entries,diff --git a/package.json b/package.jsonindex d5c746233..03d407ffb 100644--- a/package.json+++ b/package.json@@ -286,8 +286,8 @@"react-beautiful-dnd": "^13.0.0","react-color": "^2.19.3","react-d3-tree": "^1.15.3",- "react-dnd": "^11.1.3",- "react-dnd-html5-backend": "^11.1.3",+ "react-dnd": "^14.0.2",+ "react-dnd-html5-backend": "^14.0.0","react-dom": "^16.8.2","react-hot-loader": "^4.8.2","react-hotkeys": "^2.0.0",diff --git a/yarn.lock b/yarn.lockindex 4046bd057..b2179f885 100644--- a/yarn.lock+++ b/yarn.lock@@ -3038,6 +3038,13 @@dependencies:regenerator-runtime "^0.13.4"+"@babel/runtime@^7.9.2":+ version "7.14.6"+ resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.14.6.tgz#535203bc0892efc7dec60bdc27b2ecf6e409062d"+ integrity sha512-/PCB2uJ7oM44tz8YhC4Z/6PeOKXp4K588f+5M3clr1M4zbqztlo0XEfJ2LEzj/FgwfgGcIdl8n7YYjTCI0BYwg==+ dependencies:+ regenerator-runtime "^0.13.4"+"@babel/template@^7.10.4", "@babel/template@^7.3.3":version "7.10.4"resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.10.4.tgz#3251996c4200ebc71d1a8fc405fba940f36ba278"@@ -3790,14 +3797,6 @@resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.7.tgz#613957d900fab9ff84c8dfb24fa3eef0c2a40896"integrity sha512-2xtoL22/3Mv6a70i4+4RB7VgbDDORoWwjcqeNysojZA0R7NK17RbY5Gof/2QiFfJgX+KkWghbwJ+d/2SB8Ndzg==-"@types/hoist-non-react-statics@^3.3.1":- version "3.3.1"- resolved "https://registry.yarnpkg.com/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz#1124aafe5118cb591977aeb1ceaaed1070eb039f"- integrity sha512-iMIqiko6ooLrTh1joXodJK5X9xeEALT1kM5G3ZLhD3hszxBdIEd5C75U834D9mLcINgD4OyZf5uQXjkuYydWvA==- dependencies:- "@types/react" "*"- hoist-non-react-statics "^3.3.0"-"@types/http-cache-semantics@*":version "4.0.0"resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.0.tgz#9140779736aa2655635ee756e2467d787cfe8a2a"@@ -7604,14 +7603,14 @@ dmg-license@^1.0.8:smart-buffer "^4.0.2"verror "^1.10.0"-dnd-core@^11.1.3:- version "11.1.3"- resolved "https://registry.yarnpkg.com/dnd-core/-/dnd-core-11.1.3.tgz#f92099ba7245e49729d2433157031a6267afcc98"- integrity sha512-QugF55dNW+h+vzxVJ/LSJeTeUw9MCJ2cllhmVThVPEtF16ooBkxj0WBE5RB+AceFxMFo1rO6bJKXtqKl+JNnyA==+ version "14.0.0"+ resolved "https://registry.yarnpkg.com/dnd-core/-/dnd-core-14.0.0.tgz#973ab3470d0a9ac5a0fa9021c4feba93ad12347d"+ integrity sha512-wTDYKyjSqWuYw3ZG0GJ7k+UIfzxTNoZLjDrut37PbcPGNfwhlKYlPUqjAKUjOOv80izshUiqusaKgJPItXSevA==dependencies:"@react-dnd/asap" "^4.0.0""@react-dnd/invariant" "^2.0.0"- redux "^4.0.4"+ redux "^4.0.5"dns-equal@^1.0.0:version "1.0.0"@@ -15289,22 +15288,23 @@ react-d3-tree@^1.15.3:react-transition-group "^1.1.3"uuid "^3.0.1"-react-dnd-html5-backend@^11.1.3:- version "11.1.3"- resolved "https://registry.yarnpkg.com/react-dnd-html5-backend/-/react-dnd-html5-backend-11.1.3.tgz#2749f04f416ec230ea193f5c1fbea2de7dffb8f7"- integrity sha512-/1FjNlJbW/ivkUxlxQd7o3trA5DE33QiRZgxent3zKme8DwF4Nbw3OFVhTRFGaYhHFNL1rZt6Rdj1D78BjnNLw==+react-dnd-html5-backend@^14.0.0:+ version "14.0.0"+ resolved "https://registry.yarnpkg.com/react-dnd-html5-backend/-/react-dnd-html5-backend-14.0.0.tgz#28d660a2ad1e07447c34a65cd25f7de8f1657194"+ integrity sha512-2wAQqRFC1hbRGmk6+dKhOXsyQQOn3cN8PSZyOUeOun9J8t3tjZ7PS2+aFu7CVu2ujMDwTJR3VTwZh8pj2kCv7g==dependencies:- dnd-core "^11.1.3"+ dnd-core "14.0.0"-react-dnd@^11.1.3:- version "11.1.3"- resolved "https://registry.yarnpkg.com/react-dnd/-/react-dnd-11.1.3.tgz#f9844f5699ccc55dfc81462c2c19f726e670c1af"- integrity sha512-8rtzzT8iwHgdSC89VktwhqdKKtfXaAyC4wiqp0SywpHG12TTLvfOoL6xNEIUWXwIEWu+CFfDn4GZJyynCEuHIQ==+react-dnd@^14.0.2:+ version "14.0.2"+ resolved "https://registry.yarnpkg.com/react-dnd/-/react-dnd-14.0.2.tgz#57266baec92b887301f81fa3b77f87168d159733"+ integrity sha512-JoEL78sBCg8SzjOKMlkR70GWaPORudhWuTNqJ56lb2P8Vq0eM2+er3ZrMGiSDhOmzaRPuA9SNBz46nHCrjn11A==dependencies:+ "@react-dnd/invariant" "^2.0.0""@react-dnd/shallowequal" "^2.0.0"- "@types/hoist-non-react-statics" "^3.3.1"- dnd-core "^11.1.3"- hoist-non-react-statics "^3.3.0"+ dnd-core "14.0.0"+ fast-deep-equal "^3.1.3"+ hoist-non-react-statics "^3.3.2"react-dom@^16.8.2:version "16.13.1"@@ -15834,6 +15834,13 @@ redux@^4.0.0, redux@^4.0.4:loose-envify "^1.4.0"symbol-observable "^1.2.0"+redux@^4.0.5:+ version "4.1.0"+ resolved "https://registry.yarnpkg.com/redux/-/redux-4.1.0.tgz#eb049679f2f523c379f1aff345c8612f294c88d4"+ integrity sha512-uI2dQN43zqLWCt6B/BMGRMY6db7TTY4qeHHfGeKb3EOhmOKjU3KdWvNLJyqaHRksv/ErdNH7cFZWg9jXtewy4g==+ dependencies:+ "@babel/runtime" "^7.9.2"+regenerate-unicode-properties@^8.2.0:version "8.2.0"resolved "https://registry.yarnpkg.com/regenerate-unicode-properties/-/regenerate-unicode-properties-8.2.0.tgz#e5de7111d655e7ba60c057dbe9ff37c87e65cdec"