Skip to content

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

chore: refactoring chatview, adding function for extracting content from the viewers and pasting in the chat prompt

Ilian Sapundshiev18be5f39ae10
diff --git a/src/renderer/components/AiPropertiesTab.tsx b/src/renderer/components/AiPropertiesTab.tsx
deleted file mode 100644
index a183faa6e..000000000
--- a/src/renderer/components/AiPropertiesTab.tsx
+++ /dev/null
@@ -1,62 +0,0 @@
-/**
- * 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 ChatView from '-/components/chat/ChatView';
-import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
-import { isDevMode } from '-/reducers/settings';
-import { useSelector } from 'react-redux';
-
-interface Props {}
-
-function AiPropertiesTab(props: Props) {
- const { openedEntry } = useOpenedEntryContext();
- const devMode: boolean = useSelector(isDevMode);
-
- return <ChatView />;
-
- // const iframe = document.getElementsByTagName('iframe')[0];
-
- // function getIframeSelection() {
- // const iframeWindow = iframe.contentWindow;
- // const selection = iframeWindow.getSelection();
- // const selectionText = selection.toString();
- // if (selectionText) {
- // return selectionText;
- // } else {
- // const childIframe =
- // iframeWindow.document.getElementsByTagName('iframe')[0];
- // const subselection = childIframe.contentWindow.getSelection();
- // return subselection.toString();
- // }
- // }
- // alert(getIframeSelection());
-
- // return (
- // <Box position="relative" display="inline-flex">
- // <AiGenDescButton />
- // {openedEntry.meta?.description && (
- // <AiGenTagsButton
- // fromDescription={true}
- // sx={{ marginLeft: AppConfig.defaultSpaceBetweenButtons }}
- // />
- // )}
- // </Box>
- // );
-}
-
-export default AiPropertiesTab;
diff --git a/src/renderer/components/EntryContainerTabs.tsx b/src/renderer/components/EntryContainerTabs.tsx
index 1bfecd6c4..65761b5b7 100644
--- a/src/renderer/components/EntryContainerTabs.tsx
+++ b/src/renderer/components/EntryContainerTabs.tsx
@@ -46,7 +46,7 @@ const TabRevisions = React.lazy(
() => import(/* webpackChunkName: "Revisions" */ './Revisions'),
);
const TabAI = React.lazy(
- () => import(/* webpackChunkName: "AiPropertiesTab" */ './AiPropertiesTab'),
+ () => import(/* webpackChunkName: "ChatView" */ './chat/ChatView'),
);
const TabLinks = React.lazy(
() => import(/* webpackChunkName: "LinksTab" */ './LinksTab'),
diff --git a/src/renderer/components/chat/ChatMenu.tsx b/src/renderer/components/chat/ChatMenu.tsx
index bb8b214e5..b9f2ce469 100644
--- a/src/renderer/components/chat/ChatMenu.tsx
+++ b/src/renderer/components/chat/ChatMenu.tsx
@@ -46,6 +46,7 @@ interface ChatMenuProps {
handleCopy: () => void;
saveAsHtml: () => void;
saveAsMarkdown: () => void;
+ appendSelectionToPrompt: () => void;
}
function ChatMenu(props: ChatMenuProps) {
@@ -58,6 +59,7 @@ function ChatMenu(props: ChatMenuProps) {
handleCopy,
saveAsHtml,
saveAsMarkdown,
+ appendSelectionToPrompt,
} = props;
const { deleteHistory } = useChatContext();
@@ -113,6 +115,12 @@ 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 8eb614ba8..359a1076b 100644
--- a/src/renderer/components/chat/ChatView.tsx
+++ b/src/renderer/components/chat/ChatView.tsx
@@ -36,7 +36,6 @@ 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,
@@ -71,7 +70,6 @@ function ChatView() {
cancelMessage,
} = useChatContext();
const { showNotification } = useNotificationContext();
- const { openedEntry } = useOpenedEntryContext();
const aiDefaultProvider: AIProvider = useSelector(getDefaultAIProvider);
const isLoading = useRef<boolean>(false);
const currentMode = useRef<ChatMode>(undefined);
@@ -213,6 +211,27 @@ function ChatView() {
}
}, []);
+ const getSelectedIFrameContent = () => {
+ const iframe = document.getElementsByTagName('iframe')[0];
+ const iframeWindow = iframe.contentWindow;
+ const selection = iframeWindow.getSelection();
+ const selectionText = selection.toString();
+ if (selectionText) {
+ return selectionText;
+ } else {
+ const childIframe =
+ iframeWindow.document.getElementsByTagName('iframe')[0];
+ const subselection = childIframe.contentWindow.getSelection();
+ return subselection.toString();
+ }
+ };
+
+ const appendSelectionToPrompt = () => {
+ const selectedText = getSelectedIFrameContent();
+ chatMsg.current = chatMsg.current + selectedText;
+ forceUpdate();
+ };
+
const { FILE } = NativeTypes;
return (
@@ -258,6 +277,7 @@ function ChatView() {
handleCopy={handleCopy}
saveAsHtml={saveAsHtml}
saveAsMarkdown={saveAsMarkdown}
+ appendSelectionToPrompt={appendSelectionToPrompt}
/>
</Grid>
</Grid>
@@ -280,7 +300,7 @@ function ChatView() {
<ChatMdEditor
showCurrent={isLoading.current}
ref={editorRef}
- currentFolder={openedEntry.path}
+ placeholder="Test"
/>
</MilkdownProvider>
</Grid>
diff --git a/src/renderer/components/md/ChatMdEditor.tsx b/src/renderer/components/md/ChatMdEditor.tsx
index 90370e579..2f5d4249c 100644
--- a/src/renderer/components/md/ChatMdEditor.tsx
+++ b/src/renderer/components/md/ChatMdEditor.tsx
@@ -30,15 +30,14 @@ import React, { useEffect, useRef } from 'react';
import { useSelector } from 'react-redux';
interface ChatMdEditorProps {
- currentFolder?: string;
placeholder?: string;
showCurrent?: boolean;
}
const ChatMdEditor = React.forwardRef<CrepeRef, ChatMdEditorProps>(
(props, ref) => {
- const { currentFolder, placeholder, showCurrent } = props;
- const { openLink } = useOpenedEntryContext();
+ const { placeholder, showCurrent } = props;
+ const { openedEntry, openLink } = useOpenedEntryContext();
const { chatHistoryItems } = useChatContext();
const crepeInstanceRef = useRef<Crepe | null>(null);
const author = useSelector(getAuthor);
@@ -70,7 +69,7 @@ const ChatMdEditor = React.forwardRef<CrepeRef, ChatMdEditorProps>(
[Crepe.Feature.Cursor]: false,
},
placeholder,
- currentFolder,
+ openedEntry?.path,
openLink,
);
crepe.editor.onStatusChange((status: EditorStatus) => {
@@ -80,7 +79,7 @@ const ChatMdEditor = React.forwardRef<CrepeRef, ChatMdEditorProps>(
});
return crepe;
},
- [currentFolder, chatHistoryItems],
+ [openedEntry?.path, chatHistoryItems],
);
useCrepeHandler(ref, () => crepeInstanceRef.current, get, loading);