Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed Jun 17, 2024
1 parent 474ad07 commit dfd2848
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 32 deletions.
9 changes: 5 additions & 4 deletions code/client/src/domain/editor/fugue/Fugue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class Fugue {
* @param cursor
*/
reviveLocalByCursor(cursor: Cursor) {
const node = this.getNodeByCursor(cursor);
const node = this.getNodeByCursor(cursor, true);
if (node) return this.reviveNode(node.id, cursor);
}

Expand Down Expand Up @@ -345,13 +345,14 @@ export class Fugue {
/**
* Returns the node at the given cursor
* @param cursor
* @param returnDeleted
*/
getNodeByCursor({ line, column }: Cursor): FugueNode | undefined {
getNodeByCursor({ line, column }: Cursor, returnDeleted : boolean = false): FugueNode | undefined {
//if (column === 0) return this.tree.getLineRoot(line);
if (line === 0 && column === 0) return this.tree.root;
const start = { line, column };
const end = { line, column: column + 1 };
const iterator = this.traverseBySelection({ start, end });
const end = { line, column: column };
const iterator = this.traverseBySelection({ start, end }, returnDeleted);
return iterator.next().value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ function removeTextOperation(editor: Editor, operation: BaseRemoveTextOperation)
// Normalize selection to account for line root nodes
const start = pointToCursor(editor, { ...operation });


const end = {
line: start.line,
column: start.column + operation.text.length,
column: start.column + operation.text.length - 1,
};

const selection = { start, end };
Expand Down Expand Up @@ -150,7 +151,7 @@ function nodeOperation(
if (!Text.isText(operation.node) || !operation.node.text) return undefined;
end = {
...start,
column: start.column + (operation.node as Text).text.length,
column: start.column + (operation.node as Text).text.length - 1,
};
}
const selection = { start, end };
Expand Down Expand Up @@ -181,7 +182,7 @@ function handleNodeOperation(

const path = [operation.path[0] + (merge_mode ? 1 : 0), 0];
const type = merge_mode ? 'merge_node' : 'split_node';
const cursor = pointToCursor(editor, { path, offset: offset || 0 });
const cursor = pointToCursor(editor, { path, offset: offset || 0 }, true);

return {
type,
Expand Down
10 changes: 8 additions & 2 deletions code/client/src/domain/editor/slate/utils/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ const pointsToSelection = (editor: Editor, start: Point, end: Point): Selection
* Converts a slate point to a cursor
* @param editor
* @param point
* @param absolute
*/
export function pointToCursor(editor: Editor, point: Point): Cursor {
export function pointToCursor(editor: Editor, point: Point, absolute : boolean = false): Cursor {
const line = point.path[0];
const cursor: Cursor = { line, column: point.offset };

if (point.path[1] === 0) return cursor;
if (point.path[1] === 0) {
cursor.column = (absolute ? cursor.column : cursor.column + 1);
return cursor;
}

const children = Array.from(Node.children(editor, [line]));

Expand All @@ -60,6 +64,8 @@ export function pointToCursor(editor: Editor, point: Point): Cursor {
}
}

// Slate offset is off by one compared to the cursor column
cursor.column = (absolute ? cursor.column : cursor.column + 1);
return cursor;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('Undo cut', () => {
it('Should work', () => {
expect(1).toBe(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Undo delete line', () => {
node: { type: 'paragraph', children: [{ text: '' }] },
},
];
pipeline.fugue.deleteLocal({ start: { line: 1, column: 0 }, end: { line: 1, column: 1 } });
pipeline.fugue.deleteLocal({ start: { line: 1, column: 0 }, end: { line: 1, column: 0 } });
pipeline.applyOperations(slateOperations);
// Extract the undo operations
const { operations } = pipeline.extractUndoOperations();
Expand All @@ -46,7 +46,7 @@ describe('Undo delete line', () => {
node: { type: 'paragraph', children: [{ text: '' }] },
},
];
pipeline.fugue.deleteLocal({ start: { line: 1, column: 0 }, end: { line: 1, column: 1 } });
pipeline.fugue.deleteLocal({ start: { line: 1, column: 0 }, end: { line: 1, column: 0 } });
pipeline.applyOperations(slateOperations);
// Extract the undo operations
const { operations } = pipeline.extractUndoOperations();
Expand All @@ -70,7 +70,7 @@ describe('Undo delete line', () => {
node: { type: 'paragraph', children: [{ text: '' }] },
},
];
pipeline.fugue.deleteLocal({ start: { line: 1, column: 0 }, end: { line: 1, column: 1 } });
pipeline.fugue.deleteLocal({ start: { line: 1, column: 0 }, end: { line: 1, column: 0 } });
pipeline.applyOperations(slateOperations);
// Extract the undo operations
const { operations } = pipeline.extractUndoOperations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Undo delete text', () => {
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});

it('Inline style', () => {
Expand All @@ -50,7 +50,7 @@ describe('Undo delete text', () => {
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});
});
describe('Block style', () => {
Expand All @@ -71,7 +71,7 @@ describe('Undo delete text', () => {
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});
it('Inline style', () => {
// Setup editor
Expand All @@ -93,7 +93,7 @@ describe('Undo delete text', () => {
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});
});
});
Expand All @@ -106,14 +106,14 @@ describe('Undo delete text', () => {
const beforeSnapshot = pipeline.takeSnapshot();
// Apply operations
const slateOperation: RemoveTextOperation[] = [{ type: 'remove_text', path: [0, 0], offset: 6, text: 'World' }];
pipeline.fugue.deleteLocal({ start: { line: 0, column: 6 }, end: { line: 0, column: 11 } });
pipeline.fugue.deleteLocal({ start: { line: 0, column: 7 }, end: { line: 0, column: 12 } });
pipeline.applyOperations(slateOperation);
// Get undo operations
const batch = pipeline.extractUndoOperations();
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});
it('Inline style', () => {
// Setup editor
Expand All @@ -125,14 +125,14 @@ describe('Undo delete text', () => {
const beforeSnapshot = pipeline.takeSnapshot();
// Apply operations
const slateOperation: RemoveTextOperation[] = [{ type: 'remove_text', path: [0, 0], offset: 6, text: 'World' }];
pipeline.fugue.deleteLocal({ start: { line: 0, column: 6 }, end: { line: 0, column: 11 } });
pipeline.fugue.deleteLocal({ start: { line: 0, column: 7 }, end: { line: 0, column: 12 } });
pipeline.applyOperations(slateOperation);
// Get undo operations
const batch = pipeline.extractUndoOperations();
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});
});
describe('Block style', () => {
Expand All @@ -144,14 +144,14 @@ describe('Undo delete text', () => {
const beforeSnapshot = pipeline.takeSnapshot();
// Apply operations
const slateOperation: RemoveTextOperation[] = [{ type: 'remove_text', path: [0, 0], offset: 6, text: 'World' }];
pipeline.fugue.deleteLocal({ start: { line: 0, column: 6 }, end: { line: 0, column: 11 } });
pipeline.fugue.deleteLocal({ start: { line: 0, column: 7 }, end: { line: 0, column: 12 } });
pipeline.applyOperations(slateOperation);
// Get undo operations
const batch = pipeline.extractUndoOperations();
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});
it('Inline style', () => {
// Setup editor
Expand All @@ -164,14 +164,14 @@ describe('Undo delete text', () => {
const beforeSnapshot = pipeline.takeSnapshot();
// Apply operations
const slateOperation: RemoveTextOperation[] = [{ type: 'remove_text', path: [0, 0], offset: 6, text: 'World' }];
pipeline.fugue.deleteLocal({ start: { line: 0, column: 6 }, end: { line: 0, column: 11 } });
pipeline.fugue.deleteLocal({ start: { line: 0, column: 7 }, end: { line: 0, column: 12 } });
pipeline.applyOperations(slateOperation);
// Get undo operations
const batch = pipeline.extractUndoOperations();
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});
});
});
Expand All @@ -184,14 +184,14 @@ describe('Undo delete text', () => {
const beforeSnapshot = pipeline.takeSnapshot();
// Apply operations
const slateOperation: RemoveTextOperation[] = [{ type: 'remove_text', path: [0, 0], offset: 10, text: 'd' }];
pipeline.fugue.deleteLocal({ start: { line: 0, column: 10 }, end: { line: 0, column: 11 } });
pipeline.fugue.deleteLocal({ start: { line: 0, column: 11 }, end: { line: 0, column: 11 } });
pipeline.applyOperations(slateOperation);
// Get undo operations
const batch = pipeline.extractUndoOperations();
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});
it('Inline style', () => {
// Setup editor
Expand All @@ -203,14 +203,14 @@ describe('Undo delete text', () => {
const beforeSnapshot = pipeline.takeSnapshot();
// Apply operations
const slateOperation: RemoveTextOperation[] = [{ type: 'remove_text', path: [0, 0], offset: 10, text: 'd' }];
pipeline.fugue.deleteLocal({ start: { line: 0, column: 10 }, end: { line: 0, column: 11 } });
pipeline.fugue.deleteLocal({ start: { line: 0, column: 11 }, end: { line: 0, column: 11 } });
pipeline.applyOperations(slateOperation);
// Get undo operations
const batch = pipeline.extractUndoOperations();
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});
});
describe('Block style', () => {
Expand All @@ -222,14 +222,14 @@ describe('Undo delete text', () => {
const beforeSnapshot = pipeline.takeSnapshot();
// Apply operations
const slateOperation: RemoveTextOperation[] = [{ type: 'remove_text', path: [0, 0], offset: 10, text: 'd' }];
pipeline.fugue.deleteLocal({ start: { line: 0, column: 10 }, end: { line: 0, column: 11 } });
pipeline.fugue.deleteLocal({ start: { line: 0, column: 11 }, end: { line: 0, column: 11 } });
pipeline.applyOperations(slateOperation);
// Get undo operations
const batch = pipeline.extractUndoOperations();
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});
it('Inline style', () => {
// Setup editor
Expand All @@ -242,14 +242,14 @@ describe('Undo delete text', () => {
const beforeSnapshot = pipeline.takeSnapshot();
// Apply operations
const slateOperation: RemoveTextOperation[] = [{ type: 'remove_text', path: [0, 0], offset: 10, text: 'd' }];
pipeline.fugue.deleteLocal({ start: { line: 0, column: 10 }, end: { line: 0, column: 11 } });
pipeline.fugue.deleteLocal({ start: { line: 0, column: 11 }, end: { line: 0, column: 11 } });
pipeline.applyOperations(slateOperation);
// Get undo operations
const batch = pipeline.extractUndoOperations();
pipeline.applyHistoryOperations(...batch.operations);
const afterSnapshot = pipeline.takeSnapshot();
// Compare
expect(beforeSnapshot).toEqual(afterSnapshot);
expect(afterSnapshot).toEqual(beforeSnapshot);
});
});
});
Expand Down

0 comments on commit dfd2848

Please sign in to comment.