TagSpaces is an offline, open source, document manager with tagging support
chore: refactoring the directory menu and adding a submenu for the new file creation
Ilian Sapundshiev6c426c35d0b8
diff --git a/capacitor/ios/App/App.xcodeproj/project.pbxproj b/capacitor/ios/App/App.xcodeproj/project.pbxprojindex a3b07e68d..93daddc9a 100644--- a/capacitor/ios/App/App.xcodeproj/project.pbxproj+++ b/capacitor/ios/App/App.xcodeproj/project.pbxproj@@ -319,7 +319,7 @@CODE_SIGN_ENTITLEMENTS = App/App.entitlements;CODE_SIGN_IDENTITY = "Apple Development";CODE_SIGN_STYLE = Automatic;- CURRENT_PROJECT_VERSION = 10257;+ CURRENT_PROJECT_VERSION = 10258;DEVELOPMENT_TEAM = DLK22E2679;INFOPLIST_FILE = App/Info.plist;IPHONEOS_DEPLOYMENT_TARGET = 15.0;@@ -345,7 +345,7 @@CODE_SIGN_ENTITLEMENTS = App/App.entitlements;CODE_SIGN_IDENTITY = "Apple Development";CODE_SIGN_STYLE = Automatic;- CURRENT_PROJECT_VERSION = 10257;+ CURRENT_PROJECT_VERSION = 10258;DEVELOPMENT_TEAM = DLK22E2679;INFOPLIST_FILE = App/Info.plist;IPHONEOS_DEPLOYMENT_TARGET = 15.0;diff --git a/src/renderer/components/TsMenuList.tsx b/src/renderer/components/TsMenuList.tsxindex aa3b2cb86..de9b80551 100644--- a/src/renderer/components/TsMenuList.tsx+++ b/src/renderer/components/TsMenuList.tsx@@ -16,18 +16,38 @@**/+import AppConfig from '-/AppConfig';import { isDesktopMode } from '-/reducers/settings';import MenuList, { MenuListProps } from '@mui/material/MenuList';+import { useEffect, useState } from 'react';import { useSelector } from 'react-redux';export type TSButtonProps = MenuListProps & {};function TsMenuList(props: TSButtonProps) {- const { children } = props;+ const { children, sx, ...rest } = props;const desktopMode = useSelector(isDesktopMode);+ // On touch devices the tap / long-press that opens the menu is immediately+ // followed by a synthesized click that lands on the first item rendered under+ // the finger, auto-selecting it. The list mounts when the menu opens, so we+ // ignore pointer events on it for a short window to swallow that ghost click;+ // the menu becomes interactive right after. Desktop is unaffected.+ const [interactive, setInteractive] = useState(!AppConfig.isNativeMobile);+ useEffect(() => {+ if (!AppConfig.isNativeMobile) {+ return undefined;+ }+ const timer = setTimeout(() => setInteractive(true), 350);+ return () => clearTimeout(timer);+ }, []);+return (- <MenuList dense={desktopMode ? true : false} {...props}>+ <MenuList+ dense={desktopMode ? true : false}+ sx={{ ...sx, pointerEvents: interactive ? undefined : 'none' }}+ {...rest}+ >{children}</MenuList>);diff --git a/src/renderer/components/menus/FileMenu.tsx b/src/renderer/components/menus/FileMenu.tsxindex aae943b5c..39a9fcf8c 100644--- a/src/renderer/components/menus/FileMenu.tsx+++ b/src/renderer/components/menus/FileMenu.tsx@@ -417,13 +417,23 @@ function FileMenu(props: Props) {onClose={() => setOpenWithAnchor(null)}anchorOrigin={{ vertical: 'top', horizontal: 'right' }}transformOrigin={{ vertical: 'top', horizontal: 'left' }}- // Let the submenu's modal backdrop pass pointer events through to the- // parent menu, so its other items (e.g. reorder) stay clickable while- // this hover-opened submenu is shown. The paper re-enables clicks.- slotProps={{- root: { sx: { pointerEvents: 'none' } },- paper: { sx: { pointerEvents: 'auto' } },- }}+ // Desktop: let the submenu's modal backdrop pass pointer events through+ // to the parent menu, so its other items (e.g. reorder) stay clickable+ // while this hover-opened submenu is shown (the paper re-enables+ // clicks). Since it opens on hover, close it on hover-out. On touch+ // there is no hover, so keep a normal modal backdrop — tapping outside+ // dismisses it.+ slotProps={+ AppConfig.isNativeMobile+ ? undefined+ : {+ root: { sx: { pointerEvents: 'none' } },+ paper: {+ sx: { pointerEvents: 'auto' },+ onMouseLeave: () => setOpenWithAnchor(null),+ },+ }+ }><TsMenuList>{openWithCandidates.map((c) => (diff --git a/src/renderer/perspectives/common/DirectoryMenuItems.tsx b/src/renderer/perspectives/common/DirectoryMenuItems.tsxindex 874caf796..1f4e2dbf2 100644--- a/src/renderer/perspectives/common/DirectoryMenuItems.tsx+++ b/src/renderer/perspectives/common/DirectoryMenuItems.tsx@@ -1,19 +1,12 @@import AppConfig from '-/AppConfig';import {- AddExistingFileIcon,- AudioRecordIcon,ChangeBackgroundIcon,CopyMoveIcon,CopyPictureIcon,DeleteIcon,EntryPropertiesIcon,- HTMLFileIcon,ImportTagsIcon,- LinkFileIcon,LinkIcon,- MarkdownFileIcon,- NewFileIcon,- NewFolderIcon,OpenEntryNativelyIcon,OpenFolderIcon,OpenNewWindowIcon,@@ -21,11 +14,10 @@ import {ReloadIcon,RenameIcon,TagIcon,- TemplateFileIcon,} from '-/components/CommonIcons';import { ProLabel } from '-/components/HelperComponents';-import InfoIcon from '-/components/InfoIcon';import MenuKeyBinding from '-/components/menus/MenuKeyBinding';+import NewSubMenu from '-/perspectives/common/NewSubMenu';import { Pro } from '-/pro';import {getKeyBindingObject,@@ -244,157 +236,29 @@ export function getDirectoryMenuItems();}menuItems.push(<Divider key={`divider-${menuItems.length}`} />);- if (!isReadOnlyMode && !perspectiveMode) {- if (createNewFile) {- menuItems.push(- <MenuItem- key="createNewTextFile"- data-tid="createNewTextFileTID"- onClick={() => {- onClose();- createNewFile('txt');- }}- >- <ListItemIcon>- <NewFileIcon />- </ListItemIcon>- <ListItemText primary={t('core:createTextFile')} />- </MenuItem>,- );-- menuItems.push(- <MenuItem- key="createNewMarkdownFile"- data-tid="createNewMarkdownFileTID"- onClick={() => {- onClose();- createNewFile('md');- }}- >- <ListItemIcon>- <MarkdownFileIcon />- </ListItemIcon>- <ListItemText primary={t('core:createMarkdown')} />- <InfoIcon tooltip={t('core:createMarkdownTitle')} />- </MenuItem>,- );- menuItems.push(- <MenuItem- key="createHTMLTextFile"- data-tid="createHTMLTextFileTID"- onClick={() => {- onClose();- createNewFile('html');- }}- >- <ListItemIcon>- <HTMLFileIcon />- </ListItemIcon>- <ListItemText primary={t('core:createRichTextFile')} />- <InfoIcon tooltip={t('core:createNoteTitle')} />- </MenuItem>,- );- menuItems.push(- <MenuItem- key="createNewLinkFile"- data-tid="createNewLinkFileTID"- onClick={() => {- onClose();- createNewFile('url');- }}- >- <ListItemIcon>- <LinkFileIcon />- </ListItemIcon>- <ListItemText primary={t('core:createLinkFile')} />- </MenuItem>,- );- if (!hideProFeatures) {- menuItems.push(- <MenuItem- key="createNewFromTemplate"- data-tid="createNewFromTemplateTID"- disabled={!Pro}- onClick={() => {- onClose();- createNewFile();- }}- >- <ListItemIcon>- <TemplateFileIcon />- </ListItemIcon>- <ListItemText- primary={- <>- {t('core:createNewFromTemplate')}- {!Pro && <ProLabel />}- </>- }- />- </MenuItem>,- );- }- }- if (createNewAudio && !hideProFeatures) {- menuItems.push(- <MenuItem- key="createNewAudio"- data-tid="createNewAudioTID"- disabled={!Pro}- onClick={() => {- onClose();- createNewAudio();- }}- >- <ListItemIcon>- <AudioRecordIcon />- </ListItemIcon>- <ListItemText- primary={- <>- {t('core:newAudioRecording')}- {!Pro && <ProLabel />}- </>- }- />- </MenuItem>,- );- }- if (showCreateDirectoryDialog) {- menuItems.push(<Divider key={`divider-${menuItems.length}`} />);- menuItems.push(- <MenuItem- key="newSubDirectory"- data-tid="newSubDirectory"- onClick={() => {- onClose();- showCreateDirectoryDialog();- }}- >- <ListItemIcon>- <NewFolderIcon />- </ListItemIcon>- <ListItemText primary={t('core:newSubdirectory')} />- </MenuItem>,- );- }- if (addExistingFile) {- menuItems.push(- <MenuItem- key="addExistingFile"- data-tid="addExistingFile"- onClick={() => {- onClose();- addExistingFile();- }}- >- <ListItemIcon>- <AddExistingFileIcon />- </ListItemIcon>- <ListItemText primary={t('core:addFiles')} />- </MenuItem>,- );- }+ // All create/add actions (new text/markdown/rich-text/link/template/audio+ // file, new subfolder, add from device) are grouped into a single "New ▸"+ // submenu — see NewSubMenu. It owns its own submenu state, so it's pushed as+ // one self-contained element.+ if (+ !isReadOnlyMode &&+ !perspectiveMode &&+ (createNewFile ||+ createNewAudio ||+ showCreateDirectoryDialog ||+ addExistingFile)+ ) {+ menuItems.push(+ <NewSubMenu+ key="newSubMenu"+ onClose={onClose}+ t={t}+ createNewFile={createNewFile}+ createNewAudio={createNewAudio}+ showCreateDirectoryDialog={showCreateDirectoryDialog}+ addExistingFile={addExistingFile}+ />,+ );}if (selectedEntries.length < 2) {menuItems.push(<Divider key={`divider-${menuItems.length}`} />);diff --git a/src/renderer/perspectives/common/NewSubMenu.tsx b/src/renderer/perspectives/common/NewSubMenu.tsxnew file mode 100644index 000000000..c9507f519--- /dev/null+++ b/src/renderer/perspectives/common/NewSubMenu.tsx@@ -0,0 +1,231 @@+/**+ * TagSpaces - universal file and folder organizer+ * Copyright (C) 2017-present TagSpaces GmbH+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License (version 3) as+ * published by the Free Software Foundation.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++import AppConfig from '-/AppConfig';+import {+ AddExistingFileIcon,+ AudioRecordIcon,+ HTMLFileIcon,+ LinkFileIcon,+ MarkdownFileIcon,+ NewFileIcon,+ NewFolderIcon,+ SmallArrowRightIcon,+ TemplateFileIcon,+} from '-/components/CommonIcons';+import { ProLabel } from '-/components/HelperComponents';+import InfoIcon from '-/components/InfoIcon';+import TsMenuList from '-/components/TsMenuList';+import { Pro } from '-/pro';+import { isHideProFeatures } from '-/reducers/settings';+import { TS } from '-/tagspaces.namespace';+import Divider from '@mui/material/Divider';+import ListItemIcon from '@mui/material/ListItemIcon';+import ListItemText from '@mui/material/ListItemText';+import Menu from '@mui/material/Menu';+import MenuItem from '@mui/material/MenuItem';+import { useState } from 'react';+import { useSelector } from 'react-redux';++interface Props {+ onClose: () => void;+ t: (txt: string) => string;+ createNewFile?: (fileType?: TS.FileType) => void;+ createNewAudio?: () => void;+ showCreateDirectoryDialog?: () => void;+ addExistingFile?: () => void;+}++/**+ * The "New ▸" entry of the directory context menu: a single parent item that+ * opens a submenu holding every create/add action (new text/markdown/rich-text/+ * link/template/audio file, new subfolder, add from device). Encapsulates its+ * own submenu anchor state so getDirectoryMenuItems stays a flat array builder.+ * Mirrors the hover-opened submenu pattern in FileMenu's "Open with".+ */+function NewSubMenu(props: Props) {+ const {+ onClose,+ t,+ createNewFile,+ createNewAudio,+ showCreateDirectoryDialog,+ addExistingFile,+ } = props;+ const hideProFeatures: boolean = useSelector(isHideProFeatures);+ const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null);++ // Run a create action and dismiss the whole menu chain.+ const run = (action: () => void) => {+ setAnchorEl(null);+ onClose();+ action();+ };++ return (+ <>+ <MenuItem+ data-tid="createNewSubmenuTID"+ onClick={(e) => setAnchorEl(e.currentTarget)}+ onMouseEnter={(e) => setAnchorEl(e.currentTarget)}+ >+ <ListItemIcon>+ <NewFileIcon />+ </ListItemIcon>+ <ListItemText primary={t('core:new')} />+ <SmallArrowRightIcon fontSize="small" />+ </MenuItem>+ <Menu+ anchorEl={anchorEl}+ open={Boolean(anchorEl)}+ onClose={() => setAnchorEl(null)}+ anchorOrigin={{ vertical: 'top', horizontal: 'right' }}+ transformOrigin={{ vertical: 'top', horizontal: 'left' }}+ // Desktop: let the submenu's modal backdrop pass pointer events through+ // to the parent menu so its other items stay clickable while this+ // hover-opened submenu is shown (the paper re-enables clicks). Since it+ // opens on hover, close it again on hover-out. On touch there is no+ // hover, so keep a normal modal backdrop — tapping outside dismisses it.+ slotProps={+ AppConfig.isNativeMobile+ ? undefined+ : {+ root: { sx: { pointerEvents: 'none' } },+ paper: {+ sx: { pointerEvents: 'auto' },+ onMouseLeave: () => setAnchorEl(null),+ },+ }+ }+ >+ <TsMenuList>+ {createNewFile && [+ <MenuItem+ key="createNewTextFile"+ data-tid="createNewTextFileTID"+ onClick={() => run(() => createNewFile('txt'))}+ >+ <ListItemIcon>+ <NewFileIcon />+ </ListItemIcon>+ <ListItemText primary={t('core:createTextFile')} />+ </MenuItem>,+ <MenuItem+ key="createNewMarkdownFile"+ data-tid="createNewMarkdownFileTID"+ onClick={() => run(() => createNewFile('md'))}+ >+ <ListItemIcon>+ <MarkdownFileIcon />+ </ListItemIcon>+ <ListItemText primary={t('core:createMarkdown')} />+ <InfoIcon tooltip={t('core:createMarkdownTitle')} />+ </MenuItem>,+ <MenuItem+ key="createHTMLTextFile"+ data-tid="createHTMLTextFileTID"+ onClick={() => run(() => createNewFile('html'))}+ >+ <ListItemIcon>+ <HTMLFileIcon />+ </ListItemIcon>+ <ListItemText primary={t('core:createRichTextFile')} />+ <InfoIcon tooltip={t('core:createNoteTitle')} />+ </MenuItem>,+ <MenuItem+ key="createNewLinkFile"+ data-tid="createNewLinkFileTID"+ onClick={() => run(() => createNewFile('url'))}+ >+ <ListItemIcon>+ <LinkFileIcon />+ </ListItemIcon>+ <ListItemText primary={t('core:createLinkFile')} />+ </MenuItem>,+ !hideProFeatures && (+ <MenuItem+ key="createNewFromTemplate"+ data-tid="createNewFromTemplateTID"+ disabled={!Pro}+ onClick={() => run(() => createNewFile())}+ >+ <ListItemIcon>+ <TemplateFileIcon />+ </ListItemIcon>+ <ListItemText+ primary={+ <>+ {t('core:createNewFromTemplate')}+ {!Pro && <ProLabel />}+ </>+ }+ />+ </MenuItem>+ ),+ ]}+ {createNewAudio && !hideProFeatures && (+ <MenuItem+ key="createNewAudio"+ data-tid="createNewAudioTID"+ disabled={!Pro}+ onClick={() => run(() => createNewAudio())}+ >+ <ListItemIcon>+ <AudioRecordIcon />+ </ListItemIcon>+ <ListItemText+ primary={+ <>+ {t('core:newAudioRecording')}+ {!Pro && <ProLabel />}+ </>+ }+ />+ </MenuItem>+ )}+ {showCreateDirectoryDialog && [+ <Divider key="newSubDirectoryDivider" />,+ <MenuItem+ key="newSubDirectory"+ data-tid="newSubDirectory"+ onClick={() => run(() => showCreateDirectoryDialog())}+ >+ <ListItemIcon>+ <NewFolderIcon />+ </ListItemIcon>+ <ListItemText primary={t('core:newSubdirectory')} />+ </MenuItem>,+ ]}+ {addExistingFile && (+ <MenuItem+ key="addExistingFile"+ data-tid="addExistingFile"+ onClick={() => run(() => addExistingFile())}+ >+ <ListItemIcon>+ <AddExistingFileIcon />+ </ListItemIcon>+ <ListItemText primary={t('core:addFiles')} />+ </MenuItem>+ )}+ </TsMenuList>+ </Menu>+ </>+ );+}++export default NewSubMenu;