Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(base-component): 1. drap and drop 2.import xlsx plugin support drag xlsx 3. demo support import xlsx,clipboard

Dushusir8b22b3b715b6
diff --git a/examples/univer-sheet-ts/package.json b/examples/univer-sheet-ts/package.json
index 9962a49bfb..91bee10e92 100644
--- a/examples/univer-sheet-ts/package.json
+++ b/examples/univer-sheet-ts/package.json
@@ -54,6 +54,7 @@
"@univer/core": "workspace:^0.0.1",
"@univer/sheets-plugin-clipboard": "workspace:^0.0.1",
"@univer/sheets-plugin-formula": "workspace:^0.0.1",
+ "@univer/sheets-plugin-import-xlsx": "workspace:^0.0.1",
"@univer/sheets-plugin-numfmt": "workspace:^0.0.1",
"@univer/style-universheet": "workspace:^0.0.1",
"preact": "^10.5.14"
diff --git a/examples/univer-sheet-ts/src/main.tsx b/examples/univer-sheet-ts/src/main.tsx
index 7ef555ec0e..74ee397c9d 100644
--- a/examples/univer-sheet-ts/src/main.tsx
+++ b/examples/univer-sheet-ts/src/main.tsx
@@ -17,6 +17,7 @@ import {
} from '@univer/common-plugin-data';
import { ClipboardPlugin } from '@univer/sheets-plugin-clipboard';
import { BaseComponentPlugin } from '@univer/base-component';
+import { ImportXlsxPlugin } from '@univer/sheets-plugin-import-xlsx';
const uiDefaultConfigDemo = {
container: 'universheet',
@@ -42,4 +43,8 @@ const univerSheetDemo = UniverSheet.newInstance(DEFAULT_WORKBOOK_DATA_DEMO);
univerSheetDemo.installPlugin(new RenderEngine());
univerSheetDemo.installPlugin(new UniverComponentSheet());
univerSheetDemo.installPlugin(new SheetPlugin(uiDefaultConfigDemo));
+univerSheetDemo.installPlugin(new BaseComponentPlugin());
+univerSheetDemo.installPlugin(new NumfmtPlugin());
FormulaPlugin.create(DEFAULT_FORMULA_DATA_DEMO).installTo(univerSheetDemo);
+univerSheetDemo.installPlugin(new ClipboardPlugin());
+univerSheetDemo.installPlugin(new ImportXlsxPlugin());
diff --git a/examples/univer-vanilla-ts/package.json b/examples/univer-vanilla-ts/package.json
index 8f84e60ad9..dac402f291 100644
--- a/examples/univer-vanilla-ts/package.json
+++ b/examples/univer-vanilla-ts/package.json
@@ -53,6 +53,7 @@
"@univer/core": "workspace:^0.0.1",
"@univer/sheets-plugin-clipboard": "workspace:^0.0.1",
"@univer/sheets-plugin-formula": "workspace:^0.0.1",
+ "@univer/sheets-plugin-import-xlsx": "workspace:^0.0.1",
"@univer/sheets-plugin-numfmt": "workspace:^0.0.1",
"@univer/style-universheet": "workspace:^0.0.1",
"preact": "^10.5.14"
diff --git a/examples/univer-vanilla-ts/src/main.tsx b/examples/univer-vanilla-ts/src/main.tsx
index 04f5a84cec..1802c9fa46 100644
--- a/examples/univer-vanilla-ts/src/main.tsx
+++ b/examples/univer-vanilla-ts/src/main.tsx
@@ -15,6 +15,7 @@ import {
} from '@univer/common-plugin-data';
import { ClipboardPlugin } from '@univer/sheets-plugin-clipboard';
import { BaseComponentPlugin } from '@univer/base-component';
+import { ImportXlsxPlugin } from '@univer/sheets-plugin-import-xlsx';
const uiDefaultConfigUp = {
container: 'universheet-up',
@@ -59,6 +60,7 @@ univerSheetUp.installPlugin(new NumfmtPlugin());
FormulaPlugin.create(DEFAULT_FORMULA_DATA).installTo(univerSheetUp);
univerSheetUp.installPlugin(new ClipboardPlugin());
+univerSheetUp.installPlugin(new ImportXlsxPlugin());
const univerSheetDown = UniverSheet.newInstance();
univerSheetDown.installPlugin(new RenderEngine());
diff --git a/packages/base-component/src/Basics/Interfaces/IDragAndDropData.ts b/packages/base-component/src/Basics/Interfaces/IDragAndDropData.ts
index f680e4ebd2..029322e2be 100644
--- a/packages/base-component/src/Basics/Interfaces/IDragAndDropData.ts
+++ b/packages/base-component/src/Basics/Interfaces/IDragAndDropData.ts
@@ -1,4 +1,16 @@
export interface IDragAndDropData {
- plain?: string;
- html?: string;
+ /**
+ * drag data item kind, one of: "string", "file"
+ */
+ kind: string;
+
+ /**
+ * MIME type, "text/plain", "image/jpeg"
+ */
+ type: string;
+
+ /**
+ * File object
+ */
+ file: File;
}
diff --git a/packages/base-component/src/Basics/Register/Clipboard/ClipboardExtensionManager.ts b/packages/base-component/src/Basics/Register/Clipboard/ClipboardExtensionManager.ts
index 90b856909a..02300e8409 100644
--- a/packages/base-component/src/Basics/Register/Clipboard/ClipboardExtensionManager.ts
+++ b/packages/base-component/src/Basics/Register/Clipboard/ClipboardExtensionManager.ts
@@ -54,9 +54,9 @@ export class ClipboardExtensionManager {
return extension;
}
- pasteResolver(e?: ClipboardEvent) {
+ pasteResolver(evt?: ClipboardEvent) {
return new Promise((resolve: (value: IClipboardData) => void, reject) => {
- Clipboard.read(e).then((file: Array<PasteType | null> | null) => {
+ Clipboard.read(evt).then((file: Array<PasteType | null> | null) => {
if (!file) return [];
const HtmlIndex = file.findIndex((item: PasteType | null, index: number) => item && item.type === 'text/html');
const PlainIndex = file.findIndex((item: PasteType | null, index: number) => item && item.type === 'text/plain');
diff --git a/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionFactory.ts b/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionFactory.ts
index 1194f63814..2c28997a0d 100644
--- a/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionFactory.ts
+++ b/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionFactory.ts
@@ -6,7 +6,7 @@ import { IDragAndDropData } from '../../Interfaces';
*/
export class BaseDragAndDropExtension<T extends Plugin = Plugin> {
// protected _json: IKeyValue;
- constructor(protected _data: IDragAndDropData, protected _plugin: T) {}
+ constructor(protected _data: File[], protected _plugin: T) {}
/**
* Execute the core logic after the check is successful
@@ -29,7 +29,7 @@ export class BaseDragAndDropExtensionFactory<T extends Plugin = Plugin> {
* @param data
* @returns
*/
- create(data: IDragAndDropData): BaseDragAndDropExtension {
+ create(data: File[]): BaseDragAndDropExtension {
return new BaseDragAndDropExtension(data, this._plugin);
}
@@ -38,7 +38,7 @@ export class BaseDragAndDropExtensionFactory<T extends Plugin = Plugin> {
* @param data
* @returns
*/
- check(data: IDragAndDropData): false | BaseDragAndDropExtension {
+ check(data: IDragAndDropData[]): false | BaseDragAndDropExtension {
return false;
}
}
diff --git a/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionManager.ts b/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionManager.ts
index a24444a2be..7416f15333 100644
--- a/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionManager.ts
+++ b/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionManager.ts
@@ -1,5 +1,5 @@
import { IDragAndDropData } from '../../Interfaces';
-import { BaseDragAndDropExtension, BaseDragAndDropExtensionFactory } from './DragAndDropExtensionFactory';
+import { BaseDragAndDropExtensionFactory } from './DragAndDropExtensionFactory';
import { DragAndDropExtensionRegister } from './DragAndDropExtensionRegister';
export class DragAndDropExtensionManager {
@@ -20,17 +20,14 @@ export class DragAndDropExtensionManager {
* inject all actions
* @param command
*/
- handle(data: IDragAndDropData) {
+ handle(data: IDragAndDropData[]) {
const DragAndDropExtensionFactoryList = this._register?.DragAndDropExtensionFactoryList;
if (!DragAndDropExtensionFactoryList) return;
// get the sorted list
// get the dynamically added list
this._DragAndDropExtensionFactoryList = DragAndDropExtensionFactoryList;
- const extension = this._checkExtension(data);
- if (extension) {
- extension.execute();
- }
+ this._checkExtension(data);
}
/**
@@ -38,17 +35,52 @@ export class DragAndDropExtensionManager {
* @param command
* @returns
*/
- private _checkExtension(data: IDragAndDropData) {
+ private _checkExtension(data: IDragAndDropData[]) {
if (!this._DragAndDropExtensionFactoryList) return false;
-
- let extension: BaseDragAndDropExtension | false = false;
- for (let index = 0; index < this._DragAndDropExtensionFactoryList.length; index++) {
- const extensionFactory = this._DragAndDropExtensionFactoryList[index];
- extension = extensionFactory.check(data);
+ this._DragAndDropExtensionFactoryList.forEach((extensionFactory) => {
+ const extension = extensionFactory.check(data);
if (extension !== false) {
- break;
+ extension.execute();
+ }
+ });
+ }
+
+ dragResolver(evt: DragEvent) {
+ return new Promise((resolve: (data: IDragAndDropData[]) => void, reject) => {
+ if (!evt.dataTransfer) return;
+
+ const dataList: IDragAndDropData[] = [];
+ if (evt.dataTransfer.items) {
+ // Use DataTransferItemList interface to access the file(s)
+
+ [...evt.dataTransfer.items].forEach((item, i) => {
+ // If dropped items aren't files, reject them
+ if (item.kind === 'file') {
+ const file = item.getAsFile();
+
+ if (file) {
+ dataList.push({
+ kind: item.kind,
+ type: item.type,
+ file,
+ });
+ }
+ }
+ });
+ } else {
+ // Use DataTransfer interface to access the file(s)
+ [...evt.dataTransfer.files].forEach((file, i) => {
+ if (file) {
+ dataList.push({
+ kind: 'file',
+ type: file.type,
+ file,
+ });
+ }
+ });
}
- }
- return extension;
+
+ resolve(dataList);
+ });
}
}
diff --git a/packages/base-component/src/Common/RegisterManager.ts b/packages/base-component/src/Common/RegisterManager.ts
index 3c35900548..957c4f0c7d 100644
--- a/packages/base-component/src/Common/RegisterManager.ts
+++ b/packages/base-component/src/Common/RegisterManager.ts
@@ -1,5 +1,5 @@
import { Plugin } from '@univer/core';
-import { IClipboardData } from '../Basics/Interfaces';
+import { IClipboardData, IDragAndDropData } from '../Basics/Interfaces';
import { ClipboardExtensionManager, DragAndDropExtensionManager } from '../Basics/Register';
export class RegisterManager {
@@ -36,7 +36,10 @@ export class RegisterManager {
if (onDropObservable && !onDropObservable.hasObservers()) {
onDropObservable.add((evt: DragEvent) => {
- console.log('onDropObservable event======', evt);
+ this._dragAndDropExtensionManager.dragResolver(evt).then((dataList: IDragAndDropData[]) => {
+ if (dataList.length === 0) return;
+ this._dragAndDropExtensionManager.handle(dataList);
+ });
});
}
}
diff --git a/packages/base-component/tsconfig.json b/packages/base-component/tsconfig.json
index fc81cbd47e..fd9e4edd9b 100644
--- a/packages/base-component/tsconfig.json
+++ b/packages/base-component/tsconfig.json
@@ -4,7 +4,7 @@
"outDir": "lib",
"target": "ESNext",
- "lib": ["ESNext", "DOM", "WebWorker"],
+ "lib": ["ESNext", "DOM", "WebWorker", "dom.iterable"],
"module": "ESNext",
"moduleResolution": "Node", //This value is needed for Node.js.
diff --git a/packages/core/src/Sheets/Domain/Worksheet.ts b/packages/core/src/Sheets/Domain/Worksheet.ts
index 659ed34f65..51dfd3f149 100644
--- a/packages/core/src/Sheets/Domain/Worksheet.ts
+++ b/packages/core/src/Sheets/Domain/Worksheet.ts
@@ -501,8 +501,6 @@ export class Worksheet {
* @returns WorkSheet Configures
*/
getConfig(): IWorksheetConfig {
- // update merge data
- this._config.mergeData = this._merges.getMergeData();
return this._config;
}
diff --git a/packages/sheets-plugin-import-xlsx/src/Basic/Register/DragAndDropExtension.ts b/packages/sheets-plugin-import-xlsx/src/Basic/Register/DragAndDropExtension.ts
new file mode 100644
index 0000000000..93a24fd128
--- /dev/null
+++ b/packages/sheets-plugin-import-xlsx/src/Basic/Register/DragAndDropExtension.ts
@@ -0,0 +1,36 @@
+import { BaseDragAndDropExtension, BaseDragAndDropExtensionFactory, handelTableToJson, handlePlainToJson, IDragAndDropData } from '@univer/base-component';
+import { ImportXlsxPlugin } from '../../ImportXlsxPlugin';
+
+export class DragAndDropExtension extends BaseDragAndDropExtension<ImportXlsxPlugin> {
+ execute() {
+ console.log('this._data----', this._data);
+
+ this._plugin.getImportXlsxController().handleFiles(this._data);
+ }
+}
+
+export class DragAndDropExtensionFactory extends BaseDragAndDropExtensionFactory<ImportXlsxPlugin> {
+ get zIndex(): number {
+ return 1;
+ }
+
+ create(data: File[]): BaseDragAndDropExtension {
+ return new DragAndDropExtension(data, this._plugin);
+ }
+
+ check(data: IDragAndDropData[]): false | BaseDragAndDropExtension {
+ const matchData: File[] = [];
+
+ data.forEach((item) => {
+ if (item.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
+ matchData.push(item.file);
+ }
+ });
+
+ if (matchData.length > 0) {
+ return this.create(matchData);
+ }
+
+ return false;
+ }
+}
diff --git a/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.ts b/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.ts
index ed2766dc49..4868f8464d 100644
--- a/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.ts
+++ b/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.ts
@@ -1,4 +1,4 @@
-import { migrate, PLUGIN_NAMES, Tools } from '@univer/core';
+import { IKeyValue, migrate, PLUGIN_NAMES, Tools } from '@univer/core';
import { BaseComponentRender } from '@univer/base-component';
import { IToolBarItemProps, SelectTypes, SheetPlugin } from '@univer/base-sheets';
@@ -44,57 +44,60 @@ export class ImportXlsxController {
input.accept = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
input.addEventListener('change', (evt) => {
- const files = evt.target.files;
- if (files == null || files.length == 0) {
- alert('No files wait for import');
- return;
- }
+ const element = evt.currentTarget as HTMLInputElement;
+ this.handleFiles(element.files);
+ });
- let name = files[0].name;
- let suffixArr = name.split('.');
- let suffix = suffixArr[suffixArr.length - 1];
- if (suffix != 'xlsx') {
- alert('Currently only supports the import of xlsx files');
+ input.click();
+ }
+ handleFiles(files: File[] | FileList | null) {
+ if (files == null || files.length == 0) {
+ alert('No files wait for import');
+ return;
+ }
+
+ let name = files[0].name;
+ let suffixArr = name.split('.');
+ let suffix = suffixArr[suffixArr.length - 1];
+ if (suffix != 'xlsx') {
+ alert('Currently only supports the import of xlsx files');
+ return;
+ }
+ const transformExcelToLucky = LuckyExcel.default.transformExcelToLucky;
+
+ transformExcelToLucky(files[0], (exportJson: IKeyValue) => {
+ if (exportJson.sheets == null || exportJson.sheets.length == 0) {
+ alert('Failed to read the content of the excel file, currently does not support xls files!');
return;
}
- const transformExcelToLucky = LuckyExcel.default.transformExcelToLucky;
-
- transformExcelToLucky(files[0], (exportJson, luckysheetfile) => {
- if (exportJson.sheets == null || exportJson.sheets.length == 0) {
- alert('Failed to read the content of the excel file, currently does not support xls files!');
- return;
- }
- const luckysheetConfig = {
- container: 'universheet',
- data: exportJson.sheets,
- title: exportJson.info.name,
- };
- const univerWorkbookConfig = migrate(luckysheetConfig);
+ const luckysheetConfig = {
+ container: 'universheet',
+ data: exportJson.sheets,
+ title: exportJson.info.name,
+ };
+ const univerWorkbookConfig = migrate(luckysheetConfig);
- const sheets = univerWorkbookConfig.sheets;
+ const sheets = univerWorkbookConfig.sheets;
- if (!sheets) {
- return alert('No content');
- }
-
- const workbook = this._plugin.getContext().getWorkBook();
+ if (!sheets) {
+ return alert('No content');
+ }
- const order = Tools.deepClone(workbook.getConfig().sheetOrder);
+ const workbook = this._plugin.getContext().getWorkBook();
- // add new sheets
- Object.keys(sheets).forEach((sheetId) => {
- const sheetData = sheets[sheetId];
- workbook.insertSheet(sheetData);
- });
+ const order = Tools.deepClone(workbook.getConfig().sheetOrder);
- // remove other sheets
- // order.forEach((sheetId: string) => {
- // workbook.removeSheetBySheetId(sheetId);
- // });
+ // add new sheets
+ Object.keys(sheets).forEach((sheetId) => {
+ const sheetData = sheets[sheetId];
+ workbook.insertSheet(sheetData);
});
- });
- input.click();
+ // remove other sheets
+ // order.forEach((sheetId: string) => {
+ // workbook.removeSheetBySheetId(sheetId);
+ // });
+ });
}
}
diff --git a/packages/sheets-plugin-import-xlsx/src/ImportXlsxPlugin.tsx b/packages/sheets-plugin-import-xlsx/src/ImportXlsxPlugin.tsx
index f85a027486..4b0cb46b56 100644
--- a/packages/sheets-plugin-import-xlsx/src/ImportXlsxPlugin.tsx
+++ b/packages/sheets-plugin-import-xlsx/src/ImportXlsxPlugin.tsx
@@ -1,13 +1,17 @@
-import { Plugin, SheetContext, UniverSheet } from '@univer/core';
+import { Plugin, PLUGIN_NAMES, SheetContext, UniverSheet } from '@univer/core';
import { zh, en } from './Locale';
import { IMPORT_XLSX_PLUGIN_NAME } from './Basic/Const/PLUGIN_NAME';
import { ImportXlsxController } from './Controller/ImportXlsxController';
+import { BaseComponentPlugin } from '@univer/base-component';
+import { DragAndDropExtensionFactory } from './Basic/Register/DragAndDropExtension';
export interface IImportXlsxPluginConfig {}
export class ImportXlsxPlugin extends Plugin<any, SheetContext> {
private _importXlsxController: ImportXlsxController;
+ private _dragAndDropExtensionFactory: DragAndDropExtensionFactory;
+
constructor(config?: IImportXlsxPluginConfig) {
super(IMPORT_XLSX_PLUGIN_NAME);
}
@@ -32,11 +36,36 @@ export class ImportXlsxPlugin extends Plugin<any, SheetContext> {
});
this._importXlsxController = new ImportXlsxController(this);
+ this.registerExtension();
+ }
+
+ registerExtension() {
+ const dragAndDropRegister = this.getContext()
+ .getPluginManager()
+ .getRequirePluginByName<BaseComponentPlugin>(PLUGIN_NAMES.BASE_COMPONENT)
+ .getRegisterManager()
+ .getDragAndDropExtensionManager()
+ .getRegister();
+
+ this._dragAndDropExtensionFactory = new DragAndDropExtensionFactory(this);
+ dragAndDropRegister.add(this._dragAndDropExtensionFactory);
}
onMounted(): void {
this.initialize();
}
- onDestroy(): void {}
+ onDestroy(): void {
+ const dragAndDropRegister = this.getContext()
+ .getPluginManager()
+ .getRequirePluginByName<BaseComponentPlugin>(PLUGIN_NAMES.BASE_COMPONENT)
+ .getRegisterManager()
+ .getDragAndDropExtensionManager()
+ .getRegister();
+ dragAndDropRegister.delete(this._dragAndDropExtensionFactory);
+ }
+
+ getImportXlsxController() {
+ return this._importXlsxController;
+ }
}