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
fix(clipboard): cell edit handle paste event
Dushusir1ba42deeeea6
diff --git a/.github/contributing-zh.md b/.github/contributing-zh.mdindex 07b7cdd91f..ee2b8d5018 100644--- a/.github/contributing-zh.md+++ b/.github/contributing-zh.md@@ -617,6 +617,24 @@ const s = `${style.AlternatingColorsSideSetting}`;### 处理国际化+## 注册快捷键++`base-component`插件中向核心提供了`onKeyDownObservable`,可以用来快捷注册键盘快捷键。++我们允许为同一组快捷键注册多个监听。++```ts+const onKeyDownObservable = this._plugin.getContext().getObserverManager().getObserver<KeyboardEvent>('onKeyDownObservable', 'core');++onKeyDownObservable?.add((evt: KeyboardEvent) => {+ // handle Ctrl + A+ if (evt.ctrlKey && evt.key === 'a') {+ // custom function+ this.handleCtrlA();+ }+});+```+## 测试核心代码测试使用diff --git a/packages/base-component/src/Helpers/Cell.ts b/packages/base-component/src/Helpers/Cell.tsindex bd1be54d7e..d5cfb96848 100644--- a/packages/base-component/src/Helpers/Cell.ts+++ b/packages/base-component/src/Helpers/Cell.ts@@ -738,8 +738,8 @@ export function handelTableToJson(table: string) {}let txt = td.innerText;if (txt.trim().length === 0) {- cell.v = null;- cell.m = null;+ cell.v = '';+ cell.m = '';} else {// Todo,处理格式cell.v = txt;@@ -804,8 +804,8 @@ export function handlePlainToJson(plain: string) {for (let j = 0; j < data[i].length; j++) {if (data[i][j].length) {data[i][j] = {- v: data[i][j],- m: data[i][j],+ v: data[i][j] || '',+ m: data[i][j] || '',};} else {data[i][j] = null;diff --git a/packages/base-numfmt-engine/src/core/parseValue.ts b/packages/base-numfmt-engine/src/core/parseValue.tsindex 10cf1ac7ff..fbf0cb8af9 100644--- a/packages/base-numfmt-engine/src/core/parseValue.ts+++ b/packages/base-numfmt-engine/src/core/parseValue.ts@@ -385,6 +385,7 @@ const nextToken = (str, node, data) => {};export function parseDate(str, opts) {+ if (!str) return null;// possible shortcut: quickly dismiss if there isn't a number?const date = nextToken(str.trim(), dateTrie, { path: '' });if (date) {diff --git a/packages/base-sheets/src/Controller/CellEditorController.ts b/packages/base-sheets/src/Controller/CellEditorController.tsindex 2f777bfd63..a04f619ab3 100644--- a/packages/base-sheets/src/Controller/CellEditorController.ts+++ b/packages/base-sheets/src/Controller/CellEditorController.ts@@ -60,8 +60,8 @@ export class CellEditorController {// // focus// this.richTextEditEle.focus();- // this.richTextEditEle.tabIndex = 1;+ // this.focusEditEle();this.hideEditContainer();// init event@@ -279,11 +279,12 @@ export class CellEditorController {// }hideEditContainer() {- this.richTextEle.style.maxHeight = '0px';- this.richTextEle.style.maxWidth = '0px';+ // It cannot be set to 0px, otherwise the paste event cannot be listened+ this.richTextEle.style.maxHeight = '1px';+ this.richTextEle.style.maxWidth = '1px';- this.richTextEle.style.minWidth = `0px`;- this.richTextEle.style.minHeight = `0px`;+ this.richTextEle.style.minWidth = `1px`;+ this.richTextEle.style.minHeight = `1px`;this.richTextEle.style.borderWidth = '0px';}diff --git a/packages/common-plugin-register/src/Basic/Register/Clipboard/ClipboardExtensionFactory.ts b/packages/common-plugin-register/src/Basic/Register/Clipboard/ClipboardExtensionFactory.tsindex 175bd08088..aa28726f44 100644--- a/packages/common-plugin-register/src/Basic/Register/Clipboard/ClipboardExtensionFactory.ts+++ b/packages/common-plugin-register/src/Basic/Register/Clipboard/ClipboardExtensionFactory.ts@@ -4,20 +4,20 @@ import { IKeyValue, Plugin } from '@univer/core';/*** 转化table html/json为json*/-export class BaseClipboardExtension {- protected _json: IKeyValue;- constructor(protected _data: IClipboardData) {}+export class BaseClipboardExtension<T extends Plugin = Plugin> {+ // protected _json: IKeyValue;+ constructor(protected _data: IClipboardData, protected _plugin: T) {}- getJSON(): IKeyValue {- return this._json;- }+ // getJSON(): IKeyValue {+ // return this._json;+ // }- /**- * Modify the value- */- setJSON(json: IKeyValue) {- this._json = json;- }+ // /**+ // * Modify the value+ // */+ // setJSON(json: IKeyValue) {+ // this._json = json;+ // }/*** Execute the core logic after the check is successful@@ -41,7 +41,7 @@ export class BaseClipboardExtensionFactory<T extends Plugin = Plugin> {* @returns*/create(data: IClipboardData): BaseClipboardExtension {- return new BaseClipboardExtension(data);+ return new BaseClipboardExtension(data, this._plugin);}/**diff --git a/packages/common-plugin-register/src/Basic/Register/Clipboard/ClipboardExtensionManager.ts b/packages/common-plugin-register/src/Basic/Register/Clipboard/ClipboardExtensionManager.tsindex a4fc250b98..11d2c1502b 100644--- a/packages/common-plugin-register/src/Basic/Register/Clipboard/ClipboardExtensionManager.ts+++ b/packages/common-plugin-register/src/Basic/Register/Clipboard/ClipboardExtensionManager.ts@@ -32,7 +32,6 @@ export class ClipboardExtensionManager {const extension = this._checkExtension(data);if (extension) {extension.execute();- return extension.getJSON();}}diff --git a/packages/common-plugin-register/src/RegisterPlugin.tsx b/packages/common-plugin-register/src/RegisterPlugin.tsxindex 267263b2a2..2e33a51899 100644--- a/packages/common-plugin-register/src/RegisterPlugin.tsx+++ b/packages/common-plugin-register/src/RegisterPlugin.tsx@@ -38,8 +38,6 @@ export class RegisterPlugin extends Plugin {if (onKeyPasteObservable && !onKeyPasteObservable.hasObservers()) {onKeyPasteObservable.add((evt: ClipboardEvent) => {- console.log('cell edit onKeyPasteObservable====', evt);-this._clipboardExtensionManager.pasteResolver(evt).then((data: IClipboardData) => {this._clipboardExtensionManager.handle(data);});diff --git a/packages/sheets-plugin-clipboard-office/src/Basic/Register/ClipboardOfficeExtension.ts b/packages/sheets-plugin-clipboard-office/src/Basic/Register/ClipboardOfficeExtension.tsindex 69453d893c..7983bb756a 100644--- a/packages/sheets-plugin-clipboard-office/src/Basic/Register/ClipboardOfficeExtension.ts+++ b/packages/sheets-plugin-clipboard-office/src/Basic/Register/ClipboardOfficeExtension.ts@@ -7,7 +7,7 @@ export class ClipboardOfficeExtension extends BaseClipboardExtension {export class ClipboardOfficeExtensionFactory extends BaseClipboardExtensionFactory<ClipboardOfficePlugin> {get zIndex(): number {- return 1;+ return 2;}create(data: IClipboardData): BaseClipboardExtension {@@ -15,8 +15,6 @@ export class ClipboardOfficeExtensionFactory extends BaseClipboardExtensionFacto}check(data: IClipboardData): false | BaseClipboardExtension {- console.log('截获 office data', data);-return false;}}diff --git a/packages/sheets-plugin-clipboard/src/Basics/Register/ClipboardExtension.ts b/packages/sheets-plugin-clipboard/src/Basics/Register/ClipboardExtension.tsindex 476c78939f..8287a167bd 100644--- a/packages/sheets-plugin-clipboard/src/Basics/Register/ClipboardExtension.ts+++ b/packages/sheets-plugin-clipboard/src/Basics/Register/ClipboardExtension.ts@@ -1,8 +1,22 @@+import { handelTableToJson, handlePlainToJson } from '@univer/base-component';import { BaseClipboardExtension, BaseClipboardExtensionFactory, IClipboardData } from '@univer/common-plugin-register';import { ClipboardPlugin } from '../../ClipboardPlugin';-export class ClipboardExtension extends BaseClipboardExtension {- execute() {}+export class ClipboardExtension extends BaseClipboardExtension<ClipboardPlugin> {+ execute() {+ const content = this._data.html || this._data.plain;++ let data;+ if (content) {+ if (content.indexOf('<table') > -1 && content.indexOf('<td') > -1) {+ data = handelTableToJson(content);+ } else {+ data = handlePlainToJson(content);+ }+ }++ this._plugin.getUniverPaste().pasteTo(data);+ }}export class ClipboardExtensionFactory extends BaseClipboardExtensionFactory<ClipboardPlugin> {@@ -11,7 +25,7 @@ export class ClipboardExtensionFactory extends BaseClipboardExtensionFactory<Cli}create(data: IClipboardData): BaseClipboardExtension {- return new ClipboardExtension(data);+ return new ClipboardExtension(data, this._plugin);}check(data: IClipboardData): false | BaseClipboardExtension {diff --git a/packages/sheets-plugin-clipboard/src/ClipboardPlugin.tsx b/packages/sheets-plugin-clipboard/src/ClipboardPlugin.tsxindex 73ff938932..f1b428d585 100644--- a/packages/sheets-plugin-clipboard/src/ClipboardPlugin.tsx+++ b/packages/sheets-plugin-clipboard/src/ClipboardPlugin.tsx@@ -2,7 +2,7 @@ import { SheetContext, Plugin, UniverSheet } from '@univer/core';import { RegisterPlugin, REGISTER_PLUGIN_NAME } from '@univer/common-plugin-register';import { en, zh } from './Locale';import { CLIPBOARD_PLUGIN } from './Const';-import { Copy, UniverCopy, Paste, UniverPaste } from './Domain';+import { Copy, Paste, UniverCopy, UniverPaste } from './Domain';import { ClipboardExtensionFactory } from './Basics/Register/ClipboardExtension';interface CopyResolver {@@ -20,6 +20,8 @@ export class ClipboardPlugin extends Plugin<any, SheetContext> {private _pasteResolvers: PasteResolver[] = [];+ private _univerPaste: UniverPaste;+private _clipboardExtensionFactory: ClipboardExtensionFactory;constructor() {@@ -56,20 +58,25 @@ export class ClipboardPlugin extends Plugin<any, SheetContext> {en,zh,});- this.installPasteResolver({ name: 'univerPaste', resolver: new UniverPaste(context) });+ // this.installPasteResolver({ name: 'univerPaste', resolver: new UniverPaste(context) });+ this._univerPaste = new UniverPaste(context);this.installCopyResolver({ name: 'univerCopy', resolver: new UniverCopy(context) });this.initialize();}+ getUniverPaste() {+ return this._univerPaste;+ }+installCopyResolver(resolver: CopyResolver) {const index = this._copyResolvers.findIndex((item) => item.name === resolver.name);if (index > -1) return false;this._copyResolvers.push(resolver);}- installPasteResolver(resolver: PasteResolver) {- const index = this._pasteResolvers.findIndex((item) => item.name === resolver.name);- if (index > -1) return false;- this._pasteResolvers.push(resolver);- }+ // installPasteResolver(resolver: PasteResolver) {+ // const index = this._pasteResolvers.findIndex((item) => item.name === resolver.name);+ // if (index > -1) return false;+ // this._pasteResolvers.push(resolver);+ // }}diff --git a/packages/sheets-plugin-clipboard/src/Domain/Paste.ts b/packages/sheets-plugin-clipboard/src/Domain/Paste.tsindex 4790387304..0e2aa79e12 100644--- a/packages/sheets-plugin-clipboard/src/Domain/Paste.ts+++ b/packages/sheets-plugin-clipboard/src/Domain/Paste.ts@@ -1,8 +1,7 @@import { SelectionControl } from '@univer/base-sheets/src/Controller/Selection/SelectionController';import { SheetContext, PLUGIN_NAMES } from '@univer/core';import { RightMenuProps, SelectionModel, SheetPlugin } from '@univer/base-sheets';-import { handelTableToJson, handlePlainToJson, handleTableMergeData } from '@univer/base-component';-import { Clipboard } from './Clipboard';+import { handleTableMergeData } from '@univer/base-component';export interface PasteType {type: string;@@ -39,138 +38,139 @@ export class UniverPaste extends Paste {{locale: ['rightClick.paste'],onClick: () => {- this.pasteTo();+ // this.pasteTo();},},];super(context, pasteList);}- async pasteResolver(e?: ClipboardEvent) {- const file: Array<PasteType | null> | null = await Clipboard.read(e);- 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');- // let data: any[] = [];- // const content = document.createElement('DIV');+ // async pasteResolver(e?: ClipboardEvent) {+ // const file: Array<PasteType | null> | null = await Clipboard.read(e);+ // 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');+ // // let data: any[] = [];+ // // const content = document.createElement('DIV');- if (HtmlIndex > -1) {- const html = file[HtmlIndex]?.result as string;+ // if (HtmlIndex > -1) {+ // const html = file[HtmlIndex]?.result as string;- // content.innerHTML = html;- // data = new Array(content.querySelectorAll('table tr').length);- // if (!data.length) return [];- // let colLen = 0;- // const trs = content.querySelectorAll('table tr');- // const firstTds = trs[0].querySelectorAll('td');- // firstTds.forEach((item: HTMLTableCellElement) => {- // let colSpan = 0;- // const attr = item.getAttribute('colSpan');- // if (attr !== null) {- // colSpan = +attr;- // } else {- // colSpan = 1;- // }- // colLen += colSpan;- // });- // for (let i = 0; i < data.length; i++) {- // data[i] = new Array(colLen);- // }- // let r = 0;- // trs.forEach((item: any) => {- // let c = 0;- // item.querySelectorAll('td').forEach((td: HTMLTableCellElement) => {- // let cell: ICellData = {};- // if (td.querySelectorAll('span').length || td.querySelectorAll('font').length) {- // const spanStyle = handleDomToJson(td);- // if (typeof spanStyle !== 'string') {- // cell.p = spanStyle;- // }- // }- // let txt = td.innerText;- // if (txt.trim().length === 0) {- // cell.v = null;- // cell.m = null;- // } else {- // // Todo,处理格式- // cell.v = txt;- // cell.m = txt;- // }- // const style = handleStringToStyle(td);- // if (Tools.isPlainObject(style)) {- // cell.s = style;- // }- // while (c < colLen && data[r][c] != null) {- // c++;- // }- // if (c === colLen) {- // return;- // }- // if (data[r][c] == null) {- // data[r][c] = cell;- // let rowSpan = Number(td.getAttribute('rowSpan'));- // let colSpan = Number(td.getAttribute('colSpan'));- // if (Number.isNaN(rowSpan)) {- // rowSpan = 1;- // }- // if (Number.isNaN(colSpan)) {- // colSpan = 1;- // }- // if (rowSpan > 1 || colSpan > 1) {- // let first = { rs: rowSpan - 1, cs: colSpan - 1, r, c };- // data[r][c].mc = first;- // for (let rp = 0; rp < rowSpan; rp++) {- // for (let cp = 0; cp < colSpan; cp++) {- // if (rp === 0 && cp === 0) {- // continue;- // }- // data[r + rp][c + cp] = { mc: null };- // }- // }- // }- // }- // c++;- // });- // r++;- // });+ // // content.innerHTML = html;+ // // data = new Array(content.querySelectorAll('table tr').length);+ // // if (!data.length) return [];+ // // let colLen = 0;+ // // const trs = content.querySelectorAll('table tr');+ // // const firstTds = trs[0].querySelectorAll('td');+ // // firstTds.forEach((item: HTMLTableCellElement) => {+ // // let colSpan = 0;+ // // const attr = item.getAttribute('colSpan');+ // // if (attr !== null) {+ // // colSpan = +attr;+ // // } else {+ // // colSpan = 1;+ // // }+ // // colLen += colSpan;+ // // });+ // // for (let i = 0; i < data.length; i++) {+ // // data[i] = new Array(colLen);+ // // }+ // // let r = 0;+ // // trs.forEach((item: any) => {+ // // let c = 0;+ // // item.querySelectorAll('td').forEach((td: HTMLTableCellElement) => {+ // // let cell: ICellData = {};+ // // if (td.querySelectorAll('span').length || td.querySelectorAll('font').length) {+ // // const spanStyle = handleDomToJson(td);+ // // if (typeof spanStyle !== 'string') {+ // // cell.p = spanStyle;+ // // }+ // // }+ // // let txt = td.innerText;+ // // if (txt.trim().length === 0) {+ // // cell.v = null;+ // // cell.m = null;+ // // } else {+ // // // Todo,处理格式+ // // cell.v = txt;+ // // cell.m = txt;+ // // }+ // // const style = handleStringToStyle(td);+ // // if (Tools.isPlainObject(style)) {+ // // cell.s = style;+ // // }+ // // while (c < colLen && data[r][c] != null) {+ // // c++;+ // // }+ // // if (c === colLen) {+ // // return;+ // // }+ // // if (data[r][c] == null) {+ // // data[r][c] = cell;+ // // let rowSpan = Number(td.getAttribute('rowSpan'));+ // // let colSpan = Number(td.getAttribute('colSpan'));+ // // if (Number.isNaN(rowSpan)) {+ // // rowSpan = 1;+ // // }+ // // if (Number.isNaN(colSpan)) {+ // // colSpan = 1;+ // // }+ // // if (rowSpan > 1 || colSpan > 1) {+ // // let first = { rs: rowSpan - 1, cs: colSpan - 1, r, c };+ // // data[r][c].mc = first;+ // // for (let rp = 0; rp < rowSpan; rp++) {+ // // for (let cp = 0; cp < colSpan; cp++) {+ // // if (rp === 0 && cp === 0) {+ // // continue;+ // // }+ // // data[r + rp][c + cp] = { mc: null };+ // // }+ // // }+ // // }+ // // }+ // // c++;+ // // });+ // // r++;+ // // });- return handelTableToJson(html);- }- if (PlainIndex > -1) {- const html = file[PlainIndex]?.result as string;+ // return handelTableToJson(html);+ // }+ // if (PlainIndex > -1) {+ // const html = file[PlainIndex]?.result as string;- // content.innerHTML = html;- // let dataChe = html.replace(/\r/g, '');- // let che = dataChe.split('\n');- // let colCheLen = che[0].split('\t').length;- // for (let i = 0; i < che.length; i++) {- // if (che[i].split('\t').length < colCheLen) {- // continue;- // }- // data.push(che[i].split('\t'));- // }- // for (let i = 0; i < data.length; i++) {- // for (let j = 0; j < data[i].length; j++) {- // if (data[i][j].length) {- // data[i][j] = {- // v: data[i][j],- // m: data[i][j],- // };- // } else {- // data[i][j] = null;- // }- // }- // }+ // // content.innerHTML = html;+ // // let dataChe = html.replace(/\r/g, '');+ // // let che = dataChe.split('\n');+ // // let colCheLen = che[0].split('\t').length;+ // // for (let i = 0; i < che.length; i++) {+ // // if (che[i].split('\t').length < colCheLen) {+ // // continue;+ // // }+ // // data.push(che[i].split('\t'));+ // // }+ // // for (let i = 0; i < data.length; i++) {+ // // for (let j = 0; j < data[i].length; j++) {+ // // if (data[i][j].length) {+ // // data[i][j] = {+ // // v: data[i][j],+ // // m: data[i][j],+ // // };+ // // } else {+ // // data[i][j] = null;+ // // }+ // // }+ // // }- return handlePlainToJson(html);- }+ // return handlePlainToJson(html);+ // }- // return data;- }+ // // return data;+ // }- async pasteTo(e?: ClipboardEvent) {- const data = await this.pasteResolver(e);+ pasteTo(data) {+ // const data = await this.pasteResolver(e);// if (data.length === 0) return;+if (!data || data?.length === 0) return;const sheet = this.getContext().getWorkBook().getActiveSheet();if (!sheet) return;