From cf12b986608075b2a4fee78c97b0c3203f0bdb32 Mon Sep 17 00:00:00 2001 From: Ran Luo Date: Tue, 29 Oct 2024 10:55:50 +0800 Subject: [PATCH] fix(docs): merge two paragraphs (#3871) Co-authored-by: GitHub Actions --- ...elete.command.ts => doc-delete.command.ts} | 76 +++++++++++++------ .../docs-ui/src/controllers/menu.schema.ts | 2 +- .../src/controllers/menu/context-menu.ts | 2 +- packages/docs-ui/src/docs-ui-plugin.ts | 2 +- packages/docs-ui/src/index.ts | 4 +- .../services/clipboard/clipboard.service.ts | 2 +- .../src/shortcuts/core-editing.shortcut.ts | 4 +- 7 files changed, 59 insertions(+), 33 deletions(-) rename packages/docs-ui/src/commands/commands/{delete.command.ts => doc-delete.command.ts} (94%) diff --git a/packages/docs-ui/src/commands/commands/delete.command.ts b/packages/docs-ui/src/commands/commands/doc-delete.command.ts similarity index 94% rename from packages/docs-ui/src/commands/commands/delete.command.ts rename to packages/docs-ui/src/commands/commands/doc-delete.command.ts index 46178e7a7b2..e55910ea770 100644 --- a/packages/docs-ui/src/commands/commands/delete.command.ts +++ b/packages/docs-ui/src/commands/commands/doc-delete.command.ts @@ -14,6 +14,9 @@ * limitations under the License. */ +import type { DocumentDataModel, IAccessor, ICommand, ICustomBlock, IDocumentBody, IMutationInfo, IParagraph, ITextRange, ITextRun, JSONXActions, Nullable } from '@univerjs/core'; +import type { IRichTextEditingMutationParams } from '@univerjs/docs'; +import type { IRectRangeWithStyle, ITextRangeWithStyle } from '@univerjs/engine-render'; import { BuildTextUtils, CommandType, @@ -28,14 +31,12 @@ import { TextX, TextXActionType, Tools, + UniverInstanceType, UpdateDocsAttributeType, } from '@univerjs/core'; + import { DocSelectionManagerService, RichTextEditingMutation } from '@univerjs/docs'; import { getParagraphByGlyph, hasListGlyph, isFirstGlyph, isIndentByGlyph } from '@univerjs/engine-render'; -import type { IAccessor, ICommand, ICustomBlock, IDocumentBody, IMutationInfo, IParagraph, ITextRange, ITextRun, JSONXActions, Nullable } from '@univerjs/core'; - -import type { IRichTextEditingMutationParams } from '@univerjs/docs'; -import type { IRectRangeWithStyle, ITextRangeWithStyle } from '@univerjs/engine-render'; import { DeleteDirection } from '../../types/delete-direction'; import { getCommandSkeleton, getRichTextEditPath } from '../util'; import { CutContentCommand } from './clipboard.inner.command'; @@ -141,6 +142,7 @@ interface IMergeTwoParagraphParams { } export const MergeTwoParagraphCommand: ICommand = { + id: 'doc.command.merge-two-paragraph', type: CommandType.COMMAND, @@ -159,24 +161,37 @@ export const MergeTwoParagraphCommand: ICommand = { return false; } const { segmentId, style } = activeRange; - const docDataModel = univerInstanceService.getCurrentUniverDocInstance(); + const docDataModel = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_DOC); const originBody = docDataModel?.getSelfOrHeaderFooterModel(segmentId).getBody(); - if (!docDataModel || !originBody) { + if (docDataModel == null || originBody == null) { return false; } const actualRange = getDeleteSelection(activeRange, originBody); const unitId = docDataModel.getUnitId(); - const { startOffset, collapsed } = actualRange; + const { startOffset, collapsed } = actualRange; if (!collapsed) { return false; } const startIndex = direction === DeleteDirection.LEFT ? startOffset : startOffset + 1; - const endIndex = originBody.paragraphs! - .find((p) => p.startIndex >= startIndex)!.startIndex!; - const body = getParagraphBody(accessor, unitId, originBody, startIndex, endIndex); + + let curParagraph; + let nextParagraph; + + for (const paragraph of originBody.paragraphs!) { + if (paragraph.startIndex >= startIndex) { + nextParagraph = paragraph; + break; + } + curParagraph = paragraph; + } + + if (curParagraph == null || nextParagraph == null) { + return false; + } + const cursor = direction === DeleteDirection.LEFT ? startOffset - 1 : startOffset; const textRanges = [ @@ -200,18 +215,25 @@ export const MergeTwoParagraphCommand: ICommand = { const textX = new TextX(); const jsonX = JSONX.getInstance(); + if (curParagraph.startIndex > 0) { + textX.push({ + t: TextXActionType.RETAIN, + len: curParagraph.startIndex, + segmentId, + }); + } + textX.push({ - t: TextXActionType.RETAIN, - len: direction === DeleteDirection.LEFT ? startOffset - 1 : startOffset, + t: TextXActionType.DELETE, + len: 1, + line: 0, segmentId, }); - if (body.dataStream.length) { + if (nextParagraph.startIndex > curParagraph.startIndex + 1) { textX.push({ - t: TextXActionType.INSERT, - body, - len: body.dataStream.length, - line: 0, + t: TextXActionType.RETAIN, + len: nextParagraph.startIndex - curParagraph.startIndex - 1, segmentId, }); } @@ -220,13 +242,16 @@ export const MergeTwoParagraphCommand: ICommand = { t: TextXActionType.RETAIN, len: 1, segmentId, - }); - - textX.push({ - t: TextXActionType.DELETE, - len: endIndex + 1 - startIndex, - line: 0, - segmentId, + coverType: UpdateDocsAttributeType.REPLACE, + body: { + dataStream: '', + paragraphs: [ + { + ...Tools.deepClone(curParagraph), + startIndex: 0, + }, + ], + }, }); const path = getRichTextEditPath(docDataModel, segmentId); @@ -292,6 +317,7 @@ export function getCursorWhenDelete(textRanges: Readonly { @@ -302,7 +328,7 @@ export const DeleteLeftCommand: ICommand = { let result = true; const docDataModel = univerInstanceService.getCurrentUniverDocInstance(); - if (!docDataModel) { + if (docDataModel == null) { return false; } diff --git a/packages/docs-ui/src/controllers/menu.schema.ts b/packages/docs-ui/src/controllers/menu.schema.ts index 0bc9de828f1..eb7afd127ab 100644 --- a/packages/docs-ui/src/controllers/menu.schema.ts +++ b/packages/docs-ui/src/controllers/menu.schema.ts @@ -17,7 +17,7 @@ import type { MenuSchemaType } from '@univerjs/ui'; import { ContextMenuGroup, ContextMenuPosition, RibbonStartGroup } from '@univerjs/ui'; import { DocCopyCommand, DocCutCommand, DocPasteCommand } from '../commands/commands/clipboard.command'; -import { DeleteLeftCommand } from '../commands/commands/delete.command'; +import { DeleteLeftCommand } from '../commands/commands/doc-delete.command'; import { OpenHeaderFooterPanelCommand } from '../commands/commands/doc-header-footer.command'; import { ResetInlineFormatTextBackgroundColorCommand, SetInlineFormatBoldCommand, SetInlineFormatFontFamilyCommand, SetInlineFormatFontSizeCommand, SetInlineFormatItalicCommand, SetInlineFormatStrikethroughCommand, SetInlineFormatSubscriptCommand, SetInlineFormatSuperscriptCommand, SetInlineFormatTextBackgroundColorCommand, SetInlineFormatTextColorCommand, SetInlineFormatUnderlineCommand } from '../commands/commands/inline-format.command'; import { BulletListCommand, CheckListCommand, OrderListCommand } from '../commands/commands/list.command'; diff --git a/packages/docs-ui/src/controllers/menu/context-menu.ts b/packages/docs-ui/src/controllers/menu/context-menu.ts index d9e3228c22a..da4f9154356 100644 --- a/packages/docs-ui/src/controllers/menu/context-menu.ts +++ b/packages/docs-ui/src/controllers/menu/context-menu.ts @@ -22,7 +22,7 @@ import { DocSelectionManagerService } from '@univerjs/docs'; import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui'; import { combineLatest, Observable } from 'rxjs'; import { DocCopyCommand, DocCutCommand, DocPasteCommand } from '../../commands/commands/clipboard.command'; -import { DeleteLeftCommand } from '../../commands/commands/delete.command'; +import { DeleteLeftCommand } from '../../commands/commands/doc-delete.command'; import { DocTableDeleteColumnsCommand, DocTableDeleteRowsCommand, DocTableDeleteTableCommand } from '../../commands/commands/table/doc-table-delete.command'; import { DocTableInsertColumnLeftCommand, DocTableInsertColumnRightCommand, DocTableInsertRowAboveCommand, DocTableInsertRowBellowCommand } from '../../commands/commands/table/doc-table-insert.command'; import { DocParagraphSettingPanelOperation } from '../../commands/operations/doc-paragraph-setting-panel.operation'; diff --git a/packages/docs-ui/src/docs-ui-plugin.ts b/packages/docs-ui/src/docs-ui-plugin.ts index a56be9ea7af..6e5ec405b59 100644 --- a/packages/docs-ui/src/docs-ui-plugin.ts +++ b/packages/docs-ui/src/docs-ui-plugin.ts @@ -35,7 +35,7 @@ import { BreakLineCommand } from './commands/commands/break-line.command'; import { DocCopyCommand, DocCutCommand, DocPasteCommand } from './commands/commands/clipboard.command'; import { CutContentCommand, InnerPasteCommand } from './commands/commands/clipboard.inner.command'; import { DeleteCommand, InsertCommand, UpdateCommand } from './commands/commands/core-editing.command'; -import { DeleteCustomBlockCommand, DeleteLeftCommand, DeleteRightCommand, MergeTwoParagraphCommand } from './commands/commands/delete.command'; +import { DeleteCustomBlockCommand, DeleteLeftCommand, DeleteRightCommand, MergeTwoParagraphCommand } from './commands/commands/doc-delete.command'; import { CloseHeaderFooterCommand } from './commands/commands/doc-header-footer.command'; import { DocParagraphSettingCommand } from './commands/commands/doc-paragraph-setting.command'; import { IMEInputCommand } from './commands/commands/ime-input.command'; diff --git a/packages/docs-ui/src/index.ts b/packages/docs-ui/src/index.ts index 0568e12cd72..1dd3e98c194 100644 --- a/packages/docs-ui/src/index.ts +++ b/packages/docs-ui/src/index.ts @@ -77,8 +77,8 @@ export { type IUpdateCommandParams, UpdateCommand, } from './commands/commands/core-editing.command'; -export { DeleteCustomBlockCommand, DeleteLeftCommand, DeleteRightCommand, type IDeleteCustomBlockParams, MergeTwoParagraphCommand } from './commands/commands/delete.command'; -export { getCursorWhenDelete } from './commands/commands/delete.command'; +export { DeleteCustomBlockCommand, DeleteLeftCommand, DeleteRightCommand, type IDeleteCustomBlockParams, MergeTwoParagraphCommand } from './commands/commands/doc-delete.command'; +export { getCursorWhenDelete } from './commands/commands/doc-delete.command'; export { IMEInputCommand } from './commands/commands/ime-input.command'; export { diff --git a/packages/docs-ui/src/services/clipboard/clipboard.service.ts b/packages/docs-ui/src/services/clipboard/clipboard.service.ts index e1ec8d297b9..3eaa677c6f5 100644 --- a/packages/docs-ui/src/services/clipboard/clipboard.service.ts +++ b/packages/docs-ui/src/services/clipboard/clipboard.service.ts @@ -21,7 +21,7 @@ import { BuildTextUtils, createIdentifier, DataStreamTreeTokenType, Disposable, import { DocSelectionManagerService } from '@univerjs/docs'; import { HTML_CLIPBOARD_MIME_TYPE, IClipboardInterfaceService, PLAIN_TEXT_CLIPBOARD_MIME_TYPE } from '@univerjs/ui'; import { CutContentCommand, InnerPasteCommand } from '../../commands/commands/clipboard.inner.command'; -import { getCursorWhenDelete } from '../../commands/commands/delete.command'; +import { getCursorWhenDelete } from '../../commands/commands/doc-delete.command'; import { copyContentCache, extractId, genId } from './copy-content-cache'; import { HtmlToUDMService } from './html-to-udm/converter'; import PastePluginLark from './html-to-udm/paste-plugins/plugin-lark'; diff --git a/packages/docs-ui/src/shortcuts/core-editing.shortcut.ts b/packages/docs-ui/src/shortcuts/core-editing.shortcut.ts index 202fa87cc4b..da83b33f629 100644 --- a/packages/docs-ui/src/shortcuts/core-editing.shortcut.ts +++ b/packages/docs-ui/src/shortcuts/core-editing.shortcut.ts @@ -14,10 +14,10 @@ * limitations under the License. */ -import { KeyCode } from '@univerjs/ui'; import type { IShortcutItem } from '@univerjs/ui'; +import { KeyCode } from '@univerjs/ui'; import { EnterCommand } from '../commands/commands/auto-format.command'; -import { DeleteLeftCommand, DeleteRightCommand } from '../commands/commands/delete.command'; +import { DeleteLeftCommand, DeleteRightCommand } from '../commands/commands/doc-delete.command'; import { whenDocAndEditorFocused, whenDocAndEditorFocusedWithBreakLine } from './utils'; export const BreakLineShortcut: IShortcutItem = {