Skip to content

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

chore: refactoring new file creation from template dialog

Ilian Sapundshiev8bb855766c34
diff --git a/capacitor/ios/App/App.xcodeproj/project.pbxproj b/capacitor/ios/App/App.xcodeproj/project.pbxproj
index a5a44081b..88ecdf020 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 = 10328;
+ CURRENT_PROJECT_VERSION = 10332;
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 = 10328;
+ CURRENT_PROJECT_VERSION = 10332;
DEVELOPMENT_TEAM = DLK22E2679;
INFOPLIST_FILE = App/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
diff --git a/release/app/package-lock.json b/release/app/package-lock.json
index 135e2b899..434bb98ee 100644
--- a/release/app/package-lock.json
+++ b/release/app/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "tagspaces",
- "version": "6.13.5",
+ "version": "6.13.9",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "tagspaces",
- "version": "6.13.5",
+ "version": "6.13.9",
"hasInstallScript": true,
"license": "AGPL-3.0",
"dependencies": {
diff --git a/src/renderer/components/dialogs/NewFileDialog.tsx b/src/renderer/components/dialogs/NewFileDialog.tsx
index d33574425..a5149b672 100644
--- a/src/renderer/components/dialogs/NewFileDialog.tsx
+++ b/src/renderer/components/dialogs/NewFileDialog.tsx
@@ -53,7 +53,7 @@ import {
generateFileName,
} from '@tagspaces/tagspaces-common/paths';
import { getUuid } from '@tagspaces/tagspaces-common/utils-io';
-import { useContext, useEffect, useReducer, useRef } from 'react';
+import { useContext, useEffect, useReducer, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
@@ -89,10 +89,24 @@ function NewFileDialog(props: Props) {
const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
const theme = useTheme();
const smallScreen = useMediaQuery(theme.breakpoints.down('md'));
- const fileTemplate = fileTemplatesContext?.getTemplate(fileType);
+ const firstRender = useFirstRender();
+ // Template selected from the tile grid (when the dialog is opened without a
+ // fixed fileType). Selecting a tile loads its name + content into the
+ // editable form below instead of creating the file immediately.
+ const [selectedTemplate, setSelectedTemplate] = useState<TS.FileTemplate>();
+
+ // The effective type drives the editable form: either the type the dialog
+ // was opened with, or the type of the template picked from the grid.
+ const effectiveFileType = fileType ?? selectedTemplate?.type;
+ // Active template for the effective type. This also reflects the in-form
+ // "change template" dropdown, which calls setTemplateActive. Deriving it from
+ // effectiveFileType (not the raw fileType prop) is what lets the dropdown
+ // reload the editable name + content during the grid-selected phase.
+ const fileTemplate = fileTemplatesContext?.getTemplate(
+ effectiveFileType as TS.FileType,
+ );
const fileNameRef = useRef<string>(getFileName());
const fileContentRef = useRef<string>(getFileContent());
- const firstRender = useFirstRender();
useEffect(() => {
if (
@@ -103,10 +117,53 @@ function NewFileDialog(props: Props) {
) {
fileNameRef.current = getFileName();
fileContentRef.current = getFileContent();
- forceUpdate();
+ // In grid-selected mode the dropdown switches the active template; keep
+ // the dialog title + selection in sync with it.
+ if (
+ !fileType &&
+ selectedTemplate &&
+ selectedTemplate.id !== fileTemplate.id
+ ) {
+ setSelectedTemplate(fileTemplate);
+ } else {
+ forceUpdate();
+ }
}
}, [fileTemplate]);
+ // Reset to the template grid (and refresh the editable defaults) whenever the
+ // dialog is (re)opened — the dialog stays mounted via keepMounted, so state
+ // would otherwise leak between openings.
+ useEffect(() => {
+ if (open) {
+ setSelectedTemplate(undefined);
+ fileNameRef.current = getFileName();
+ fileContentRef.current = getFileContent();
+ haveError.current = false;
+ urlInputError.current = undefined;
+ }
+ }, [open]);
+
+ function selectTemplate(template: TS.FileTemplate) {
+ // Make the clicked template the active one for its type so the in-form
+ // "change template" dropdown shows it as selected.
+ fileTemplatesContext?.setTemplateActive(template.id);
+ fileNameRef.current =
+ template.fileNameTmpl !== undefined
+ ? getFileNameFromTemplate(template)
+ : getFileName();
+ fileContentRef.current = getFileContentFromTemplate(template);
+ haveError.current = false;
+ setSelectedTemplate(template);
+ }
+
+ function backToTemplates() {
+ fileNameRef.current = getFileName();
+ fileContentRef.current = getFileContent();
+ haveError.current = false;
+ setSelectedTemplate(undefined);
+ }
+
function getFileName() {
if (fileName) {
const tags: string[] = extractTags(fileName, tagDelimiter);
@@ -168,6 +225,9 @@ function NewFileDialog(props: Props) {
}
function getFileType() {
+ if (selectedTemplate) {
+ return selectedTemplate.name || t('createNewFromTemplate');
+ }
if (fileType === 'txt') {
return t('createTXTFile');
}
@@ -219,9 +279,9 @@ function NewFileDialog(props: Props) {
data-tid="createTID"
variant="contained"
onClick={() => {
- createFile(fileType, targetDirectoryPath);
+ createFile(effectiveFileType, targetDirectoryPath);
}}
- disabled={haveError.current}
+ disabled={haveError.current || !effectiveFileType}
sx={
{
WebkitAppRegion: 'no-drag',
@@ -246,7 +306,7 @@ function NewFileDialog(props: Props) {
dialogTitle={getFileType()}
onClose={onClose}
closeButtonTestId="closeNewFileDialogTID"
- actionSlot={okButton}
+ actionSlot={effectiveFileType ? okButton : undefined}
></TsDialogTitle>
<DialogContent
sx={{
@@ -273,7 +333,8 @@ function NewFileDialog(props: Props) {
/>
) : (
<CreateFile
- fileType={fileType}
+ fileType={effectiveFileType}
+ onSelectTemplate={selectTemplate}
createFile={(type, template) =>
createFile(type, targetDirectoryPath, template)
}
@@ -292,16 +353,22 @@ function NewFileDialog(props: Props) {
)}
<TargetPath />
</DialogContent>
- {!smallScreen && fileType && (
+ {!smallScreen && effectiveFileType && (
<TsDialogActions>
- <TsButton
- data-tid="backTID"
- onClick={() => {
- onClose();
- }}
- >
- {t('core:cancel')}
- </TsButton>
+ {!fileType && selectedTemplate ? (
+ <TsButton data-tid="backToTemplatesTID" onClick={backToTemplates}>
+ {t('core:goback')}
+ </TsButton>
+ ) : (
+ <TsButton
+ data-tid="backTID"
+ onClick={() => {
+ onClose();
+ }}
+ >
+ {t('core:cancel')}
+ </TsButton>
+ )}
{okButton}
</TsDialogActions>
)}
diff --git a/src/renderer/components/dialogs/components/CreateFile.tsx b/src/renderer/components/dialogs/components/CreateFile.tsx
index 77c6ba07f..b96f4e7ae 100644
--- a/src/renderer/components/dialogs/components/CreateFile.tsx
+++ b/src/renderer/components/dialogs/components/CreateFile.tsx
@@ -29,7 +29,7 @@ import useFirstRender from '-/utils/useFirstRender';
import {
Box,
Card,
- CardActions,
+ CardActionArea,
CardContent,
FormControl,
Typography,
@@ -55,6 +55,12 @@ interface Props {
tidPrefix?: string;
fileType?: TS.FileType;
onClose: (event?: Object, reason?: string) => void;
+ /**
+ * Called when a template tile is clicked. The parent loads the template's
+ * name + content into the editable form instead of creating the file
+ * immediately.
+ */
+ onSelectTemplate?: (template: TS.FileTemplate) => void;
}
function CreateFile(props: Props) {
@@ -68,6 +74,7 @@ function CreateFile(props: Props) {
handleFileContentChange,
haveError,
onClose,
+ onSelectTemplate,
} = props;
const { t } = useTranslation();
const { targetDirectoryPath } = useTargetPathContext();
@@ -104,6 +111,17 @@ function CreateFile(props: Props) {
return tid;
}
+ // Stable test-id fragment per template type, e.g. selectMarkdownTemplateTID.
+ function templateTidType(type?: TS.FileType) {
+ if (type === 'md') {
+ return 'Markdown';
+ }
+ if (type === 'html') {
+ return 'Html';
+ }
+ return 'Text';
+ }
+
const onInputFocus = (event) => {
setTimeout(() => {
if (fileName) {
@@ -186,79 +204,70 @@ function CreateFile(props: Props) {
</>
) : (
<Box sx={{ flexGrow: 1 }}>
- <TsButton
- size="small"
- variant="text"
- onClick={() => {
- onClose();
- openSettingsDialog(SettingsTab.Templates);
- }}
- disabled={!Pro}
- >
- {t('manageTemplates')}
- </TsButton>
<Grid container spacing={2}>
{templatesArray?.map((template: TS.FileTemplate, index) => (
<Grid key={index} size={6}>
- <Card variant="outlined">
- <CardContent>
- <Box
- sx={{
- display: 'flex',
- flexWrap: 'nowrap',
- alignItems: 'anchor-center',
- }}
- >
- <Typography variant="h6">{template.name}</Typography>
+ <Card
+ variant="outlined"
+ sx={{ borderRadius: `${AppConfig.defaultCSSRadius}px` }}
+ >
+ <CardActionArea
+ disabled={noSuitableLocation}
+ onClick={() => onSelectTemplate?.(template)}
+ data-tid={`select${templateTidType(template.type)}TemplateTID`}
+ >
+ <CardContent>
<Box
sx={{
- marginLeft: '5px',
- padding: '2px',
- fontSize: '12px',
- textTransform: 'uppercase',
- border: '1px solid gray',
- borderRadius: '3px',
- height: '13px',
+ display: 'flex',
+ flexWrap: 'nowrap',
+ alignItems: 'anchor-center',
}}
>
- {template.type}
+ <Typography variant="h6">{template.name}</Typography>
+ <Box
+ sx={{
+ marginLeft: '5px',
+ padding: '2px',
+ fontSize: '12px',
+ textTransform: 'uppercase',
+ border: '1px solid gray',
+ borderRadius: '3px',
+ height: '13px',
+ }}
+ >
+ {template.type}
+ </Box>
</Box>
- </Box>
- <Typography
- variant="body2"
- title={template.content}
- sx={{
- maxHeight: '75px',
- height: '75px',
- overflowY: 'auto',
- }}
- >
- {template.description || template.content}
- </Typography>
- </CardContent>
- <CardActions>
- <TsButton
- size="small"
- sx={{
- marginLeft: '10px',
- marginBottom: '10px',
- marginTop: '-10px',
- }}
- onClick={() => createFile(template.type, template)}
- data-tid={
- 'create' +
- (template.type === 'md' ? 'Markdown' : template.type) +
- 'Button'
- }
- disabled={noSuitableLocation}
- >
- {t('useTemplate')}
- </TsButton>
- </CardActions>
+ <Typography
+ variant="body2"
+ title={template.content}
+ sx={{
+ maxHeight: '75px',
+ height: '75px',
+ overflowY: 'auto',
+ }}
+ >
+ {template.description || template.content}
+ </Typography>
+ </CardContent>
+ </CardActionArea>
</Card>
</Grid>
))}
</Grid>
+ <TsButton
+ size="small"
+ variant="text"
+ sx={{ marginTop: '10px' }}
+ onClick={() => {
+ onClose();
+ openSettingsDialog(SettingsTab.Templates);
+ }}
+ disabled={!Pro}
+ >
+ {t('manageTemplates')}
+ </TsButton>
</Box>
)}
</Grid>