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-ui): delete paragraph error on table (#4955)

ZhangWei205389258ac8
diff --git a/packages/core/src/docs/data-model/text-x/build-utils/paragraph.ts b/packages/core/src/docs/data-model/text-x/build-utils/paragraph.ts
index a6f33fa3da..3ba2774efc 100644
--- a/packages/core/src/docs/data-model/text-x/build-utils/paragraph.ts
+++ b/packages/core/src/docs/data-model/text-x/build-utils/paragraph.ts
@@ -319,6 +319,7 @@ export interface ISetParagraphStyleParams {
textX?: TextX;
}
+// eslint-disable-next-line max-lines-per-function
export const setParagraphStyle = (params: ISetParagraphStyleParams) => {
const {
textRanges,
@@ -330,8 +331,10 @@ export const setParagraphStyle = (params: ISetParagraphStyleParams) => {
deleteLen,
textX: _textX,
} = params;
- const paragraphs = docDataModel.getSelfOrHeaderFooterModel(segmentId).getBody()?.paragraphs ?? [];
- const currentParagraphs = getParagraphsInRanges(textRanges, paragraphs);
+ const segment = docDataModel.getSelfOrHeaderFooterModel(segmentId);
+ const paragraphs = segment.getBody()?.paragraphs ?? [];
+ const dataStream = segment.getBody()?.dataStream ?? '';
+ const currentParagraphs = getParagraphsInRanges(textRanges, paragraphs, dataStream);
const memoryCursor = new MemoryCursor();
if (cursor) {
memoryCursor.moveCursorTo(cursor);
diff --git a/packages/core/src/docs/data-model/text-x/build-utils/selection.ts b/packages/core/src/docs/data-model/text-x/build-utils/selection.ts
index b3608862ec..024b584d0b 100644
--- a/packages/core/src/docs/data-model/text-x/build-utils/selection.ts
+++ b/packages/core/src/docs/data-model/text-x/build-utils/selection.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
-import type { Nullable } from '../../../../shared';
import type { ITextRange } from '../../../../sheets/typedef';
import type { IParagraph, IParagraphRange } from '../../../../types/interfaces';
+import { DataStreamTreeTokenType } from '../../types';
export function makeSelection(startOffset: number, endOffset?: number): ITextRange {
if (typeof endOffset === 'undefined') {
@@ -46,29 +46,20 @@ export function isSegmentIntersects(start: number, end: number, start2: number,
return Math.max(start, start2) <= Math.min(end, end2);
}
-export function getParagraphsInRange(activeRange: ITextRange, paragraphs: IParagraph[]) {
+export function getParagraphsInRange(activeRange: ITextRange, paragraphs: IParagraph[], dataStream: string, paragraphRanges?: IParagraphRange[]) {
const { startOffset, endOffset } = activeRange;
+ const fullParagraphs = paragraphRanges ?? transformParagraphs(paragraphs, dataStream);
const results: IParagraphRange[] = [];
let start = -1;
- for (let i = 0; i < paragraphs.length; i++) {
- const paragraph = paragraphs[i];
- const prevParagraph: Nullable<IParagraph> = paragraphs[i - 1];
+ for (let i = 0; i < fullParagraphs.length; i++) {
+ const paragraph = fullParagraphs[i];
const { startIndex } = paragraph;
-
if ((startOffset > start && startOffset <= startIndex) || (endOffset > start && endOffset <= startIndex)) {
- results.push({
- ...paragraph,
- paragraphStart: (prevParagraph?.startIndex ?? -1) + 1,
- paragraphEnd: paragraph.startIndex,
- });
+ results.push(paragraph);
} else if (startIndex >= startOffset && startIndex <= endOffset) {
- results.push({
- ...paragraph,
- paragraphStart: (prevParagraph?.startIndex ?? -1) + 1,
- paragraphEnd: paragraph.startIndex,
- });
+ results.push(paragraph);
}
start = startIndex;
@@ -77,11 +68,11 @@ export function getParagraphsInRange(activeRange: ITextRange, paragraphs: IParag
return results;
}
-export function getParagraphsInRanges(ranges: readonly ITextRange[], paragraphs: IParagraph[]) {
+export function getParagraphsInRanges(ranges: readonly ITextRange[], paragraphs: IParagraph[], dataStream: string) {
const results: IParagraphRange[] = [];
-
+ const fullParagraphs = transformParagraphs(paragraphs, dataStream);
for (const range of ranges) {
- const ps = getParagraphsInRange(range, paragraphs);
+ const ps = getParagraphsInRange(range, paragraphs, dataStream, fullParagraphs);
results.push(...ps);
}
@@ -89,23 +80,33 @@ export function getParagraphsInRanges(ranges: readonly ITextRange[], paragraphs:
return results;
}
-export function transformParagraphs(paragraphs: IParagraph[]) {
- const results: IParagraphRange[] = [];
+const NOT_PARAGRAPH_TOKENS: string[] = [
+ DataStreamTreeTokenType.PARAGRAPH,
+ DataStreamTreeTokenType.TABLE_START,
+ DataStreamTreeTokenType.TABLE_END,
+ DataStreamTreeTokenType.TABLE_ROW_START,
+ DataStreamTreeTokenType.TABLE_CELL_START,
+ DataStreamTreeTokenType.TABLE_CELL_END,
- let start = -1;
+];
+export function transformParagraphs(paragraphs: IParagraph[], dataStream: string) {
+ const results: IParagraphRange[] = [];
for (let i = 0; i < paragraphs.length; i++) {
const paragraph = paragraphs[i];
- const prevParagraph: Nullable<IParagraph> = paragraphs[i - 1];
const { startIndex } = paragraph;
+ let j = startIndex - 1;
+
+ while (!NOT_PARAGRAPH_TOKENS.includes(dataStream[j]) && j >= 0) {
+ j--;
+ }
+
results.push({
...paragraph,
- paragraphStart: (prevParagraph?.startIndex ?? -1) + 1,
+ paragraphStart: j + 1,
paragraphEnd: paragraph.startIndex,
});
-
- start = startIndex;
}
return results;
diff --git a/packages/docs-ui/src/commands/commands/doc-delete.command.ts b/packages/docs-ui/src/commands/commands/doc-delete.command.ts
index 9285ef2905..f5de72bee6 100644
--- a/packages/docs-ui/src/commands/commands/doc-delete.command.ts
+++ b/packages/docs-ui/src/commands/commands/doc-delete.command.ts
@@ -39,6 +39,7 @@ import { DeleteDirection } from '../../types/delete-direction';
import { getCommandSkeleton, getRichTextEditPath } from '../util';
import { CutContentCommand } from './clipboard.inner.command';
import { DeleteCommand, UpdateCommand } from './core-editing.command';
+import { getCurrentParagraph } from './util';
export interface IDeleteCustomBlockParams {
direction: DeleteDirection;
@@ -805,7 +806,6 @@ export const DeleteCurrentParagraphCommand: ICommand = {
id: 'doc.command.delete-current-paragraph',
type: CommandType.COMMAND,
handler: async (accessor) => {
- const docSelectionManagerService = accessor.get(DocSelectionManagerService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const commandService = accessor.get(ICommandService);
const docDataModel = univerInstanceService.getCurrentUnitOfType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);
@@ -813,16 +813,9 @@ export const DeleteCurrentParagraphCommand: ICommand = {
return false;
}
- const activeRange = docSelectionManagerService.getActiveTextRange();
- if (!activeRange) {
- return false;
- }
- const range = docSelectionManagerService.getActiveTextRange();
- if (!range || !range.collapsed || range.segmentId) {
- return false;
- }
+ const dataStream = docDataModel.getBody()?.dataStream ?? '';
+ const paragraph = getCurrentParagraph(accessor);
- const paragraph = BuildTextUtils.range.getParagraphsInRange(range, docDataModel.getBody()?.paragraphs ?? [])[0];
if (!paragraph) {
return false;
}
@@ -830,7 +823,9 @@ export const DeleteCurrentParagraphCommand: ICommand = {
const actions = BuildTextUtils.selection.delete(
[{
startOffset: paragraph.paragraphStart,
- endOffset: paragraph.paragraphEnd + 1,
+ endOffset: dataStream[paragraph.paragraphEnd + 1] === '\n'
+ ? paragraph.paragraphEnd
+ : paragraph.paragraphEnd + 1,
collapsed: false,
}],
docDataModel.getBody()!,
diff --git a/packages/docs-ui/src/commands/commands/doc-paragraph-setting.command.ts b/packages/docs-ui/src/commands/commands/doc-paragraph-setting.command.ts
index 57a266d618..cbf340fce4 100644
--- a/packages/docs-ui/src/commands/commands/doc-paragraph-setting.command.ts
+++ b/packages/docs-ui/src/commands/commands/doc-paragraph-setting.command.ts
@@ -44,8 +44,10 @@ export const DocParagraphSettingCommand: ICommand<IDocParagraphSettingCommandPar
const unitId = docDataModel.getUnitId();
- const allParagraphs = docDataModel.getSelfOrHeaderFooterModel(segmentId).getBody()?.paragraphs ?? [];
- const paragraphs = BuildTextUtils.range.getParagraphsInRanges(docRanges, allParagraphs) ?? [];
+ const segment = docDataModel.getSelfOrHeaderFooterModel(segmentId);
+ const allParagraphs = segment.getBody()?.paragraphs ?? [];
+ const dataStream = segment.getBody()?.dataStream ?? '';
+ const paragraphs = BuildTextUtils.range.getParagraphsInRanges(docRanges, allParagraphs, dataStream) ?? [];
const doMutation: IMutationInfo<IRichTextEditingMutationParams> = {
id: RichTextEditingMutation.id,
diff --git a/packages/docs-ui/src/commands/commands/insert-below/insert-bullet-below.command.ts b/packages/docs-ui/src/commands/commands/insert-below/insert-bullet-below.command.ts
index 457a4398de..b7e4cd542d 100644
--- a/packages/docs-ui/src/commands/commands/insert-below/insert-bullet-below.command.ts
+++ b/packages/docs-ui/src/commands/commands/insert-below/insert-bullet-below.command.ts
@@ -45,8 +45,10 @@ export const InsertBulletBelowCommand: ICommand<IInsertBulletBelowCommandParams>
if (!(textRanges?.length === 1 && textRanges[0].startOffset === textRanges[0].endOffset)) {
return false;
}
- const paragraphs = docDataModel.getSelfOrHeaderFooterModel(textRanges[0].segmentId).getBody()?.paragraphs ?? [];
- const currentParagraph = BuildTextUtils.range.getParagraphsInRange(textRanges[0], paragraphs)[0];
+ const segment = docDataModel.getSelfOrHeaderFooterModel(textRanges[0].segmentId);
+ const paragraphs = segment.getBody()?.paragraphs ?? [];
+ const dataStream = segment.getBody()?.dataStream ?? '';
+ const currentParagraph = BuildTextUtils.range.getParagraphsInRange(textRanges[0], paragraphs, dataStream)[0];
if (!currentParagraph) {
return false;
}
diff --git a/packages/docs-ui/src/commands/commands/list.command.ts b/packages/docs-ui/src/commands/commands/list.command.ts
index 19d49b6fa3..008e5b04da 100644
--- a/packages/docs-ui/src/commands/commands/list.command.ts
+++ b/packages/docs-ui/src/commands/commands/list.command.ts
@@ -63,13 +63,15 @@ export const ListOperationCommand: ICommand<IListOperationCommandParams> = {
const segmentId = docRanges[0].segmentId;
- const paragraphs = docDataModel.getSelfOrHeaderFooterModel(segmentId).getBody()?.paragraphs;
+ const segment = docDataModel.getSelfOrHeaderFooterModel(segmentId);
+ const paragraphs = segment.getBody()?.paragraphs ?? [];
+ const dataStream = segment.getBody()?.dataStream ?? '';
- if (paragraphs == null) {
+ if (!paragraphs.length) {
return false;
}
- const currentParagraphs = BuildTextUtils.range.getParagraphsInRanges(docRanges, paragraphs);
+ const currentParagraphs = BuildTextUtils.range.getParagraphsInRanges(docRanges, paragraphs, dataStream);
const unitId = docDataModel.getUnitId();
@@ -125,13 +127,15 @@ export const ChangeListTypeCommand: ICommand<IChangeListTypeCommandParams> = {
}
const { segmentId } = activeRanges[0];
- const paragraphs = docDataModel.getSelfOrHeaderFooterModel(segmentId).getBody()?.paragraphs;
+ const segment = docDataModel.getSelfOrHeaderFooterModel(segmentId);
+ const paragraphs = segment.getBody()?.paragraphs ?? [];
+ const dataStream = segment.getBody()?.dataStream ?? '';
- if (paragraphs == null) {
+ if (!paragraphs.length) {
return false;
}
- const currentParagraphs = BuildTextUtils.range.getParagraphsInRanges(activeRanges, paragraphs);
+ const currentParagraphs = BuildTextUtils.range.getParagraphsInRanges(activeRanges, paragraphs, dataStream);
const unitId = docDataModel.getUnitId();
const textX = BuildTextUtils.paragraph.bullet.set({
@@ -195,13 +199,15 @@ export const ChangeListNestingLevelCommand: ICommand<IChangeListNestingLevelComm
const { segmentId } = activeRange;
const selections = docSelectionManagerService.getDocRanges() ?? [];
- const paragraphs = docDataModel.getSelfOrHeaderFooterModel(segmentId).getBody()?.paragraphs;
+ const segment = docDataModel.getSelfOrHeaderFooterModel(segmentId);
+ const paragraphs = segment.getBody()?.paragraphs ?? [];
+ const dataStream = segment.getBody()?.dataStream ?? '';
- if (paragraphs == null) {
+ if (!paragraphs.length) {
return false;
}
- const currentParagraphs = BuildTextUtils.range.getParagraphsInRange(activeRange, paragraphs);
+ const currentParagraphs = BuildTextUtils.range.getParagraphsInRange(activeRange, paragraphs, dataStream);
const unitId = docDataModel.getUnitId();
const jsonX = JSONX.getInstance();
@@ -569,8 +575,8 @@ export const InsertCheckListBellowCommand: ICommand<IQuickListCommandParams> = {
},
};
-export function getParagraphsRelative(ranges: ITextRangeWithStyle[], paragraphs: IParagraph[]) {
- const selectionParagraphs: IParagraph[] = BuildTextUtils.range.getParagraphsInRanges(ranges, paragraphs);
+export function getParagraphsRelative(ranges: ITextRangeWithStyle[], paragraphs: IParagraph[], dataStream: string) {
+ const selectionParagraphs: IParagraph[] = BuildTextUtils.range.getParagraphsInRanges(ranges, paragraphs, dataStream);
const startIndex = paragraphs.indexOf(selectionParagraphs[0]);
const endIndex = paragraphs.indexOf(selectionParagraphs[selectionParagraphs.length - 1]);
if (selectionParagraphs[0].bullet) {
diff --git a/packages/docs-ui/src/commands/commands/paragraph-align.command.ts b/packages/docs-ui/src/commands/commands/paragraph-align.command.ts
index 72c03f997d..e2627110fa 100644
--- a/packages/docs-ui/src/commands/commands/paragraph-align.command.ts
+++ b/packages/docs-ui/src/commands/commands/paragraph-align.command.ts
@@ -60,14 +60,15 @@ export const AlignOperationCommand: ICommand<IAlignOperationCommandParams> = {
}
const segmentId = allRanges[0].segmentId;
-
- const paragraphs = docDataModel.getSelfOrHeaderFooterModel(segmentId).getBody()?.paragraphs;
+ const segment = docDataModel.getSelfOrHeaderFooterModel(segmentId);
+ const paragraphs = segment.getBody()?.paragraphs ?? [];
+ const dataStream = segment.getBody()?.dataStream ?? '';
if (paragraphs == null) {
return false;
}
- const currentParagraphs = BuildTextUtils.range.getParagraphsInRanges(allRanges, paragraphs);
+ const currentParagraphs = BuildTextUtils.range.getParagraphsInRanges(allRanges, paragraphs, dataStream);
const unitId = docDataModel.getUnitId();
const isAlreadyAligned = currentParagraphs.every((paragraph) => paragraph.paragraphStyle?.horizontalAlign === alignType);
diff --git a/packages/docs-ui/src/commands/commands/set-heading.command.ts b/packages/docs-ui/src/commands/commands/set-heading.command.ts
index 084939d76b..357ce2a743 100644
--- a/packages/docs-ui/src/commands/commands/set-heading.command.ts
+++ b/packages/docs-ui/src/commands/commands/set-heading.command.ts
@@ -97,7 +97,10 @@ export const QuickHeadingCommand: ICommand<ISetParagraphNamedStyleCommandParams>
}
const { segmentId, startOffset } = activeRange;
- const paragraph = BuildTextUtils.paragraph.util.getParagraphsInRange(activeRange, docDataModel.getBody()?.paragraphs ?? [])[0];
+ const segment = docDataModel.getSelfOrHeaderFooterModel(segmentId);
+ const paragraphs = segment.getBody()?.paragraphs ?? [];
+ const dataStream = segment.getBody()?.dataStream ?? '';
+ const paragraph = BuildTextUtils.paragraph.util.getParagraphsInRange(activeRange, paragraphs, dataStream)[0];
if (!paragraph) {
return false;
}
diff --git a/packages/docs-ui/src/commands/commands/util.ts b/packages/docs-ui/src/commands/commands/util.ts
index a75f846380..8d5ac8e1e2 100644
--- a/packages/docs-ui/src/commands/commands/util.ts
+++ b/packages/docs-ui/src/commands/commands/util.ts
@@ -27,6 +27,6 @@ export function getCurrentParagraph(accessor: IAccessor) {
return false;
}
- const paragraph = BuildTextUtils.range.getParagraphsInRange(range, doc.getBody()?.paragraphs ?? [])[0];
+ const paragraph = BuildTextUtils.range.getParagraphsInRange(range, doc.getBody()?.paragraphs ?? [], doc.getBody()?.dataStream ?? '')[0];
return paragraph;
}
diff --git a/packages/docs-ui/src/controllers/menu/menu.ts b/packages/docs-ui/src/controllers/menu/menu.ts
index 6ce4830697..e76df1a338 100644
--- a/packages/docs-ui/src/controllers/menu/menu.ts
+++ b/packages/docs-ui/src/controllers/menu/menu.ts
@@ -859,7 +859,8 @@ const listValueFactory$ = (accessor: IAccessor) => {
if (range) {
const doc = docDataModel.getSelfOrHeaderFooterModel(range?.segmentId);
- const paragraphs = BuildTextUtils.range.getParagraphsInRange(range, doc.getBody()?.paragraphs ?? []);
+
+ const paragraphs = BuildTextUtils.range.getParagraphsInRange(range, doc.getBody()?.paragraphs ?? [], doc.getBody()?.dataStream ?? '');
let listType: string | undefined;
if (paragraphs.every((p) => {
if (!listType) {
diff --git a/packages/docs-ui/src/services/doc-auto-format.service.ts b/packages/docs-ui/src/services/doc-auto-format.service.ts
index 12bb3bb949..c4195b33b4 100644
--- a/packages/docs-ui/src/services/doc-auto-format.service.ts
+++ b/packages/docs-ui/src/services/doc-auto-format.service.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { DocumentDataModel, ICommandInfo, ICustomRange, IDisposable, IParagraph, IParagraphRange, ITextRange, Nullable } from '@univerjs/core';
+import type { DocumentDataModel, ICommandInfo, ICustomRange, IDisposable, IParagraphRange, Nullable } from '@univerjs/core';
import type { ITextRangeWithStyle } from '@univerjs/engine-render';
import { BuildTextUtils, Disposable, Inject, IUniverInstanceService, toDisposable, UniverInstanceType } from '@univerjs/core';
import { DocSelectionManagerService } from '@univerjs/docs';
@@ -46,37 +46,6 @@ export interface IAutoFormat {
priority?: number;
}
-function getParagraphsInRange(activeRange: ITextRange, paragraphs: IParagraph[]) {
- const { startOffset, endOffset } = activeRange;
- const results: IParagraphRange[] = [];
-
- let start = -1;
-
- for (let i = 0; i < paragraphs.length; i++) {
- const paragraph = paragraphs[i];
- const prevParagraph: Nullable<IParagraph> = paragraphs[i - 1];
- const { startIndex } = paragraph;
-
- if ((startOffset > start && startOffset <= startIndex) || (endOffset > start && endOffset <= startIndex)) {
- results.push({
- ...paragraph,
- paragraphStart: (prevParagraph?.startIndex ?? -1) + 1,
- paragraphEnd: paragraph.startIndex,
- });
- } else if (startIndex >= startOffset && startIndex <= endOffset) {
- results.push({
- ...paragraph,
- paragraphStart: (prevParagraph?.startIndex ?? -1) + 1,
- paragraphEnd: paragraph.startIndex,
- });
- }
-
- start = startIndex;
- }
-
- return results;
-}
-
/**
* service for auto-formatting, handle shortcut like `Space` or `Tab`.
*/
@@ -121,7 +90,7 @@ export class DocAutoFormatService extends Disposable {
unit: doc,
selection,
isBody: !selection.segmentId,
- paragraphs: getParagraphsInRange(selection, doc.getBody()?.paragraphs ?? []),
+ paragraphs: BuildTextUtils.range.getParagraphsInRange(selection, doc.getBody()?.paragraphs ?? [], doc.getBody()?.dataStream ?? ''),
customRanges: BuildTextUtils.customRange.getCustomRangesInterestsWithSelection(selection, doc.getBody()?.customRanges ?? []),
commandId: id,
commandParams: params,
diff --git a/packages/docs-ui/src/views/paragraph-setting/hook/utils.ts b/packages/docs-ui/src/views/paragraph-setting/hook/utils.ts
index 6256625062..0530deead6 100644
--- a/packages/docs-ui/src/views/paragraph-setting/hook/utils.ts
+++ b/packages/docs-ui/src/views/paragraph-setting/hook/utils.ts
@@ -58,8 +58,10 @@ export const useCurrentParagraph = () => {
const segmentId = docRanges[0].segmentId;
- const paragraphs = docDataModel.getSelfOrHeaderFooterModel(segmentId).getBody()?.paragraphs ?? [];
- const currentParagraphs = BuildTextUtils.range.getParagraphsInRanges(docRanges, paragraphs) ?? [];
+ const segment = docDataModel.getSelfOrHeaderFooterModel(segmentId);
+ const paragraphs = segment.getBody()?.paragraphs ?? [];
+ const dataStream = segment.getBody()?.dataStream ?? '';
+ const currentParagraphs = BuildTextUtils.range.getParagraphsInRanges(docRanges, paragraphs, dataStream) ?? [];
return currentParagraphs;
};