Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed Jun 15, 2024
1 parent 72993cb commit 3a94c2d
Show file tree
Hide file tree
Showing 16 changed files with 960 additions and 937 deletions.
7 changes: 2 additions & 5 deletions code/client/src/domain/editor/connectors/history/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { Fugue } from '@domain/editor/fugue/Fugue';
import { ReviveOperation } from '@notespace/shared/src/document/types/operations';
import { BlockStyle, getStyleType, InlineStyle } from '@notespace/shared/src/document/types/styles';
import { ServiceConnector } from '@domain/editor/connectors/service/connector';
import {
ApplyHistory,
HistoryConnector
} from '@domain/editor/connectors/history/types'
import { ApplyHistory, HistoryConnector } from '@domain/editor/connectors/history/types';
import {
HistoryOperation,
InsertNodeOperation,
Expand Down Expand Up @@ -84,7 +81,7 @@ export default (fugue: Fugue, servicesConnector: ServiceConnector): HistoryConne
if (!Element.isElement(node)) return;
const reviveOperation = fugue.reviveLocalByCursor(selection.start) as ReviveOperation;
const style = node.type;
const styleOperation = fugue.updateBlockStyleLocal(selection.start.line, style as BlockStyle)
const styleOperation = fugue.updateBlockStyleLocal(selection.start.line, style as BlockStyle);
return [reviveOperation, styleOperation];
}

