From d8709e63ee6bfb21043f580c5dbddcfcc1bf5019 Mon Sep 17 00:00:00 2001 From: Ricardo Costa Date: Thu, 28 Mar 2024 23:37:09 +0000 Subject: [PATCH] Code Refactoring --- code/client/dev-dist/sw.js | 2 +- code/client/src/editor/hooks/useEvents.ts | 1 + .../plugins/normalize/withNormalize.ts | 24 ------------------- .../{slate.js => slate}/SlateEditor.scss | 0 .../{slate.js => slate}/SlateEditor.tsx | 3 +-- .../{slate.js => slate}/hooks/useEditor.ts | 0 .../hooks/useInputHandlers.ts | 23 +++++++++++------- .../hooks/useRenderers.tsx | 0 .../{slate.js => slate}/model/CustomEditor.ts | 0 .../{slate.js => slate}/model/cursor.ts | 0 .../editor/{slate.js => slate}/model/types.ts | 0 .../editor/{slate.js => slate}/model/utils.ts | 0 .../plugins/markdown/operations.ts | 0 .../rendering/components/components.ts | 0 .../components/elements/Blockquote.tsx | 0 .../rendering/components/elements/Code.tsx | 0 .../components/elements/Heading1.tsx | 0 .../components/elements/Heading2.tsx | 0 .../components/elements/Heading3.tsx | 0 .../components/elements/Heading4.tsx | 0 .../components/elements/Heading5.tsx | 0 .../components/elements/Heading6.tsx | 0 .../components/elements/LineBreak.tsx | 0 .../components/elements/ListItem.tsx | 0 .../components/elements/NumberedListItem.tsx | 0 .../components/elements/Paragraph.tsx | 0 .../plugins/markdown/rendering/elements.tsx | 0 .../plugins/markdown/rendering/renderers.tsx | 0 .../plugins/markdown/shortcuts.ts | 0 .../plugins/markdown/withMarkdown.ts | 0 .../{slate.js => slate}/toolbar/Toolbar.tsx | 0 .../{slate.js => slate}/utils/selection.ts | 0 .../{slate.js => slate}/utils/toSlate.ts | 0 .../src/{socket.io => socket}/socket.ts | 0 .../useSocketListeners.ts | 0 .../document/onCursorChange.ts | 0 .../document/onOperation.ts | 0 .../{socket.io => websocket}/events.ts | 0 .../{socket.io => websocket}/onConnection.ts | 2 +- 39 files changed, 19 insertions(+), 36 deletions(-) delete mode 100644 code/client/src/editor/slate.js/plugins/normalize/withNormalize.ts rename code/client/src/editor/{slate.js => slate}/SlateEditor.scss (100%) rename code/client/src/editor/{slate.js => slate}/SlateEditor.tsx (91%) rename code/client/src/editor/{slate.js => slate}/hooks/useEditor.ts (100%) rename code/client/src/editor/{slate.js => slate}/hooks/useInputHandlers.ts (83%) rename code/client/src/editor/{slate.js => slate}/hooks/useRenderers.tsx (100%) rename code/client/src/editor/{slate.js => slate}/model/CustomEditor.ts (100%) rename code/client/src/editor/{slate.js => slate}/model/cursor.ts (100%) rename code/client/src/editor/{slate.js => slate}/model/types.ts (100%) rename code/client/src/editor/{slate.js => slate}/model/utils.ts (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/operations.ts (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/components.ts (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/Blockquote.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/Code.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/Heading1.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/Heading2.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/Heading3.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/Heading4.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/Heading5.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/Heading6.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/LineBreak.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/ListItem.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/NumberedListItem.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/components/elements/Paragraph.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/elements.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/rendering/renderers.tsx (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/shortcuts.ts (100%) rename code/client/src/editor/{slate.js => slate}/plugins/markdown/withMarkdown.ts (100%) rename code/client/src/editor/{slate.js => slate}/toolbar/Toolbar.tsx (100%) rename code/client/src/editor/{slate.js => slate}/utils/selection.ts (100%) rename code/client/src/editor/{slate.js => slate}/utils/toSlate.ts (100%) rename code/client/src/{socket.io => socket}/socket.ts (100%) rename code/client/src/{socket.io => socket}/useSocketListeners.ts (100%) rename code/server/src/controllers/{socket.io => websocket}/document/onCursorChange.ts (100%) rename code/server/src/controllers/{socket.io => websocket}/document/onOperation.ts (100%) rename code/server/src/controllers/{socket.io => websocket}/events.ts (100%) rename code/server/src/controllers/{socket.io => websocket}/onConnection.ts (95%) diff --git a/code/client/dev-dist/sw.js b/code/client/dev-dist/sw.js index 8991f339..277e1728 100644 --- a/code/client/dev-dist/sw.js +++ b/code/client/dev-dist/sw.js @@ -82,7 +82,7 @@ define(['./workbox-fda11f75'], (function (workbox) { 'use strict'; "revision": "3ca0b8505b4bec776b69afdba2768812" }, { "url": "index.html", - "revision": "0.965t3hopnrg" + "revision": "0.jepaoi887g" }], {}); workbox.cleanupOutdatedCaches(); workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), { diff --git a/code/client/src/editor/hooks/useEvents.ts b/code/client/src/editor/hooks/useEvents.ts index b500f3cc..97fbf341 100644 --- a/code/client/src/editor/hooks/useEvents.ts +++ b/code/client/src/editor/hooks/useEvents.ts @@ -8,6 +8,7 @@ function useEvents(fugue: Fugue, onDone: () => void) { switch (operation.type) { case 'insert': fugue.insertRemote(operation); + // TODO: update the style of the added node in slate break; case 'delete': fugue.deleteRemote(operation); diff --git a/code/client/src/editor/slate.js/plugins/normalize/withNormalize.ts b/code/client/src/editor/slate.js/plugins/normalize/withNormalize.ts deleted file mode 100644 index 35be95aa..00000000 --- a/code/client/src/editor/slate.js/plugins/normalize/withNormalize.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Editor, Transforms } from 'slate'; -import { isEmpty } from 'lodash'; - -// ensure editor always has at least one child. -export function withNormalize(editor: Editor) { - const { normalizeNode } = editor; - editor.normalizeNode = entry => { - const [node] = entry; - - if (!Editor.isEditor(node) || !isEmpty(node.children)) { - normalizeNode(entry); - return; - } - Transforms.insertNodes( - editor, - { - type: 'paragraph', - children: [{ text: '' }], - }, - { at: [0] } - ); - }; - return editor; -} diff --git a/code/client/src/editor/slate.js/SlateEditor.scss b/code/client/src/editor/slate/SlateEditor.scss similarity index 100% rename from code/client/src/editor/slate.js/SlateEditor.scss rename to code/client/src/editor/slate/SlateEditor.scss diff --git a/code/client/src/editor/slate.js/SlateEditor.tsx b/code/client/src/editor/slate/SlateEditor.tsx similarity index 91% rename from code/client/src/editor/slate.js/SlateEditor.tsx rename to code/client/src/editor/slate/SlateEditor.tsx index ec98207c..7380f016 100644 --- a/code/client/src/editor/slate.js/SlateEditor.tsx +++ b/code/client/src/editor/slate/SlateEditor.tsx @@ -8,7 +8,6 @@ import Toolbar from '@editor/slate.js/toolbar/Toolbar'; import { withHistory } from 'slate-history'; import useEditor from '@editor/slate.js/hooks/useEditor'; import { withMarkdown } from '@editor/slate.js/plugins/markdown/withMarkdown'; -import { withNormalize } from './plugins/normalize/withNormalize'; import { toSlate } from '@editor/slate.js/utils/toSlate'; const initialValue = [ @@ -19,7 +18,7 @@ const initialValue = [ ]; function SlateEditor() { - const editor = useEditor(withHistory, withReact, withMarkdown, withNormalize); + const editor = useEditor(withHistory, withReact, withMarkdown); const fugue = useFugue(); const { onKeyDown, onPaste, onCut } = useInputHandlers(editor, fugue); const { renderElement, renderLeaf } = useRenderers(); diff --git a/code/client/src/editor/slate.js/hooks/useEditor.ts b/code/client/src/editor/slate/hooks/useEditor.ts similarity index 100% rename from code/client/src/editor/slate.js/hooks/useEditor.ts rename to code/client/src/editor/slate/hooks/useEditor.ts diff --git a/code/client/src/editor/slate.js/hooks/useInputHandlers.ts b/code/client/src/editor/slate/hooks/useInputHandlers.ts similarity index 83% rename from code/client/src/editor/slate.js/hooks/useInputHandlers.ts rename to code/client/src/editor/slate/hooks/useInputHandlers.ts index 3dceed69..458dcad2 100644 --- a/code/client/src/editor/slate.js/hooks/useInputHandlers.ts +++ b/code/client/src/editor/slate/hooks/useInputHandlers.ts @@ -18,7 +18,6 @@ function useInputHandlers(editor: Editor, fugue: Fugue) { const selection = getSelection(editor); const { start, end } = selection; - switch (e.key) { case 'Enter': fugue.insertLocal(start, insertNode('\n', [])); @@ -37,10 +36,9 @@ function useInputHandlers(editor: Editor, fugue: Fugue) { default: { if (e.key.length !== 1) break; if (selection.start.column !== selection.end.column) { - // replace selection - fugue.deleteLocal(selection); + fugue.deleteLocal(selection); // replace selection } - const previousNode = fugue.getNodeByCursor({ line: start.line, column: start.column - 1 }); + const previousNode = fugue.getNodeByCursor(start); const styles = previousNode?.styles || []; fugue.insertLocal(start, insertNode(e.key, styles)); break; @@ -71,11 +69,20 @@ function useInputHandlers(editor: Editor, fugue: Fugue) { } function shortcutHandler(event: React.KeyboardEvent) { - const mark = hotkeys[event.key]; - CustomEditor.toggleMark(editor, mark, fugue); + switch (event.key) { + case 'z': + onUndo(); + break; + case 'y': + onRedo(); + break; + default: { + const mark = hotkeys[event.key]; + CustomEditor.toggleMark(editor, mark, fugue); + } + } } - - return { onKeyDown, onPaste, onCut, onUndo, onRedo }; + return { onKeyDown, onPaste, onCut }; } export default useInputHandlers; diff --git a/code/client/src/editor/slate.js/hooks/useRenderers.tsx b/code/client/src/editor/slate/hooks/useRenderers.tsx similarity index 100% rename from code/client/src/editor/slate.js/hooks/useRenderers.tsx rename to code/client/src/editor/slate/hooks/useRenderers.tsx diff --git a/code/client/src/editor/slate.js/model/CustomEditor.ts b/code/client/src/editor/slate/model/CustomEditor.ts similarity index 100% rename from code/client/src/editor/slate.js/model/CustomEditor.ts rename to code/client/src/editor/slate/model/CustomEditor.ts diff --git a/code/client/src/editor/slate.js/model/cursor.ts b/code/client/src/editor/slate/model/cursor.ts similarity index 100% rename from code/client/src/editor/slate.js/model/cursor.ts rename to code/client/src/editor/slate/model/cursor.ts diff --git a/code/client/src/editor/slate.js/model/types.ts b/code/client/src/editor/slate/model/types.ts similarity index 100% rename from code/client/src/editor/slate.js/model/types.ts rename to code/client/src/editor/slate/model/types.ts diff --git a/code/client/src/editor/slate.js/model/utils.ts b/code/client/src/editor/slate/model/utils.ts similarity index 100% rename from code/client/src/editor/slate.js/model/utils.ts rename to code/client/src/editor/slate/model/utils.ts diff --git a/code/client/src/editor/slate.js/plugins/markdown/operations.ts b/code/client/src/editor/slate/plugins/markdown/operations.ts similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/operations.ts rename to code/client/src/editor/slate/plugins/markdown/operations.ts diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/components.ts b/code/client/src/editor/slate/plugins/markdown/rendering/components/components.ts similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/components.ts rename to code/client/src/editor/slate/plugins/markdown/rendering/components/components.ts diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Blockquote.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Blockquote.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Blockquote.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Blockquote.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Code.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Code.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Code.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Code.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading1.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading1.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading1.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading1.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading2.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading2.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading2.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading2.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading3.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading3.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading3.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading3.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading4.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading4.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading4.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading4.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading5.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading5.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading5.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading5.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading6.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading6.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Heading6.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Heading6.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/LineBreak.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/LineBreak.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/LineBreak.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/LineBreak.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/ListItem.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/ListItem.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/ListItem.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/ListItem.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/NumberedListItem.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/NumberedListItem.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/NumberedListItem.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/NumberedListItem.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Paragraph.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Paragraph.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/components/elements/Paragraph.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/components/elements/Paragraph.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/elements.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/elements.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/elements.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/elements.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/rendering/renderers.tsx b/code/client/src/editor/slate/plugins/markdown/rendering/renderers.tsx similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/rendering/renderers.tsx rename to code/client/src/editor/slate/plugins/markdown/rendering/renderers.tsx diff --git a/code/client/src/editor/slate.js/plugins/markdown/shortcuts.ts b/code/client/src/editor/slate/plugins/markdown/shortcuts.ts similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/shortcuts.ts rename to code/client/src/editor/slate/plugins/markdown/shortcuts.ts diff --git a/code/client/src/editor/slate.js/plugins/markdown/withMarkdown.ts b/code/client/src/editor/slate/plugins/markdown/withMarkdown.ts similarity index 100% rename from code/client/src/editor/slate.js/plugins/markdown/withMarkdown.ts rename to code/client/src/editor/slate/plugins/markdown/withMarkdown.ts diff --git a/code/client/src/editor/slate.js/toolbar/Toolbar.tsx b/code/client/src/editor/slate/toolbar/Toolbar.tsx similarity index 100% rename from code/client/src/editor/slate.js/toolbar/Toolbar.tsx rename to code/client/src/editor/slate/toolbar/Toolbar.tsx diff --git a/code/client/src/editor/slate.js/utils/selection.ts b/code/client/src/editor/slate/utils/selection.ts similarity index 100% rename from code/client/src/editor/slate.js/utils/selection.ts rename to code/client/src/editor/slate/utils/selection.ts diff --git a/code/client/src/editor/slate.js/utils/toSlate.ts b/code/client/src/editor/slate/utils/toSlate.ts similarity index 100% rename from code/client/src/editor/slate.js/utils/toSlate.ts rename to code/client/src/editor/slate/utils/toSlate.ts diff --git a/code/client/src/socket.io/socket.ts b/code/client/src/socket/socket.ts similarity index 100% rename from code/client/src/socket.io/socket.ts rename to code/client/src/socket/socket.ts diff --git a/code/client/src/socket.io/useSocketListeners.ts b/code/client/src/socket/useSocketListeners.ts similarity index 100% rename from code/client/src/socket.io/useSocketListeners.ts rename to code/client/src/socket/useSocketListeners.ts diff --git a/code/server/src/controllers/socket.io/document/onCursorChange.ts b/code/server/src/controllers/websocket/document/onCursorChange.ts similarity index 100% rename from code/server/src/controllers/socket.io/document/onCursorChange.ts rename to code/server/src/controllers/websocket/document/onCursorChange.ts diff --git a/code/server/src/controllers/socket.io/document/onOperation.ts b/code/server/src/controllers/websocket/document/onOperation.ts similarity index 100% rename from code/server/src/controllers/socket.io/document/onOperation.ts rename to code/server/src/controllers/websocket/document/onOperation.ts diff --git a/code/server/src/controllers/socket.io/events.ts b/code/server/src/controllers/websocket/events.ts similarity index 100% rename from code/server/src/controllers/socket.io/events.ts rename to code/server/src/controllers/websocket/events.ts diff --git a/code/server/src/controllers/socket.io/onConnection.ts b/code/server/src/controllers/websocket/onConnection.ts similarity index 95% rename from code/server/src/controllers/socket.io/onConnection.ts rename to code/server/src/controllers/websocket/onConnection.ts index 037c71e6..d51ec060 100644 --- a/code/server/src/controllers/socket.io/onConnection.ts +++ b/code/server/src/controllers/websocket/onConnection.ts @@ -18,7 +18,7 @@ function onConnection(service: DocumentService, events: Record { socket.on(event, data => { try { - // console.log(event); + console.log(event, data); handler(socket, data); } catch (e) { socket.emit('error');