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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
chore(operation): copy paste
Dushusirbdb4aa7dd2b0
diff --git a/packages/base-ui/src/Basics/Interfaces/ICopyData.ts b/packages/base-ui/src/Basics/Interfaces/ICopyData.tsindex 94aae1ffe3..8364023c7a 100644--- a/packages/base-ui/src/Basics/Interfaces/ICopyData.ts+++ b/packages/base-ui/src/Basics/Interfaces/ICopyData.ts@@ -1,28 +1,22 @@export interface ICopyData {/**- * table type or property,- *- * noEmbed does not need to be serialized to the table tag- */- key: 'type' | 'property' | 'noEmbed';-- /**- * table type: univer / office / google / wps ...,+ * table* e.g- * univer- * <table data-type="universheet_copy_action_table"+ * <table data-type="universheet_copy_action_table"></table>*- * <google-sheets-html-origin><table xmlns="http://www.w3.org/1999/xhtml"- *- * property: image / filter ..., use plugin name+ * or plugin name: image / filter ...* e.g* <table data-image="[{left:0,right:0,width:100,height:100,url:''}] data-filter"*/- tag: string;+ name: string;/**- * property value+ *+ * table html content or plugin config*/value: string;+ /**+ * Whether to embed plugin configuration+ */+ embed: boolean;}diff --git a/packages/base-ui/src/Basics/Register/Copy/CopyExtensionFactory.ts b/packages/base-ui/src/Basics/Register/Copy/CopyExtensionFactory.tsindex 579bb8dacc..f9f1db3dfc 100644--- a/packages/base-ui/src/Basics/Register/Copy/CopyExtensionFactory.ts+++ b/packages/base-ui/src/Basics/Register/Copy/CopyExtensionFactory.ts@@ -5,15 +5,22 @@ import { ICopyData } from '../../Interfaces/ICopyData';* base copy extension*/export class BaseCopyExtension<T extends Plugin = Plugin> {+ _data:ICopyData// protected _json: IKeyValue;- constructor(protected _data: ICopyData, protected _plugin: T) {}+ constructor(protected _plugin: T) {+ this._data = {+ name:'',+ value:'',+ embed:true+ }+ }/*** Execute the core logic after the check is successful*/execute() {}- getData() {+ getData():ICopyData {return this._data;}}@@ -33,8 +40,8 @@ export class BaseCopyExtensionFactory<T extends Plugin = Plugin> {* @param data* @returns*/- create(data: ICopyData): BaseCopyExtension {- return new BaseCopyExtension(data, this._plugin);+ create(): BaseCopyExtension {+ return new BaseCopyExtension(this._plugin);}/**@@ -42,7 +49,7 @@ export class BaseCopyExtensionFactory<T extends Plugin = Plugin> {* @param data* @returns*/- check(data: ICopyData): false | BaseCopyExtension {+ check(): false | BaseCopyExtension {return false;}}diff --git a/packages/base-ui/src/Basics/Register/Copy/CopyExtensionManager.ts b/packages/base-ui/src/Basics/Register/Copy/CopyExtensionManager.tsindex 75ca6ec808..fcd3babf34 100644--- a/packages/base-ui/src/Basics/Register/Copy/CopyExtensionManager.ts+++ b/packages/base-ui/src/Basics/Register/Copy/CopyExtensionManager.ts@@ -22,13 +22,13 @@ export class CopyExtensionManager {* inject all actions* @param command*/- handle(data: ICopyData) {+ handle() {const clipboardExtensionFactoryList = this._register?.copyExtensionFactoryList;if (!clipboardExtensionFactoryList) return;// get the sorted list// get the dynamically added listthis._copyExtensionFactoryList = clipboardExtensionFactoryList;- this._checkExtension(data);+ this._checkExtension();// const extension = this._checkExtension(data);// Need to handle multiple extensions@@ -54,25 +54,30 @@ export class CopyExtensionManager {* @param command* @returns*/- private _checkExtension(data: ICopyData) {+ private _checkExtension() {if (!this._copyExtensionFactoryList) return false;let extension: BaseCopyExtension | false = false;let table = '';let property: IKeyValue = {};for (let index = 0; index < this._copyExtensionFactoryList.length; index++) {const extensionFactory = this._copyExtensionFactoryList[index];- extension = extensionFactory.check(data);+ extension = extensionFactory.check();if (extension !== false) {extension.execute();const data = extension.getData();- if (data.key === 'type') {+ if (data.name === 'table') {table = data.value;- if (data.tag !== 'univer') {++ const embed = true; //TODO: 待讨论,配置是在operation插件,但是base-ui不能从插件取配置,只能从核心取配置 @alex++ table = this._insertEmbedFlag(table,embed)+ if(!embed){break;}- } else if (data.key === 'property') {- property[data.tag] = data.value;++ } else {+ property[data.name] = data.value;}}}@@ -92,4 +97,12 @@ export class CopyExtensionManager {this._copyResolver(result);}++ private _insertEmbedFlag(table: string,embed:boolean):string{++ let insertIndex = table.indexOf('<table') + '<table'.length; // 插入位置+ let result = table.slice(0, insertIndex) + `embed="${embed}"` + table.slice(insertIndex); // 插入新字符串++ return result+ }}diff --git a/packages/base-ui/src/Common/RegisterManager.ts b/packages/base-ui/src/Common/RegisterManager.tsindex 4e97ae616f..8bdd308fed 100644--- a/packages/base-ui/src/Common/RegisterManager.ts+++ b/packages/base-ui/src/Common/RegisterManager.ts@@ -1,5 +1,5 @@import { Plugin } from '@univerjs/core';-import { IPasteData, IDragAndDropData, ICopyData } from '../Basics/Interfaces';+import { IPasteData, IDragAndDropData } from '../Basics/Interfaces';import { PasteExtensionManager, DragAndDropExtensionManager, CopyExtensionManager } from '../Basics/Register';export class RegisterManager {@@ -33,12 +33,7 @@ export class RegisterManager {}if (onKeyCopyObservable && !onKeyCopyObservable.hasObservers()) {onKeyCopyObservable.add((evt: ClipboardEvent) => {- const data: ICopyData = {- key: 'type',- tag: 'univer',- value: '',- };- this._copyExtensionManager.handle(data);+ this._copyExtensionManager.handle();});}}diff --git a/packages/base-ui/src/Utils/util.ts b/packages/base-ui/src/Utils/util.tsindex d3411e24d2..70044b2937 100644--- a/packages/base-ui/src/Utils/util.ts+++ b/packages/base-ui/src/Utils/util.ts@@ -301,3 +301,18 @@ export function findLocale<T>(obj: T, fn: Function) {// return data;// }++export function isCtrlPressed(event:KeyboardEvent){+ var isCtrl = false;++ // Determine the corresponding key code of the Ctrl key according to the operating system+ if (navigator.userAgent.indexOf('Mac') !== -1) {+ // Mac+ isCtrl = event.metaKey;+ } else {+ // Windows+ isCtrl = event.ctrlKey;+ }++ return isCtrl+}\ No newline at end of filediff --git a/packages/sheets-plugin-operation/src/Basics/Interfaces/IOperationPluginConfig.ts b/packages/sheets-plugin-operation/src/Basics/Interfaces/IOperationPluginConfig.tsindex 6245f7d3f8..612ffa74f4 100644--- a/packages/sheets-plugin-operation/src/Basics/Interfaces/IOperationPluginConfig.ts+++ b/packages/sheets-plugin-operation/src/Basics/Interfaces/IOperationPluginConfig.ts@@ -1,6 +1,7 @@export interface ICopyConfig {embed: boolean;}+export interface IOperationPluginConfig {copy: ICopyConfig;}diff --git a/packages/sheets-plugin-operation/src/Basics/Register/CopyExtension.ts b/packages/sheets-plugin-operation/src/Basics/Register/CopyExtension.tsindex ad0a794ebb..7bf077e347 100644--- a/packages/sheets-plugin-operation/src/Basics/Register/CopyExtension.ts+++ b/packages/sheets-plugin-operation/src/Basics/Register/CopyExtension.ts@@ -5,6 +5,7 @@ export class CopyExtension extends BaseCopyExtension<OperationPlugin> {execute() {const value = this._plugin.getUniverCopy().getCopyContent();if (!value) return;+ this._data.name = 'table'this._data.value = value;}}@@ -14,15 +15,12 @@ export class CopyExtensionFactory extends BaseCopyExtensionFactory<OperationPlugreturn 1;}- create(data: ICopyData): BaseCopyExtension {- return new CopyExtension(data, this._plugin);+ create(): BaseCopyExtension {+ return new CopyExtension(this._plugin);}- check(data: ICopyData): false | BaseCopyExtension {- if (data.key === 'type' && data.tag === 'univer') {- return this.create(data);- }-- return false;+ // To generate a fixed-format table, you must execute+ check(): false | BaseCopyExtension {+ return this.create();}}diff --git a/packages/sheets-plugin-operation/src/Basics/Register/CopyImageExtension.ts b/packages/sheets-plugin-operation/src/Basics/Register/CopyImageExtension.tsindex 52bea26d72..0eb018ead9 100644--- a/packages/sheets-plugin-operation/src/Basics/Register/CopyImageExtension.ts+++ b/packages/sheets-plugin-operation/src/Basics/Register/CopyImageExtension.ts@@ -1,14 +1,17 @@import { BaseCopyExtension, BaseCopyExtensionFactory, ICopyData } from '@univerjs/base-ui';import { OperationPlugin } from '../../OperationPlugin';+// TODO 这个模块应该放到图片插件 @jerryexport class CopyImageExtension extends BaseCopyExtension<OperationPlugin> {execute() {const value = this._handleImage();if (!value) return;+ this._data.name = 'image';this._data.value = value;}private _handleImage() {+ // TODO 根据当前选区范围,从图片差距中取出配置 @jerryconst image = [{ left: 0, right: 0, width: 100, height: 100, url: 'image_url' }];return JSON.stringify(image);}@@ -19,20 +22,14 @@ export class CopyImageExtensionFactory extends BaseCopyExtensionFactory<Operatioreturn 2;}- create(data: ICopyData): BaseCopyExtension {- return new CopyImageExtension(data, this._plugin);+ create(): BaseCopyExtension {+ return new CopyImageExtension(this._plugin);}- check(data: ICopyData): false | BaseCopyExtension {- const { copy } = this._plugin.getConfig();- const embed = copy.embed;- if (embed) {- data.key = 'property';- data.tag = 'image';- //TODO <table data-image="[]" embed="false">- return this.create(data);- }-- return false;+ check(): false | BaseCopyExtension {+++ return this.create();+}}diff --git a/packages/sheets-plugin-operation/src/Basics/Register/PasteExtension.ts b/packages/sheets-plugin-operation/src/Basics/Register/PasteExtension.tsindex 666bae4782..ee26ba8efa 100644--- a/packages/sheets-plugin-operation/src/Basics/Register/PasteExtension.ts+++ b/packages/sheets-plugin-operation/src/Basics/Register/PasteExtension.ts@@ -19,7 +19,7 @@ export class PasteExtension extends BasePasteExtension<OperationPlugin> {let colInfo;let rowInfo;if (content) {- // TODO move to PasteOfficeExtension+ // TODO move to PasteOfficeExtension @tonyif (content.indexOf('xmlns:x="urn:schemas-microsoft-com:office:excel"') > -1) {data = handelExcelToJson(content);colInfo = handleTableColgroup(content);diff --git a/packages/sheets-plugin-operation/src/Basics/Register/PasteImageExtension.ts b/packages/sheets-plugin-operation/src/Basics/Register/PasteImageExtension.tsindex e22b16154c..1ee1195df6 100644--- a/packages/sheets-plugin-operation/src/Basics/Register/PasteImageExtension.ts+++ b/packages/sheets-plugin-operation/src/Basics/Register/PasteImageExtension.ts@@ -1,11 +1,11 @@import { BasePasteExtension, BasePasteExtensionFactory, IPasteData } from '@univerjs/base-ui';import { OperationPlugin } from '../../OperationPlugin';-// copy paste delete cut insert // SheetsPluginOperation+// copy paste delete cut insert // TODO 这个模块应该放到图片插件 @jerryexport class PasteImageExtension extends BasePasteExtension<OperationPlugin> {execute() {let content = this._data.html || this._data.plain;-+ console.info('cc--',content)return [];}}@@ -22,9 +22,8 @@ export class PasteImageExtensionFactory extends BasePasteExtensionFactory<Operatcheck(data: IPasteData): false | BasePasteExtension {const content = data.html || data.plain;- // TODO 识别univerId,sheetId,rangeData中的范围是否包含image信息,有则直接从本地取- // TODO office 来的图片- // TODO: embed从manager解析而来+ // TODO embed="true",直接解析table标签上的配置data-image="[{config}]" @jerry+ // TODO embed="false",识别table标签上的univerId,sheetId,rangeData中的范围是否包含image信息,有则直接从本地取,没有就略过 @jerryif (content && content.indexOf('data-image') > -1) {return this.create(data);}diff --git a/packages/sheets-plugin-operation/src/Domain/Copy.ts b/packages/sheets-plugin-operation/src/Domain/Copy.tsindex f4ccc18e00..260273946e 100644--- a/packages/sheets-plugin-operation/src/Domain/Copy.ts+++ b/packages/sheets-plugin-operation/src/Domain/Copy.ts@@ -1,4 +1,4 @@-import { SheetContext, PLUGIN_NAMES, Tools, handleJsonToDom, handleStyleToString } from '@univerjs/core';+import { SheetContext, PLUGIN_NAMES, Tools, handleJsonToDom, handleStyleToString, ObjectArray, IColumnData, IRowData } from '@univerjs/core';import { SheetPlugin, SelectionModel, SelectionControl } from '@univerjs/base-sheets';import { RightMenuProps, SheetUIPlugin, SHEET_UI_PLUGIN_NAME, RightMenuItem } from '@univerjs/ui-plugin-sheets';import { Clipboard } from '@univerjs/base-ui';@@ -14,12 +14,6 @@ export abstract class Copy {// this._initRegisterComponent(componentList);// SheetPlugin?.addRightMenu(copyList);-- // const manager = this._context.getUniver().getGlobalContext().getObserverManager();- // manager.requiredObserver<ClipboardEvent>('onKeyCopyObservable', 'core').add((e) => {- // // TODO: 注册,插件序列化model放到table,key=>model,根据开关是否需要序列号化(在extension里判断)- // this.copy(e);- // });}getContext() {@@ -318,15 +312,15 @@ export class UniverCopy extends Copy {}/**- * TODO 加入univerId,sheetId,rangeData等+ * TODO 加入univerId,sheetId,rangeData等 @tony* @returns*/getCopyContent() {const { sheet, spreadsheet, selections } = this._getSheetInfo();if (selections.length > 1) return;- const rowManager: any = sheet.getRowManager().getRowData();- const colManager: any = sheet.getColumnManager().getColumnData();+ const rowManager = sheet.getRowManager().getRowData();+ const colManager = sheet.getColumnManager().getColumnData();let rowIndexArr: number[] = [];let colIndexArr: number[] = [];diff --git a/packages/sheets-plugin-operation/src/Domain/Paste.ts b/packages/sheets-plugin-operation/src/Domain/Paste.tsindex 4bcfb7be8b..a130c311b2 100644--- a/packages/sheets-plugin-operation/src/Domain/Paste.ts+++ b/packages/sheets-plugin-operation/src/Domain/Paste.ts@@ -218,7 +218,7 @@ export class UniverPaste extends Paste {private _getSelectionActionData(sheetId: string) {const selectionManager = this.getContext().getPluginManager().getRequirePluginByName<SheetPlugin>(PLUGIN_NAMES.SPREADSHEET).getSelectionManager();- // TODO 从粘贴的表格中解析出来+ // TODO 从粘贴的表格中解析出来 @tonyconst selectionRange = DEFAULT_SELECTION;const curCellRange = DEFAULT_CELL;selectionManager.clearSelectionControls();diff --git a/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.ts b/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.tsindex bf88745a19..3ffa16cb1a 100644--- a/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.ts+++ b/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.ts@@ -1,6 +1,6 @@import { IPointerEvent, IMouseEvent } from '@univerjs/base-render';import { CANVAS_VIEW_KEY, SheetPlugin } from '@univerjs/base-sheets';-import { getRefElement, CellEditExtensionManager, $$, setLastCaretPosition, handleStringToStyle, handleDomToJson } from '@univerjs/base-ui';+import { getRefElement, CellEditExtensionManager, $$, setLastCaretPosition, handleStringToStyle, handleDomToJson, isCtrlPressed } from '@univerjs/base-ui';import { Direction, handleStyleToString, ICellData, isKeyPrintable, PLUGIN_NAMES, Tools } from '@univerjs/core';import { SheetUIPlugin } from '../SheetUIPlugin';import { RichText } from '../View/RichText';@@ -244,7 +244,7 @@ export class CellEditorUIController {if (onKeyDownObservable && !onKeyDownObservable.hasObservers()) {onKeyDownObservable.add((evt: KeyboardEvent) => {- if (!evt.ctrlKey && isKeyPrintable(evt.key)) {+ if (!isCtrlPressed(evt) && isKeyPrintable(evt.key)) {// character keythis.enterEditMode(true);} else {diff --git a/packages/ui-plugin-sheets/src/SheetUIPlugin.ts b/packages/ui-plugin-sheets/src/SheetUIPlugin.tsindex 751ad74b76..a9f3a7abd6 100644--- a/packages/ui-plugin-sheets/src/SheetUIPlugin.ts+++ b/packages/ui-plugin-sheets/src/SheetUIPlugin.ts@@ -6,7 +6,7 @@ import { AppUIController } from './Controller/AppUIController';import { Fx } from './View/FormulaBar';import { SlotComponentProps } from './Controller/SlotController';import { IToolbarItemProps } from './Controller';-// import {zh,en} from './Locale';+import {zh,en} from './Locale';export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {private _appUIController: AppUIController;@@ -37,26 +37,30 @@ export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {/*** load more Locale object*/- const locale = this.getGlobalContext().getLocale().getCurrentLocale();- console.info(`./Locale/${locale}`);- if (locale === LocaleType.ZH) {- import(`./Locale/zh`).then((module) => {- this.loadLocale(locale, module);- });- } else if (locale === LocaleType.EN) {- import(`./Locale/en`).then((module) => {- this.loadLocale(locale, module);- });- }-- // this._componentManager = new ComponentManager();- // this._keyboardManager = new KeyboardManager(this);- // this._registerManager = new RegisterManager(this);- // this._appUIController = new AppUIController(this);- // // AppUIController initializes the DOM as an asynchronous rendering process, and must wait for the UI rendering to complete before starting to render the canvas- // this.UIDidMount(() => {- // this.initRender();- // });+ this.getLocale().load({+ zh,+ en+ });+ // const locale = this.getGlobalContext().getLocale().getCurrentLocale();+ // console.info(`./Locale/${locale}`);+ // if (locale === LocaleType.ZH) {+ // import(`./Locale/zh`).then((module) => {+ // this.loadLocale(locale, module);+ // });+ // } else if (locale === LocaleType.EN) {+ // import(`./Locale/en`).then((module) => {+ // this.loadLocale(locale, module);+ // });+ // }++ this._componentManager = new ComponentManager();+ this._keyboardManager = new KeyboardManager(this);+ this._registerManager = new RegisterManager(this);+ this._appUIController = new AppUIController(this);+ // AppUIController initializes the DOM as an asynchronous rendering process, and must wait for the UI rendering to complete before starting to render the canvas+ this.UIDidMount(() => {+ this.initRender();+ });}loadLocale(locale: LocaleType, module: IKeyValue) {