Expand Down
25 changes: 13 additions & 12 deletions code/client/src/domain/editor/fugue/Fugue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FugueTree } from '@domain/editor/fugue/FugueTree';
import { generateReplicaId, nodeInsert } from './utils';
import { type FugueNode, type NodeInsert } from '@domain/editor/fugue/types';
import { Cursor, Selection } from '@domain/editor/cursor';
import { isEmpty, last, range } from 'lodash';
import { clamp, isEmpty, last, range } from 'lodash';
import { Id } from '@notespace/shared/src/document/types/types';
import {
BlockStyleOperation,
Expand Down Expand Up @@ -269,7 +269,7 @@ export class Fugue {

/**
* Traverses the tree by the given selection
* @param selection - the selection from which to traverse, in format ]start, end]
* @param selection - the selection from which to traverse, in format [start, end[
* @param returnDeleted
*/
*traverseBySelection(selection: Selection, returnDeleted: boolean = false): IterableIterator<FugueNode> {
Expand All @@ -280,20 +280,20 @@ export class Fugue {
// const lineRootNode = this.tree.getLineRoot(start.line);

for (const node of this.tree.traverse(this.tree.root, returnDeleted)) {
// start condition
if (lineCounter === start.line && columnCounter === start.column) inBounds = true;

// yield node if in bounds
if (inBounds) yield node;

// update counters
if (node.value === '\n') {
lineCounter++;
columnCounter = 0;
} else {
columnCounter++;
}
// end condition
// check if in bounds
if (lineCounter === start.line && columnCounter === start.column) inBounds = true;

// yield node if in bounds
if (inBounds) yield node;

// end condition - check if next node is out of bounds
if (lineCounter === end.line && columnCounter === end.column) break;
}
}
Expand Down Expand Up @@ -347,9 +347,10 @@ export class Fugue {
* @param cursor
*/
getNodeByCursor({ line, column }: Cursor): FugueNode | undefined {
if (column === 0) return this.tree.getLineRoot(line);
const start = { line, column: column - 1 };
const end = { line, column };
//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 });
return iterator.next().value;
}
Expand Down
4 changes: 2 additions & 2 deletions code/client/src/domain/editor/fugue/FugueTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ export class FugueTree<T> {

// Update line root at the root node with
if (!node.isDeleted) node.isDeleted = true;
if (node.value === '\n'){
if (node.value === '\n') {
const idx = this._root.value.findIndex(n => n.id === id);
this._root.value[idx].isDeleted = true;
this._root.value[idx].isDeleted = true;
}
}

Expand Down
86 changes: 43 additions & 43 deletions code/client/src/domain/editor/shared/historyTypes.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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[];
};

/**
Expand All @@ -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;
};

/**
Expand All @@ -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;
};

/**
Expand All @@ -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;
};

/**
Expand All @@ -75,9 +75,9 @@ export type RemoveNodeOperation = {
* @param cursor - The cursor position to split the node
*/
export type SplitNodeOperation = {
type: BaseSplitNodeOperation['type'];
properties: Partial<Node>;
cursor: Cursor;
type: BaseSplitNodeOperation['type'];
properties: Partial<Node>;
cursor: Cursor;
};

/**
Expand All @@ -87,9 +87,9 @@ export type SplitNodeOperation = {
* @param cursor - The cursor position to merge the node
*/
export type MergeNodeOperation = {
type: BaseMergeNodeOperation['type'];
properties: Partial<Node>;
cursor: Cursor;
type: BaseMergeNodeOperation['type'];
properties: Partial<Node>;
cursor: Cursor;
};

/**
Expand All @@ -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<Node>;
type: BaseSetNodeOperation['type'];
lineOperation: boolean;
selection: Selection;
properties: Partial<Node>;
};

/**
Expand All @@ -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<Node>;
type: 'unset_node';
lineOperation: boolean;
selection: Selection;
properties: Partial<Node>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,16 @@ function insertTextOperation(editor: Editor, operation: BaseInsertTextOperation)
* @param editor
* @param operation
*/
function removeTextOperation(
editor: Editor,
operation: BaseRemoveTextOperation,
): RemoveTextOperation | undefined {
function removeTextOperation(editor: Editor, operation: BaseRemoveTextOperation): RemoveTextOperation | undefined {
if (operation.text === '') return undefined;

// Normalize selection to account for line root nodes
const start = pointToCursor(editor, {...operation });
const start = pointToCursor(editor, { ...operation });

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

const selection = { start, end };
return { type: 'remove_text', selection };
Expand All @@ -141,20 +138,20 @@ 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 });

const start = lineOperation
? { line: operation.path[0] + 1, column: 0 }
: { ...cursor, column: cursor.column + lineOffset(cursor.line) };

let end = start;

if(!lineOperation){
if(!Text.isText(operation.node) || !operation.node.text) return undefined;
if (!lineOperation) {
if (!Text.isText(operation.node) || !operation.node.text) return undefined;
end = {
...start,
column: start.column + (operation.node as Text).text.length,
}
};
}
const selection = { start, end };
return {
Expand Down
22 changes: 10 additions & 12 deletions code/client/tests/editor/domain/document/fugueOperations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import {
InsertOperation,
DeleteOperation,
InlineStyleOperation,
BlockStyleOperation, Operation,
BlockStyleOperation,
Operation,
} from '@notespace/shared/src/document/types/operations';
import { Node, RootNode } from '@domain/editor/fugue/nodes';
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 { 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";



import { mockCommunication } from '@tests/mocks/mockCommunication';

describe('Fugue Operations', () => {
let fugue: Fugue;
Expand All @@ -37,7 +35,7 @@ describe('Fugue Operations', () => {
value: 'a',
parent: { sender: 'root', counter: 0 },
side: 'R',
cursor: { line: 0, column: 0}
cursor: { line: 0, column: 0 },
};

// when
Expand All @@ -50,7 +48,7 @@ describe('Fugue Operations', () => {
const deleteOperation: DeleteOperation = {
type: 'delete',
id: { sender: 'A', counter: 0 },
cursor: { line: 0, column: 0 }
cursor: { line: 0, column: 0 },
};

// when
Expand Down Expand Up @@ -103,8 +101,8 @@ describe('Fugue Operations', () => {
node1.rightChildren = [node2.id];
const document: DocumentContent = {
operations: [
{ type: 'insert', ...node1, parent: root.id, styles: [], cursor: { line: 0, column: 0 }},
{ type: 'insert', ...node2, parent: node1.id, styles: [], cursor: { line: 0, column: 1 }},
{ type: 'insert', ...node1, parent: root.id, styles: [], cursor: { line: 0, column: 0 } },
{ type: 'insert', ...node2, parent: node1.id, styles: [], cursor: { line: 0, column: 1 } },
],
};

Expand Down
10 changes: 5 additions & 5 deletions code/client/tests/editor/domain/document/inputOperations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { describe, test, expect, beforeEach } from 'vitest';
import { Fugue } from '@domain/editor/fugue/Fugue';

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";
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 _inputConnector : InputConnector;
let servicesConnector : ServiceConnector;
let _inputConnector: InputConnector;
let servicesConnector: ServiceConnector;

beforeEach(() => {
fugue = new Fugue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { describe, test, expect, beforeEach } from 'vitest';
import { Fugue } from '@domain/editor/fugue/Fugue';
import { mockCommunication } from '@tests/mocks/mockCommunication';
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";
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();
Expand Down
Loading

0 comments on commit 3a94c2d

Please sign in to comment.