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.

fix: copy paste between sheet and doc (#2993)

Ran Luo617986df9e46
diff --git a/packages/docs-ui/src/commands/commands/clipboard.command.ts b/packages/docs-ui/src/commands/commands/clipboard.command.ts
index 9529d6fa52..e2504bcf8f 100644
--- a/packages/docs-ui/src/commands/commands/clipboard.command.ts
+++ b/packages/docs-ui/src/commands/commands/clipboard.command.ts
@@ -23,6 +23,10 @@ export function whenDocOrEditor(contextService: IContextService): boolean {
return contextService.getContextValue(FOCUSING_DOC) || contextService.getContextValue(EDITOR_ACTIVATED);
}
+export function whenFocusEditor(contextService: IContextService): boolean {
+ return contextService.getContextValue(EDITOR_ACTIVATED);
+}
+
const DOC_CLIPBOARD_PRIORITY = 999;
// Commands here should have higher priority than commands of sheets
diff --git a/packages/docs-ui/src/controllers/clipboard.controller.ts b/packages/docs-ui/src/controllers/clipboard.controller.ts
index b94fe16aae..908fcc69cd 100644
--- a/packages/docs-ui/src/controllers/clipboard.controller.ts
+++ b/packages/docs-ui/src/controllers/clipboard.controller.ts
@@ -26,7 +26,7 @@ import { IClipboardInterfaceService } from '@univerjs/ui';
import { ITextSelectionRenderManager } from '@univerjs/engine-render';
import { takeUntil } from 'rxjs';
import { CutContentCommand, InnerPasteCommand } from '@univerjs/docs';
-import { DocCopyCommand, DocCutCommand, DocPasteCommand, whenDocOrEditor } from '../commands/commands/clipboard.command';
+import { DocCopyCommand, DocCutCommand, DocPasteCommand, whenDocOrEditor, whenFocusEditor } from '../commands/commands/clipboard.command';
import { IDocClipboardService } from '../services/clipboard/clipboard.service';
@OnLifecycle(LifecycleStages.Rendered, DocClipboardController)
@@ -58,9 +58,16 @@ export class DocClipboardController extends RxDisposable {
config!.event.preventDefault();
const clipboardEvent = config!.event as ClipboardEvent;
- const htmlContent = clipboardEvent.clipboardData?.getData('text/html');
+ let htmlContent = clipboardEvent.clipboardData?.getData('text/html');
const textContent = clipboardEvent.clipboardData?.getData('text/plain');
+ // TODO: @JOCS, work around to fix https://github.com/dream-num/univer-pro/issues/2006. and then when you paste it,
+ // you need to distinguish between different editors,
+ // because different editors have different pasting effects. For example, when editing a state, you can't paste a table
+ if (whenFocusEditor(this._contextService) && (htmlContent ?? '').indexOf('</table>') > -1) {
+ htmlContent = '';
+ }
+
this._docClipboardService.legacyPaste(htmlContent, textContent);
});
}
diff --git a/packages/docs-ui/src/services/clipboard/clipboard.service.ts b/packages/docs-ui/src/services/clipboard/clipboard.service.ts
index 3d41a08e2e..9eb89d1d1a 100644
--- a/packages/docs-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/docs-ui/src/services/clipboard/clipboard.service.ts
@@ -255,7 +255,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ
segmentId,
textRanges,
});
- } catch (_e) {
+ } catch (_) {
this._logService.error('[DocClipboardController]', 'clipboard is empty.');
return false;
}
diff --git a/packages/docs-ui/src/services/clipboard/html-to-udm/converter.ts b/packages/docs-ui/src/services/clipboard/html-to-udm/converter.ts
index 9adf7b4471..01efe1dd28 100644
--- a/packages/docs-ui/src/services/clipboard/html-to-udm/converter.ts
+++ b/packages/docs-ui/src/services/clipboard/html-to-udm/converter.ts
@@ -73,6 +73,9 @@ export class HtmlToUDMService {
const body: IDocumentBody = {
dataStream: '',
+ paragraphs: [],
+ sectionBreaks: [],
+ tables: [],
textRuns: [],
};
@@ -277,9 +280,18 @@ export class HtmlToUDMService {
}
case 'TD': {
+ if (body.dataStream[body.dataStream.length - 1] !== '\r') {
+ body.paragraphs?.push({
+ startIndex: body.dataStream.length,
+ });
+
+ body.dataStream += '\r';
+ }
+
body.sectionBreaks?.push({
startIndex: body.dataStream.length,
});
+
body.dataStream += `\n${DataStreamTreeTokenType.TABLE_CELL_END}`;
break;
diff --git a/packages/engine-render/src/components/sheets/sheet-skeleton.ts b/packages/engine-render/src/components/sheets/sheet-skeleton.ts
index 6c2f659c82..d7189353e3 100644
--- a/packages/engine-render/src/components/sheets/sheet-skeleton.ts
+++ b/packages/engine-render/src/components/sheets/sheet-skeleton.ts
@@ -1886,6 +1886,9 @@ export class SpreadsheetSkeleton extends Skeleton {
},
},
],
+ sectionBreaks: [{
+ startIndex: contentLength + 1,
+ }],
},
documentStyle: {
pageSize: {
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index 3b6e243576..b2bd2d25a2 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -475,6 +475,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
return res;
}
+ // eslint-disable-next-line max-lines-per-function
private async _pasteInternal(copyId: string, pasteType: string): Promise<boolean> {
// const target = this._getPastingTarget();
// const { selection, unitId, subUnitId } = target;
@@ -806,6 +807,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
* @param cellMatrix
* @param range
*/
+ // eslint-disable-next-line max-lines-per-function, complexity
private _transformPastedData(
rowCount: number,
colCount: number,
diff --git a/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx b/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx
index 265d983dd0..78b2c26297 100644
--- a/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx
+++ b/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx
@@ -58,13 +58,20 @@ export const EditorContainer: React.FC<ICellIEditorProps> = () => {
id: DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
body: {
dataStream: `${DEFAULT_EMPTY_DOCUMENT_VALUE}`,
+ tables: [],
textRuns: [],
paragraphs: [
{
startIndex: 0,
},
],
+ sectionBreaks: [
+ {
+ startIndex: 1,
+ },
+ ],
},
+ tableSource: {},
documentStyle: {
documentFlavor: DocumentFlavor.MODERN,
},
diff --git a/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx b/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
index 5b8cc886be..a8e10f2794 100644
--- a/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
+++ b/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { Nullable, Workbook } from '@univerjs/core';
+import type { IDocumentData, Nullable, Workbook } from '@univerjs/core';
import { BooleanNumber, DEFAULT_EMPTY_DOCUMENT_VALUE, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DocumentFlavor, HorizontalAlign, IPermissionService, IUniverInstanceService, Rectangle, ThemeService, UniverInstanceType, useDependency, VerticalAlign, WrapStrategy } from '@univerjs/core';
import { DeviceInputEventType } from '@univerjs/engine-render';
import { CheckMarkSingle, CloseSingle, DropdownSingle, FxSingle } from '@univerjs/icons';
@@ -101,17 +101,22 @@ export function FormulaBar() {
};
}, []);
- const INITIAL_SNAPSHOT = {
+ const INITIAL_SNAPSHOT: IDocumentData = {
id: DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
body: {
dataStream: `${DEFAULT_EMPTY_DOCUMENT_VALUE}`,
textRuns: [],
+ tables: [],
paragraphs: [
{
startIndex: 0,
},
],
+ sectionBreaks: [{
+ startIndex: 1,
+ }],
},
+ tableSource: {},
documentStyle: {
pageSize: {
width: Number.POSITIVE_INFINITY,
diff --git a/packages/sheets-zen-editor/src/controllers/zen-editor.controller.ts b/packages/sheets-zen-editor/src/controllers/zen-editor.controller.ts
index 99fa9fcad5..4ddc281e64 100644
--- a/packages/sheets-zen-editor/src/controllers/zen-editor.controller.ts
+++ b/packages/sheets-zen-editor/src/controllers/zen-editor.controller.ts
@@ -77,13 +77,18 @@ export class ZenEditorController extends RxDisposable {
body: {
dataStream: `${DEFAULT_EMPTY_DOCUMENT_VALUE}`,
textRuns: [],
+ tables: [],
customBlocks: [],
paragraphs: [
{
startIndex: 0,
},
],
+ sectionBreaks: [{
+ startIndex: 1,
+ }],
},
+ tableSource: {},
documentStyle: {
pageSize: {
width: 595,