Skip to content

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

TSN694 - Change the order of tags in the entries (#1463)

Stanimir85b08874706d
diff --git a/app/components/TagContainerDnd.tsx b/app/components/TagContainerDnd.tsx
index 95a545e25..38f4fac30 100644
--- a/app/components/TagContainerDnd.tsx
+++ b/app/components/TagContainerDnd.tsx
@@ -36,9 +36,9 @@ const boxSource = {
// console.log('beginDrag', props);
return {
index: props.index,
- tagTitle: props.tag.title,
+ // tagTitle: props.tag.title,
tag: props.tag,
- sourceTagGroupId: props.tagGroup ? props.tagGroup.uuid : undefined
+ ...(props.tagGroup ? { sourceTagGroupId: props.tagGroup.uuid } : {})
};
},
@@ -56,7 +56,7 @@ const boxSource = {
if (props.moveTag) {
// console.log(`Dropped ${item.tagId} from ${item.sourceTagGroupId} into ${dropResult.tagGroupId}!`);
props.moveTag(
- item.tagTitle,
+ item.tag.title,
item.sourceTagGroupId,
dropResult.tagGroupId
);
@@ -80,10 +80,15 @@ const boxSource = {
};
const boxTarget = {
+ // Expected the drop target specification to only have some of the following keys: canDrop, hover, drop
+ /* canDrop(props, monitor) {
+ const dragItem = monitor.getItem();
+ return dragItem.tag.type === props.tag.type;
+ }, */
hover(props, monitor) {
const dragItem = monitor.getItem();
if (props.tagGroup && dragItem.sourceTagGroupId === props.tagGroup.uuid) {
- // sort only tagGroups
+ // sort tagGroups tags
const dragIndex = dragItem.index;
const hoverIndex = props.index;
// Don't replace items with themselves
@@ -125,6 +130,22 @@ const boxTarget = {
// 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;
+ } else if (props.entryPath) {
+ // sort fileSystemEntries tags
+ const dragIndex = dragItem.index;
+ const hoverIndex = props.index;
+ // Don't replace items with themselves
+ if (dragIndex === hoverIndex) {
+ return;
+ }
+ // Cannot drop into different tag type
+ if (dragItem.tag.type !== props.tag.type) {
+ return;
+ }
+ dragItem.tag.position = hoverIndex;
+ props.editTagForEntry(props.entryPath, dragItem.tag);
+
dragItem.index = hoverIndex;
}
}
@@ -147,6 +168,7 @@ interface Props {
fromIndex: number,
toIndex: number
) => void;
+ editTagForEntry?: (path: string, tag: TS.Tag) => void;
connectDragSource?: ConnectDragSource;
connectDropTarget: ConnectDropTarget;
connectDragPreview?: ConnectDragPreview;
diff --git a/app/perspectives/grid-perspective/components/CellContent.tsx b/app/perspectives/grid-perspective/components/CellContent.tsx
index 21b199ccf..812a4c910 100644
--- a/app/perspectives/grid-perspective/components/CellContent.tsx
+++ b/app/perspectives/grid-perspective/components/CellContent.tsx
@@ -27,6 +27,8 @@ import FolderIcon from '@material-ui/icons/Folder';
import SelectedIcon from '@material-ui/icons/CheckBox';
import UnSelectedIcon from '@material-ui/icons/CheckBoxOutlineBlank';
import TagIcon from '@material-ui/icons/LocalOfferOutlined';
+import { bindActionCreators } from 'redux';
+import { connect } from 'react-redux';
import { formatFileSize, formatDateTime } from '-/utils/misc';
import { extractTagsAsObjects, extractTitle } from '-/utils/paths';
import { findColorForFileEntry } from '-/services/utils-io';
@@ -37,6 +39,7 @@ import PlatformIO from '-/services/platform-io';
import { AppConfig } from '-/config';
import EntryIcon from '-/components/EntryIcon';
import { TS } from '-/tagspaces.namespace';
+import TaggingActions from '-/reducers/tagging-actions';
const maxDescriptionPreviewLength = 100;
@@ -63,6 +66,7 @@ interface Props {
handleGridContextMenu: (event: Object, fsEntry: TS.FileSystemEntry) => void;
handleGridCellDblClick: (event: Object, fsEntry: TS.FileSystemEntry) => void;
handleGridCellClick: (event: Object, fsEntry: TS.FileSystemEntry) => void;
+ editTagForEntry?: (path: string, tag: TS.Tag) => void;
}
const CellContent = (props: Props) => {
@@ -176,9 +180,7 @@ const CellContent = (props: Props) => {
<EntryIcon isFile={fsEntry.isFile} />
)}
<div id="gridCellTags" className={classes.gridCellTags}>
- {showTags && entryTags
- ? entryTags.map(tag => renderTag(tag))
- : tagPlaceholder}
+ {showTags && entryTags ? renderTags(entryTags) : tagPlaceholder}
</div>
{description.length > 0 && (
<Typography
@@ -306,9 +308,7 @@ const CellContent = (props: Props) => {
<Typography style={{ wordBreak: 'break-all', alignSelf: 'center' }}>
{entryTitle}
&nbsp;
- {showTags && entryTags
- ? entryTags.map(tag => renderTag(tag))
- : tagPlaceholder}
+ {showTags && entryTags ? renderTags(entryTags) : tagPlaceholder}
</Typography>
</Grid>
) : (
@@ -316,9 +316,7 @@ const CellContent = (props: Props) => {
<Typography style={{ wordBreak: 'break-all' }}>
{entryTitle}
</Typography>
- {showTags && entryTags
- ? entryTags.map(tag => renderTag(tag))
- : tagPlaceholder}
+ {showTags && entryTags ? renderTags(entryTags) : tagPlaceholder}
<Typography
style={{
color: 'gray'
@@ -365,27 +363,37 @@ const CellContent = (props: Props) => {
);
}
- function renderTag(tag: TS.Tag) {
- return isReadOnlyMode ? (
- <TagContainer
- tag={tag}
- key={fsEntry.path + tag.title}
- entryPath={fsEntry.path}
- addTags={addTags}
- handleTagMenu={handleTagMenu}
- selectedEntries={selectedEntries}
- />
- ) : (
- <TagContainerDnd
- tag={tag}
- key={fsEntry.path + tag.title}
- entryPath={fsEntry.path}
- addTags={addTags}
- addTag={addTag}
- handleTagMenu={handleTagMenu}
- selectedEntries={selectedEntries}
- />
- );
+ function renderTags(tags: Array<TS.Tag>) {
+ let sideCarLength = 0;
+ return tags.map((tag: TS.Tag, index) => {
+ const tagContainer = isReadOnlyMode ? (
+ <TagContainer
+ tag={tag}
+ key={fsEntry.path + tag.title}
+ entryPath={fsEntry.path}
+ addTags={addTags}
+ handleTagMenu={handleTagMenu}
+ selectedEntries={selectedEntries}
+ />
+ ) : (
+ <TagContainerDnd
+ tag={tag}
+ index={tag.type === 'sidecar' ? index : index - sideCarLength}
+ key={fsEntry.path + tag.title}
+ entryPath={fsEntry.path}
+ addTags={addTags}
+ addTag={addTag}
+ handleTagMenu={handleTagMenu}
+ selectedEntries={selectedEntries}
+ editTagForEntry={props.editTagForEntry}
+ />
+ );
+
+ if (tag.type === 'sidecar') {
+ sideCarLength = index + 1;
+ }
+ return tagContainer;
+ });
}
let entryHeight = 130;
@@ -432,4 +440,13 @@ const CellContent = (props: Props) => {
);
};
-export default CellContent;
+function mapActionCreatorsToProps(dispatch) {
+ return bindActionCreators(
+ {
+ editTagForEntry: TaggingActions.editTagForEntry
+ },
+ dispatch
+ );
+}
+
+export default connect(undefined, mapActionCreatorsToProps)(CellContent);
diff --git a/app/reducers/tagging-actions.ts b/app/reducers/tagging-actions.ts
index 0140b6c05..98718eba6 100644
--- a/app/reducers/tagging-actions.ts
+++ b/app/reducers/tagging-actions.ts
@@ -323,11 +323,15 @@ const actions = {
* @param newTagTitle
* @returns {Function}
*/
- editTagForEntry: (path: string, tag: TS.Tag, newTagTitle: string) => async (
+ editTagForEntry: (path: string, tag: TS.Tag, newTagTitle?: string) => async (
dispatch: (actions: Object) => void,
getState: () => any
) => {
const { settings, taglibrary } = getState();
+ if (newTagTitle === undefined) {
+ // eslint-disable-next-line no-param-reassign
+ newTagTitle = tag.title;
+ }
if (
tag.functionality === 'geoTagging' ||
tag.functionality === 'dateTagging'
@@ -355,17 +359,22 @@ const actions = {
settings.tagDelimiter,
PlatformIO.getDirSeparator()
);
- let tagFound = false;
+ let tagFoundPosition = -1;
for (let i = 0; i < extractedTags.length; i += 1) {
// check if tag is already in the tag array
if (extractedTags[i] === tag.title) {
extractedTags[i] = newTagTitle.trim();
- tagFound = true;
+ tagFoundPosition = i;
}
}
- if (!tagFound) {
+ if (tagFoundPosition === -1) {
// needed for the current implementation of geo tagging
extractedTags.push(newTagTitle); // tag.title);
+ } else if (tag.position !== undefined) {
+ // move tag
+ const element = extractedTags[tagFoundPosition];
+ extractedTags.splice(tagFoundPosition, 1);
+ extractedTags.splice(tag.position, 0, element);
}
const newFileName = generateFileName(
fileName,
@@ -384,14 +393,22 @@ const actions = {
loadMetaDataPromise(path)
.then(fsEntryMeta => {
let addMode = true;
- fsEntryMeta.tags.map(sidecarTag => {
+ let tagFoundPosition = -1;
+ fsEntryMeta.tags.map((sidecarTag, index) => {
if (sidecarTag.title === tag.title) {
// eslint-disable-next-line no-param-reassign
sidecarTag.title = newTagTitle;
addMode = false;
+ tagFoundPosition = index;
}
return true;
});
+ if (tag.position !== undefined) {
+ // move tag
+ const element = fsEntryMeta.tags[tagFoundPosition];
+ fsEntryMeta.tags.splice(tagFoundPosition, 1);
+ fsEntryMeta.tags.splice(tag.position, 0, element);
+ }
if (addMode) {
// eslint-disable-next-line no-param-reassign
tag.title = newTagTitle;
@@ -674,47 +691,6 @@ const actions = {
}
}
},
- changeTagOrder: (
- path: string,
- tag: TS.Tag,
- direction: 'prev' | 'next' | 'first'
- ) => () =>
- // dispatch: (actions: Object) => void
- {
- /*
- console.log('Moves the location of tag in the file name: ' + filePath);
- var fileName = extractFileName(filePath, PlatformIO.getDirSeparator());
- var containingDirectoryPath = extractContainingDirectoryPath(filePath, PlatformIO.getDirSeparator());
- var extractedTags = extractTags(filePath, settings.tagDelimiter, PlatformIO.getDirSeparator());
- if (extractedTags.indexOf(tagName) < 0) {
- showAlertDialog("The tag you are trying to move is not part of the file name and that's why it cannot be moved.", $.i18n.t("ns.common:warning"));
- return;
- }
- var tmpTag;
- for (var i = 0; i < extractedTags.length; i++) {
- // check if tag is already in the tag array
- if (extractedTags[i] === tagName) {
- if (direction === 'prev' && i > 0) {
- tmpTag = extractedTags[i - 1];
- extractedTags[i - 1] = extractedTags[i];
- extractedTags[i] = tmpTag;
- break;
- } else if (direction === 'next' && i < extractedTags.length - 1) {
- tmpTag = extractedTags[i];
- extractedTags[i] = extractedTags[i + 1];
- extractedTags[i + 1] = tmpTag;
- break;
- } else if (direction === 'first' && i > 0) {
- tmpTag = extractedTags[i];
- extractedTags[i] = extractedTags[0];
- extractedTags[0] = tmpTag;
- break;
- }
- }
- }
- var newFileName = generateFileName(fileName, extractedTags);
- renameFile(filePath, containingDirectoryPath + PlatformIO.getDirSeparator(), + newFileName); */
- },
// smart tagging -> PRO
addDateTag: (paths: Array<string>) => () =>
// dispatch: (actions: Object) => void
diff --git a/app/reducers/taglibrary.ts b/app/reducers/taglibrary.ts
index 167489b7a..3785c3bb6 100644
--- a/app/reducers/taglibrary.ts
+++ b/app/reducers/taglibrary.ts
@@ -530,7 +530,7 @@ export const actions = {
let newTags: Array<TS.Tag>;
if (typeof tag === 'object' && tag !== null) {
- if (!tagGroup.children.some(t => t.title === tag.title)) {
+ if (tagGroup.children.some(t => t.title === tag.title)) {
// tag exist
return;
}
diff --git a/app/tagspaces.namespace.d.ts b/app/tagspaces.namespace.d.ts
index 14e074c8d..3a103dc48 100644
--- a/app/tagspaces.namespace.d.ts
+++ b/app/tagspaces.namespace.d.ts
@@ -84,6 +84,7 @@ export namespace TS {
color?: string;
textcolor?: string;
originTitle?: string;
+ position?: number;
}
interface TagGroup {