Skip to content

Commit

Permalink
fix: the apply result should be consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Oct 19, 2024
1 parent 3b8af5a commit 2e2cffe
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 22 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/docs/data-model/json-x/json-x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/

import type { Doc, JSONOp, Path } from 'ot-json1';
import * as json1 from 'ot-json1';
import type { Nullable } from '../../../shared';
import type { IDocumentBody, IDocumentData } from '../../../types/interfaces';
import type { TextXAction } from '../text-x/action-types';
import type { TPriority } from '../text-x/text-x';
import * as json1 from 'ot-json1';
import { TextX } from '../text-x/text-x';
import type { TextXAction } from '../text-x/action-types';
import type { Nullable } from '../../../shared';

export interface ISubType {
name: string;
Expand All @@ -37,7 +37,7 @@ export interface ISubType {
[k: string]: any;
};

export { JSONOp as JSONXActions, Path as JSONXPath, json1 as JSON1 };
export { json1 as JSON1, JSONOp as JSONXActions, Path as JSONXPath };

export class JSONX {
// static name = 'json-x';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

import { describe, expect, it } from 'vitest';
import { BooleanNumber } from '../../../../types/enum/text-style';
import { ActionIterator } from '../action-iterator';
import { TextXActionType } from '../action-types';
import { BooleanNumber } from '../../../../types/enum/text-style';

describe('Test action iterator', () => {
it('test action iterator basic use', () => {
Expand Down
91 changes: 91 additions & 0 deletions packages/core/src/docs/data-model/text-x/__tests__/apply.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { IDocumentBody } from '../../../../types/interfaces';
import type { TextXAction } from '../action-types';
import { describe, expect, it } from 'vitest';
import { BooleanNumber } from '../../../../types/enum';
import { TextXActionType } from '../action-types';
import { TextX } from '../text-x';

function getDefaultDoc() {
const doc: IDocumentBody = {
dataStream: 'w\r\n',
textRuns: [
{
st: 0,
ed: 1,
ts: {
bl: BooleanNumber.TRUE,
},
},
],
};

return doc;
}

describe('apply method', () => {
it('should get the same result when apply two actions by order OR composed first', () => {
const actionsA: TextXAction[] = [
{
t: TextXActionType.INSERT,
len: 1,
line: 0,
body: {
dataStream: 'h',
},
},
];
const actionsB: TextXAction[] = [
{
t: TextXActionType.RETAIN,
len: 1,
body: {
dataStream: '',
textRuns: [
{
st: 0,
ed: 1,
ts: {
bl: BooleanNumber.FALSE,
},
},
],
},
segmentId: '',
},
];

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

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);

expect(resultA).toEqual(resultB);
expect(resultC).toEqual(resultD);
expect(composedAction1).toEqual(composedAction2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

import { describe, expect, it } from 'vitest';
import type { TextXAction } from '../action-types';
import { TextXActionType } from '../action-types';
import { describe, expect, it } from 'vitest';
import { BooleanNumber } from '../../../../types/enum/text-style';
import { TextXActionType } from '../action-types';
import { TextX } from '../text-x';

describe('compose test cases', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

import { describe, expect, it } from 'vitest';
import type { IDocumentBody } from '../../../../types/interfaces';
import { BooleanNumber } from '../../../../types/enum';
import { TextX } from '../text-x';
import type { TextXAction } from '../action-types';
import { TextXActionType } from '../action-types';
import { describe, expect, it } from 'vitest';
import { UpdateDocsAttributeType } from '../../../../shared/command-enum';
import { BooleanNumber } from '../../../../types/enum';
import { TextXActionType } from '../action-types';
import { TextX } from '../text-x';

describe('test TextX static methods invert and makeInvertible', () => {
it('test TextX static method invert', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import { describe, expect, it } from 'vitest';
import { TextX } from '../text-x';
import type { IDocumentBody } from '../../../../types/interfaces/i-document-data';
import { BooleanNumber } from '../../../../types/enum/text-style';
import { describe, expect, it } from 'vitest';
import { UpdateDocsAttributeType } from '../../../../shared/command-enum';
import { BooleanNumber } from '../../../../types/enum/text-style';
import { TextXActionType } from '../action-types';
import { TextX } from '../text-x';

describe('test TextX methods and branches', () => {
describe('test TextX methods', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import { describe, expect, it } from 'vitest';
import type { IDocumentBody } from '../../../../types/interfaces/i-document-data';
import { BooleanNumber } from '../../../../types/enum/text-style';
import { composeBody, getBodySlice, isUselessRetainAction } from '../utils';
import type { IRetainAction } from '../action-types';
import { describe, expect, it } from 'vitest';
import { BooleanNumber } from '../../../../types/enum/text-style';
import { TextXActionType } from '../action-types';
import { composeBody, getBodySlice, isUselessRetainAction } from '../utils';

describe('test text-x utils', () => {
it('test getBodySlice fn', () => {
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/docs/data-model/text-x/apply-utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
* limitations under the License.
*/

import { horizontalLineSegmentsSubtraction, sortRulesFactory, Tools } from '../../../../shared';
import { isSameStyleTextRun } from '../../../../shared/compare';
import { DataStreamTreeTokenType } from '../../types';
import type { Nullable } from '../../../../shared';
import type {
ICustomBlock,
Expand All @@ -28,6 +25,9 @@ import type {
ISectionBreak,
ITextRun,
} from '../../../../types/interfaces';
import { horizontalLineSegmentsSubtraction, sortRulesFactory, Tools } from '../../../../shared';
import { isSameStyleTextRun } from '../../../../shared/compare';
import { DataStreamTreeTokenType } from '../../types';

export function normalizeTextRuns(textRuns: ITextRun[]) {
const results: ITextRun[] = [];
Expand Down Expand Up @@ -123,6 +123,11 @@ export function insertTextRuns(
hasInserted = true;
textRun.ed += textLength;

// Inline styles should not be extended forward.
if (currentIndex === st && ed !== st) {
textRun.st += textLength;
}

const pendingTextRuns = [];

if (insertTextRuns.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import type { IDocumentBody } from '../../../../types/interfaces';
import { insertTextToContent } from '../../../../shared';
import {
insertCustomBlocks,
Expand All @@ -24,7 +25,6 @@ import {
insertTables,
insertTextRuns,
} from './common';
import type { IDocumentBody } from '../../../../types/interfaces';

export function updateAttributeByInsert(
body: IDocumentBody,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/docs/data-model/text-x/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

import type { IDocumentBody } from '../../../types/interfaces';
import { MemoryCursor } from '../../../common/memory-cursor';
import { Tools } from '../../../shared';
import { UpdateDocsAttributeType } from '../../../shared/command-enum';
import type { IDocumentBody } from '../../../types/interfaces';
import { type TextXAction, TextXActionType } from './action-types';
import { updateAttributeByDelete } from './apply-utils/delete-apply';
import { updateAttributeByInsert } from './apply-utils/insert-apply';
Expand Down

0 comments on commit 2e2cffe

Please sign in to comment.