diff --git a/code/client/src/domain/editor/connectors/history/connector.ts b/code/client/src/domain/editor/connectors/history/connector.ts index 9eca8f35..a3fd4167 100644 --- a/code/client/src/domain/editor/connectors/history/connector.ts +++ b/code/client/src/domain/editor/connectors/history/connector.ts @@ -5,7 +5,9 @@ import { BlockStyle, getStyleType, InlineStyle } from '@notespace/shared/src/doc import { ServiceConnector } from '@domain/editor/connectors/service/connector'; import { ApplyHistory, - HistoryConnector, + HistoryConnector +} from '@domain/editor/connectors/history/types' +import { HistoryOperation, InsertNodeOperation, InsertTextOperation, @@ -15,7 +17,7 @@ import { SetNodeOperation, SplitNodeOperation, UnsetNodeOperation, -} from '@domain/editor/connectors/history/types'; +} from '@domain/editor/shared/historyTypes'; import { Operation } from '@notespace/shared/src/document/types/operations'; export default (fugue: Fugue, servicesConnector: ServiceConnector): HistoryConnector => { diff --git a/code/client/src/domain/editor/connectors/history/types.d.ts b/code/client/src/domain/editor/connectors/history/types.d.ts index de0a7369..2138e978 100644 --- a/code/client/src/domain/editor/connectors/history/types.d.ts +++ b/code/client/src/domain/editor/connectors/history/types.d.ts @@ -1,4 +1,4 @@ -import { HistoryOperation } from '@domain/editor/slate/operations/history/types'; +import { HistoryOperation } from '@domain/editor/shared/historyTypes'; export type HistoryConnector = { applyHistoryOperation: ApplyHistory; diff --git a/code/client/src/domain/editor/slate/operations/history/types.d.ts b/code/client/src/domain/editor/shared/historyTypes.d.ts similarity index 62% rename from code/client/src/domain/editor/slate/operations/history/types.d.ts rename to code/client/src/domain/editor/shared/historyTypes.d.ts index 113dbf7f..5028cab9 100644 --- a/code/client/src/domain/editor/slate/operations/history/types.d.ts +++ b/code/client/src/domain/editor/shared/historyTypes.d.ts @@ -1,24 +1,24 @@ import { - BaseInsertNodeOperation, - BaseInsertTextOperation, - BaseMergeNodeOperation, - BaseRemoveNodeOperation, - BaseRemoveTextOperation, - BaseSetNodeOperation, - BaseSplitNodeOperation, - Node, + BaseInsertNodeOperation, + BaseInsertTextOperation, + BaseMergeNodeOperation, + BaseRemoveNodeOperation, + BaseRemoveTextOperation, + BaseSetNodeOperation, + BaseSplitNodeOperation, + Node, } from 'slate'; import { Cursor, Selection } from '@domain/editor/cursor'; export type HistoryOperation = - | InsertTextOperation - | RemoveTextOperation - | InsertNodeOperation - | RemoveNodeOperation - | SplitNodeOperation - | MergeNodeOperation - | SetNodeOperation - | UnsetNodeOperation; + | InsertTextOperation + | RemoveTextOperation + | InsertNodeOperation + | RemoveNodeOperation + | SplitNodeOperation + | MergeNodeOperation + | SetNodeOperation + | UnsetNodeOperation; /** * Base operation for inserting text @@ -27,9 +27,9 @@ export type HistoryOperation = * @param text - The text to insert */ export type InsertTextOperation = { - type: BaseInsertTextOperation['type']; - cursor: Cursor; - text: string[]; + type: BaseInsertTextOperation['type']; + cursor: Cursor; + text: string[]; }; /** @@ -38,8 +38,8 @@ export type InsertTextOperation = { * @param selection - The selection to remove the text */ export type RemoveTextOperation = { - type: BaseRemoveTextOperation['type']; - selection: Selection; + type: BaseRemoveTextOperation['type']; + selection: Selection; }; /** @@ -49,10 +49,10 @@ export type RemoveTextOperation = { * @param selection - The selection to insert the node */ export type InsertNodeOperation = { - type: BaseInsertNodeOperation['type']; - node: Node; - lineOperation: boolean; - selection: Selection; + type: BaseInsertNodeOperation['type']; + node: Node; + lineOperation: boolean; + selection: Selection; }; /** @@ -62,10 +62,10 @@ export type InsertNodeOperation = { * @param selection - The selection to remove the node */ export type RemoveNodeOperation = { - type: BaseRemoveNodeOperation['type']; - node: Node; - lineOperation: boolean; - selection: Selection; + type: BaseRemoveNodeOperation['type']; + node: Node; + lineOperation: boolean; + selection: Selection; }; /** @@ -75,9 +75,9 @@ export type RemoveNodeOperation = { * @param cursor - The cursor position to split the node */ export type SplitNodeOperation = { - type: BaseSplitNodeOperation['type']; - properties: Partial; - cursor: Cursor; + type: BaseSplitNodeOperation['type']; + properties: Partial; + cursor: Cursor; }; /** @@ -87,9 +87,9 @@ export type SplitNodeOperation = { * @param cursor - The cursor position to merge the node */ export type MergeNodeOperation = { - type: BaseMergeNodeOperation['type']; - properties: Partial; - cursor: Cursor; + type: BaseMergeNodeOperation['type']; + properties: Partial; + cursor: Cursor; }; /** @@ -100,10 +100,10 @@ export type MergeNodeOperation = { * @param newProperties - The new properties of the node */ export type SetNodeOperation = { - type: BaseSetNodeOperation['type']; - lineOperation: boolean; - selection: Selection; - properties: Partial; + type: BaseSetNodeOperation['type']; + lineOperation: boolean; + selection: Selection; + properties: Partial; }; /** @@ -114,8 +114,8 @@ export type SetNodeOperation = { * @param newProperties - The new properties of the node */ export type UnsetNodeOperation = { - type: 'unset_node'; - lineOperation: boolean; - selection: Selection; - properties: Partial; + type: 'unset_node'; + lineOperation: boolean; + selection: Selection; + properties: Partial; }; diff --git a/code/client/src/domain/editor/slate/operations/history/toHistoryOperations.ts b/code/client/src/domain/editor/slate/operations/history/toHistoryOperations.ts index b94cffc1..f1ea02a8 100644 --- a/code/client/src/domain/editor/slate/operations/history/toHistoryOperations.ts +++ b/code/client/src/domain/editor/slate/operations/history/toHistoryOperations.ts @@ -23,7 +23,7 @@ import { SetNodeOperation, SplitNodeOperation, UnsetNodeOperation, -} from '@domain/editor/slate/operations/history/types'; +} from '@domain/editor/shared/historyTypes'; import { pointToCursor } from '@domain/editor/slate/utils/selection'; const reverseTypes: { [key: string]: HistoryOperation['type'] } = { diff --git a/code/client/tests/editor/domain/document/fugueOperations.test.ts b/code/client/tests/editor/domain/document/fugueOperations.test.ts index 55d89258..b2b46aef 100644 --- a/code/client/tests/editor/domain/document/fugueOperations.test.ts +++ b/code/client/tests/editor/domain/document/fugueOperations.test.ts @@ -4,21 +4,29 @@ import { InsertOperation, DeleteOperation, InlineStyleOperation, - BlockStyleOperation, + BlockStyleOperation, Operation, } from '@notespace/shared/src/document/types/operations'; -import getFugueOperations from '@domain/editor/fugue/operations/fugue/operations'; -import { FugueDomainOperations } from '@domain/editor/fugue/operations/fugue/types'; -import { Document } from '@notespace/shared/src/workspace/types/document'; import { Node, RootNode } from '@domain/editor/fugue/nodes'; -import { rootNode, treeNode } from '@notespace/shared/src/document/utils'; +import {ServiceConnector} from "@domain/editor/connectors/service/connector"; +import {DocumentContent} from "@notespace/shared/src/workspace/types/document"; +import {rootNode, treeNode} from "@domain/editor/fugue/utils"; + +import serviceConnector from '@domain/editor/connectors/service/connector'; +import {mockCommunication} from "@tests/mocks/mockCommunication"; + + + describe('Fugue Operations', () => { let fugue: Fugue; - let fugueOperations: FugueDomainOperations; + const communication = mockCommunication(); + let _serviceConnector: ServiceConnector; + let applyOperations: (operations: Operation[]) => void; beforeEach(() => { fugue = new Fugue(); - fugueOperations = getFugueOperations(fugue); + _serviceConnector = serviceConnector(fugue, communication); + applyOperations = _serviceConnector.applyFugueOperations; }); test('should apply operations', () => { @@ -29,10 +37,11 @@ describe('Fugue Operations', () => { value: 'a', parent: { sender: 'root', counter: 0 }, side: 'R', + cursor: { line: 0, column: 0} }; // when - fugueOperations.applyOperations([insertOperation]); + applyOperations([insertOperation]); // then expect(fugue.toString()).toEqual('a'); @@ -41,10 +50,11 @@ describe('Fugue Operations', () => { const deleteOperation: DeleteOperation = { type: 'delete', id: { sender: 'A', counter: 0 }, + cursor: { line: 0, column: 0 } }; // when - fugueOperations.applyOperations([deleteOperation]); + applyOperations([deleteOperation]); // then expect(fugue.toString()).toEqual(''); @@ -53,7 +63,7 @@ describe('Fugue Operations', () => { const insertOperation2 = { ...insertOperation, id: { sender: 'A', counter: 1 }, value: 'b' }; // when - fugueOperations.applyOperations([insertOperation2]); + applyOperations([insertOperation2]); // given const inlineStyleOperation: InlineStyleOperation = { @@ -64,7 +74,7 @@ describe('Fugue Operations', () => { }; // when - fugueOperations.applyOperations([inlineStyleOperation]); + applyOperations([inlineStyleOperation]); // then expect(fugue.getNodeByCursor({ line: 0, column: 1 })?.styles).toEqual(['bold']); @@ -78,7 +88,7 @@ describe('Fugue Operations', () => { }; // when - fugueOperations.applyOperations([blockStyleOperation]); + applyOperations([blockStyleOperation]); // then expect(fugue.getBlockStyle(0)).toEqual('paragraph'); @@ -91,17 +101,15 @@ describe('Fugue Operations', () => { const node2: Node = treeNode({ sender: 'A', counter: 1 }, 'b', node1.id, 'R', 2); root.rightChildren = [node1.id]; node1.rightChildren = [node2.id]; - const document: Document = { - id: 'test', - title: 'test', + const document: DocumentContent = { operations: [ - { type: 'insert', ...node1, parent: root.id, styles: [] }, - { type: 'insert', ...node2, parent: node1.id, styles: [] }, + { type: 'insert', ...node1, parent: root.id, styles: [], cursor: { line: 0, column: 0 }}, + { type: 'insert', ...node2, parent: node1.id, styles: [], cursor: { line: 0, column: 1 }}, ], }; // when - fugueOperations.applyOperations(document.operations); + applyOperations(document.operations); // then expect(fugue.toString()).toEqual('ab'); diff --git a/code/client/tests/editor/domain/document/inputOperations.test.ts b/code/client/tests/editor/domain/document/inputOperations.test.ts index b7f53448..e0ef232b 100644 --- a/code/client/tests/editor/domain/document/inputOperations.test.ts +++ b/code/client/tests/editor/domain/document/inputOperations.test.ts @@ -1,17 +1,21 @@ import { describe, test, expect, beforeEach } from 'vitest'; import { Fugue } from '@domain/editor/fugue/Fugue'; -import getInputOperations from '@domain/editor/fugue/operations/input/operations'; -import { InputDomainOperations } from '@domain/editor/fugue/operations/input/types'; + import { mockCommunication } from '@tests/mocks/mockCommunication'; +import {InputConnector} from "@domain/editor/connectors/input/types"; +import serviceConnector, {ServiceConnector} from "@domain/editor/connectors/service/connector"; +import inputConnector from "@domain/editor/connectors/input/connector"; describe('Input Operations', () => { const communication = mockCommunication(); let fugue: Fugue; - let inputOperations: InputDomainOperations; + let _inputConnector : InputConnector; + let servicesConnector : ServiceConnector; beforeEach(() => { fugue = new Fugue(); - inputOperations = getInputOperations(fugue, communication); + servicesConnector = serviceConnector(fugue, communication); + _inputConnector = inputConnector(fugue, servicesConnector); }); test('should insert character', () => { @@ -19,7 +23,7 @@ describe('Input Operations', () => { const cursor = { line: 0, column: 0 }; // when - inputOperations.insertCharacter('a', cursor); + _inputConnector.insertCharacter('a', cursor); // then expect(fugue.toString()).toEqual('a'); @@ -30,7 +34,7 @@ describe('Input Operations', () => { const cursor = { line: 0, column: 0 }; // when - inputOperations.insertLineBreak(cursor); + _inputConnector.insertLineBreak(cursor); // then expect(fugue.toString()).toEqual('\n'); @@ -40,10 +44,10 @@ describe('Input Operations', () => { // given const cursor1 = { line: 0, column: 0 }; const cursor2 = { line: 0, column: 1 }; - inputOperations.insertCharacter('a', cursor1); + _inputConnector.insertCharacter('a', cursor1); // when - inputOperations.deleteCharacter(cursor2); + _inputConnector.deleteCharacter(cursor2); // then expect(fugue.toString()).toEqual(''); @@ -54,11 +58,11 @@ describe('Input Operations', () => { const cursor1 = { line: 0, column: 0 }; const cursor2 = { line: 0, column: 1 }; const cursor3 = { line: 0, column: 2 }; - inputOperations.insertCharacter('a', cursor1); - inputOperations.insertCharacter('b', cursor2); + _inputConnector.insertCharacter('a', cursor1); + _inputConnector.insertCharacter('b', cursor2); // when - inputOperations.deleteSelection({ start: cursor1, end: cursor3 }); + _inputConnector.deleteSelection({ start: cursor1, end: cursor3 }); // then expect(fugue.toString()).toEqual(''); @@ -72,15 +76,15 @@ describe('Input Operations', () => { // when text.split('').forEach((char, index) => { - inputOperations.insertCharacter(char, { line: 0, column: index }); + _inputConnector.insertCharacter(char, { line: 0, column: index }); }); - inputOperations.deleteWord(cursor1, true); + _inputConnector.deleteWord(cursor1, true); // then expect(fugue.toString()).toEqual('hello '); // when - inputOperations.deleteWord(cursor2, false); + _inputConnector.deleteWord(cursor2, false); // then expect(fugue.toString()).toEqual(' '); @@ -92,7 +96,7 @@ describe('Input Operations', () => { const cursor = { line: 0, column: 0 }; // when - inputOperations.pasteText(cursor, text); + _inputConnector.pasteText(cursor, text); // then expect(fugue.toString()).toEqual(text); diff --git a/code/client/tests/editor/domain/document/markdownOperations.test.ts b/code/client/tests/editor/domain/document/markdownOperations.test.ts index 05d7018e..aad48d96 100644 --- a/code/client/tests/editor/domain/document/markdownOperations.test.ts +++ b/code/client/tests/editor/domain/document/markdownOperations.test.ts @@ -1,23 +1,27 @@ import { describe, test, expect, beforeEach } from 'vitest'; import { Fugue } from '@domain/editor/fugue/Fugue'; -import getMarkdownOperations from '@domain/editor/fugue/operations/markdown/operations'; import { mockCommunication } from '@tests/mocks/mockCommunication'; -import { MarkdownDomainOperations } from '@domain/editor/fugue/operations/markdown/types'; import { toSlate } from '@domain/editor/slate/utils/slate'; +import {MarkdownConnector} from "@domain/editor/connectors/markdown/types"; +import {ServiceConnector} from "@domain/editor/connectors/service/connector"; +import markdownConnector from "@domain/editor/connectors/markdown/connector"; +import serviceConnector from "@domain/editor/connectors/service/connector"; describe('Markdown Operations', () => { const communication = mockCommunication(); let fugue: Fugue; - let markdownOperations: MarkdownDomainOperations; + let _markdownConnector: MarkdownConnector; + let servicesConnector: ServiceConnector; beforeEach(() => { fugue = new Fugue(); - markdownOperations = getMarkdownOperations(fugue, communication); + servicesConnector = serviceConnector(fugue, communication); + _markdownConnector = markdownConnector(fugue, servicesConnector); }); test('should apply block style to line', () => { // when - markdownOperations.applyBlockStyle('heading-one', 0); + _markdownConnector.applyBlockStyle('heading-one', 0); // then expect(toSlate(fugue)).toEqual([{ type: 'heading-one', children: [{ text: '' }] }]); @@ -31,7 +35,7 @@ describe('Markdown Operations', () => { fugue.insertLocal(cursor, ...text.split('')); // when - markdownOperations.applyInlineStyle('bold', selection, true); + _markdownConnector.applyInlineStyle('bold', selection, true); // then expect(toSlate(fugue)).toEqual([ @@ -50,9 +54,9 @@ describe('Markdown Operations', () => { // when fugue.insertLocal(cursor1, ...text.split('')); - markdownOperations.applyBlockStyle('heading-one', 0); - markdownOperations.applyBlockStyle('list-item', 1); - markdownOperations.deleteBlockStyles(selection1); + _markdownConnector.applyBlockStyle('heading-one', 0); + _markdownConnector.applyBlockStyle('list-item', 1); + _markdownConnector.deleteBlockStyles(selection1); // then expect(toSlate(fugue)).toEqual([ @@ -61,8 +65,8 @@ describe('Markdown Operations', () => { ]); // when - markdownOperations.applyBlockStyle('heading-two', 0); - markdownOperations.deleteBlockStyles(selection2); + _markdownConnector.applyBlockStyle('heading-two', 0); + _markdownConnector.deleteBlockStyles(selection2); // then expect(toSlate(fugue)).toEqual([ diff --git a/code/client/tests/editor/fugue/fugue.test.ts b/code/client/tests/editor/fugue/fugue.test.ts index a3ad5f68..56c4b3fb 100644 --- a/code/client/tests/editor/fugue/fugue.test.ts +++ b/code/client/tests/editor/fugue/fugue.test.ts @@ -42,6 +42,7 @@ describe('Fugue', () => { value: 'a', parent: { sender: 'root', counter: 0 }, side: 'R', + cursor: { line: 0, column: 0 }, }; // when @@ -88,10 +89,12 @@ describe('Fugue', () => { value: 'x', parent: { sender: 'root', counter: 0 }, side: 'R', + cursor: { line: 0, column: 0 }, }; const deleteOperation: DeleteOperation = { type: 'delete', id: { sender: 'A', counter: 0 }, + cursor: { line: 0, column: 0 }, }; // when @@ -126,6 +129,7 @@ describe('Fugue', () => { value: 'x', parent: { sender: 'root', counter: 0 }, side: 'R', + cursor: { line: 0, column: 0 }, }; const styleOperation: InlineStyleOperation = { type: 'inline-style', diff --git a/code/client/tests/editor/fugue/tree.test.ts b/code/client/tests/editor/fugue/tree.test.ts index dded7b53..e5b7db3e 100644 --- a/code/client/tests/editor/fugue/tree.test.ts +++ b/code/client/tests/editor/fugue/tree.test.ts @@ -18,6 +18,7 @@ describe('FugueTree', () => { value: 'a', parent: { sender: 'root', counter: 0 }, side: 'L', + cursor: { line: 0, column: 0 }, }; const rootId = { sender: 'root', counter: 0 }; const { id, value, parent, side } = operation; @@ -41,6 +42,7 @@ describe('FugueTree', () => { value: 'a', parent: { sender: 'root', counter: 0 }, side: 'L', + cursor: { line: 0, column: 0 }, }; // when tree.addNode(id, value, parent, side); diff --git a/code/client/tests/editor/fugue/utils.ts b/code/client/tests/editor/fugue/utils.ts deleted file mode 100644 index 74701459..00000000 --- a/code/client/tests/editor/fugue/utils.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Operation } from '@notespace/shared/src/document/types/operations'; -import { FugueTree } from '@domain/editor/fugue/FugueTree'; - -/** - * Applies the given operations to the tree - * @param tree the tree to apply the operations to - * @param operations the operations to apply - */ -export function applyOperations(tree: FugueTree, operations: Operation[]) { - for (const operation of operations) { - switch (operation.type) { - case 'insert': - tree.addNode(operation.id, operation.value, operation.parent || tree.root.id, operation.side, []); - break; - case 'delete': - tree.deleteNode(operation.id); - break; - case 'inline-style': - tree.updateInlineStyle(operation.id, operation.style, operation.value); - break; - case 'block-style': - tree.updateBlockStyle(operation.style, operation.line); - break; - case 'revive': - tree.reviveNode(operation.id); - break; - default: - throw new Error('Invalid operation type'); - } - } -} diff --git a/code/client/tests/editor/slate/handlers/history/cut.test.ts b/code/client/tests/editor/slate/handlers/history/cut.test.ts index 05ab0163..60e9516d 100644 --- a/code/client/tests/editor/slate/handlers/history/cut.test.ts +++ b/code/client/tests/editor/slate/handlers/history/cut.test.ts @@ -9,9 +9,9 @@ import { getUndoOperations, getRedoOperations, removeNode, -} from '@tests/editor/slate/operations/history/utils'; +} from '@tests/editor/slate/handlers/history/utils'; import { toSlate } from '@domain/editor/slate/utils/slate'; -import { InsertTextOperation, RemoveTextOperation } from '@domain/editor/fugue/operations/history/types'; +import { InsertTextOperation, RemoveTextOperation } from '@domain/editor/shared/historyTypes'; import { pointToCursor } from '@domain/editor/slate/utils/selection'; import { nodeInsert } from '@domain/editor/fugue/utils'; diff --git a/code/client/tests/editor/slate/handlers/history/delete-text.test.ts b/code/client/tests/editor/slate/handlers/history/delete-text.test.ts index 948e98c5..75a96cad 100644 --- a/code/client/tests/editor/slate/handlers/history/delete-text.test.ts +++ b/code/client/tests/editor/slate/handlers/history/delete-text.test.ts @@ -10,14 +10,14 @@ import { removeNode, setNode, toBatch, -} from '@tests/editor/slate/operations/history/utils'; +} from '@tests/editor/slate/handlers/history/utils'; import { toSlate } from '@domain/editor/slate/utils/slate'; import { InsertTextOperation, RemoveTextOperation, SetNodeOperation, UnsetNodeOperation, -} from '@domain/editor/fugue/operations/history/types'; +} from '@domain/editor/shared/historyTypes'; import { pointToCursor } from '@domain/editor/slate/utils/selection'; import { BlockStyles } from '@notespace/shared/src/document/types/styles'; diff --git a/code/client/tests/editor/slate/handlers/history/insert-text.test.ts b/code/client/tests/editor/slate/handlers/history/insert-text.test.ts index bfadde61..85adf651 100644 --- a/code/client/tests/editor/slate/handlers/history/insert-text.test.ts +++ b/code/client/tests/editor/slate/handlers/history/insert-text.test.ts @@ -18,7 +18,7 @@ import { RemoveTextOperation, SetNodeOperation, UnsetNodeOperation, -} from '@domain/editor/fugue/operations/history/types'; +} from '@domain/editor/shared/historyTypes'; import { BaseInsertNodeOperation, BaseInsertTextOperation, diff --git a/code/client/tests/editor/slate/handlers/history/paste.test.ts b/code/client/tests/editor/slate/handlers/history/paste.test.ts index 45908907..3b1fb28b 100644 --- a/code/client/tests/editor/slate/handlers/history/paste.test.ts +++ b/code/client/tests/editor/slate/handlers/history/paste.test.ts @@ -8,9 +8,9 @@ import { insertText, mockEditor, toBatch, -} from '@tests/editor/slate/operations/history/utils'; +} from '@tests/editor/slate/handlers/history/utils'; import { toSlate } from '@domain/editor/slate/utils/slate'; -import { InsertTextOperation, RemoveTextOperation } from '@domain/editor/fugue/operations/history/types'; +import { InsertTextOperation, RemoveTextOperation } from '@domain/editor/shared/historyTypes'; import { pointToCursor } from '@domain/editor/slate/utils/selection'; let editor: Editor;