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(docs): merge two paragraphs (#3871)

Ran Luocf12b9866080
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 46178e7a7b..e55910ea77 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<IMergeTwoParagraphParams> = {
+
id: 'doc.command.merge-two-paragraph',
type: CommandType.COMMAND,
@@ -159,24 +161,37 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {
return false;
}
const { segmentId, style } = activeRange;
- const docDataModel = univerInstanceService.getCurrentUniverDocInstance();
+ const docDataModel = univerInstanceService.getCurrentUnitForType<DocumentDataModel>(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<IMergeTwoParagraphParams> = {
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<IMergeTwoParagraphParams> = {
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<Nullable<ITextRangeWith
// Handle BACKSPACE key.
export const DeleteLeftCommand: ICommand = {
id: 'doc.command.delete-left',
+
type: CommandType.COMMAND,
// eslint-disable-next-line max-lines-per-function, complexity
handler: async (accessor) => {
@@ -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 0bc9de828f..eb7afd127a 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 d9e3228c22..da4f915435 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 a56be9ea7a..6e5ec405b5 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 0568e12cd7..1dd3e98c19 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 e1ec8d297b..3eaa677c6f 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 202fa87cc4..da83b33f62 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 = {