Skip to content

Commit

Permalink
Started history tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed Apr 30, 2024
1 parent 0e04e09 commit 8ebf233
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 25 deletions.
3 changes: 2 additions & 1 deletion code/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@
"vite-plugin-qrcode": "^0.2.3",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.5.2"
}
},
"packageManager": "[email protected]+sha256.0624e30eff866cdeb363b15061bdb7fd9425b17bc1bb42c22f5f4efdea21f6b3"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Editor } from 'slate';
import { last } from 'lodash';
import { HistoryDomainOperations } from '@/domain/editor/domain/document/history/types';
import { HistoryDomainOperations } from '@/domain/editor/operations/history/types';
import { toHistoryOperations } from '@/domain/editor/slate/handlers/history/utils';

export type HistoryHandlers = {
Expand Down
18 changes: 5 additions & 13 deletions code/client/src/domain/editor/slate/handlers/history/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
SetNodeOperation,
SplitNodeOperation,
UnsetNodeOperation,
} from '@/domain/editor/domain/document/history/types';
} from '@/domain/editor/operations/history/types';
import { pointToCursor } from '@/domain/editor/slate/utils/selection';

const reverseTypes: { [key: string]: HistoryOperation['type'] } = {
Expand All @@ -36,7 +36,7 @@ const reverseTypes: { [key: string]: HistoryOperation['type'] } = {

const getReverseType = (type: BaseOperation['type']) => reverseTypes[type] || type;

interface Batch {
export interface Batch {
operations: BaseOperation[];
selectionBefore: Range | null;
}
Expand All @@ -63,7 +63,7 @@ function toHistoryOperations(editor: Editor, operations: Batch | undefined, reve
): HistoryOperation | undefined {
switch (type) {
case 'insert_text':
return insertTextOperation(operation as BaseInsertTextOperation, selectionBefore?.focus.offset);
return insertTextOperation(operation as BaseInsertTextOperation);
case 'remove_text':
return removeTextOperation(operation as BaseRemoveTextOperation);
case 'insert_node':
Expand All @@ -86,19 +86,11 @@ function toHistoryOperations(editor: Editor, operations: Batch | undefined, reve
/**
* Converts a slate insert text operation to a history insert text operation
* @param operation
* @param focus_offset
*/
function insertTextOperation(
operation: BaseInsertTextOperation,
focus_offset: number | undefined
): InsertTextOperation | undefined {
function insertTextOperation(operation: BaseInsertTextOperation): InsertTextOperation | undefined {
if (operation.text === '') return undefined;

const start = {
line: operation.path[0],
column: focus_offset || 0,
};

const start = pointToCursor(editor, { path: operation.path, offset: operation.offset });
const text = operation.text.split('');
return { type: 'insert_text', cursor: { ...start, column: start.column }, text };
}
Expand Down

This file was deleted.

71 changes: 71 additions & 0 deletions code/client/tests/editor/slate/handlers/history/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { withHistory } from 'slate-history';
import {
BaseOperation,
createEditor,
InsertNodeOperation,
InsertTextOperation,
MergeNodeOperation,
Node,
Path,
RemoveNodeOperation,
RemoveTextOperation,
SetNodeOperation,
SplitNodeOperation,
} from 'slate';
import { Batch } from '@/domain/editor/slate/handlers/history/utils';
import { descendant } from '@/domain/editor/slate/utils/slate';

export const mockEditor = () => {
const editor = withHistory(createEditor());
editor.children = [descendant('paragraph', '')];
return editor;
};

export const applyBatch = (editor: any, batch: Batch) => {
for (const operation of batch.operations) {
editor.apply(operation);
}
};

export const toBatch = (...operations: BaseOperation[]): Batch => {
return {
operations: operations,
selectionBefore: null,
};
};

export const insertText = (text: string, path: Path, offset: number): InsertTextOperation => {
return { type: 'insert_text', path, offset, text };
};

export const deleteText = (text: string, path: Path, offset: number): RemoveTextOperation => ({
type: 'remove_text',
path,
offset,
text,
});

export const insertNode = (node: Node, path: Path): InsertNodeOperation => ({ type: 'insert_node', path, node });

export const removeNode = (node: Node, path: Path): RemoveNodeOperation => ({ type: 'remove_node', node, path });

export const splitNode = (properties: Partial<Node>, path: Path, position: number): SplitNodeOperation => ({
type: 'split_node',
path,
properties,
position,
});

export const mergeNode = (properties: Partial<Node>, path: Path, position: number): MergeNodeOperation => ({
type: 'merge_node',
path,
properties,
position,
});

export const setNode = (properties: Partial<Node>, newProperties: Partial<Node>, path: Path): SetNodeOperation => ({
type: 'set_node',
path,
properties,
newProperties,
});
3 changes: 2 additions & 1 deletion code/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@
"tsconfig-paths": "^4.2.0",
"tsx": "^4.7.3",
"typescript": "^5.4.5"
}
},
"packageManager": "[email protected]+sha256.0624e30eff866cdeb363b15061bdb7fd9425b17bc1bb42c22f5f4efdea21f6b3"
}

0 comments on commit 8ebf233

Please sign in to comment.