Skip to content

Commit

Permalink
Merge branch 'main' of github.com:editor-js/document-model into feat/…
Browse files Browse the repository at this point in the history
…tools-builders
  • Loading branch information
gohabereg committed Aug 29, 2024
2 parents 151f471 + f2e7a8a commit 4ff6f8e
Show file tree
Hide file tree
Showing 54 changed files with 1,371 additions and 198 deletions.
296 changes: 232 additions & 64 deletions packages/collaboration-manager/src/CollaborationManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,251 @@ import { CollaborationManager } from './CollaborationManager.js';
import { Operation, OperationType } from './Operation.js';

describe('CollaborationManager', () => {
it('should add text on apply Insert Operation', () => {
const model = new EditorJSModel({
blocks: [ {
name: 'paragraph',
data: {
text: {
value: '',
$t: 't',
describe('applyOperation', () => {
it('should add text on apply Insert Operation', () => {
const model = new EditorJSModel({
blocks: [ {
name: 'paragraph',
data: {
text: {
value: '',
$t: 't',
},
},
},
} ],
});
const collaborationManager = new CollaborationManager(model);
const index = new IndexBuilder().addBlockIndex(0)
.addDataKey(createDataKey('text'))
.addTextRange([0, 4])
.build();
const operation = new Operation(OperationType.Insert, index, {
prevValue: '',
newValue: 'test',
} ],
});
const collaborationManager = new CollaborationManager(model);
const index = new IndexBuilder().addBlockIndex(0)
.addDataKey(createDataKey('text'))
.addTextRange([0, 4])
.build();
const operation = new Operation(OperationType.Insert, index, {
prevValue: '',
newValue: 'test',
});

collaborationManager.applyOperation(operation);
expect(model.serialized).toStrictEqual({
blocks: [ {
name: 'paragraph',
tunes: {},
data: {
text: {
$t: 't',
value: 'test',
fragments: [],
},
},
} ],
properties: {},
});
});

collaborationManager.applyOperation(operation);
expect(model.serialized).toStrictEqual({
blocks: [ {
name: 'paragraph',
tunes: {},
data: {
text: {
$t: 't',
value: 'test',
fragments: [],

it('should remove text on apply Remove Operation', () => {
const model = new EditorJSModel({
blocks: [ {
name: 'paragraph',
data: {
text: {
value: 'hel11lo',
$t: 't',
},
},
} ],
});
const collaborationManager = new CollaborationManager(model);
const index = new IndexBuilder().addBlockIndex(0)
.addDataKey(createDataKey('text'))
.addTextRange([
3, 5])
.build();
const operation = new Operation(OperationType.Delete, index, {
prevValue: '11',
newValue: '',
});

collaborationManager.applyOperation(operation);
expect(model.serialized).toStrictEqual({
blocks: [ {
name: 'paragraph',
tunes: {},
data: {
text: {
$t: 't',
value: 'hello',
fragments: [],
},
},
},
} ],
properties: {},
} ],
properties: {},
});
});
});

describe('undo logic', () => {
it('should invert Insert operation', () => {
const model = new EditorJSModel({
blocks: [ {
name: 'paragraph',
data: {
text: {
value: '',
$t: 't',
},
},
} ],
});
const collaborationManager = new CollaborationManager(model);
const index = new IndexBuilder().addBlockIndex(0)
.addDataKey(createDataKey('text'))
.addTextRange([0, 4])
.build();
const operation = new Operation(OperationType.Insert, index, {
prevValue: '',
newValue: 'test',
});

it('should remove text on apply Remove Operation', () => {
const model = new EditorJSModel({
blocks: [ {
name: 'paragraph',
data: {
text: {
value: 'hel11lo',
$t: 't',
collaborationManager.applyOperation(operation);
collaborationManager.undo();
expect(model.serialized).toStrictEqual({
blocks: [ {
name: 'paragraph',
tunes: {},
data: {
text: {
$t: 't',
value: '',
fragments: [],
},
},
},
} ],
} ],
properties: {},
});
});
const collaborationManager = new CollaborationManager(model);
const index = new IndexBuilder().addBlockIndex(0)
.addDataKey(createDataKey('text'))
.addTextRange([
3, 5])
.build();
const operation = new Operation(OperationType.Delete, index, {
prevValue: '11',
newValue: '',

it('should invert Remove operation', () => {
const model = new EditorJSModel({
blocks: [ {
name: 'paragraph',
data: {
text: {
value: 'hel11lo',
$t: 't',
},
},
} ],
});
const collaborationManager = new CollaborationManager(model);
const index = new IndexBuilder().addBlockIndex(0)
.addDataKey(createDataKey('text'))
.addTextRange([
3, 5])
.build();
const operation = new Operation(OperationType.Delete, index, {
prevValue: '11',
newValue: '',
});

collaborationManager.applyOperation(operation);
collaborationManager.undo();
expect(model.serialized).toStrictEqual({
blocks: [ {
name: 'paragraph',
tunes: {},
data: {
text: {
$t: 't',
value: 'hel11lo',
fragments: [],
},
},
} ],
properties: {},
});
});

collaborationManager.applyOperation(operation);
expect(model.serialized).toStrictEqual({
blocks: [ {
name: 'paragraph',
tunes: {},
data: {
text: {
$t: 't',
value: 'hello',
fragments: [],
it('should revert only one operation if stack length is 1', () => {
const model = new EditorJSModel({
blocks: [ {
name: 'paragraph',
data: {
text: {
value: '',
$t: 't',
},
},
} ],
});
const collaborationManager = new CollaborationManager(model);
const index = new IndexBuilder().addBlockIndex(0)
.addDataKey(createDataKey('text'))
.addTextRange([0, 4])
.build();
const operation = new Operation(OperationType.Insert, index, {
prevValue: '',
newValue: 'test',
});

collaborationManager.applyOperation(operation);
collaborationManager.undo();
collaborationManager.undo();
expect(model.serialized).toStrictEqual({
blocks: [ {
name: 'paragraph',
tunes: {},
data: {
text: {
$t: 't',
value: '',
fragments: [],
},
},
} ],
properties: {},
});
});

it('should revert back to original state after undo and redo operations', () => {
const model = new EditorJSModel({
blocks: [ {
name: 'paragraph',
data: {
text: {
value: '',
$t: 't',
},
},
} ],
});
const collaborationManager = new CollaborationManager(model);
const index = new IndexBuilder().addBlockIndex(0)
.addDataKey(createDataKey('text'))
.addTextRange([0, 4])
.build();
const operation = new Operation(OperationType.Insert, index, {
prevValue: '',
newValue: 'test',
});

collaborationManager.applyOperation(operation);
collaborationManager.undo();
collaborationManager.redo();

expect(model.serialized).toStrictEqual({
blocks: [ {
name: 'paragraph',
tunes: {},
data: {
text: {
$t: 't',
value: 'test',
fragments: [],
},
},
},
} ],
properties: {},
} ],
properties: {},
});
});
});
});
Loading

0 comments on commit 4ff6f8e

Please sign in to comment.