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(doc): inline format when cross two paragraphs (#408)

Ran Luo4a8d8a2c1366
diff --git a/packages/base-docs/src/commands/commands/__tests__/create-command-test-bed.ts b/packages/base-docs/src/commands/commands/__tests__/create-command-test-bed.ts
index 00093332a0..8b7acc825a 100644
--- a/packages/base-docs/src/commands/commands/__tests__/create-command-test-bed.ts
+++ b/packages/base-docs/src/commands/commands/__tests__/create-command-test-bed.ts
@@ -14,11 +14,11 @@ import { TextSelectionManagerService } from '../../../services/text-selection-ma
const TEST_DOCUMENT_DATA_EN: IDocumentData = {
id: 'test-doc',
body: {
- dataStream: 'What’s New in the 2022 Gartner Hype Cycle for Emerging Technologies\r\n',
+ dataStream: 'What’s New in the 2022\r Gartner Hype Cycle for Emerging Technologies\r\n',
textRuns: [
{
st: 0,
- ed: 67,
+ ed: 22,
ts: {
bl: BooleanNumber.FALSE,
fs: 24,
@@ -27,8 +27,30 @@ const TEST_DOCUMENT_DATA_EN: IDocumentData = {
},
},
},
+ {
+ st: 23,
+ ed: 68,
+ ts: {
+ bl: BooleanNumber.TRUE,
+ fs: 24,
+ cl: {
+ rgb: 'rgb(0, 40, 86)',
+ },
+ },
+ },
+ ],
+ paragraphs: [
+ {
+ startIndex: 22,
+ },
+ {
+ startIndex: 68,
+ paragraphStyle: {
+ spaceAbove: 20,
+ indentFirstLine: 20,
+ },
+ },
],
- paragraphs: [],
sectionBreaks: [],
customBlocks: [],
},
diff --git a/packages/base-docs/src/commands/commands/__tests__/inline-format.command.spec.ts b/packages/base-docs/src/commands/commands/__tests__/inline-format.command.spec.ts
index 4c7e04c95e..355cf51ed4 100644
--- a/packages/base-docs/src/commands/commands/__tests__/inline-format.command.spec.ts
+++ b/packages/base-docs/src/commands/commands/__tests__/inline-format.command.spec.ts
@@ -74,6 +74,15 @@ describe('Test inline format commands', () => {
segmentId: '',
},
]);
+
+ selectionManager.add([
+ {
+ startOffset: 20,
+ endOffset: 30,
+ collapsed: false,
+ segmentId: '',
+ },
+ ]);
});
afterEach(() => univer.dispose());
@@ -81,6 +90,8 @@ describe('Test inline format commands', () => {
describe('Set Bold by SetInlineFormatCommand', () => {
it('Should change text in range(0, 5) to bold', async () => {
expect(getFormatValueAt('bl', 1)).toBe(BooleanNumber.FALSE);
+ expect(getFormatValueAt('bl', 21)).toBe(BooleanNumber.FALSE);
+ expect(getFormatValueAt('bl', 25)).toBe(BooleanNumber.TRUE);
const commandParams = {
segmentId: '',
@@ -90,12 +101,18 @@ describe('Test inline format commands', () => {
await commandService.executeCommand(SetInlineFormatCommand.id, commandParams);
expect(getFormatValueAt('bl', 1)).toBe(BooleanNumber.TRUE);
+ expect(getFormatValueAt('bl', 21)).toBe(BooleanNumber.TRUE);
+ expect(getFormatValueAt('bl', 25)).toBe(BooleanNumber.TRUE);
await commandService.executeCommand(UndoCommand.id);
expect(getFormatValueAt('bl', 1)).toBe(BooleanNumber.FALSE);
+ expect(getFormatValueAt('bl', 21)).toBe(BooleanNumber.FALSE);
+ expect(getFormatValueAt('bl', 25)).toBe(BooleanNumber.TRUE);
await commandService.executeCommand(RedoCommand.id);
expect(getFormatValueAt('bl', 1)).toBe(BooleanNumber.TRUE);
+ expect(getFormatValueAt('bl', 21)).toBe(BooleanNumber.TRUE);
+ expect(getFormatValueAt('bl', 25)).toBe(BooleanNumber.TRUE);
});
});
diff --git a/packages/base-docs/src/commands/mutations/core-editing.mutation.ts b/packages/base-docs/src/commands/mutations/core-editing.mutation.ts
index 023e9afe93..452361ec6c 100644
--- a/packages/base-docs/src/commands/mutations/core-editing.mutation.ts
+++ b/packages/base-docs/src/commands/mutations/core-editing.mutation.ts
@@ -71,8 +71,6 @@ export const RichTextEditingMutation: IMutation<IRichTextEditingMutationParams,
memoryCursor.reset();
- console.log(JSON.stringify(mutations, null, 2));
-
mutations.forEach((mutation) => {
// FIXME: @jocs Since UpdateAttributeApply modifies the mutation(used in undo/redo),
// so make a deep copy here, does UpdateAttributeApply need to
diff --git a/packages/base-docs/src/commands/mutations/functions/common.ts b/packages/base-docs/src/commands/mutations/functions/common.ts
index 7d05191923..09078850d1 100644
--- a/packages/base-docs/src/commands/mutations/functions/common.ts
+++ b/packages/base-docs/src/commands/mutations/functions/common.ts
@@ -37,8 +37,12 @@ export function insertTextRuns(
const textRun = textRuns[i];
const { st, ed } = textRun;
- if (st > currentIndex) {
- textRun.st += textLength;
+ if (st >= currentIndex) {
+ // Handles the insertion of text at the beginning of a paragraph
+ if (st === currentIndex && insertBody.textRuns) {
+ textRun.st += textLength;
+ }
+
textRun.ed += textLength;
if (insertIndex === Infinity) {