Skip to content

Commit

Permalink
feat: paste image if no excel html (#4558)
Browse files Browse the repository at this point in the history
  • Loading branch information
siam-ese authored Jan 23, 2025
1 parent bfd9ba4 commit adf4c39
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
18 changes: 10 additions & 8 deletions packages/sheets-ui/src/services/clipboard/clipboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import { UniverPastePlugin } from './html-to-usm/paste-plugins/plugin-univer';
import { WordPastePlugin } from './html-to-usm/paste-plugins/plugin-word';
import { COPY_TYPE } from './type';
import { USMToHtmlService } from './usm-to-html/convertor';
import { clipboardItemIsFromExcel, convertTextToTable, discreteRangeContainsRange, mergeSetRangeValues, rangeIntersectWithDiscreteRange } from './utils';
import { convertTextToTable, discreteRangeContainsRange, htmlIsFromExcel, mergeSetRangeValues, rangeIntersectWithDiscreteRange } from './utils';

export const PREDEFINED_HOOK_NAME = {
DEFAULT_COPY: 'default-copy',
Expand Down Expand Up @@ -259,9 +259,11 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
? await item.getType(HTML_CLIPBOARD_MIME_TYPE).then((blob) => blob && blob.text())
: '';

const imageIndex = types.findIndex((type) => imageMimeTypeSet.has(type));
const isFromExcel = htmlIsFromExcel(html);

if (imageIndex !== -1) {
// clipboard item from excel may contain image, so we need to check if the clipboard item is from excel
const imageIndex = types.findIndex((type) => imageMimeTypeSet.has(type));
if (imageIndex !== -1 && !isFromExcel) {
const imageMimeType = types[imageIndex]!;
const imageBlob = await item.getType(imageMimeType);

Expand All @@ -277,7 +279,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard

if (html) {
// Firstly see if the html content is from Excel
if (this._platformService.isWindows && (await clipboardItemIsFromExcel(html))) {
if (this._platformService.isWindows && isFromExcel) {
this._notificationService.show({
type: 'warning',
title: this._localeService.t('clipboard.shortCutNotify.title'),
Expand All @@ -299,8 +301,10 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
return false;
}

legacyPaste(html?: string, text?: string, files?: File[]): Promise<boolean> {
if (files) {
async legacyPaste(html?: string, text?: string, files?: File[]): Promise<boolean> {
const isFromExcel = htmlIsFromExcel(html ?? '');

if (files && !isFromExcel) {
return this._pasteFiles(files, PREDEFINED_HOOK_NAME.DEFAULT_PASTE);
} else if (html) {
return this._pasteHTML(html, PREDEFINED_HOOK_NAME.DEFAULT_PASTE);
Expand All @@ -314,8 +318,6 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
} else {
return this._pasteUnrecognized();
}

// return Promise.resolve(false);
}

rePasteWithPasteType(type: IPasteHookKeyType): boolean {
Expand Down
28 changes: 21 additions & 7 deletions packages/sheets-ui/src/services/clipboard/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

import { ObjectMatrix } from '@univerjs/core';
import { SetRangeValuesMutation } from '@univerjs/sheets';
import type { ICellData, IMutationInfo, IObjectMatrixPrimitiveType, IRange, Nullable } from '@univerjs/core';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import type { IDiscreteRange } from '../../controllers/utils/range-tools';
import { ObjectMatrix } from '@univerjs/core';
import { SetRangeValuesMutation } from '@univerjs/sheets';

/**
*
Expand Down Expand Up @@ -95,13 +95,27 @@ export const getRepeatRange = (sourceRange: IRange, targetRange: IRange, isStric
return repeatList;
};

export async function clipboardItemIsFromExcel(html: string): Promise<boolean> {
if (html) {
const regex = /<td[^>]*class=".*?xl.*?"[^>]*>.*?<\/td>/;
return regex.test(html);
export function htmlIsFromExcel(html: string): boolean {
if (!html) {
return false;
}

return false;
const excelMarkers = [
// Excel class names
/<td[^>]*class=".*?xl.*?"[^>]*>/i,
// Excel namespace
/xmlns:x="urn:schemas-microsoft-com:office:excel"/i,
// Excel ProgID
/ProgId="Excel.Sheet"/i,
// Office specific namespace
/xmlns:o="urn:schemas-microsoft-com:office:office"/i,
// Excel specific style markers
/@mso-|mso-excel/i,
// Excel workbook metadata
/<x:ExcelWorkbook>/i,
];

return excelMarkers.some((marker) => marker.test(html));
}

export function mergeCellValues(...cellValues: IObjectMatrixPrimitiveType<Nullable<ICellData>>[]) {
Expand Down

0 comments on commit adf4c39

Please sign in to comment.