Skip to content

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

feat: added dropup menu with ai prompts and ux improvements

Ilian Sapundshiev48c440e53087
diff --git a/src/renderer/components/CommonIcons.tsx b/src/renderer/components/CommonIcons.tsx
index f9bd143dd..d802d5218 100644
--- a/src/renderer/components/CommonIcons.tsx
+++ b/src/renderer/components/CommonIcons.tsx
@@ -31,6 +31,14 @@ export const TagLibraryIcon = (props) => (
<TagsIcon sx={{ transform: 'rotate(90deg)' }} {...props} />
);
+import ArrowDownIcon from '@mui/icons-material/ArrowDropDown';
+
+export const ArrowDropDownIcon = (props) => <ArrowDownIcon {...props} />;
+
+export const ArrowDropUpIcon = (props) => (
+ <ArrowDownIcon sx={{ transform: 'rotate(180deg)' }} {...props} />
+);
+
export const ParentFolderIcon = (props) => (
<KeyboardReturnIcon sx={{ transform: 'rotate(90deg)' }} {...props} />
);
@@ -342,10 +350,8 @@ export { default as VisibilityOffIcon } from '@mui/icons-material/VisibilityOff'
export { default as PasswordIcon } from '@mui/icons-material/Password';
-export { default as ArrowDropDownIcon } from '@mui/icons-material/ArrowDropDown';
-
export const MastodonIcon = (props) => (
- <SvgIcon>
+ <SvgIcon {...props}>
<svg
width="24"
height="24"
@@ -364,7 +370,7 @@ export const MastodonIcon = (props) => (
);
export const MainMenuIcon = (props) => (
- <SvgIcon>
+ <SvgIcon {...props}>
<svg
width="24"
height="24"
@@ -378,7 +384,7 @@ export const MainMenuIcon = (props) => (
);
export const OllamaIcon = (props) => (
- <SvgIcon>
+ <SvgIcon {...props}>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
diff --git a/src/renderer/components/chat/ChatMenu.tsx b/src/renderer/components/chat/ChatMenu.tsx
index b9f2ce469..bb8b214e5 100644
--- a/src/renderer/components/chat/ChatMenu.tsx
+++ b/src/renderer/components/chat/ChatMenu.tsx
@@ -46,7 +46,6 @@ interface ChatMenuProps {
handleCopy: () => void;
saveAsHtml: () => void;
saveAsMarkdown: () => void;
- appendSelectionToPrompt: () => void;
}
function ChatMenu(props: ChatMenuProps) {
@@ -59,7 +58,6 @@ function ChatMenu(props: ChatMenuProps) {
handleCopy,
saveAsHtml,
saveAsMarkdown,
- appendSelectionToPrompt,
} = props;
const { deleteHistory } = useChatContext();
@@ -115,12 +113,6 @@ function ChatMenu(props: ChatMenuProps) {
</ListItemIcon>
<ListItemText primary={t('core:clearHistory')} />
</MenuItem>
- <MenuItem onClick={appendSelectionToPrompt}>
- <ListItemIcon>
- <DeleteIcon />
- </ListItemIcon>
- <ListItemText primary={t('core:appendSelectionToPrompt')} />
- </MenuItem>
<Divider />
<MenuItem onClick={saveAsHtml}>
<ListItemIcon>
diff --git a/src/renderer/components/chat/ChatView.tsx b/src/renderer/components/chat/ChatView.tsx
index fb1b0ae25..064d72d23 100644
--- a/src/renderer/components/chat/ChatView.tsx
+++ b/src/renderer/components/chat/ChatView.tsx
@@ -18,14 +18,14 @@
import AppConfig from '-/AppConfig';
import {
+ AIIcon,
+ ArrowDropUpIcon,
CancelIcon,
CloseIcon,
MoreMenuIcon,
- OllamaIcon,
SendIcon,
} from '-/components/CommonIcons';
import DragItemTypes from '-/components/DragItemTypes';
-import TooltipTS from '-/components/Tooltip';
import TsIconButton from '-/components/TsIconButton';
import TsTextField from '-/components/TsTextField';
import ChatDndTargetFile from '-/components/chat/ChatDndTargetFile';
@@ -36,6 +36,7 @@ import ChatMdEditor from '-/components/md/ChatMdEditor';
import { CrepeRef } from '-/components/md/useCrepeHandler';
import { useChatContext } from '-/hooks/useChatContext';
import { useNotificationContext } from '-/hooks/useNotificationContext';
+import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
import { getDefaultAIProvider } from '-/reducers/settings';
import {
convertMarkDownToHtml,
@@ -43,11 +44,13 @@ import {
saveAsTextFile,
} from '-/services/utils-io';
import { MilkdownProvider } from '@milkdown/react';
-import { Box, Grid } from '@mui/material';
+import { Box, Divider, Grid } from '@mui/material';
import CircularProgress from '@mui/material/CircularProgress';
import FormControl from '@mui/material/FormControl';
import FormHelperText from '@mui/material/FormHelperText';
import InputAdornment from '@mui/material/InputAdornment';
+import Menu from '@mui/material/Menu';
+import MenuItem from '@mui/material/MenuItem';
import { useTheme } from '@mui/material/styles';
import { formatDateTime4Tag } from '@tagspaces/tagspaces-common/misc';
import { extractFileExtension } from '@tagspaces/tagspaces-common/paths';
@@ -55,6 +58,20 @@ import React, { ChangeEvent, useCallback, useReducer, useRef } from 'react';
import { NativeTypes } from 'react-dnd-html5-backend';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
+import TsMenuList from '../TsMenuList';
+
+const aiPrompts = [
+ {
+ title: 'Translate into German',
+ prompt:
+ 'You are a professional {sourceLang} to {targeLang} translator. Your goal is to accurately convey the meaning and nuances of the original {sourceLang} text while adhering to {targeLang} grammar, vocabulary, and cultural sensitivities. Produce only the {targeLang} translation, without any additional explanations or commentary. Please translate the following {sourceLang} text into {targeLang}:',
+ },
+ {
+ title: 'Correct German spelling ',
+ prompt:
+ 'You are a professional text corrector. Improve the text grammatically in German.',
+ },
+];
function ChatView() {
const { t } = useTranslation();
@@ -71,13 +88,17 @@ function ChatView() {
} = useChatContext();
const { showNotification } = useNotificationContext();
const aiDefaultProvider: AIProvider = useSelector(getDefaultAIProvider);
+ const { openedEntry } = useOpenedEntryContext();
const isLoading = useRef<boolean>(false);
const currentMode = useRef<ChatMode>(undefined);
const editorRef = useRef<CrepeRef>(null);
const milkdownDivRef = useRef<HTMLDivElement>(null);
const chatMsg = useRef<string>('');
+ const textInputRef = useRef<HTMLInputElement>(null);
const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
+ const [promptAnchorEl, setPromptAnchorEl] =
+ React.useState<null | HTMLElement>(null);
// Input change handler
const handleInputChange = useCallback(
@@ -136,6 +157,38 @@ function ChatView() {
});
}, [newChatMessage]);
+ // Prompt menu handlers
+ const handlePromptClick = useCallback(
+ (event: React.MouseEvent<HTMLElement>) => {
+ setPromptAnchorEl(event.currentTarget);
+ },
+ [],
+ );
+
+ const handlePromptClose = useCallback(() => {
+ setPromptAnchorEl(null);
+ }, []);
+
+ const handlePromptSelect = useCallback((prompt: string) => {
+ setPromptAnchorEl(null);
+ if (textInputRef.current) {
+ const cursorPos =
+ textInputRef.current.selectionStart || chatMsg.current.length;
+ const before = chatMsg.current.substring(0, cursorPos);
+ const after = chatMsg.current.substring(cursorPos);
+ chatMsg.current = before + prompt + after;
+ forceUpdate();
+ // Restore cursor position after prompt insertion
+ setTimeout(() => {
+ if (textInputRef.current) {
+ textInputRef.current.selectionStart = cursorPos + prompt.length;
+ textInputRef.current.selectionEnd = cursorPos + prompt.length;
+ textInputRef.current.focus();
+ }
+ }, 0);
+ }
+ }, []);
+
// Menu handlers
const handleMoreClick = useCallback(
(event: React.MouseEvent<HTMLElement>) => {
@@ -228,7 +281,7 @@ function ChatView() {
const appendSelectionToPrompt = () => {
const selectedText = getSelectedIFrameContent();
- chatMsg.current = chatMsg.current + selectedText;
+ chatMsg.current = chatMsg.current + '\n\n' + selectedText;
forceUpdate();
};
@@ -277,7 +330,6 @@ function ChatView() {
handleCopy={handleCopy}
saveAsHtml={saveAsHtml}
saveAsMarkdown={saveAsMarkdown}
- appendSelectionToPrompt={appendSelectionToPrompt}
/>
</Grid>
</Grid>
@@ -355,12 +407,9 @@ function ChatView() {
minRows={1}
maxRows={6}
value={chatMsg.current}
- // updateValue={(message) => {
- // chatMsg.current = chatMsg.current + message;
- // forceUpdate();
- // }}
+ inputRef={textInputRef}
sx={{
- '& .MuiInputBase-root': { padding: '2px 5px' },
+ '& .MuiInputBase-root': { padding: '4px' },
}}
onKeyDown={(event) => {
if (
@@ -376,19 +425,59 @@ function ChatView() {
input: {
startAdornment: (
<InputAdornment position="start">
- <TooltipTS
- title={
- aiDefaultProvider?.name +
- ' - ' +
- aiDefaultProvider?.url
+ <TsIconButton
+ size="small"
+ onClick={handlePromptClick}
+ aria-label={t('core:aiPrompts')}
+ aria-controls={
+ Boolean(promptAnchorEl)
+ ? 'prompt-menu'
+ : undefined
+ }
+ tooltip={t('core:aiPrompts')}
+ aria-haspopup="true"
+ aria-expanded={
+ Boolean(promptAnchorEl) ? 'true' : undefined
}
>
- <Box
- sx={{ display: 'flex', alignContent: 'center' }}
- >
- <OllamaIcon />
- </Box>
- </TooltipTS>
+ <AIIcon fontSize="small" />
+ <ArrowDropUpIcon />
+ </TsIconButton>
+ <Menu
+ id="prompt-menu"
+ anchorEl={promptAnchorEl}
+ open={Boolean(promptAnchorEl)}
+ onClose={handlePromptClose}
+ anchorOrigin={{
+ vertical: 'top',
+ horizontal: 'left',
+ }}
+ transformOrigin={{
+ vertical: 'bottom',
+ horizontal: 'left',
+ }}
+ >
+ <TsMenuList>
+ {aiPrompts.map((aiPrompt, index) => (
+ <MenuItem
+ key={index}
+ onClick={() => {
+ handlePromptSelect(aiPrompt.prompt);
+ }}
+ >
+ {aiPrompt.title}
+ </MenuItem>
+ ))}
+ {openedEntry?.isFile && (
+ <>
+ <Divider />
+ <MenuItem onClick={appendSelectionToPrompt}>
+ {t('core:appendSelectionToPrompt')}
+ </MenuItem>
+ </>
+ )}
+ </TsMenuList>
+ </Menu>
</InputAdornment>
),
endAdornment: (
@@ -408,7 +497,13 @@ function ChatView() {
</TsIconButton>
)}
<TsIconButton
- tooltip={t('core:send')}
+ tooltip={
+ t('core:send') +
+ ' -> ' +
+ aiDefaultProvider?.name +
+ ' - ' +
+ aiDefaultProvider?.url
+ }
onClick={handleChatMessage}
>
<SendIcon />
diff --git a/src/renderer/components/chat/SelectChatModel.tsx b/src/renderer/components/chat/SelectChatModel.tsx
index 8437b541a..ea3b4142f 100644
--- a/src/renderer/components/chat/SelectChatModel.tsx
+++ b/src/renderer/components/chat/SelectChatModel.tsx
@@ -16,7 +16,7 @@
*
*/
-import { AIIcon, DownloadIcon, RemoveIcon } from '-/components/CommonIcons';
+import { DownloadIcon, OllamaIcon, RemoveIcon } from '-/components/CommonIcons';
import TsIconButton from '-/components/TsIconButton';
import TsSelect from '-/components/TsSelect';
import { AIProvider } from '-/components/chat/ChatTypes';
@@ -24,7 +24,12 @@ import { getOllamaModels } from '-/components/chat/OllamaClient';
import { useChatContext } from '-/hooks/useChatContext';
import { useNotificationContext } from '-/hooks/useNotificationContext';
import { getDefaultAIProvider } from '-/reducers/settings';
-import { ListItemIcon, ListSubheader, MenuItem } from '@mui/material';
+import {
+ ListItemIcon,
+ ListItemText,
+ ListSubheader,
+ MenuItem,
+} from '@mui/material';
import InputAdornment from '@mui/material/InputAdornment';
import { format, parseISO } from 'date-fns';
import { ModelResponse } from 'ollama';
@@ -193,6 +198,7 @@ function SelectChatModel(props: Props) {
disabled={disabled}
value={chosenModel ? chosenModel : 'init'}
onChange={changeModel}
+ sx={{ '& .MuiSelect-select': { padding: '4px' } }}
label={label ? label : ''}
id={id ? id : 'selectChatModelId'}
slotProps={{
@@ -221,10 +227,25 @@ function SelectChatModel(props: Props) {
{installedModels && installedModels.length > 0 ? (
installedModels.map((model) => (
<MenuItem key={model.name} value={model.name} title={getTitle(model)}>
- <ListItemIcon>
- <AIIcon fontSize="small" />
+ <ListItemIcon
+ sx={{
+ display: 'inline-block',
+ minWidth: '30px',
+ paddingLeft: '3px',
+ }}
+ >
+ <OllamaIcon
+ sx={{ width: '24px', height: '24px', verticalAlign: 'middle' }}
+ />
</ListItemIcon>
- {model.name} {(model.size / (1024 * 1024 * 1024)).toFixed(2)} GB
+ <ListItemText
+ sx={{
+ display: 'inline-flex',
+ alignItems: 'center',
+ }}
+ >
+ {model.name} {(model.size / (1024 * 1024 * 1024)).toFixed(2)} GB
+ </ListItemText>
</MenuItem>
))
) : (
@@ -240,7 +261,7 @@ function SelectChatModel(props: Props) {
title={model.details.format}
>
<ListItemIcon>
- <DownloadIcon fontSize="small" />
+ <DownloadIcon />
</ListItemIcon>
{model.name}
</MenuItem>
@@ -248,7 +269,7 @@ function SelectChatModel(props: Props) {
<ListSubheader>{t('core:moreActions')}</ListSubheader>
<MenuItem value="customModel">
<ListItemIcon>
- <DownloadIcon fontSize="small" />
+ <DownloadIcon />
</ListItemIcon>
{t('core:installCustomModel')}
</MenuItem>
diff --git a/src/renderer/locales/en/core.json b/src/renderer/locales/en/core.json
index 36903c64f..22dafdd0b 100644
--- a/src/renderer/locales/en/core.json
+++ b/src/renderer/locales/en/core.json
@@ -969,6 +969,7 @@
"parentFolder": "Parent folder",
"remove": "Remove",
"send": "Send",
+ "appendSelectionToPrompt": "Append selected text to prompt",
"folderSpecificSettings": "folder specific",
"compareAndUpgrade": "Compare & Upgrade",
"start": "Start",
@@ -1149,6 +1150,7 @@
"hideToolbox": "Hide Toolbox\nUseful for making screenshots",
"descriptionAvailable": "Description available",
"aiChatAvailable": "AI Chat available",
+ "aiPrompts": "AI Prompts",
"filesPerMonth": "Files per month",
"confirmResetFileTypes": "Are you sure you want to reset all custom file type settings to default?",
"filterByFileExtension": "Filter by extension...",