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): drag and drop event support

Dushusir013de26483cd
diff --git a/examples/univer-vanilla-ts/index.html b/examples/univer-vanilla-ts/index.html
index ee9054e4cb..483c6ffb49 100644
--- a/examples/univer-vanilla-ts/index.html
+++ b/examples/univer-vanilla-ts/index.html
@@ -12,13 +12,30 @@
height: 100%;
margin: 0;
}
+
+ .univer-container {
+ display: flex;
+ }
+
+ .univer-container>div {
+ height: 40vh;
+ }
</style>
</head>
<body>
<div id="app" style="z-index: 1; position: fixed; font-size: 20px; color: green;"></div>
- <div id="universheet-demo-up" style="height: 50vh;"></div>
- <div id="universheet-demo-down" style="height: 50vh;"></div>
+
+ <div class="univer-container">
+ <div id="universheet-up"></div>
+ <div id="univerdoc"></div>
+ </div>
+
+ <div class="univer-container">
+ <div id="universheet-down"></div>
+ <div id="universlide"></div>
+ </div>
+
<script type="module" src="/src/main.tsx"></script>
</body>
diff --git a/examples/univer-vanilla-ts/src/main.tsx b/examples/univer-vanilla-ts/src/main.tsx
index d5f20fc0e4..04f5a84cec 100644
--- a/examples/univer-vanilla-ts/src/main.tsx
+++ b/examples/univer-vanilla-ts/src/main.tsx
@@ -1,7 +1,9 @@
-import { UniverSheet } from '@univer/core';
+import { UniverSheet, UniverDoc, UniverSlide } from '@univer/core';
import { RenderEngine } from '@univer/base-render';
import { UniverComponentSheet } from '@univer/style-universheet';
import { SheetPlugin } from '@univer/base-sheets';
+import { DocPlugin } from '@univer/base-docs';
+import { SlidePlugin } from '@univer/base-slides';
import {
DEFAULT_FORMULA_DATA,
FormulaPlugin,
@@ -15,7 +17,7 @@ import { ClipboardPlugin } from '@univer/sheets-plugin-clipboard';
import { BaseComponentPlugin } from '@univer/base-component';
const uiDefaultConfigUp = {
- container: 'universheet-demo-up',
+ container: 'universheet-up',
layout: {
innerRight: false,
outerLeft: false,
@@ -64,7 +66,7 @@ univerSheetDown.installPlugin(new UniverComponentSheet());
univerSheetDown.installPlugin(
new SheetPlugin({
- container: 'universheet-demo-down',
+ container: 'universheet-down',
})
);
univerSheetDown.installPlugin(new BaseComponentPlugin());
@@ -72,3 +74,30 @@ univerSheetDown.installPlugin(new NumfmtPlugin());
FormulaPlugin.create().installTo(univerSheetDown);
univerSheetDown.installPlugin(new ClipboardPlugin());
+
+// doc
+
+const docPluginConfig = {
+ container: 'univerdoc',
+};
+const univerDoc = UniverDoc.newInstance();
+
+univerDoc.installPlugin(new RenderEngine());
+univerDoc.installPlugin(new UniverComponentSheet());
+
+univerDoc.installPlugin(new DocPlugin(docPluginConfig));
+univerDoc.installPlugin(new BaseComponentPlugin());
+
+// SlidePlugin
+
+const slidePluginConfig = {
+ container: 'universlide',
+};
+
+const univerSlide = UniverSlide.newInstance();
+
+univerSlide.installPlugin(new RenderEngine());
+univerSlide.installPlugin(new UniverComponentSheet());
+
+univerSlide.installPlugin(new SlidePlugin(slidePluginConfig));
+univerSlide.installPlugin(new BaseComponentPlugin());
diff --git a/packages/base-component/src/Basics/Interfaces/IDragAndDropData.ts b/packages/base-component/src/Basics/Interfaces/IDragAndDropData.ts
new file mode 100644
index 0000000000..f680e4ebd2
--- /dev/null
+++ b/packages/base-component/src/Basics/Interfaces/IDragAndDropData.ts
@@ -0,0 +1,4 @@
+export interface IDragAndDropData {
+ plain?: string;
+ html?: string;
+}
diff --git a/packages/base-component/src/Basics/Interfaces/index.ts b/packages/base-component/src/Basics/Interfaces/index.ts
index f1dd2baf98..50f46389e8 100644
--- a/packages/base-component/src/Basics/Interfaces/index.ts
+++ b/packages/base-component/src/Basics/Interfaces/index.ts
@@ -1,3 +1,5 @@
// @index(['./*.ts','./**/index.ts'], f => `export * from '${f.path}'`)
export * from './IClipboardData';
+export * from './IDragAndDropData';
+export * from './PasteType';
// @endindex
diff --git a/packages/base-component/src/Basics/Register/Clipboard/ClipboardExtensionFactory.ts b/packages/base-component/src/Basics/Register/Clipboard/ClipboardExtensionFactory.ts
index 5aaee4c8e8..342a470a7b 100644
--- a/packages/base-component/src/Basics/Register/Clipboard/ClipboardExtensionFactory.ts
+++ b/packages/base-component/src/Basics/Register/Clipboard/ClipboardExtensionFactory.ts
@@ -8,17 +8,6 @@ export class BaseClipboardExtension<T extends Plugin = Plugin> {
// protected _json: IKeyValue;
constructor(protected _data: IClipboardData, protected _plugin: T) {}
- // getJSON(): IKeyValue {
- // return this._json;
- // }
-
- // /**
- // * Modify the value
- // */
- // setJSON(json: IKeyValue) {
- // this._json = json;
- // }
-
/**
* Execute the core logic after the check is successful
*/
diff --git a/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionFactory.ts b/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionFactory.ts
new file mode 100644
index 0000000000..1194f63814
--- /dev/null
+++ b/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionFactory.ts
@@ -0,0 +1,44 @@
+import { Plugin } from '@univer/core';
+import { IDragAndDropData } from '../../Interfaces';
+
+/**
+ * 转化table html/json为json
+ */
+export class BaseDragAndDropExtension<T extends Plugin = Plugin> {
+ // protected _json: IKeyValue;
+ constructor(protected _data: IDragAndDropData, protected _plugin: T) {}
+
+ /**
+ * Execute the core logic after the check is successful
+ */
+ execute() {}
+}
+
+/**
+ * Determine whether to intercept and create BaseDragAndDropExtension
+ */
+export class BaseDragAndDropExtensionFactory<T extends Plugin = Plugin> {
+ constructor(protected _plugin: T) {}
+
+ get zIndex() {
+ return 0;
+ }
+
+ /**
+ * Generate Extension
+ * @param data
+ * @returns
+ */
+ create(data: IDragAndDropData): BaseDragAndDropExtension {
+ return new BaseDragAndDropExtension(data, this._plugin);
+ }
+
+ /**
+ * Check if this cell needs to be intercepted currently
+ * @param data
+ * @returns
+ */
+ 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
new file mode 100644
index 0000000000..a24444a2be
--- /dev/null
+++ b/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionManager.ts
@@ -0,0 +1,54 @@
+import { IDragAndDropData } from '../../Interfaces';
+import { BaseDragAndDropExtension, BaseDragAndDropExtensionFactory } from './DragAndDropExtensionFactory';
+import { DragAndDropExtensionRegister } from './DragAndDropExtensionRegister';
+
+export class DragAndDropExtensionManager {
+ private _DragAndDropExtensionFactoryList: BaseDragAndDropExtensionFactory[];
+
+ // mounted on the instance
+ private _register: DragAndDropExtensionRegister;
+
+ getRegister(): DragAndDropExtensionRegister {
+ return this._register;
+ }
+
+ constructor() {
+ this._register = new DragAndDropExtensionRegister();
+ }
+
+ /**
+ * inject all actions
+ * @param command
+ */
+ 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();
+ }
+ }
+
+ /**
+ * Execute when the action is matched
+ * @param command
+ * @returns
+ */
+ 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);
+ if (extension !== false) {
+ break;
+ }
+ }
+ return extension;
+ }
+}
diff --git a/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionRegister.ts b/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionRegister.ts
new file mode 100644
index 0000000000..94124fc9c0
--- /dev/null
+++ b/packages/base-component/src/Basics/Register/DragAndDrop/DragAndDropExtensionRegister.ts
@@ -0,0 +1,20 @@
+import { sortRules } from '@univer/core';
+import { BaseDragAndDropExtensionFactory } from './DragAndDropExtensionFactory';
+
+export class DragAndDropExtensionRegister {
+ private _DragAndDropExtensionFactoryList: BaseDragAndDropExtensionFactory[] = [];
+
+ get DragAndDropExtensionFactoryList(): BaseDragAndDropExtensionFactory[] {
+ return this._DragAndDropExtensionFactoryList;
+ }
+
+ add(...extensionFactoryList: BaseDragAndDropExtensionFactory[]) {
+ this._DragAndDropExtensionFactoryList.push(...extensionFactoryList);
+ this._DragAndDropExtensionFactoryList.sort(sortRules);
+ }
+
+ delete(extensionFactory: BaseDragAndDropExtensionFactory) {
+ const index = this._DragAndDropExtensionFactoryList.indexOf(extensionFactory);
+ this._DragAndDropExtensionFactoryList.splice(index, 1);
+ }
+}
diff --git a/packages/base-component/src/Basics/Register/DragAndDrop/index.ts b/packages/base-component/src/Basics/Register/DragAndDrop/index.ts
new file mode 100644
index 0000000000..ed2774f54c
--- /dev/null
+++ b/packages/base-component/src/Basics/Register/DragAndDrop/index.ts
@@ -0,0 +1,6 @@
+// @index(['./*.ts','./**/index.ts'], f => `export * from '${f.path}'`)
+
+export * from './DragAndDropExtensionFactory';
+export * from './DragAndDropExtensionManager';
+export * from './DragAndDropExtensionRegister';
+// @endindex
diff --git a/packages/base-component/src/Basics/Register/index.ts b/packages/base-component/src/Basics/Register/index.ts
index d1ac56a376..05d4d7a652 100644
--- a/packages/base-component/src/Basics/Register/index.ts
+++ b/packages/base-component/src/Basics/Register/index.ts
@@ -1,3 +1,4 @@
// @index(['./*.ts','./**/index.ts'], f => `export * from '${f.path}'`)
export * from './Clipboard/index';
+export * from './DragAndDrop/index';
// @endindex
diff --git a/packages/base-component/src/Common/DragManager.ts b/packages/base-component/src/Common/DragManager.ts
new file mode 100644
index 0000000000..1055fa6dac
--- /dev/null
+++ b/packages/base-component/src/Common/DragManager.ts
@@ -0,0 +1,38 @@
+import { Observable, ObserverManager, Plugin } from '@univer/core';
+
+export type DragObserver = {
+ onKeyDownObservable: Observable<DragEvent>;
+};
+
+export class DragManager {
+ private _observerManager: ObserverManager;
+
+ constructor(private _plugin: Plugin) {
+ this._observerManager = this._plugin.getContext().getObserverManager();
+ this._installObserver();
+ }
+
+ private _installObserver() {
+ // Drag
+ this._observerManager.addObserver('onDropObservable', 'core', new Observable());
+ }
+
+ /**
+ * init Drag listener
+ *
+ * add to docs/slides/
+ */
+ handleDragAction(element: HTMLElement) {
+ const DropEvent = (evt: DragEvent) => {
+ evt.preventDefault();
+ this._observerManager.requiredObserver<DragEvent>('onDropObservable', 'core')?.notifyObservers(evt);
+ };
+
+ const DragOverEvent = (evt: DragEvent) => {
+ evt.preventDefault();
+ };
+
+ element.addEventListener('drop', DropEvent);
+ element.addEventListener('dragover', DragOverEvent);
+ }
+}
diff --git a/packages/base-component/src/Common/RegisterManager.ts b/packages/base-component/src/Common/RegisterManager.ts
index 4e7028d079..3c35900548 100644
--- a/packages/base-component/src/Common/RegisterManager.ts
+++ b/packages/base-component/src/Common/RegisterManager.ts
@@ -1,19 +1,23 @@
import { Plugin } from '@univer/core';
import { IClipboardData } from '../Basics/Interfaces';
-import { ClipboardExtensionManager } from '../Basics/Register';
+import { ClipboardExtensionManager, DragAndDropExtensionManager } from '../Basics/Register';
export class RegisterManager {
private _clipboardExtensionManager: ClipboardExtensionManager;
+ private _dragAndDropExtensionManager: DragAndDropExtensionManager;
+
constructor(private _plugin: Plugin) {
this.initialize();
}
initialize(): void {
- this.setExtensionManager();
+ this.setClipboardExtensionManager();
+ this.setDragAndDropExtensionManager();
}
- setExtensionManager() {
+ setClipboardExtensionManager() {
+ this._clipboardExtensionManager = new ClipboardExtensionManager();
const onKeyPasteObservable = this._plugin.getContext().getObserverManager().getObserver<ClipboardEvent>('onKeyPasteObservable', 'core');
if (onKeyPasteObservable && !onKeyPasteObservable.hasObservers()) {
@@ -23,7 +27,18 @@ export class RegisterManager {
});
});
}
- this._clipboardExtensionManager = new ClipboardExtensionManager();
+ }
+
+ setDragAndDropExtensionManager() {
+ this._dragAndDropExtensionManager = new DragAndDropExtensionManager();
+
+ const onDropObservable = this._plugin.getContext().getObserverManager().getObserver<DragEvent>('onDropObservable', 'core');
+
+ if (onDropObservable && !onDropObservable.hasObservers()) {
+ onDropObservable.add((evt: DragEvent) => {
+ console.log('onDropObservable event======', evt);
+ });
+ }
}
/**
@@ -34,6 +49,10 @@ export class RegisterManager {
return this._clipboardExtensionManager;
}
+ getDragAndDropExtensionManager(): DragAndDropExtensionManager {
+ return this._dragAndDropExtensionManager;
+ }
+
onMounted(): void {
this.initialize();
}
diff --git a/packages/base-component/src/Common/index.ts b/packages/base-component/src/Common/index.ts
index f2f1f5d85d..6f157013bb 100644
--- a/packages/base-component/src/Common/index.ts
+++ b/packages/base-component/src/Common/index.ts
@@ -1,2 +1,3 @@
export * from './AppContext';
export * from './KeyboardManager';
+export * from './DragManager';
diff --git a/packages/base-sheets/src/Controller/SheetContainerController.ts b/packages/base-sheets/src/Controller/SheetContainerController.ts
index 14f277bf39..b1bf37199c 100644
--- a/packages/base-sheets/src/Controller/SheetContainerController.ts
+++ b/packages/base-sheets/src/Controller/SheetContainerController.ts
@@ -1,5 +1,5 @@
import { BooleanNumber, CommandManager, ContextBase, ISheetActionData, LocaleType, PLUGIN_NAMES, SheetActionBase, Workbook } from '@univer/core';
-import { BaseComponentSheet, BaseComponentRender, BaseComponentProps } from '@univer/base-component';
+import { BaseComponentSheet, BaseComponentRender, BaseComponentProps, DragManager, getRefElement } from '@univer/base-component';
import { CellRangeModal } from '../View/UI/Common/CellRange/CellRangeModal';
import { SheetPlugin } from '../SheetPlugin';
@@ -74,6 +74,8 @@ export class SheetContainerController {
private _sheetContainer: SheetContainer;
+ private _dragManager: DragManager;
+
constructor(plugin: SheetPlugin, config: BaseSheetContainerConfig) {
this._plugin = plugin;
@@ -130,7 +132,12 @@ export class SheetContainerController {
layout: this._defaultLayout,
currentLocale: this._plugin.getContext().getLocale().options.currentLocale,
});
+
+ // handle drag event
+ this._dragManager.handleDragAction(getRefElement(component));
});
+
+ this._dragManager = new DragManager(this._plugin);
}
// 注册常用icon和组件
diff --git a/packages/base-sheets/src/main.tsx b/packages/base-sheets/src/main.tsx
index aad913b238..3dbb4b423e 100644
--- a/packages/base-sheets/src/main.tsx
+++ b/packages/base-sheets/src/main.tsx
@@ -5,7 +5,6 @@ import { NumfmtPlugin } from '@univer/sheets-plugin-numfmt';
import { DEFAULT_WORKBOOK_DATA, DEFAULT_WORKBOOK_DATA_DEMO, DEFAULT_WORKBOOK_DATA_DOWN } from '@univer/common-plugin-data';
import { DEFAULT_FORMULA_DATA, DEFAULT_FORMULA_DATA_DEMO, FormulaPlugin } from '@univer/sheets-plugin-formula';
import { ClipboardPlugin } from '@univer/sheets-plugin-clipboard';
-import { RegisterPlugin } from '@univer/common-plugin-register';
import { SheetPlugin } from './SheetPlugin';
const uiDefaultConfigUp = {
@@ -157,6 +156,5 @@ const univerSheetDemo = UniverSheet.newInstance(DEFAULT_WORKBOOK_DATA_DEMO);
univerSheetDemo.installPlugin(new RenderEngine());
univerSheetDemo.installPlugin(new UniverComponentSheet());
univerSheetDemo.installPlugin(new SheetPlugin(uiDefaultConfigDemo));
-RegisterPlugin.create().installTo(univerSheetDemo);
univerSheetDemo.installPlugin(new ClipboardPlugin());
FormulaPlugin.create(DEFAULT_FORMULA_DATA_DEMO).installTo(univerSheetDemo);
diff --git a/packages/core/src/Basics/ContextBase.ts b/packages/core/src/Basics/ContextBase.ts
index 7fae48f85b..fcc367e4ce 100644
--- a/packages/core/src/Basics/ContextBase.ts
+++ b/packages/core/src/Basics/ContextBase.ts
@@ -24,6 +24,8 @@ export abstract class ContextBase {
protected _observerManager: ObserverManager;
+ protected _univer: Univer;
+
protected abstract _setObserver(): void;
constructor() {
@@ -36,7 +38,9 @@ export abstract class ContextBase {
this._observerManager = new ObserverManager();
}
- onUniver(univer: Univer): void {}
+ onUniver(univer: Univer): void {
+ this._univer = univer;
+ }
getHook<T>(path: string): Nullable<ObservableHooks<T>> {
return this._hooksManager.getHooks<T>(path);
diff --git a/packages/core/src/Command/Command.ts b/packages/core/src/Command/Command.ts
index 8b8711a9f2..e7b9c64b4a 100644
--- a/packages/core/src/Command/Command.ts
+++ b/packages/core/src/Command/Command.ts
@@ -15,7 +15,6 @@ export class CommandUnit {
/**
* Execute the undo-redo command
*
- * TODO: SlideCommand/DocCommand
*/
export class Command {
_unit: CommandUnit;
@@ -69,7 +68,11 @@ export class Command {
}
undo(): void {
- this._actionList.forEach((action) => action.undo());
+ // Reverse is required, such as moving C:E to the back of column A, after copying the data of C:E
+ // 1. removeColumn C:E 2.insertColumnData A,
+ // when undo, it should be
+ // 1. removeColumn A, 2. insertColumnData C:E
+ this._actionList.reverse().forEach((action) => action.undo());
CommandManager.getCommandObservers().notifyObservers({
type: ActionType.UNDO,
actions: this._actionList,
diff --git a/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts b/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts
index 7e911f957e..4cca965107 100644
--- a/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts
+++ b/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts
@@ -36,6 +36,7 @@ export class FormulaActionExtension extends BaseActionExtension<FormulaPlugin> {
// Filter out Actions that contain formulas, inject setRangeDataAction
if (actionData.actionName === ACTION_NAMES.SET_RANGE_DATA_ACTION) {
+ // TODO FALSE PASS
const { sheetId, cellValue } = actionData;
if (Tools.isEmptyObject(formulaData)) return;