Skip to content

Commit

Permalink
fix(docs): line break in collaboration (#3863)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Actions <[email protected]>
  • Loading branch information
Jocs and actions-user authored Oct 26, 2024
1 parent e99aec1 commit e95449e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 29 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 66 additions & 7 deletions packages/core/src/docs/data-model/text-x/__tests__/apply.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,6 @@ describe('apply method', () => {
const resultC = TextX.apply(doc3, composedAction1);
const resultD = TextX.apply(doc4, composedAction2);

// console.log(JSON.stringify(resultA, null, 2));
// console.log(JSON.stringify(resultB, null, 2));

// console.log('composedAction1', JSON.stringify(composedAction1, null, 2));

// console.log(JSON.stringify(resultC, null, 2));

expect(resultA).toEqual(resultB);
expect(resultC).toEqual(resultD);
expect(resultA).toEqual(resultC);
Expand Down Expand Up @@ -527,6 +520,72 @@ describe('apply method', () => {
const resultC = TextX.apply(doc3, composedAction1);
const resultD = TextX.apply(doc4, composedAction2);

expect(resultA).toEqual(resultB);
expect(resultC).toEqual(resultD);
expect(resultA).toEqual(resultC);
expect(composedAction1).toEqual(composedAction2);
});

it('should get the same result when one is break line and the other is edit after the break point', () => {
const actionsA: TextXAction[] = [
{
t: TextXActionType.RETAIN,
len: 1,
segmentId: '',
}, {
t: TextXActionType.INSERT,
len: 1,
segmentId: '',
line: 0,
body: {
dataStream: '\r',
paragraphs: [{
startIndex: 0,
paragraphStyle: {
horizontalAlign: HorizontalAlign.LEFT,
},
}],
},
},
];

const actionsB: TextXAction[] = [
{
t: TextXActionType.RETAIN,
len: 2,
segmentId: '',
}, {
t: TextXActionType.INSERT,
len: 1,
segmentId: '',
line: 0,
body: {
dataStream: 'X',
textRuns: [{
st: 0,
ed: 1,
ts: {
bl: BooleanNumber.TRUE,
},
}],
},
},
];

const doc1 = getDefaultDocWithLength2();
const doc2 = getDefaultDocWithLength2();
const doc3 = getDefaultDocWithLength2();
const doc4 = getDefaultDocWithLength2();

const resultA = TextX.apply(TextX.apply(doc1, actionsA), TextX.transform(actionsB, actionsA, 'left'));
const resultB = TextX.apply(TextX.apply(doc2, actionsB), TextX.transform(actionsA, actionsB, 'right'));

const composedAction1 = TextX.compose(actionsA, TextX.transform(actionsB, actionsA, 'left'));
const composedAction2 = TextX.compose(actionsB, TextX.transform(actionsA, actionsB, 'right'));

const resultC = TextX.apply(doc3, composedAction1);
const resultD = TextX.apply(doc4, composedAction2);

// console.log(JSON.stringify(resultA, null, 2));
// console.log(JSON.stringify(resultB, null, 2));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export const toggleChecklistParagraph = (params: IToggleChecklistParagraphParams
}

const currentParagraph = paragraphs.find((p) => p.startIndex === paragraphIndex);

if (!currentParagraph?.bullet || currentParagraph.bullet.listType.indexOf(PresetListType.CHECK_LIST) === -1) {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/docs/data-model/text-x/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export function normalizeBody(body: IDocumentBody): IDocumentBody {
paragraphs?.forEach((p) => {
p.startIndex += leftOffset;
});

customRanges?.forEach((range) => {
range.startIndex += leftOffset;
range.endIndex += leftOffset;
Expand Down
48 changes: 26 additions & 22 deletions packages/docs-ui/src/commands/commands/break-line.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/

import type { DocumentDataModel, ICommand, IParagraph } from '@univerjs/core';
import { BuildTextUtils, CommandType, DataStreamTreeTokenType, getBodySlice, ICommandService, IUniverInstanceService, normalizeBody, PresetListType, Tools, UniverInstanceType, updateAttributeByInsert } from '@univerjs/core';
import { BuildTextUtils, CommandType, DataStreamTreeTokenType, ICommandService, IUniverInstanceService, PresetListType, Tools, UniverInstanceType } from '@univerjs/core';
import { DocSelectionManagerService } from '@univerjs/docs';
import { InsertCommand } from './core-editing.command';
import { ToggleCheckListCommand } from './list.command';

export function generateParagraphs(dataStream: string, prevParagraph?: IParagraph): IParagraph[] {
const paragraphs: IParagraph[] = [];
Expand Down Expand Up @@ -93,37 +94,40 @@ export const BreakLineCommand: ICommand = {
if (!prevParagraph) {
return false;
}
// line breaks to 2
const bodyAfter = normalizeBody(getBodySlice(body, endOffset, prevParagraph.startIndex + 1));
bodyAfter.customRanges = bodyAfter.customRanges?.map(BuildTextUtils.customRange.copyCustomRange);
const deleteRange = {
startOffset,
endOffset: prevParagraph.startIndex + 1,
collapsed: false,
};

if (bodyAfter.paragraphs?.[0].bullet?.listType === PresetListType.CHECK_LIST_CHECKED) {
bodyAfter.paragraphs[0].bullet.listType = PresetListType.CHECK_LIST;
const prevParagraphIndex = prevParagraph.startIndex;

const insertBody = {
dataStream: DataStreamTreeTokenType.PARAGRAPH,
paragraphs: generateParagraphs(DataStreamTreeTokenType.PARAGRAPH, prevParagraph),
};

updateAttributeByInsert(
bodyAfter,
{
dataStream: DataStreamTreeTokenType.PARAGRAPH,
paragraphs: generateParagraphs(DataStreamTreeTokenType.PARAGRAPH, prevParagraph),
},
1,
0
);
const deleteRange = {
startOffset,
endOffset,
};

const result = await commandService.executeCommand(InsertCommand.id, {
unitId,
body: bodyAfter,
body: insertBody,
range: deleteRange,
segmentId,
cursorOffset: 1,
});

if (prevParagraph.bullet?.listType === PresetListType.CHECK_LIST_CHECKED) {
const params = {
index: prevParagraphIndex + 1 - (endOffset - startOffset),
segmentId,
textRanges: [{
startOffset: startOffset + 1,
endOffset: startOffset + 1,
}],
};
const toggleCheckListResult = await commandService.executeCommand(ToggleCheckListCommand.id, params);

return Boolean(toggleCheckListResult) && result;
}

return result;
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/docs-ui/src/commands/commands/list.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ export interface IToggleCheckListCommandParams {

export const ToggleCheckListCommand: ICommand<IToggleCheckListCommandParams> = {
id: 'doc.command.toggle-check-list',

type: CommandType.COMMAND,

handler: (accessor, params) => {
Expand Down

0 comments on commit e95449e

Please sign in to comment.