Skip to content

Commit

Permalink
Added redo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed Jun 19, 2024
1 parent 5449bed commit 0e8ab34
Show file tree
Hide file tree
Showing 10 changed files with 768 additions and 478 deletions.
6 changes: 6 additions & 0 deletions code/client/src/domain/editor/connectors/history/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ export default (fugue: Fugue, servicesConnector: ServiceConnector): HistoryConne
if (!Text.isText(node)) return;

if (!node.text) return;

if (node.text !== '\n') {
selection.start.column += 1;
selection.end.column += 1;
}

const reviveOperations = fugue.reviveLocal(selection);
const styleOperations = styles.map(style => {
const styleType = getStyleType(style);
Expand Down
2 changes: 2 additions & 0 deletions code/client/src/domain/editor/connectors/input/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { InputConnector } from '@domain/editor/connectors/input/types';
export default (fugue: Fugue, servicesConnector: ServiceConnector): InputConnector => {
function insertCharacter(char: string, cursor: Cursor, styles: InlineStyle[] = []) {
if (char.length !== 1) throw new Error('Invalid character');
cursor.column -= 1; // adjust column to insert character at the correct position
const operations = fugue.insertLocal(cursor, nodeInsert(char, styles));
servicesConnector.emitOperations(operations);
}
Expand All @@ -28,6 +29,7 @@ export default (fugue: Fugue, servicesConnector: ServiceConnector): InputConnect
function deleteCharacter(cursor: Cursor) {
// don't delete line if it's not a paragraph - this is to prevent deleting the block style & line simultaneously
if (cursor.column === 0 && fugue.getBlockStyle(cursor.line) !== 'paragraph') return;
cursor.column -= 1; // adjust column to delete character at the correct position
const operations = fugue.deleteLocalByCursor(cursor);
if (operations) {
servicesConnector.emitOperations(operations);
Expand Down
11 changes: 6 additions & 5 deletions code/client/src/domain/editor/fugue/Fugue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,15 @@ export class Fugue {
* @param selection
*/
reviveLocal(selection: Selection): ReviveOperation[] {
const nodes = Array.from(this.traverseBySelection(selection, true));
const _selection = { start: { ...selection.start }, end: { ...selection.end } };
const nodes = Array.from(this.traverseBySelection(_selection, true));
return nodes.map(node => {
if (node.value === '\n') {
selection.start.line++;
selection.start.column = 0;
} else selection.start.column++;
_selection.start.line++;
_selection.start.column = 0;
} else _selection.start.column++;

return this.reviveNode(node.id, selection.start);
return this.reviveNode(node.id, _selection.start);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function nodeOperation(
): InsertNodeOperation | RemoveNodeOperation | undefined {
const lineOperation = operation.path.length === 1;

const cursor = pointToCursor(editor, { path: operation.path, offset: 0 });
const cursor = pointToCursor(editor, { path: operation.path, offset: 0 }, insert_mode);

const start = lineOperation
? { line: operation.path[0] + 1, column: 0 }
Expand Down
5 changes: 3 additions & 2 deletions code/client/src/domain/editor/slate/utils/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ const pointsToSelection = (editor: Editor, start: Point, end: Point): Selection
* Converts a slate point to a cursor
* @param editor
* @param point
* @param absolutePosition
*/
export function pointToCursor(editor: Editor, point: Point): Cursor {
export function pointToCursor(editor: Editor, point: Point, absolutePosition: boolean = false): Cursor {
const line = point.path[0];
const cursor: Cursor = { line, column: point.offset };
const cursor: Cursor = { line, column: point.offset + (absolutePosition ? 0 : 1) };

if (point.path[1] === 0) return cursor;

Expand Down
18 changes: 9 additions & 9 deletions code/client/tests/editor/domain/document/inputOperations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Input Operations', () => {

test('should insert character', () => {
// given
const cursor = { line: 0, column: 0 };
const cursor = { line: 0, column: 1 }; // must mimic slate cursor

// when
_inputConnector.insertCharacter('a', cursor);
Expand All @@ -31,7 +31,7 @@ describe('Input Operations', () => {

test('should insert line break', () => {
// given
const cursor = { line: 0, column: 0 };
const cursor = { line: 0, column: 0 }; // must mimic slate cursor

// when
_inputConnector.insertLineBreak(cursor);
Expand All @@ -42,8 +42,8 @@ describe('Input Operations', () => {

test('should delete character', () => {
// given
const cursor1 = { line: 0, column: 0 };
const cursor2 = { line: 0, column: 1 };
const cursor1 = { line: 0, column: 1 }; // must mimic slate cursor
const cursor2 = { line: 0, column: 2 };
_inputConnector.insertCharacter('a', cursor1);

// when
Expand All @@ -55,9 +55,9 @@ describe('Input Operations', () => {

test('should delete by selection', () => {
// given
const cursor1 = { line: 0, column: 0 };
const cursor2 = { line: 0, column: 1 };
const cursor3 = { line: 0, column: 2 };
const cursor1 = { line: 0, column: 1 }; // must mimic slate cursor
const cursor2 = { line: 0, column: 2 };
const cursor3 = { line: 0, column: 3 };
_inputConnector.insertCharacter('a', cursor1);
_inputConnector.insertCharacter('b', cursor2);

Expand All @@ -71,12 +71,12 @@ describe('Input Operations', () => {
test('should delete word', () => {
// given
const text = 'hello world';
const cursor1 = { line: 0, column: text.length };
const cursor1 = { line: 0, column: text.length + 1 };
const cursor2 = { line: 0, column: 1 };

// when
text.split('').forEach((char, index) => {
_inputConnector.insertCharacter(char, { line: 0, column: index });
_inputConnector.insertCharacter(char, { line: 0, column: index + 1 });
});
_inputConnector.deleteWord(cursor1, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ beforeEach(() => {

describe('Undo delete line', () => {
describe('No block styles', () => {
// TODO - Add redo tests
it('empty line', () => {
// Setup the editor
pipeline.fugue.insertLocal({ line: 0, column: 0 }, '\n');
Expand Down
Loading

0 comments on commit 0e8ab34

Please sign in to comment.