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 fad4091
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 51 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/types/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

0 comments on commit fad4091

Please sign in to comment.