Skip to content

Commit

Permalink
Backend Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed May 10, 2024
1 parent 0ddb415 commit 0c9b822
Show file tree
Hide file tree
Showing 88 changed files with 369 additions and 424 deletions.
8 changes: 4 additions & 4 deletions code/client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BrowserRouter as Router, Route, Routes, Navigate } from 'react-router-dom';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import { CommunicationProvider } from '@/services/communication/context/CommunicationContext';
import { communication } from '@/services/communication/communication';
import Document from '@ui/pages/document/Document';
Expand All @@ -21,9 +21,9 @@ function App() {
<Header />
<Routes>
<WorkspaceProvider>
<Route path="/" element={<Navigate to={`/documents`} />} />
<Route path="/documents" element={<Workspace />} />
<Route path="/documents/:id" element={<Document />} />
<Route path="/" element={<Home />} />
<Route path="/workspaces/:wid" element={<Workspace />} />
<Route path="/workspaces/:wid/:id" element={<Document />} />
</WorkspaceProvider>
<Route path="*" element={<NotFound />} />
</Routes>
Expand Down
10 changes: 5 additions & 5 deletions code/client/src/domain/editor/crdt/fugue.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type Id } from '@notespace/shared/crdt/types/nodes';
import { BlockStyle, InlineStyle } from '@notespace/shared/types/styles';
import { FugueTree } from '@notespace/shared/crdt/FugueTree';
import { type Id } from '@notespace/shared/document/types/nodes';
import { BlockStyle, InlineStyle } from '@notespace/shared/document/types/styles';
import { FugueTree } from '@notespace/shared/document/FugueTree.ts';
import { generateReplicaId, nodeInsert } from './utils';
import { type FugueNode, type NodeInsert } from '@domain/editor/crdt/types';
import { Cursor, Selection } from '@notespace/shared/types/cursor';
import { Cursor, Selection } from '@notespace/shared/document/types/cursor';
import { isEmpty, last, range } from 'lodash';
import {
BlockStyleOperation,
Expand All @@ -12,7 +12,7 @@ import {
InsertOperation,
Operation,
ReviveOperation,
} from '@notespace/shared/crdt/types/operations';
} from '@notespace/shared/document/types/operations';

