Skip to content

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

chore: improved chat export functionality and design

Ilian Sapundshiev4264a34f6dea
diff --git a/src/renderer/components/CommonIcons.tsx b/src/renderer/components/CommonIcons.tsx
index 98dc2cf18..35540577d 100644
--- a/src/renderer/components/CommonIcons.tsx
+++ b/src/renderer/components/CommonIcons.tsx
@@ -317,6 +317,8 @@ export { default as PinGroupingOnIcon } from '@mui/icons-material/ScatterPlotTwo
export { default as CopyToClipboardIcon } from '@mui/icons-material/ContentCopy';
+export { default as SelectAllIcon } from '@mui/icons-material/SelectAll';
+
export const MastodonIcon = (props) => (
<SvgIcon>
<svg
diff --git a/src/renderer/components/EntryProperties.tsx b/src/renderer/components/EntryProperties.tsx
index 7e5c1c6e9..2b8bc1b97 100644
--- a/src/renderer/components/EntryProperties.tsx
+++ b/src/renderer/components/EntryProperties.tsx
@@ -869,12 +869,12 @@ function EntryProperties({ tileServer }: Props) {
!openedEntry.isFile,
location?.getDirSeparator(),
);
- const clibboardItem = generateClipboardLink(
+ const clipboardItem = generateClipboardLink(
sharingLink,
entryTitle,
);
const promise =
- navigator.clipboard.write(clibboardItem);
+ navigator.clipboard.write(clipboardItem);
showNotification(t('core:linkCopied'));
}}
>
diff --git a/src/renderer/components/chat/ChatMenu.tsx b/src/renderer/components/chat/ChatMenu.tsx
index 42e1a8826..bb8b214e5 100644
--- a/src/renderer/components/chat/ChatMenu.tsx
+++ b/src/renderer/components/chat/ChatMenu.tsx
@@ -16,16 +16,20 @@
*
*/
-import { AIIcon, DeleteIcon } from '-/components/CommonIcons';
+import AppConfig from '-/AppConfig';
+import {
+ AIIcon,
+ CopyToClipboardIcon,
+ DeleteIcon,
+ HTMLFileIcon,
+ MarkdownFileIcon,
+ SelectAllIcon,
+} from '-/components/CommonIcons';
import { useSettingsDialogContext } from '-/components/dialogs/hooks/useSettingsDialogContext';
import { SettingsTab } from '-/components/dialogs/SettingsDialog';
import TsMenuList from '-/components/TsMenuList';
import { useChatContext } from '-/hooks/useChatContext';
import { useNotificationContext } from '-/hooks/useNotificationContext';
-import CopyIcon from '@mui/icons-material/FileCopy';
-import HtmlIcon from '@mui/icons-material/Html';
-import RttIcon from '@mui/icons-material/Rtt';
-import SelectAllIcon from '@mui/icons-material/SelectAll';
import {
Divider,
ListItemIcon,
@@ -95,12 +99,14 @@ function ChatMenu(props: ChatMenuProps) {
</ListItemIcon>
<ListItemText primary={t('core:selectAll')} />
</MenuItem>
- <MenuItem onClick={handleCopy}>
- <ListItemIcon>
- <CopyIcon />
- </ListItemIcon>
- <ListItemText primary={t('core:copy')} />
- </MenuItem>
+ {!AppConfig.isElectron && (
+ <MenuItem onClick={handleCopy}>
+ <ListItemIcon>
+ <CopyToClipboardIcon />
+ </ListItemIcon>
+ <ListItemText primary={t('core:copy')} />
+ </MenuItem>
+ )}
<MenuItem onClick={clearHistory}>
<ListItemIcon>
<DeleteIcon />
@@ -110,13 +116,13 @@ function ChatMenu(props: ChatMenuProps) {
<Divider />
<MenuItem onClick={saveAsHtml}>
<ListItemIcon>
- <HtmlIcon />
+ <HTMLFileIcon />
</ListItemIcon>
<ListItemText primary={t('core:saveAsHtml')} />
</MenuItem>
<MenuItem onClick={saveAsMarkdown}>
<ListItemIcon>
- <RttIcon />
+ <MarkdownFileIcon />
</ListItemIcon>
<ListItemText primary={t('core:saveAsMd')} />
</MenuItem>
diff --git a/src/renderer/components/chat/ChatView.tsx b/src/renderer/components/chat/ChatView.tsx
index 64310f85f..3fb5ca8ee 100644
--- a/src/renderer/components/chat/ChatView.tsx
+++ b/src/renderer/components/chat/ChatView.tsx
@@ -31,7 +31,7 @@ import { useChatContext } from '-/hooks/useChatContext';
import { useNotificationContext } from '-/hooks/useNotificationContext';
import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
import { getDefaultAIProvider } from '-/reducers/settings';
-import { saveAsTextFile } from '-/services/utils-io';
+import { getMimeType, saveAsTextFile } from '-/services/utils-io';
import { MilkdownProvider } from '@milkdown/react';
import CancelIcon from '@mui/icons-material/Cancel';
import SendIcon from '@mui/icons-material/Send';
@@ -150,30 +150,32 @@ function ChatView() {
const handleCopy = useCallback(() => {
setAnchorEl(null);
if (milkdownDivRef.current) {
- const textContent =
- // @ts-ignore
- milkdownDivRef.current.querySelector('.ProseMirror').innerText;
- const htmlContent =
- milkdownDivRef.current.querySelector('.ProseMirror').innerHTML;
- const textToCopy = textContent || milkdownDivRef.current.innerText;
- const htmlToCopy = htmlContent || milkdownDivRef.current.innerHTML;
- const htmlType = 'text/html';
- const plainTextType = 'text/plain';
- const cbi = [
- new ClipboardItem({
- [htmlType]: new Blob([htmlToCopy], {
- type: htmlType,
- }),
- [plainTextType]: new Blob([textToCopy], { type: plainTextType }),
- }),
- ];
+ const selection = window.getSelection();
+ if (!selection || selection.rangeCount === 0) {
+ console.warn('No text selected');
+ return;
+ }
+
+ // Extract plain text
+ const text = selection.toString();
+
+ // Extract HTML
+ const range = selection.getRangeAt(0);
+ const container = document.createElement('div');
+ container.appendChild(range.cloneContents());
+ const html = container.innerHTML;
+
+ // Create ClipboardItem with both types
+ const clipboardItem = new ClipboardItem({
+ [getMimeType('txt')]: new Blob([text], { type: getMimeType('txt') }),
+ [getMimeType('html')]: new Blob([html], { type: getMimeType('html') }),
+ });
+
navigator.clipboard
- .write(cbi)
- .then(() => {
- showNotification(t('core:copyToClipboard'));
- })
+ .write([clipboardItem])
+ .then(() => showNotification(t('core:copyToClipboard')))
.catch((err) => {
- console.error('Failed to copy text:', err);
+ console.log('Error copy to clipboard: ' + err);
showNotification(t('core:generatedHTMLFailed'));
});
}
@@ -184,13 +186,23 @@ function ChatView() {
const milkdownContent =
milkdownDivRef.current.querySelector('.ProseMirror').innerHTML;
const html = milkdownContent || milkdownDivRef.current.innerHTML;
+ // tmp solution for hiding unneeded markup
+ const milkdownCleanerCSS = `
+ <style>
+ .label-wrapper, .tools, .cm-gutter, .cm-gap,
+ .milkdown-table-block, .handle { display: none }
+ </style>
+ `;
const blob = new Blob(
[
- '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>',
- html,
- '</body></html>',
+ `<!DOCTYPE html><html>
+ <head><meta charset="UTF-8">${milkdownCleanerCSS}</head>
+ <body>${html}</body>
+ </html>`,
],
- { type: 'text/html' },
+ {
+ type: getMimeType('html'),
+ },
);
const dateTimeTag = formatDateTime4Tag(new Date(), true);
const filename = `tagspaces-chat [export ${dateTimeTag}].html`;
@@ -201,7 +213,7 @@ function ChatView() {
setAnchorEl(null);
if (editorRef.current) {
const md = editorRef.current.getMarkdown();
- const blob = new Blob([md], { type: 'text/markdown' });
+ const blob = new Blob([md], { type: getMimeType('md') });
const dateTimeTag = formatDateTime4Tag(new Date(), true);
const filename = `tagspaces-chat [export ${dateTimeTag}].md`;
saveAsTextFile(blob, filename);
diff --git a/src/renderer/services/utils-io.ts b/src/renderer/services/utils-io.ts
index 92cbe9532..2e5718da6 100644
--- a/src/renderer/services/utils-io.ts
+++ b/src/renderer/services/utils-io.ts
@@ -1222,6 +1222,7 @@ export function removePrefix(str, prefix) {
export function getMimeType(extension: string): string | undefined {
const mimeTypes: { [key: string]: string } = {
txt: 'text/plain',
+ md: 'text/markdown',
html: 'text/html',
htm: 'text/html',
css: 'text/css',
diff --git a/src/renderer/utils/dom.ts b/src/renderer/utils/dom.ts
index 06d596737..aaca2759d 100644
--- a/src/renderer/utils/dom.ts
+++ b/src/renderer/utils/dom.ts
@@ -1,4 +1,4 @@
-import { getRelativeEntryPath } from '-/services/utils-io';
+import { getMimeType, getRelativeEntryPath } from '-/services/utils-io';
export function isVisibleOnScreen(element: any) {
const rectangle = element.getBoundingClientRect();
@@ -160,15 +160,13 @@ export function base64ToBlob(base64) {
}
export function generateClipboardLink(url, name?) {
- const htmlType = 'text/html';
- const plainTextType = 'text/plain';
const htmlLink = `<a href="${url}">${name ? name : url}</a>`;
const cbi = [
new ClipboardItem({
- [htmlType]: new Blob([htmlLink], {
- type: htmlType,
+ [getMimeType('html')]: new Blob([htmlLink], {
+ type: getMimeType('html'),
}),
- [plainTextType]: new Blob([url], { type: plainTextType }),
+ [getMimeType('txt')]: new Blob([url], { type: getMimeType('txt') }),
}),
];
return cbi;