TagSpaces is an offline, open source, document manager with tagging support
Add menu entry use as background for the current folder in the file context menu (#1750)
Stanimir449ebb40ca07
diff --git a/app/components/menus/FileMenu.tsx b/app/components/menus/FileMenu.tsxindex b250461d6..00a8eb5c0 100644--- a/app/components/menus/FileMenu.tsx+++ b/app/components/menus/FileMenu.tsx@@ -47,10 +47,15 @@ import PlatformIO from '-/services/platform-facade';import {generateFileName,getAllPropertiesPromise,+ setFolderBackgroundPromise,setFolderThumbnailPromise} from '-/services/utils-io';import { Pro } from '-/pro';import { TS } from '-/tagspaces.namespace';+import { bindActionCreators } from 'redux';+import { actions as AppActions } from '-/reducers/app';+import { connect } from 'react-redux';+import { supportedImgs } from '-/services/thumbsgenerator';// import AddIcon from '@mui/icons-material/Add';interface Props {@@ -81,6 +86,7 @@ interface Props {selectedEntries: Array<any>;currentLocation: TS.Location;locations: Array<TS.Location>;+ setLastBackgroundImageChange: (number) => void;}function FileMenu(props: Props) {@@ -157,6 +163,24 @@ function FileMenu(props: Props) {});}+ function setFolderBackground() {+ onClose();+ setFolderBackgroundPromise(props.selectedFilePath)+ .then((directoryPath: string) => {+ props.setLastBackgroundImageChange(new Date().getTime());+ showNotification('Background created for: ' + directoryPath);+ return true;+ })+ .catch(error => {+ showNotification('Background creation failed.');+ console.warn(+ 'Error setting Background for entry: ' + props.selectedFilePath,+ error+ );+ return true;+ });+ }+function showAddRemoveTagsDialog() {onClose();props.openAddRemoveTagsDialog();@@ -366,6 +390,22 @@ function FileMenu(props: Props) {<ListItemText primary={i18n.t('core:setAsThumbnail')} /></MenuItem>);+ if (+ supportedImgs.some(ext => props.selectedFilePath.endsWith('.' + ext))+ ) {+ menuItems.push(+ <MenuItem+ key="setAsBgndTID"+ data-tid="setAsBgndTID"+ onClick={setFolderBackground}+ >+ <ListItemIcon>+ <ImageIcon />+ </ListItemIcon>+ <ListItemText primary={i18n.t('core:setAsBackground')} />+ </MenuItem>+ );+ }}menuItems.push(<MenuItem@@ -417,4 +457,13 @@ function FileMenu(props: Props) {);}-export default FileMenu;+function mapDispatchToProps(dispatch) {+ return bindActionCreators(+ {+ setLastBackgroundImageChange: AppActions.setLastBackgroundImageChange+ },+ dispatch+ );+}++export default connect(undefined, mapDispatchToProps)(FileMenu);diff --git a/app/locales/en/core.json b/app/locales/en/core.jsonindex 145b563f8..5f0d0ccac 100644--- a/app/locales/en/core.json+++ b/app/locales/en/core.json@@ -269,6 +269,7 @@"info": "Info","signOut": "Sign Out","setAsThumbnail": "Use as Thumbnail for Current Folder",+ "setAsBackground": "Use as Background for Current Folder","setAsParentFolderThumbnail": "Use as Thumbnail for Parent Folder","fileExtension": "File Ext.","fileTitle": "Title",diff --git a/app/services/thumbsgenerator.ts b/app/services/thumbsgenerator.tsindex 641288aa5..b015a5656 100644--- a/app/services/thumbsgenerator.ts+++ b/app/services/thumbsgenerator.ts@@ -43,7 +43,8 @@ export const supportedImgs = ['gif','svg','webp',- 'bmp'+ 'bmp',+ 'avif'];export const supportedContainers = ['zip',diff --git a/app/services/utils-io.ts b/app/services/utils-io.tsindex bbd80fd48..6472392ab 100644--- a/app/services/utils-io.ts+++ b/app/services/utils-io.ts@@ -43,7 +43,8 @@ import {extractContainingDirectoryPath,extractDirectoryName,getThumbFileLocationForFile,- getThumbFileLocationForDirectory+ getThumbFileLocationForDirectory,+ getBgndFileLocationForDirectory} from '@tagspaces/tagspaces-platforms/paths';import AppConfig from '@tagspaces/tagspaces-platforms/AppConfig';import PlatformIO from './platform-facade';@@ -58,9 +59,11 @@ import {supportedImgs,supportedText,supportedVideos,- supportedMisc+ supportedMisc,+ generateImageThumbnail} from '-/services/thumbsgenerator';import { defaultSettings } from '-/perspectives/grid-perspective';+import { base64ToArrayBuffer } from '-/utils/dom';const supportedImgsWS = ['jpg',@@ -1049,6 +1052,41 @@ export function setFolderThumbnailPromise(filePath: string): Promise<string> {).then(() => directoryPath);}+/**+ * @param filePath+ * return Promise<directoryPath> of directory in order to open Folder properties next+ */+export function setFolderBackgroundPromise(filePath: string): Promise<string> {+ const directoryPath = extractContainingDirectoryPath(+ filePath,+ PlatformIO.getDirSeparator()+ );++ const folderBgndPath = getBgndFileLocationForDirectory(+ directoryPath,+ PlatformIO.getDirSeparator()+ );++ return generateImageThumbnail(filePath, AppConfig.maxBgndSize) // 4K -> 3840, 2K -> 2560+ .then(base64Image => {+ if (base64Image) {+ const data = base64ToArrayBuffer(base64Image.split(',').pop());+ return PlatformIO.saveBinaryFilePromise(folderBgndPath, data, true)+ .then(() => {+ // props.setLastBackgroundImageChange(new Date().getTime());+ return directoryPath;+ })+ .catch(error => {+ console.error('Save to file failed ', error);+ return true;+ });+ }+ })+ .catch(error => {+ console.error('Background generation failed ' + error);+ });+}+export function findBackgroundColorForFolder(fsEntry: TS.FileSystemEntry) {if (!fsEntry.isFile) {if (fsEntry.color) {