Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed May 10, 2024
1 parent daf6790 commit fe89fe2
Show file tree
Hide file tree
Showing 34 changed files with 62 additions and 76 deletions.
42 changes: 14 additions & 28 deletions code/client/src/domain/editor/crdt/fugue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Fugue {
}

applyOperations(operations: Operation[], override: boolean = false) {
if (override) this.tree.clear();
if (override) this.tree.clear(); // clear the tree if operations will override the current state
for (const operation of operations) {
switch (operation.type) {
case 'insert':
Expand Down Expand Up @@ -89,9 +89,9 @@ export class Fugue {

const leftOrigin = this.getNodeByCursor({ line, column })!;

if (isEmpty(leftOrigin.rightChildren))
if (isEmpty(leftOrigin.rightChildren)) {
operation = { type: 'insert', id, value, parent: leftOrigin.id, side: 'R', styles };
else {
} else {
const rightOrigin = this.tree.getLeftmostDescendant(leftOrigin.rightChildren[0]);
operation = { type: 'insert', id, value, parent: rightOrigin.id, side: 'L', styles };
}
Expand Down Expand Up @@ -183,9 +183,7 @@ export class Fugue {
* Revives a node based on the given operation
* @param operation
*/
reviveRemote(operation: ReviveOperation): void {
this.tree.reviveNode(operation.id);
}
reviveRemote = (operation: ReviveOperation) => this.tree.reviveNode(operation.id);

/**
* Updates the style of the nodes by the given selection
Expand All @@ -199,12 +197,7 @@ export class Fugue {
const { id } = node;
const style = format as InlineStyle;
this.tree.updateInlineStyle(id, style, value);
return {
type: 'inline-style',
id,
style,
value,
};
return { type: 'inline-style', id, style, value };
});
return operations;
}
Expand All @@ -215,9 +208,8 @@ export class Fugue {
* @param style
* @param value
*/
updateInlineStyleRemote({ id, style, value }: InlineStyleOperation): void {
updateInlineStyleRemote = ({ id, style, value }: InlineStyleOperation) =>
this.tree.updateInlineStyle(id, style, value);
}

/**
* Updates the style of the node based on the given operation
Expand All @@ -227,36 +219,30 @@ export class Fugue {
*/
updateBlockStyleLocal(line: number, style: BlockStyle, append: boolean = false): BlockStyleOperation {
this.tree.updateBlockStyle(style, line, append);
return {
type: 'block-style',
line,
style,
append,
};
return { type: 'block-style', line, style, append };
}

/**
* Updates the style of the nodes by the given selection
* @param style
* @param selection
*/
updateBlockStylesLocalBySelection(style: BlockStyle, selection: Selection) {
return range(selection.start.line, selection.end.line + 1).map(line => this.updateBlockStyleLocal(line, style));
}
updateBlockStylesLocalBySelection = (style: BlockStyle, selection: Selection) =>
range(selection.start.line, selection.end.line + 1)
.map(line => this.updateBlockStyleLocal(line, style));

/**
* Updates the style of the node based on the given operation
* @param line
* @param style
* @param append
*/
updateBlockStyleRemote({ line, style, append }: BlockStyleOperation) {
updateBlockStyleRemote = ({ line, style, append }: BlockStyleOperation) =>
this.tree.updateBlockStyle(style, line, append);
}

getBlockStyle(line: number): BlockStyle {
return (this.tree.root.styles[line] as BlockStyle) || 'paragraph';
}
getBlockStyle = (line: number) : BlockStyle =>
(this.tree.root.styles[line] as BlockStyle) || 'paragraph';


/**
* Traverses the tree in in-order traversal
Expand Down
File renamed without changes.
6 changes: 2 additions & 4 deletions code/client/src/domain/editor/crdt/useFugue.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useMemo } from 'react';
import { Fugue } from '@domain/editor/crdt/fugue';

function useFugue() {
return useMemo(() => new Fugue(), []);
}
const useFugue = () => useMemo(() => new Fugue(), []);

export default useFugue;
export default useFugue;
2 changes: 1 addition & 1 deletion code/client/src/domain/editor/hooks/useEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ function useEvents(fugueOperations: FugueDomainOperations, { socket }: Communica
});
}

export default useEvents;
export default useEvents;
8 changes: 2 additions & 6 deletions code/client/src/domain/editor/operations/fugue/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { FugueDomainOperations } from '@domain/editor/operations/fugue/types';
import { Operation } from '@notespace/shared/crdt/types/operations';

export default (fugue: Fugue): FugueDomainOperations => {
function applyOperations(operations: Operation[]) {
return fugue.applyOperations(operations);
}
const applyOperations = (operations: Operation[]) => fugue.applyOperations(operations);

return {
applyOperations,
};
return { applyOperations };
};
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ const normalizeDeferral = (editor: Editor, match: RegExpExecArray, apply: (edito
}

const applyRange = matchRangeRef.unref();
if (applyRange) {
apply(editor, applyRange);
}
if (applyRange) apply(editor, applyRange);
};

/**
Expand Down
4 changes: 1 addition & 3 deletions code/client/src/domain/error/useError.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useContext } from 'react';
import { ErrorContext } from '@domain/error/ErrorContext';

function useError() {
return useContext(ErrorContext);
}
const useError = () =>useContext(ErrorContext);

export default useError;
4 changes: 1 addition & 3 deletions code/client/src/domain/workspace/useWorkspace.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useContext } from 'react';
import { WorkspaceContext } from '@domain/workspace/WorkspaceContext';

function useWorkspace() {
return useContext(WorkspaceContext);
}
const useWorkspace = () => useContext(WorkspaceContext);

export default useWorkspace;
6 changes: 3 additions & 3 deletions code/client/src/services/documentServices.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { HttpCommunication } from '@/services/communication/http/httpCommunication';
import { Document } from '@notespace/shared/workspace/types/document.d.ts';
import { DocumentContent } from '@notespace/shared/workspace/document';

async function getDocument(http: HttpCommunication, id: string): Promise<Document> {
async function getDocument(http: HttpCommunication, id: string): Promise<DocumentContent> {
const { operations, title } = await http.get(`/documents/${id}`);
return { operations, title } as Document;
return { operations, title } as DocumentContent;
}

async function createDocument(http: HttpCommunication): Promise<string> {
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/ui/pages/workspace/Workspace.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState } from 'react';
import { useCommunication } from '@/services/communication/context/useCommunication';
import { DocumentData } from '@notespace/shared/workspace/types/document.d.ts';
import { DocumentData } from '@notespace/shared/workspace/document.d.ts';
import WorkspaceHeader from '@ui/pages/workspace/components/WorkspaceHeader';
import DocumentPreview from '@ui/pages/workspace/components/DocumentPreview';
import useError from '@domain/error/useError';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IoDocumentText } from 'react-icons/io5';
import { Link } from 'react-router-dom';
import { DocumentData } from '@notespace/shared/workspace/types/document.d.ts';
import { DocumentData } from '@notespace/shared/workspace/document.d.ts';
import DocumentContextMenu from '@ui/pages/workspace/components/DocumentContextMenu';
import { useEffect, useRef, useState } from 'react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@notespace/shared/crdt/types/operations';
import getFugueOperations from '@domain/editor/operations/fugue/operations';
import { FugueDomainOperations } from '@domain/editor/operations/fugue/types';
import { Document } from '@notespace/shared/workspace/types/document.d.ts';
import { Document } from '@notespace/shared/workspace/document.d.ts';
import { Node, RootNode } from '@notespace/shared/crdt/types/nodes';
import { rootNode, treeNode } from '@notespace/shared/crdt/utils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PromiseRouter from 'express-promise-router';
import { ResourceInputModel, WorkspaceResource } from '@notespace/shared/workspace/types/resource';
import { ResourceInputModel, WorkspaceResource } from '../../../../../../shared/workspace/resource';
import { httpResponse } from '@controllers/http/httpResponse';
import { Request, Response } from 'express';
import { ResourcesService } from '@services/resourcesService';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PromiseRouter from 'express-promise-router';
import { httpResponse } from '@controllers/http/httpResponse';
import { Request, Response } from 'express';
import resourcesHandlers from '@controllers/http/workspace/resourcesHandlers';
import { WorkspaceInfo } from '@notespace/shared/workspace/types/workspace';
import { WorkspaceInfo } from '@notespace/shared/workspace/workspace';
import { NoteSpaceServices } from '@services/noteSpaceServices';

function workspaceHandlers(service : NoteSpaceServices){
Expand Down
2 changes: 1 addition & 1 deletion code/server/src/ts/database/firestore/firestoreDB.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cert, initializeApp, ServiceAccount } from 'firebase-admin/app';
import serviceAccount from './firestore-key-5cddf-472039f8dbb6.json';
import { getFirestore } from 'firebase-admin/firestore';
import { DocumentContent } from '@notespace/shared/workspace/types/document';
import { DocumentContent } from '@notespace/shared/workspace/document';
import { NotFoundError } from '@domain/errors/errors';
import { Operation } from '@notespace/shared/crdt/types/operations';
import { firestore } from 'firebase-admin';
Expand Down
2 changes: 1 addition & 1 deletion code/server/src/ts/database/memory/memoryDB.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NotFoundError } from '@domain/errors/errors';
import { Operation } from '@notespace/shared/crdt/types/operations';
import { DocumentDatabase } from '@database/types';
import { DocumentContent } from '@notespace/shared/workspace/types/document';
import { DocumentContent } from '@notespace/shared/workspace/document';

export default function DocumentMemoryDB(): DocumentDatabase {
const documents: Record<string, DocumentContent> = {};
Expand Down
2 changes: 1 addition & 1 deletion code/server/src/ts/database/pg/resourcesDB.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sql from '@database/pg/sql';
import { ResourceInputModel, WorkspaceResource } from '@notespace/shared/workspace/types/resource';
import { ResourceInputModel, WorkspaceResource } from '../../../../../shared/workspace/resource';
import { ResourceDatabase } from '@database/types';

export class ResourcesDB implements ResourceDatabase{
Expand Down
4 changes: 2 additions & 2 deletions code/server/src/ts/database/pg/workspaceDB.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WorkspaceResource } from '@notespace/shared/workspace/types/resource';
import { WorkspaceInfo } from '@notespace/shared/workspace/types/workspace';
import { WorkspaceResource } from '../../../../../shared/workspace/resource';
import { WorkspaceInfo } from '@notespace/shared/workspace/workspace';
import sql from './sql';
import { NotFoundError } from '@domain/errors/errors';

Expand Down
6 changes: 3 additions & 3 deletions code/server/src/ts/database/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DocumentStorageData } from '@notespace/shared/workspace/types/document';
import { DocumentStorageData } from '@notespace/shared/workspace/document';
import { Operation } from '@notespace/shared/crdt/types/operations';
import { WorkspaceResource } from '@notespace/shared/workspace/types/resource';
import { WorkspaceInfo } from '@notespace/shared/workspace/types/workspace';
import { WorkspaceResource } from '../../../../shared/workspace/resource';
import { WorkspaceInfo } from '@notespace/shared/workspace/workspace';

/**
* Document Database - Interface for handling resources content management
Expand Down
2 changes: 1 addition & 1 deletion code/server/src/ts/services/resourcesService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ResourcesDB } from '@database/pg/resourcesDB';
import { ResourceInputModel, ResourceType, WorkspaceResource } from '@notespace/shared/workspace/types/resource';
import { ResourceInputModel, ResourceType, WorkspaceResource } from '../../../../shared/workspace/resource';
import { DocumentDatabase } from '@database/types';

export class ResourcesService {
Expand Down
4 changes: 2 additions & 2 deletions code/server/src/ts/services/workspaceService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WorkspaceResource } from '@notespace/shared/workspace/types/resource';
import { WorkspaceInfo } from '@notespace/shared/workspace/types/workspace';
import { WorkspaceResource } from '../../../../shared/workspace/resource';
import { WorkspaceInfo } from '@notespace/shared/workspace/workspace';
import { WorkspaceDB } from '@database/pg/workspaceDB';

export class WorkspaceService {
Expand Down
22 changes: 17 additions & 5 deletions code/shared/crdt/FugueTree.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Id, Node, Nodes } from "./types/nodes";
import { BlockStyle, InlineStyle } from "../types/styles";
import { BlockStyle, InlineStyle } from "../domain/styles";
import { rootNode, treeNode } from "./utils";
import { RootNode, NodeType } from "./types/nodes";

Expand All @@ -12,8 +12,8 @@ export class FugueTree<T> {
private _root: RootNode<T>;

/* Holds all the nodes in the tree.
The key is the sender id and the value is an array of nodes sent by that sender
*/
The key is the sender id and the value is an array of nodes sent by that sender
*/
private _nodes = new Map<string, NodeType<T>[]>();

constructor() {
Expand Down Expand Up @@ -41,6 +41,14 @@ export class FugueTree<T> {
this._addNode(node);
}

/**
* Adds a line root node to the tree.
* @param line
* @param id
* @param parent
* @param side
* @param styles
*/
addLineRoot(
line: number,
id: Id,
Expand All @@ -55,6 +63,10 @@ export class FugueTree<T> {
this._addNode(node);
}

/**
* Internal method to add a node to the tree.
* @param node
*/
_addNode(node: Node<T>) {
// add to nodes map
const { id } = node;
Expand All @@ -79,9 +91,9 @@ export class FugueTree<T> {
const siblings =
side === "L" ? parentNode.leftChildren : parentNode.rightChildren;
let i = 0;
while (i < siblings.length) {
while (i < siblings.length)
if (!(id.sender > siblings[i++].sender)) break;
}

siblings.splice(i, 0, id);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Style } from "../../types/styles";
import { Style } from "../../domain/styles";

export type Id = {
sender: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Id } from "./nodes";
import { InlineStyle, BlockStyle } from "../../types/styles";
import { InlineStyle, BlockStyle } from "../../domain/styles";

export type InsertOperation = {
line?: number;
Expand Down
2 changes: 1 addition & 1 deletion code/shared/crdt/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Node, Id, RootNode } from "./types/nodes";
import { InlineStyle } from "../types/styles";
import { InlineStyle } from "../domain/styles";

export function rootNode<T>(): RootNode<T> {
return {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Operation } from "../../crdt/types/operations";
import { Operation } from "../crdt/types/operations";

export type DocumentContent = {
operations: Operation[];
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit fe89fe2

Please sign in to comment.