/**
* Class that represents a local replica of a FugueTree
Expand Down
4 changes: 2 additions & 2 deletions code/client/src/domain/editor/crdt/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type InlineStyle } from '@notespace/shared/types/styles';
import { NodeType } from '@notespace/shared/crdt/types/nodes';
import { type InlineStyle } from '@notespace/shared/document/types/styles';
import { NodeType } from '@notespace/shared/document/types/nodes';

export type NodeInsert = {
value: string;
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/domain/editor/crdt/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { range } from 'lodash';
import { InlineStyle } from '@notespace/shared/types/styles';
import { InlineStyle } from '@notespace/shared/document/types/styles';

const BASE64CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const DEFAULT_REPLICA_ID_LENGTH = 10;
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/domain/editor/hooks/useEvents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useSocketListeners from '@/services/communication/socket/useSocketListeners';
import { type Operation } from '@notespace/shared/crdt/types/operations';
import { type Operation } from '@notespace/shared/document/types/operations';
import { Communication } from '@/services/communication/communication';
import { FugueDomainOperations } from '@domain/editor/operations/fugue/types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Fugue } from '@domain/editor/crdt/fugue';
import { FugueDomainOperations } from '@domain/editor/operations/fugue/types';
import { Operation } from '@notespace/shared/crdt/types/operations';
import { Operation } from '@notespace/shared/document/types/operations';

export default (fugue: Fugue): FugueDomainOperations => {
const applyOperations = (operations: Operation[]) => fugue.applyOperations(operations);
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/domain/editor/operations/fugue/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Operation } from '@notespace/shared/crdt/types/operations';
import { Operation } from '@notespace/shared/document/types/operations';

export type FugueDomainOperations = {
applyOperations: (operations: Operation[]) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
UnsetNodeOperation,
} from '@domain/editor/operations/history/types';
import { Communication } from '@/services/communication/communication';
import { BlockStyle, InlineStyle } from '@notespace/shared/types/styles';
import { getStyleType } from '@notespace/shared/types/styles';
import { BlockStyle, InlineStyle } from '@notespace/shared/document/types/styles';
import { getStyleType } from '@notespace/shared/document/types/styles';
import { Text, Element } from 'slate';

export default (fugue: Fugue, { socket }: Communication): HistoryDomainOperations => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cursor, Selection } from '@notespace/shared/types/cursor';
import { Cursor, Selection } from '@notespace/shared/document/types/cursor';
import {
BaseInsertTextOperation,
BaseRemoveTextOperation,
Expand Down
6 changes: 3 additions & 3 deletions code/client/src/domain/editor/operations/input/operations.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BaseSelection } from 'slate';
import { Fugue } from '@domain/editor/crdt/fugue';
import { InputDomainOperations } from '@domain/editor/operations/input/types';
import { Cursor, Selection } from '@notespace/shared/types/cursor';
import { Cursor, Selection } from '@notespace/shared/document/types/cursor';
import { nodeInsert } from '@domain/editor/crdt/utils';
import { InlineStyle } from '@notespace/shared/types/styles';
import { Operation } from '@notespace/shared/crdt/types/operations';
import { InlineStyle } from '@notespace/shared/document/types/styles';
import { Operation } from '@notespace/shared/document/types/operations';
import { Communication } from '@/services/communication/communication';

export default (fugue: Fugue, { socket }: Communication): InputDomainOperations => {
Expand Down
4 changes: 2 additions & 2 deletions code/client/src/domain/editor/operations/input/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InlineStyle } from '@notespace/shared/types/styles';
import { Cursor, Selection } from '@notespace/shared/types/cursor';
import { InlineStyle } from '@notespace/shared/document/types/styles';
import { Cursor, Selection } from '@notespace/shared/document/types/cursor';
import { BaseSelection } from 'slate';

export type InputDomainOperations = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Fugue } from '@domain/editor/crdt/fugue';
import { BlockStyle, InlineStyle } from '@notespace/shared/types/styles';
import { BlockStyle, InlineStyle } from '@notespace/shared/document/types/styles';
import { FugueNode } from '@domain/editor/crdt/types';
import { Selection } from '@notespace/shared/types/cursor';
import { Selection } from '@notespace/shared/document/types/cursor';
import { MarkdownDomainOperations } from '@domain/editor/operations/markdown/types';
import { deleteAroundSelection } from '@domain/editor/operations/markdown/utils';
import { Communication } from '@/services/communication/communication';
import { Operation } from '@notespace/shared/crdt/types/operations';
import { Operation } from '@notespace/shared/document/types/operations';
import { isSelectionEmpty } from '@domain/editor/slate/utils/selection';

/**
Expand Down
4 changes: 2 additions & 2 deletions code/client/src/domain/editor/operations/markdown/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BlockStyle, InlineStyle } from '@notespace/shared/types/styles';
import { Selection } from '@notespace/shared/types/cursor';
import { BlockStyle, InlineStyle } from '@notespace/shared/document/types/styles';
import { Selection } from '@notespace/shared/document/types/cursor';

export type MarkdownDomainOperations = {
applyBlockStyle: ApplyBlockStyle;
Expand Down
6 changes: 3 additions & 3 deletions code/client/src/domain/editor/operations/markdown/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Fugue } from '@domain/editor/crdt/fugue';
import { DeleteOperation } from '@notespace/shared/crdt/types/operations';
import { Id } from '@notespace/shared/crdt/types/nodes';
import { Selection } from '@notespace/shared/types/cursor';
import { DeleteOperation } from '@notespace/shared/document/types/operations';
import { Id } from '@notespace/shared/document/types/nodes';
import { Selection } from '@notespace/shared/document/types/cursor';

/**
* Deletes characters around the selection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import CustomEditor from '@domain/editor/slate/CustomEditor';
import { isEqual } from 'lodash';
import { getKeyFromInputEvent } from '@domain/editor/slate/utils/domEvents';
import { getSelection, isSelected } from '@domain/editor/slate/utils/selection';
import { Cursor, emptyCursor } from '@notespace/shared/types/cursor';
import { InlineStyle } from '@notespace/shared/types/styles';
import { Cursor, emptyCursor } from '@notespace/shared/document/types/cursor';
import { InlineStyle } from '@notespace/shared/document/types/styles';
import { InputDomainOperations } from '@domain/editor/operations/input/types';

const hotkeys: Record<string, string> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getSelection, isSelected } from '@domain/editor/slate/utils/selection';
import { Editor } from 'slate';
import CustomEditor from '@domain/editor/slate/CustomEditor';
import { MarkdownDomainOperations } from '@domain/editor/operations/markdown/types';
import { InlineStyle } from '@notespace/shared/types/styles';
import { InlineStyle } from '@notespace/shared/document/types/styles';

export default (editor: Editor, handlers: MarkdownDomainOperations) => {
/**
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/domain/editor/slate/hooks/useCursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Range } from 'slate';
import { useState } from 'react';
import useSocketListeners from '@/services/communication/socket/useSocketListeners';
import { Communication } from '@/services/communication/communication';
import { InlineStyle } from '@notespace/shared/types/styles';
import { InlineStyle } from '@notespace/shared/document/types/styles';

export type CursorData = {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/domain/editor/slate/hooks/useRenderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ReactEditor, type RenderElementProps, type RenderLeafProps } from 'slat
import { getElementRenderer, getLeafRenderer } from '@domain/editor/slate/plugins/markdown/rendering/renderers';
import { Editor } from 'slate';
import { Fugue } from '@domain/editor/crdt/fugue';
import { BlockStyle } from '@notespace/shared/types/styles';
import { BlockStyle } from '@notespace/shared/document/types/styles';
import { Communication } from '@/services/communication/communication.ts';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Editor, Element, Range, Text, Transforms } from 'slate';
import { getSelectionByRange } from '@domain/editor/slate/utils/selection';
import { BlockStyle, InlineStyle } from '@notespace/shared/types/styles';
import { BlockStyle, InlineStyle } from '@notespace/shared/document/types/styles';
import { ApplyBlockStyle, ApplyInlineStyle } from '@domain/editor/operations/markdown/types';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getSelection } from '@domain/editor/slate/utils/selection';
import { TextDeleteOptions } from 'slate/dist/interfaces/transforms/text';
import { MarkdownDomainOperations } from '@domain/editor/operations/markdown/types';
import { RuleType } from '@domain/editor/slate/plugins/markdown/rules';
import { BlockStyle } from '@notespace/shared/types/styles';
import { BlockStyle } from '@notespace/shared/document/types/styles';

type InlineFunction = (n: Element) => boolean;
type DeleteBackwardFunction = (unit: TextUnit, options?: { at: Range }) => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ReactNode } from 'react';
import { BlockStyles } from '@notespace/shared/types/styles';
import { BlockStyles } from '@notespace/shared/document/types/styles';
import { RenderElementProps } from 'slate-react';
import {
Blockquote,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ElementRenderers, LeafRenderers } from './elements';
import Selection from '@ui/pages/document/components/cursor/Selection';
import Cursor from '@ui/pages/document/components/cursor/Cursor';
import { Range } from 'slate';
import { type BlockStyle, BlockStyles } from '@notespace/shared/types/styles';
import { type BlockStyle, BlockStyles } from '@notespace/shared/document/types/styles';
import CheckListItem from '@domain/editor/slate/plugins/markdown/rendering/components/elements/CheckListItem';
import { isStatefulBlock } from '@domain/editor/slate/utils/slate';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createSetBlockApply,
createSetInlineApply,
} from '@domain/editor/slate/plugins/markdown/operations/applyOperations';
import { BlockStyle, InlineStyle } from '@notespace/shared/types/styles';
import { BlockStyle, InlineStyle } from '@notespace/shared/document/types/styles';
import { ApplyBlockStyle, ApplyInlineStyle } from '@domain/editor/operations/markdown/types';
import { Editor, Range } from 'slate';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BlockStyles, InlineStyles } from '@notespace/shared/types/styles';
import { BlockStyles, InlineStyles } from '@notespace/shared/document/types/styles';
import { blockRules, inlineRules } from '@domain/editor/slate/plugins/markdown/rules';

export const shortcuts = [
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/domain/editor/slate/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type BaseEditor, Descendant } from 'slate';
import { type ReactEditor } from 'slate-react';
import { type HistoryEditor } from 'slate-history';
import { type BlockStyle, InlineStyle } from '@notespace/shared/types/styles';
import { type BlockStyle, InlineStyle } from '@notespace/shared/document/types/styles';
import { CursorData } from '@/domain/editor/slate/hooks/useCursors';

export interface CustomFormat {
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/domain/editor/slate/utils/selection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Editor, Node, Path, Point, Range, Text } from 'slate';
import { Cursor, emptyCursor, emptySelection, Selection } from '@notespace/shared/types/cursor';
import { Cursor, emptyCursor, emptySelection, Selection } from '@notespace/shared/document/types/cursor';
import { first, isEqual } from 'lodash';

/**
Expand Down
4 changes: 2 additions & 2 deletions code/client/src/domain/editor/slate/utils/slate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createEditor, Descendant, Editor, Element } from 'slate';
import type { BlockStyle, InlineStyle } from '@notespace/shared/types/styles';
import type { BlockStyle, InlineStyle } from '@notespace/shared/document/types/styles';
import type { CustomText } from '@domain/editor/slate/types';
import { isEqual, last } from 'lodash';
import { BlockStyles } from '@notespace/shared/types/styles';
import { BlockStyles } from '@notespace/shared/document/types/styles';
import { Fugue } from '@domain/editor/crdt/fugue';

const multiBlocks: BlockStyle[] = [
Expand Down
1 change: 1 addition & 0 deletions code/client/src/domain/workspace/WorkspaceContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useCommunication } from '@/services/communication/context/useCommunicat
import useError from '@domain/error/useError.ts';

export type WorkspaceContextType = {
workspace?: string;
resources: WorkspaceResource[];
setResources: (resources: WorkspaceResource[]) => void;
filePath: string | undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Operation } from '@notespace/shared/crdt/types/operations';
import { Operation } from '@notespace/shared/document/types/operations';
import { isEmpty, range } from 'lodash';
import { Socket } from 'socket.io-client';

Expand Down
7 changes: 3 additions & 4 deletions code/client/src/services/documentServices.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { HttpCommunication } from '@/services/communication/http/httpCommunication';
import { DocumentContent } from '@notespace/shared/workspace/document';
import { DocumentResource } from '@notespace/shared/workspace/types/resource';

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

async function createDocument(http: HttpCommunication): Promise<string> {
Expand Down
8 changes: 4 additions & 4 deletions code/client/src/ui/pages/document/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ function Document() {
useEffect(() => {
async function fetchDocument() {
if (!id) return;
const { operations, title } = await services.getDocument(id);
fugue.applyOperations(operations, true);
setTitle(title);
const { content, name } = await services.getDocument(id);
fugue.applyOperations(content, true);
setTitle(name);
setLoaded(true);
setFilePath(`/documents/${title || 'Untitled'}`);
socket.emit('document:join', id);
Expand All @@ -44,7 +44,7 @@ function Document() {
return () => {
socket.emit('document:leave');
};
}, [fugue, id, http, socket, showError, services, setFilePath, navigate]);
}, [fugue, id, http, socket, showError, services, setFilePath, navigate, title]);

return <div>{loaded && <Editor title={title} fugue={fugue} communication={communication} />}</div>;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactNode } from 'react';
import { InlineStyle } from '@notespace/shared/types/styles';
import { InlineStyle } from '@notespace/shared/document/types/styles';
import './Cursor.scss';

type CursorProps = {
Expand Down
11 changes: 5 additions & 6 deletions code/client/src/ui/pages/document/components/title/Title.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useState } from 'react';
import useSocketListeners from '@/services/communication/socket/useSocketListeners';
import { ReactEditor, useSlate } from 'slate-react';
import { Communication } from '@/services/communication/communication';
import useWorkspace from '@domain/workspace/useWorkspace';
Expand All @@ -13,7 +12,7 @@ function Title(props: TitleProps) {
const [title, setTitle] = useState(props.title);
const [prevTitle, setPrevTitle] = useState(props.title);
const editor = useSlate();
const { socket } = props.communication;
const { http } = props.communication;
const { setFilePath } = useWorkspace();

function onInput(e: React.FormEvent<HTMLInputElement>) {
Expand All @@ -24,7 +23,7 @@ function Title(props: TitleProps) {

function onConfirm() {
if (title === prevTitle) return;
socket.emit('document:title', title);
// await http.put(`/documents/${title}`);
setPrevTitle(title);
setFilePath(`/documents/${title || 'Untitled'}`);
}
Expand All @@ -37,9 +36,9 @@ function Title(props: TitleProps) {
}
}

useSocketListeners(socket, {
'document:title': setTitle,
});
// useSocketListeners(socket, {
// 'document:title': setTitle,
// });

return (
<input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useFocused, useSlate } from 'slate-react';
import CustomEditor from '@domain/editor/slate/CustomEditor';
import { isSelected } from '@domain/editor/slate/utils/selection';
import { FaBold, FaItalic, FaUnderline, FaStrikethrough, FaCode } from 'react-icons/fa';
import { InlineStyle } from '@notespace/shared/types/styles';
import { InlineStyle } from '@notespace/shared/document/types/styles';

type ToolbarProps = {
onApplyMark: (mark: InlineStyle) => void;
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/document.d.ts';
import { DocumentData } from '@notespace/shared/workspace/types/document.d.ts';
import DocumentContextMenu from '@ui/pages/workspace/components/DocumentContextMenu';
import { useEffect, useRef, useState } from 'react';

Expand Down
4 changes: 2 additions & 2 deletions code/client/tests/editor/crdt/fugue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
DeleteOperation,
InlineStyleOperation,
BlockStyleOperation,
} from '@notespace/shared/crdt/types/operations';
import { Selection, Cursor } from '@notespace/shared/types/cursor';
} from '@notespace/shared/document/types/operations';
import { Selection, Cursor } from '@notespace/shared/document/types/cursor';
import { describe, test, expect, beforeEach } from 'vitest';
import { FugueNode } from '@domain/editor/crdt/types';

Expand Down
6 changes: 3 additions & 3 deletions code/client/tests/editor/crdt/tree.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { FugueTree } from '@notespace/shared/crdt/FugueTree';
import { InsertOperation } from '@notespace/shared/crdt/types/operations';
import { FugueTree } from '@notespace/shared/document/FugueTree.ts';
import { InsertOperation } from '@notespace/shared/document/types/operations';
import { describe, test, expect, beforeEach } from 'vitest';
import { FugueNode } from '@domain/editor/crdt/types';
import { Node, Nodes } from '@notespace/shared/crdt/types/nodes';
import { Node, Nodes } from '@notespace/shared/document/types/nodes';

describe('FugueTree', () => {
let tree: FugueTree<string>;
Expand Down
Loading

0 comments on commit 0c9b822

Please sign in to comment.