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: inline format support cursor (#2347)

Ran Luo94f21494fe0e
diff --git a/packages/core/src/docs/data-model/document-data-model.ts b/packages/core/src/docs/data-model/document-data-model.ts
index 010669af2e..a9d7daa995 100644
--- a/packages/core/src/docs/data-model/document-data-model.ts
+++ b/packages/core/src/docs/data-model/document-data-model.ts
@@ -264,9 +264,7 @@ export class DocumentDataModel extends DocumentDataModelSimple {
return;
}
- const doc = this.snapshot;
-
- return JSONX.apply(doc, actions);
+ return JSONX.apply(this.snapshot, actions);
}
sliceBody(startOffset: number, endOffset: number): Nullable<IDocumentBody> {
diff --git a/packages/core/src/docs/data-model/text-x/__tests__/apply-utils.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/apply-utils.spec.ts
index ac46687c2f..4b9f50c6a9 100644
--- a/packages/core/src/docs/data-model/text-x/__tests__/apply-utils.spec.ts
+++ b/packages/core/src/docs/data-model/text-x/__tests__/apply-utils.spec.ts
@@ -133,6 +133,84 @@ describe('test case in apply utils', () => {
expect(body?.textRuns![2].st).toBe(18);
expect(body?.textRuns![2].ed).toBe(20);
});
+
+ it('Should return collapsed textRun when textLength is 0', () => {
+ const removeTextRuns = deleteTextRuns(body as IDocumentBody, 0, 5);
+
+ const expectedTextRuns = [{
+ st: 0,
+ ed: 0,
+ ts: {
+ bl: BooleanNumber.FALSE,
+ },
+ }];
+
+ expect(removeTextRuns).toEqual(expectedTextRuns);
+ });
+
+ it('Should return collapsed textRun when textLength is 0 and remove the collapsed textRun in body', () => {
+ const body = {
+ dataStream: 'hello\rworld hello\rworld hello\rworld he\r\n',
+ textRuns: [
+ {
+ st: 0,
+ ed: 15,
+ ts: {
+ bl: BooleanNumber.FALSE,
+ },
+ },
+ {
+ st: 15,
+ ed: 15,
+ ts: {
+ it: BooleanNumber.TRUE,
+ },
+ },
+ {
+ st: 15,
+ ed: 20,
+ ts: {
+ bl: BooleanNumber.FALSE,
+ it: BooleanNumber.TRUE,
+ },
+ },
+ {
+ st: 30,
+ ed: 40,
+ ts: {
+ bl: BooleanNumber.FALSE,
+ },
+ },
+ ],
+ paragraphs: [
+ {
+ startIndex: 5,
+ },
+ {
+ startIndex: 17,
+ },
+ {
+ startIndex: 29,
+ },
+ {
+ startIndex: 38,
+ },
+ ],
+ };
+
+ const removeTextRuns = deleteTextRuns(body as IDocumentBody, 0, 15);
+
+ const expectedTextRuns = [{
+ st: 0,
+ ed: 0,
+ ts: {
+ it: BooleanNumber.TRUE,
+ },
+ }];
+
+ expect(removeTextRuns).toEqual(expectedTextRuns);
+ expect(body?.textRuns?.length).toBe(3);
+ });
});
describe('test cases in function coverTextRuns', () => {
@@ -164,8 +242,30 @@ describe('test case in apply utils', () => {
},
];
+ const expectedTextRuns = [
+ {
+ st: 0,
+ ed: 5,
+ ts: {
+ it: BooleanNumber.TRUE,
+ bl: BooleanNumber.TRUE,
+ fs: 28,
+ },
+ },
+ {
+ st: 5,
+ ed: 10,
+ ts: {
+ it: BooleanNumber.FALSE,
+ bl: BooleanNumber.TRUE,
+ fs: 28,
+ },
+ },
+ ];
+
const needUpdateTextRuns = coverTextRuns(updateTextRuns, removeTextRuns, UpdateDocsAttributeType.COVER);
+ expect(needUpdateTextRuns).toEqual(expectedTextRuns);
expect(needUpdateTextRuns.length).toBe(2);
expect(needUpdateTextRuns[0]?.ts?.bl).toBe(BooleanNumber.TRUE);
expect(needUpdateTextRuns[1]?.ts?.bl).toBe(BooleanNumber.TRUE);
@@ -298,143 +398,175 @@ describe('test case in apply utils', () => {
expect(needUpdateTextRuns[2]?.ts?.bl).toBe(BooleanNumber.FALSE);
expect(needUpdateTextRuns[3]?.ts?.bl).toBe(BooleanNumber.TRUE);
});
- });
-
- describe('test cases in function insertTextRuns', () => {
- it('it should pass the case when the insertTextRuns is at the beginning of one testRun', async () => {
- insertTextRuns(
- body as IDocumentBody,
- {
- textRuns: updateTextRuns,
- } as IDocumentBody,
- 10,
- 0
- );
-
- expect(body?.textRuns!.length).toBe(4);
- });
-
- it('it should pass the case when the insertTextRuns is between original testRuns', async () => {
- insertTextRuns(
- body as IDocumentBody,
- {
- textRuns: updateTextRuns,
- } as IDocumentBody,
- 10,
- 25
- );
-
- expect(body?.textRuns!.length).toBe(4);
- expect(body?.textRuns![0].ts?.bl).toBe(BooleanNumber.FALSE);
- expect(body?.textRuns![1].ts?.bl).toBe(BooleanNumber.FALSE);
- expect(body?.textRuns![2].ts?.bl).toBe(BooleanNumber.TRUE);
- expect(body?.textRuns![3].ts?.bl).toBe(BooleanNumber.FALSE);
- });
-
- it('it should pass the case when the insertTextRuns is at the end of one testRun', async () => {
- insertTextRuns(
- body as IDocumentBody,
- {
- textRuns: updateTextRuns,
- } as IDocumentBody,
- 10,
- 15
- );
-
- expect(body?.textRuns!.length).toBe(4);
- });
-
- it('it should pass the case when the insertTextRuns is at the in the middle of one testRun', async () => {
- insertTextRuns(
- body as IDocumentBody,
- {
- textRuns: updateTextRuns,
- } as IDocumentBody,
- 10,
- 10
- );
-
- expect(body?.textRuns!.length).toBe(5);
- });
- it('it should pass the case when the insertTextRuns has the same style with the origin textRun, and should be merged', async () => {
+ it('it should be pass the test when the updateTextRuns and removeTextRuns are both collapsed', () => {
const updateTextRuns = [
{
st: 0,
- ed: 10,
+ ed: 0,
ts: {
- bl: BooleanNumber.FALSE,
+ bl: BooleanNumber.TRUE,
},
},
];
- insertTextRuns(
- body as IDocumentBody,
- {
- textRuns: updateTextRuns,
- } as IDocumentBody,
- 10,
- 10
- );
-
- expect(body?.textRuns!.length).toBe(3);
- expect(body?.textRuns![0].ts?.bl).toBe(BooleanNumber.FALSE);
- expect(body?.textRuns![1].ts?.bl).toBe(BooleanNumber.FALSE);
- expect(body?.textRuns![2].ts?.bl).toBe(BooleanNumber.FALSE);
- });
- it('it should pass the case when the insertTextRuns is empty', async () => {
- insertTextRuns(
- body as IDocumentBody,
- {
- textRuns: [],
- } as unknown as IDocumentBody,
- 10,
- 10
- );
-
- expect(body?.textRuns!.length).toBe(3);
- expect(body?.textRuns![0].ts?.bl).toBe(BooleanNumber.FALSE);
- expect(body?.textRuns![1].ts?.bl).toBe(BooleanNumber.FALSE);
- expect(body?.textRuns![2].ts?.bl).toBe(BooleanNumber.FALSE);
- });
-
- it('If textRuns doesn\'t intersect, they shouldn\'t be merged', async () => {
- const updateTextRuns = [
+ const removedTextRuns = [
{
st: 0,
- ed: 10,
+ ed: 0,
ts: {
- bl: BooleanNumber.FALSE,
+ cl: { rgb: 'rgb(30, 30, 30)' },
+ ff: 'Microsoft YaHei',
+ fs: 12,
},
},
];
- insertTextRuns(
- body as IDocumentBody,
- {
- textRuns: updateTextRuns,
- } as unknown as IDocumentBody,
- 10,
- 25
- );
-
- expect(body?.textRuns!.length).toBe(4);
- expect(body?.textRuns![0].ts?.bl).toBe(BooleanNumber.FALSE);
- expect(body?.textRuns![1].ts?.bl).toBe(BooleanNumber.FALSE);
- expect(body?.textRuns![2].ts?.bl).toBe(BooleanNumber.FALSE);
- expect(body?.textRuns![3].ts?.bl).toBe(BooleanNumber.FALSE);
+
+ const needUpdateTextRuns = coverTextRuns(updateTextRuns, removedTextRuns, UpdateDocsAttributeType.COVER);
+
+ expect(needUpdateTextRuns.length).toBe(1);
+ expect(needUpdateTextRuns[0]?.ts?.bl).toBe(BooleanNumber.TRUE);
+ expect(needUpdateTextRuns[0]?.ts?.cl?.rgb).toBe('rgb(30, 30, 30)');
+ expect(needUpdateTextRuns[0]?.ts?.ff).toBe('Microsoft YaHei');
+ expect(needUpdateTextRuns[0]?.ts?.fs).toBe(12);
});
- });
- describe('test cases in function deleteParagraphs', () => {
- it('it should pass the case when the delete range has no paragraphs', async () => {
- const removedParagraphs = deleteParagraphs(body!, 2, 0);
- expect(removedParagraphs.length).toBe(0);
+ describe('test cases in function insertTextRuns', () => {
+ it('it should pass the case when the insertTextRuns is at the beginning of one testRun', async () => {
+ insertTextRuns(
+ body as IDocumentBody,
+ {
+ textRuns: updateTextRuns,
+ } as IDocumentBody,
+ 10,
+ 0
+ );
+
+ expect(body?.textRuns!.length).toBe(4);
+ });
+
+ it('it should pass the case when the insertTextRuns is between original testRuns', async () => {
+ insertTextRuns(
+ body as IDocumentBody,
+ {
+ textRuns: updateTextRuns,
+ } as IDocumentBody,
+ 10,
+ 25
+ );
+
+ expect(body?.textRuns!.length).toBe(4);
+ expect(body?.textRuns![0].ts?.bl).toBe(BooleanNumber.FALSE);
+ expect(body?.textRuns![1].ts?.bl).toBe(BooleanNumber.FALSE);
+ expect(body?.textRuns![2].ts?.bl).toBe(BooleanNumber.TRUE);
+ expect(body?.textRuns![3].ts?.bl).toBe(BooleanNumber.FALSE);
+ });
+
+ it('it should pass the case when the insertTextRuns is at the end of one testRun', async () => {
+ insertTextRuns(
+ body as IDocumentBody,
+ {
+ textRuns: updateTextRuns,
+ } as IDocumentBody,
+ 10,
+ 15
+ );
+
+ expect(body?.textRuns!.length).toBe(4);
+ });
+
+ it('it should pass the case when the insertTextRuns is at the in the middle of one testRun', async () => {
+ insertTextRuns(
+ body as IDocumentBody,
+ {
+ textRuns: updateTextRuns,
+ } as IDocumentBody,
+ 10,
+ 10
+ );
+
+ expect(body?.textRuns!.length).toBe(5);
+ });
+
+ it('it should pass the case when the insertTextRuns has the same style with the origin textRun, and should be merged', async () => {
+ const updateTextRuns = [
+ {
+ st: 0,
+ ed: 10,
+ ts: {
+ bl: BooleanNumber.FALSE,
+ },
+ },
+ ];
+ insertTextRuns(
+ body as IDocumentBody,
+ {
+ textRuns: updateTextRuns,
+ } as IDocumentBody,
+ 10,
+ 10
+ );
+
+ expect(body?.textRuns!.length).toBe(3);
+ expect(body?.textRuns![0].ts?.bl).toBe(BooleanNumber.FALSE);
+ expect(body?.textRuns![1].ts?.bl).toBe(BooleanNumber.FALSE);
+ expect(body?.textRuns![2].ts?.bl).toBe(BooleanNumber.FALSE);
+ });
+
+ it('it should pass the case when the insertTextRuns is empty', async () => {
+ insertTextRuns(
+ body as IDocumentBody,
+ {
+ textRuns: [],
+ } as unknown as IDocumentBody,
+ 10,
+ 10
+ );
+
+ expect(body?.textRuns!.length).toBe(3);
+ expect(body?.textRuns![0].ts?.bl).toBe(BooleanNumber.FALSE);
+ expect(body?.textRuns![1].ts?.bl).toBe(BooleanNumber.FALSE);
+ expect(body?.textRuns![2].ts?.bl).toBe(BooleanNumber.FALSE);
+ });
+
+ it('If textRuns doesn\'t intersect, they shouldn\'t be merged', async () => {
+ const updateTextRuns = [
+ {
+ st: 0,
+ ed: 10,
+ ts: {
+ bl: BooleanNumber.FALSE,
+ },
+ },
+ ];
+ insertTextRuns(
+ body as IDocumentBody,
+ {
+ textRuns: updateTextRuns,
+ } as unknown as IDocumentBody,
+ 10,
+ 25
+ );
+
+ expect(body?.textRuns!.length).toBe(4);
+ expect(body?.textRuns![0].ts?.bl).toBe(BooleanNumber.FALSE);
+ expect(body?.textRuns![1].ts?.bl).toBe(BooleanNumber.FALSE);
+ expect(body?.textRuns![2].ts?.bl).toBe(BooleanNumber.FALSE);
+ expect(body?.textRuns![3].ts?.bl).toBe(BooleanNumber.FALSE);
+ });
});
- it('it should pass the case when the delete range has one paragraphs', async () => {
- const removedParagraphs = deleteParagraphs(body!, 2, 5);
- expect(removedParagraphs.length).toBe(1);
- expect(removedParagraphs[0].startIndex).toBe(0);
+ describe('test cases in function deleteParagraphs', () => {
+ it('it should pass the case when the delete range has no paragraphs', async () => {
+ const removedParagraphs = deleteParagraphs(body!, 2, 0);
+ expect(removedParagraphs.length).toBe(0);
+ });
+
+ it('it should pass the case when the delete range has one paragraphs', async () => {
+ const removedParagraphs = deleteParagraphs(body!, 2, 5);
+ expect(removedParagraphs.length).toBe(1);
+ expect(removedParagraphs[0].startIndex).toBe(0);
+ });
});
});
});
diff --git a/packages/core/src/docs/data-model/text-x/__tests__/common.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/common.spec.ts
index b92a42c723..768076b050 100644
--- a/packages/core/src/docs/data-model/text-x/__tests__/common.spec.ts
+++ b/packages/core/src/docs/data-model/text-x/__tests__/common.spec.ts
@@ -56,6 +56,13 @@ describe('common utils test cases', () => {
];
const expectedTextRuns: ITextRun[] = [
+ {
+ st: 0,
+ ed: 0,
+ ts: {
+ bl: BooleanNumber.FALSE,
+ },
+ },
{
st: 15,
ed: 35,
diff --git a/packages/core/src/docs/data-model/text-x/apply-utils/common.ts b/packages/core/src/docs/data-model/text-x/apply-utils/common.ts
index b9a3778dfa..faefea6713 100644
--- a/packages/core/src/docs/data-model/text-x/apply-utils/common.ts
+++ b/packages/core/src/docs/data-model/text-x/apply-utils/common.ts
@@ -32,15 +32,15 @@ export function normalizeTextRuns(textRuns: ITextRun[]) {
const results: ITextRun[] = [];
for (const textRun of textRuns) {
- const { st, ed, ts } = textRun;
+ const { ed, ts } = textRun;
if (textRun.sId === undefined) {
delete textRun.sId;
}
- if (st === ed) {
- continue;
- }
+ // if (st === ed) {
+ // continue;
+ // }
// Delete textRun if it has no style(ts is empty or has no sId)
if (Tools.isEmptyObject(ts) && textRun.sId == null) {
@@ -96,8 +96,7 @@ export function insertTextRuns(
const insertTextRuns = insertBody.textRuns ?? [];
if (insertTextRuns.length) {
- for (let i = 0, len = insertTextRuns.length; i < len; i++) {
- const insertTextRun = insertTextRuns[i];
+ for (const insertTextRun of insertTextRuns) {
insertTextRun.st += currentIndex;
insertTextRun.ed += currentIndex;
}
@@ -105,11 +104,20 @@ export function insertTextRuns(
for (let i = 0; i < len; i++) {
const textRun = textRuns[i];
+ const nextRun = textRuns[i + 1];
const { st, ed } = textRun;
if (ed < currentIndex) {
newTextRuns.push(textRun);
} else if (currentIndex >= st && currentIndex <= ed) {
+ // The inline format used to handle no selection will insert a textRun
+ // with `st` equal to `ed` at the current cursor,
+ // and when we insert text, the style should follow the new textRun.
+ if (nextRun && nextRun.st === nextRun.ed && currentIndex === nextRun.st) {
+ newTextRuns.push(textRun);
+ continue;
+ }
+
if (!hasInserted) {
hasInserted = true;
textRun.ed += textLength;
@@ -123,7 +131,10 @@ export function insertTextRuns(
ed: insertTextRuns[0].st,
};
- pendingTextRuns.push(startSplitTextRun);
+ if (startSplitTextRun.ed > startSplitTextRun.st) {
+ pendingTextRuns.push(startSplitTextRun);
+ }
+
pendingTextRuns.push(...insertTextRuns);
const lastInsertTextRuns = insertTextRuns[insertTextRuns.length - 1];
@@ -134,7 +145,9 @@ export function insertTextRuns(
ed: ed + textLength,
};
- pendingTextRuns.push(endSplitTextRun);
+ if (endSplitTextRun.ed > endSplitTextRun.st) {
+ pendingTextRuns.push(endSplitTextRun);
+ }
} else {
pendingTextRuns.push(textRun);
}
@@ -165,6 +178,8 @@ export function insertTextRuns(
newTextRuns.push(...insertTextRuns);
}
+ // console.log(JSON.stringify(newTextRuns, null, 2));
+
body.textRuns = normalizeTextRuns(newTextRuns);
}
@@ -386,6 +401,20 @@ export function deleteTextRuns(body: IDocumentBody, textLength: number, currentI
const endIndex = currentIndex + textLength;
const removeTextRuns: ITextRun[] = [];
+ // Handles special case where repeated set inline format style by cursor.
+ if (startIndex === endIndex && textRuns?.find((t) => t.st === currentIndex && t.ed === currentIndex)) {
+ const textRun = textRuns.find((t) => t.st === currentIndex && t.ed === currentIndex)!;
+ removeTextRuns.push({
+ ...textRun,
+ st: textRun.st - currentIndex,
+ ed: textRun.ed - currentIndex,
+ });
+
+ body.textRuns = body.textRuns?.filter((t) => t !== textRun);
+
+ return removeTextRuns;
+ }
+
if (textRuns) {
const newTextRuns = [];
diff --git a/packages/core/src/docs/data-model/text-x/apply-utils/update-apply.ts b/packages/core/src/docs/data-model/text-x/apply-utils/update-apply.ts
index 9d18450cff..f60d1b873c 100644
--- a/packages/core/src/docs/data-model/text-x/apply-utils/update-apply.ts
+++ b/packages/core/src/docs/data-model/text-x/apply-utils/update-apply.ts
@@ -48,15 +48,10 @@ export function updateAttribute(
coverType: UpdateDocsAttributeType
): IDocumentBody {
const removeTextRuns = updateTextRuns(body, updateBody, textLength, currentIndex, coverType);
-
const removeParagraphs = updateParagraphs(body, updateBody, textLength, currentIndex, coverType);
-
const removeSectionBreaks = updateSectionBreaks(body, updateBody, textLength, currentIndex, coverType);
-
const removeCustomBlocks = updateCustomBlocks(body, updateBody, textLength, currentIndex, coverType);
-
const removeTables = updateTables(body, updateBody, textLength, currentIndex, coverType);
-
const removeCustomRanges = updateCustomRanges(body, updateBody, textLength, currentIndex, coverType);
return {
@@ -78,17 +73,16 @@ function updateTextRuns(
coverType: UpdateDocsAttributeType
) {
const { textRuns } = body;
+ const { textRuns: updateTextRuns } = updateBody;
- const { textRuns: updateDataTextRuns } = updateBody;
-
- if (textRuns == null || updateDataTextRuns == null) {
+ if (textRuns == null || updateTextRuns == null) {
return;
}
const removeTextRuns = deleteTextRuns(body, textLength, currentIndex);
if (coverType !== UpdateDocsAttributeType.REPLACE) {
- const newUpdateTextRun = coverTextRuns(updateDataTextRuns, removeTextRuns, coverType);
+ const newUpdateTextRun = coverTextRuns(updateTextRuns, removeTextRuns, coverType);
updateBody.textRuns = newUpdateTextRun;
}
@@ -110,7 +104,7 @@ export function coverTextRuns(
updateDataTextRuns = Tools.deepClone(updateDataTextRuns);
removeTextRuns = Tools.deepClone(removeTextRuns);
- const newUpdateTextRun: ITextRun[] = [];
+ const newUpdateTextRuns: ITextRun[] = [];
const updateLength = updateDataTextRuns.length;
const removeLength = removeTextRuns.length;
let updateIndex = 0;
@@ -119,8 +113,9 @@ export function coverTextRuns(
function pushPendingAndReturnStatus() {
if (pending) {
- newUpdateTextRun.push(pending);
+ newUpdateTextRuns.push(pending);
pending = null;
+
return true;
}
@@ -140,25 +135,29 @@ export function coverTextRuns(
if (updateEd < removeSt) {
if (!pushPendingAndReturnStatus()) {
- newUpdateTextRun.push(updateDataTextRuns[updateIndex]);
+ newUpdateTextRuns.push(updateDataTextRuns[updateIndex]);
}
updateIndex++;
} else if (removeEd < updateSt) {
if (!pushPendingAndReturnStatus()) {
- newUpdateTextRun.push(removeTextRuns[removeIndex]);
+ newUpdateTextRuns.push(removeTextRuns[removeIndex]);
}
removeIndex++;
} else {
- newUpdateTextRun.push({
+ const newTextRun = {
st: Math.min(updateSt, removeSt),
ed: Math.max(updateSt, removeSt),
ts: updateSt < removeSt ? { ...updateStyle } : { ...removeStyle },
sId: updateSt < removeSt ? undefined : sId,
- });
+ };
+
+ if (newTextRun.ed > newTextRun.st) {
+ newUpdateTextRuns.push(newTextRun);
+ }
- newUpdateTextRun.push({
+ newUpdateTextRuns.push({
st: Math.max(updateSt, removeSt),
ed: Math.min(updateEd, removeEd),
ts: newTs,
@@ -168,36 +167,44 @@ export function coverTextRuns(
if (updateEd < removeEd) {
updateIndex++;
removeTextRuns[removeIndex].st = updateEd;
+ if (removeTextRuns[removeIndex].st === removeTextRuns[removeIndex].ed) {
+ removeIndex++;
+ }
} else {
removeIndex++;
updateDataTextRuns[updateIndex].st = removeEd;
+ if (updateDataTextRuns[updateIndex].st === updateDataTextRuns[updateIndex].ed) {
+ updateIndex++;
+ }
}
- pending = {
+ const pendingTextRun = {
st: Math.min(updateEd, removeEd),
ed: Math.max(updateEd, removeEd),
ts: updateEd < removeEd ? { ...removeStyle } : { ...updateStyle },
sId: updateEd < removeEd ? sId : undefined,
};
+
+ pending = pendingTextRun.ed > pendingTextRun.st ? pendingTextRun : null;
}
}
pushPendingAndReturnStatus();
// If the last textRun is also disjoint, then the last textRun needs to be pushed in `newUpdateTextRun`
- const tempTopTextRun = newUpdateTextRun[newUpdateTextRun.length - 1];
+ const tempTopTextRun = newUpdateTextRuns[newUpdateTextRuns.length - 1];
const updateLastTextRun = updateDataTextRuns[updateLength - 1];
const removeLastTextRun = removeTextRuns[removeLength - 1];
if (tempTopTextRun.ed !== Math.max(updateLastTextRun.ed, removeLastTextRun.ed)) {
if (updateLastTextRun.ed > removeLastTextRun.ed) {
- newUpdateTextRun.push(updateLastTextRun);
+ newUpdateTextRuns.push(updateLastTextRun);
} else {
- newUpdateTextRun.push(removeLastTextRun);
+ newUpdateTextRuns.push(removeLastTextRun);
}
}
- return normalizeTextRuns(newUpdateTextRun);
+ return normalizeTextRuns(newUpdateTextRuns);
}
function updateParagraphs(
diff --git a/packages/core/src/docs/data-model/text-x/apply.ts b/packages/core/src/docs/data-model/text-x/apply.ts
index ace318e061..ad083a5fb0 100644
--- a/packages/core/src/docs/data-model/text-x/apply.ts
+++ b/packages/core/src/docs/data-model/text-x/apply.ts
@@ -15,7 +15,6 @@
*/
import { MemoryCursor } from '../../../common/memory-cursor';
-import type { Nullable } from '../../../shared';
import { Tools } from '../../../shared';
import { UpdateDocsAttributeType } from '../../../shared/command-enum';
import type { IDocumentBody } from '../../../types/interfaces';
@@ -26,15 +25,11 @@ import { updateAttribute } from './apply-utils/update-apply';
function updateApply(
doc: IDocumentBody,
- updateBody: Nullable<IDocumentBody>,
+ updateBody: IDocumentBody,
textLength: number,
currentIndex: number,
coverType = UpdateDocsAttributeType.COVER
): IDocumentBody {
- if (updateBody == null) {
- throw new Error('updateBody is none');
- }
-
return updateAttribute(doc, updateBody, textLength, currentIndex, coverType);
}
@@ -74,23 +69,32 @@ export function textXApply(doc: IDocumentBody, actions: TextXAction[]): IDocumen
// be modified to have no side effects in the future?
action = Tools.deepClone(action);
- if (action.t === TextXActionType.RETAIN) {
- const { coverType, body, len } = action;
- if (body != null) {
- updateApply(doc, body, len, memoryCursor.cursor, coverType);
+ switch (action.t) {
+ case TextXActionType.RETAIN: {
+ const { coverType, body, len } = action;
+ if (body != null) {
+ updateApply(doc, body, len, memoryCursor.cursor, coverType);
+ }
+
+ memoryCursor.moveCursor(len);
+ break;
}
- memoryCursor.moveCursor(len);
- } else if (action.t === TextXActionType.INSERT) {
- const { body, len } = action;
+ case TextXActionType.INSERT: {
+ const { body, len } = action;
- insertApply(doc, body!, len, memoryCursor.cursor);
- memoryCursor.moveCursor(len);
- } else if (action.t === TextXActionType.DELETE) {
- const { len } = action;
- deleteApply(doc, len, memoryCursor.cursor);
- } else {
- throw new Error(`Unknown action type for action: ${action}.`);
+ insertApply(doc, body!, len, memoryCursor.cursor);
+ memoryCursor.moveCursor(len);
+ break;
+ }
+
+ case TextXActionType.DELETE: {
+ const { len } = action;
+ deleteApply(doc, len, memoryCursor.cursor);
+ break;
+ }
+ default:
+ throw new Error(`Unknown action type for action: ${action}.`);
}
});
diff --git a/packages/docs-ui/src/controllers/menu/menu.ts b/packages/docs-ui/src/controllers/menu/menu.ts
index 71a5bab859..31a644dbad 100644
--- a/packages/docs-ui/src/controllers/menu/menu.ts
+++ b/packages/docs-ui/src/controllers/menu/menu.ts
@@ -629,7 +629,22 @@ function getFontStyleAtCursor(accessor: IAccessor) {
const { startOffset } = activeTextRange;
- const textRun = textRuns.find(({ st, ed }) => startOffset >= st && startOffset < ed);
+ let textRun;
+
+ for (let i = 0; i < textRuns.length; i++) {
+ const curTextRun = textRuns[i];
+ const nextTextRun = textRuns[i + 1];
+
+ if (nextTextRun && nextTextRun.st === nextTextRun.ed && startOffset === nextTextRun.st) {
+ textRun = nextTextRun;
+ break;
+ }
+
+ if (curTextRun.st <= startOffset && startOffset <= curTextRun.ed) {
+ textRun = curTextRun;
+ break;
+ }
+ }
return textRun;
}
diff --git a/packages/docs/src/commands/commands/inline-format.command.ts b/packages/docs/src/commands/commands/inline-format.command.ts
index 166ba46d71..4f899630a3 100644
--- a/packages/docs/src/commands/commands/inline-format.command.ts
+++ b/packages/docs/src/commands/commands/inline-format.command.ts
@@ -14,7 +14,9 @@
* limitations under the License.
*/
-import type { ICommand, IDocumentBody, IMutationInfo, IStyleBase, ITextDecoration, ITextRun } from '@univerjs/core';
+import type {
+ ICommand, IDocumentBody, IMutationInfo, IStyleBase, ITextDecoration, ITextRun,
+} from '@univerjs/core';
import {
BaselineOffset,
BooleanNumber,
@@ -29,7 +31,6 @@ import {
TextXActionType,
} from '@univerjs/core';
import type { TextRange } from '@univerjs/engine-render';
-
import { serializeTextRange, TextSelectionManagerService } from '../../services/text-selection-manager.service';
import type { IRichTextEditingMutationParams } from '../mutations/core-editing.mutation';
import { RichTextEditingMutation } from '../mutations/core-editing.mutation';
@@ -277,18 +278,18 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {
return false;
}
- let docsModel = univerInstanceService.getCurrentUniverDocInstance();
- if (!docsModel) {
+ let documentDataModel = univerInstanceService.getCurrentUniverDocInstance();
+ if (!documentDataModel) {
return false;
}
- let unitId = docsModel.getUnitId();
+ let unitId = documentDataModel.getUnitId();
// When setting the formula bar style, the effect will be displayed in the cell editor,
// and the formula bar only displays plain text.
if (unitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) {
- docsModel = univerInstanceService.getUniverDocInstance(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)!;
- unitId = docsModel.getUnitId();
+ documentDataModel = univerInstanceService.getUniverDocInstance(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)!;
+ unitId = documentDataModel.getUnitId();
}
let formatValue;
@@ -301,7 +302,7 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {
case SetInlineFormatSubscriptCommand.id: // fallthrough
case SetInlineFormatSuperscriptCommand.id: {
formatValue = getReverseFormatValueInSelection(
- docsModel.getBody()!.textRuns!,
+ documentDataModel.getBody()!.textRuns!,
preCommandId,
selections
);
@@ -348,7 +349,6 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {
const jsonX = JSONX.getInstance();
const memoryCursor = new MemoryCursor();
-
memoryCursor.reset();
for (const selection of selections) {
diff --git a/packages/docs/src/commands/mutations/core-editing.mutation.ts b/packages/docs/src/commands/mutations/core-editing.mutation.ts
index 234d3b0cae..2113143d54 100644
--- a/packages/docs/src/commands/mutations/core-editing.mutation.ts
+++ b/packages/docs/src/commands/mutations/core-editing.mutation.ts
@@ -94,9 +94,6 @@ export const RichTextEditingMutation: IMutation<IRichTextEditingMutationParams,
const undoActions = JSONX.invertWithDoc(actions, documentDataModel.getSnapshot());
documentDataModel.apply(actions);
- // console.log(actions);
- // console.log(undoActions);
-
// Step 2: Update Doc View Model.
const segmentDocumentDataModel = documentDataModel.getSelfOrHeaderFooterModel(segmentId);
const segmentViewModel = documentViewModel.getSelfOrHeaderFooterViewModel(segmentId);