Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: copy paste between sheet and doc #2993

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/docs-ui/src/commands/commands/clipboard.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions packages/docs-ui/src/controllers/clipboard.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ export class HtmlToUDMService {

const body: IDocumentBody = {
dataStream: '',
paragraphs: [],
sectionBreaks: [],
tables: [],
textRuns: [],
};

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,9 @@ export class SpreadsheetSkeleton extends Skeleton {
},
},
],
sectionBreaks: [{
startIndex: contentLength + 1,
}],
},
documentStyle: {
pageSize: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
9 changes: 7 additions & 2 deletions packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading