From d3085e284850f5bedcdce90e43421d2de9dfdc90 Mon Sep 17 00:00:00 2001 From: zhangw Date: Fri, 15 Nov 2024 17:10:15 +0800 Subject: [PATCH] fix(doc-mention): doc mention error (#4077) --- .../src/commands/commands/doc-mention.command.ts | 7 +++++-- .../src/commands/commands/core-editing.command.ts | 12 ++++++++++-- packages/docs/src/utils/replace-selection-factory.ts | 4 +--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/docs-mention-ui/src/commands/commands/doc-mention.command.ts b/packages/docs-mention-ui/src/commands/commands/doc-mention.command.ts index b197c86ff8e..1e7c8dae9ee 100644 --- a/packages/docs-mention-ui/src/commands/commands/doc-mention.command.ts +++ b/packages/docs-mention-ui/src/commands/commands/doc-mention.command.ts @@ -40,6 +40,8 @@ export const AddDocMentionCommand: ICommand = { if (!activeRange) { return false; } + + const { extra, ...mentionConfig } = mention; const dataStream = ` @${mention.label} `; const body: IDocumentBody = { dataStream, @@ -50,7 +52,8 @@ export const AddDocMentionCommand: ICommand = { rangeType: CustomRangeType.MENTION, wholeEntity: true, properties: { - mention, + ...mentionConfig, + ...extra, }, }], }; @@ -69,7 +72,7 @@ export const AddDocMentionCommand: ICommand = { ); if (doMutation) { - return commandService.executeCommand(doMutation.id, doMutation.params); + return commandService.syncExecuteCommand(doMutation.id, doMutation.params); } return false; diff --git a/packages/docs-ui/src/commands/commands/core-editing.command.ts b/packages/docs-ui/src/commands/commands/core-editing.command.ts index eef7ed2d031..f350c84859d 100644 --- a/packages/docs-ui/src/commands/commands/core-editing.command.ts +++ b/packages/docs-ui/src/commands/commands/core-editing.command.ts @@ -152,7 +152,15 @@ export const DeleteCommand: ICommand = { } const { startOffset } = range; - const start = direction === DeleteDirection.LEFT ? startOffset - len : startOffset; + let start = direction === DeleteDirection.LEFT ? startOffset - len : startOffset; + let end = direction === DeleteDirection.LEFT ? startOffset - 1 : startOffset + len - 1; + + const customRange = body.customRanges?.find((customRange) => customRange.startIndex <= start && customRange.endIndex >= end); + + if (customRange?.wholeEntity) { + start = customRange.startIndex; + end = Math.max(end, customRange.endIndex); + } const doMutation: IMutationInfo = { id: RichTextEditingMutation.id, @@ -179,7 +187,7 @@ export const DeleteCommand: ICommand = { textX.push({ t: TextXActionType.DELETE, - len, + len: end - start + 1, }); const path = getRichTextEditPath(docDataModel, segmentId); diff --git a/packages/docs/src/utils/replace-selection-factory.ts b/packages/docs/src/utils/replace-selection-factory.ts index 0bcc7d60676..7fbee96b803 100644 --- a/packages/docs/src/utils/replace-selection-factory.ts +++ b/packages/docs/src/utils/replace-selection-factory.ts @@ -20,7 +20,6 @@ import type { IRichTextEditingMutationParams } from '../commands/mutations/core- import { BuildTextUtils, IUniverInstanceService, JSONX } from '@univerjs/core'; import { RichTextEditingMutation } from '../commands/mutations/core-editing.mutation'; import { DocSelectionManagerService } from '../services/doc-selection-manager.service'; -import { getRichTextEditPath } from './custom-range-factory'; export interface IReplaceSelectionFactoryParams { unitId: string; @@ -89,7 +88,6 @@ export function replaceSelectionFactory(accessor: IAccessor, params: IReplaceSel }; const jsonX = JSONX.getInstance(); - const path = getRichTextEditPath(docDataModel, segmentId); - doMutation.params.actions = jsonX.editOp(textX.serialize(), path); + doMutation.params.actions = jsonX.editOp(textX.serialize()); return doMutation; }