Skip to content

Commit 7ebeea3

Browse files
committed
Attachments: Excel: paste as Table/HTML/Text rather than image. Fixes #286
1 parent a7a234e commit 7ebeea3

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/apps/chat/components/composer/attachments/useAttachments.tsx

+16-9
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ export const useAttachments = (enableLoadURLs: boolean) => {
3939

4040

4141
const attachAppendDataTransfer = React.useCallback((dt: DataTransfer, method: AttachmentSourceOriginDTO, attachText: boolean): 'as_files' | 'as_url' | 'as_text' | false => {
42+
43+
// https://github.com/enricoros/big-AGI/issues/286
44+
const textHtml = dt.getData('text/html') || '';
45+
const heuristicsIsExcel = textHtml.includes('"urn:schemas-microsoft-com:office:excel"');
46+
4247
if (ATTACHMENTS_DEBUG)
43-
console.log('attachAppendDataTransfer', dt.types, dt.files);
48+
console.log('attachAppendDataTransfer', dt.types, dt.items, dt.files, textHtml);
4449

4550
// attach File(s)
46-
if (dt.files.length >= 1) {
51+
if (dt.files.length >= 1 && !heuristicsIsExcel /* special case: ignore images from Microsoft Office pastes (prioritize the HTML paste) */) {
4752
// rename files from a common prefix, to better relate them (if the transfer contains a list of paths)
4853
let overrideFileNames: string[] = [];
4954
if (dt.types.includes('text/plain')) {
@@ -77,7 +82,6 @@ export const useAttachments = (enableLoadURLs: boolean) => {
7782
}
7883

7984
// attach as Text/Html (further conversion, e.g. to markdown is done later)
80-
const textHtml = dt.getData('text/html') || '';
8185
if (attachText && (textHtml || textPlain)) {
8286
void createAttachment({
8387
media: 'text', method, textPlain, textHtml,
@@ -109,16 +113,20 @@ export const useAttachments = (enableLoadURLs: boolean) => {
109113
return;
110114
}
111115

112-
if (ATTACHMENTS_DEBUG)
113-
console.log('attachAppendClipboardItems', clipboardItems);
114-
115-
// loop on all the possible attachments
116+
// loop on all the clipboard items
116117
for (const clipboardItem of clipboardItems) {
117118

119+
// https://github.com/enricoros/big-AGI/issues/286
120+
const textHtml = clipboardItem.types.includes('text/html') ? await clipboardItem.getType('text/html').then(blob => blob.text()) : '';
121+
const heuristicsIsExcel = textHtml.startsWith('<table ');
122+
123+
if (ATTACHMENTS_DEBUG)
124+
console.log(' - attachAppendClipboardItems.item:', clipboardItem, textHtml, heuristicsIsExcel);
125+
118126
// attach as image
119127
let imageAttached = false;
120128
for (const mimeType of clipboardItem.types) {
121-
if (mimeType.startsWith('image/')) {
129+
if (mimeType.startsWith('image/') && !heuristicsIsExcel) {
122130
try {
123131
const imageBlob = await clipboardItem.getType(mimeType);
124132
const imageName = mimeType.replace('image/', 'clipboard.').replaceAll('/', '.') || 'clipboard.png';
@@ -148,7 +156,6 @@ export const useAttachments = (enableLoadURLs: boolean) => {
148156
}
149157

150158
// attach as Text
151-
const textHtml = clipboardItem.types.includes('text/html') ? await clipboardItem.getType('text/html').then(blob => blob.text()) : '';
152159
if (textHtml || textPlain) {
153160
void createAttachment({
154161
media: 'text', method: 'clipboard-read', textPlain, textHtml,

0 commit comments

Comments
 (0)