Skip to content

Commit

Permalink
Updates on SlateJs
Browse files Browse the repository at this point in the history
* Fixed style modifying whole line
-----
* Need to add <ul> or <ol> element for list management
  • Loading branch information
GuilhermeF03 committed Mar 27, 2024
1 parent b36d5b4 commit 61b97ff
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 29 deletions.
2 changes: 1 addition & 1 deletion code/client/dev-dist/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ define(['./workbox-a0f72815'], (function (workbox) { 'use strict';
"revision": "3ca0b8505b4bec776b69afdba2768812"
}, {
"url": "index.html",
"revision": "0.3hobnoep3ho"
"revision": "0.ptn1a5ctte"
}], {});
workbox.cleanupOutdatedCaches();
workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), {
Expand Down
5 changes: 3 additions & 2 deletions code/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@notespace/client",
"version": "1.0.0",
"description": "",
"type": "module",
"author": "notespace-team",
"private": true,
"license": "MIT",
Expand Down Expand Up @@ -31,7 +32,7 @@
"devDependencies": {
"@types/lodash": "^4.17.0",
"@types/node": "^20.11.30",
"@types/react": "^18.2.70",
"@types/react": "^18.2.72",
"@types/react-dom": "^18.2.22",
"@types/react-router-dom": "^5.3.3",
"@typescript-eslint/eslint-plugin": "^7.4.0",
Expand All @@ -47,7 +48,7 @@
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"jsdom": "^24.0.0",
"knip": "^5.5.0",
"knip": "^5.6.0",
"prettier": "^3.2.5",
"sass": "^1.72.0",
"typescript": "^5.4.3",
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import SlateEditor from '@editor/slate/SlateEditor.tsx';
import SlateEditor from '@editor/slate.js/SlateEditor.tsx';
import './App.scss';

function App() {
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/editor/hooks/useEvents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import useSocketListeners from '@src/socket/useSocketListeners';
import useSocketListeners from '@src/socket.io/useSocketListeners';
import { type Fugue } from '@src/editor/crdt/fugue';
import { type Node } from '@notespace/shared/crdt/types/nodes';
import { type Operation } from '@notespace/shared/crdt/types/operations';
Expand Down
15 changes: 8 additions & 7 deletions code/client/src/editor/slate.js/SlateEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Editable, Slate, withReact } from 'slate-react';
import useInputHandlers from '@editor/slate/hooks/useInputHandlers';
import useInputHandlers from '@editor/slate.js/hooks/useInputHandlers';
import useFugue from '@editor/hooks/useFugue';
import useEvents from '@editor/hooks/useEvents';
import useRenderers from '@editor/slate/hooks/useRenderers';
import useRenderers from '@editor/slate.js/hooks/useRenderers';
import './SlateEditor.scss';
import Toolbar from '@editor/slate/toolbar/Toolbar';
import Toolbar from '@editor/slate.js/toolbar/Toolbar';
import { withHistory } from 'slate-history';
import useEditor from '@editor/slate/hooks/useEditor';
import { withMarkdown } from '@editor/slate/plugins/markdown/withMarkdown';
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/utils/toSlate';
import { toSlate } from '@editor/slate.js/utils/toSlate';

const initialValue = [
{
Expand All @@ -21,7 +21,7 @@ const initialValue = [
function SlateEditor() {
const editor = useEditor(withHistory, withReact, withMarkdown, withNormalize);
const fugue = useFugue();
const { onKeyDown, onPaste } = useInputHandlers(editor, fugue);
const { onKeyDown, onPaste, onCut } = useInputHandlers(editor, fugue);
const { renderElement, renderLeaf } = useRenderers();

useEvents(fugue, () => {
Expand All @@ -45,6 +45,7 @@ function SlateEditor() {
placeholder={'Start writing...'}
onKeyDown={onKeyDown}
onPaste={onPaste}
onCut={onCut}
/>
</Slate>
</div>
Expand Down
15 changes: 11 additions & 4 deletions code/client/src/editor/slate.js/hooks/useInputHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type React from 'react';
import { type Fugue } from '@editor/crdt/fugue';
import CustomEditor from '@editor/slate/model/CustomEditor';
import CustomEditor from '@editor/slate.js/model/CustomEditor';
import { type Editor } from 'slate';
import { getSelection } from '../utils/selection';
import { isEqual } from 'lodash';
Expand Down Expand Up @@ -45,15 +45,22 @@ function useInputHandlers(editor: Editor, fugue: Fugue) {
function onPaste(e: React.ClipboardEvent<HTMLDivElement>) {
const clipboardData = e.clipboardData?.getData('text');
if (!clipboardData) return;
// const selection = getSelection();
// fugue.insertLocal(start, insertNode(clipboardData, []));
const selection = getSelection(editor);
const { start } = selection;
fugue.insertLocal(start, insertNode(clipboardData, [])); // TODO: Fix this
}

function onCut() {
const selection = getSelection(editor);
fugue.deleteLocal(selection); // TODO: Fix this
}

function shortcutHandler(event: React.KeyboardEvent<HTMLDivElement>) {
const mark = hotkeys[event.key];
CustomEditor.toggleMark(editor, mark, fugue);
}
return { onKeyDown, onPaste };

return { onKeyDown, onPaste, onCut };
}

export default useInputHandlers;
2 changes: 1 addition & 1 deletion code/client/src/editor/slate.js/hooks/useRenderers.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
import { type RenderElementProps, type RenderLeafProps } from 'slate-react';
import { getElementRenderer, getLeafRenderer } from '@editor/slate/plugins/markdown/rendering/renderers.tsx';
import { getElementRenderer, getLeafRenderer } from '@editor/slate.js/plugins/markdown/rendering/renderers.tsx';

/**
* Returns the renderers for the editor.
Expand Down
6 changes: 3 additions & 3 deletions code/client/src/editor/slate.js/model/CustomEditor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Editor, Transforms } from 'slate';
import { Editor } from 'slate';
import { type Fugue } from '@editor/crdt/fugue.ts';
import { getAbsoluteSelection } from '@editor/slate/model/utils.ts';
import { getAbsoluteSelection } from '@editor/slate.js/model/utils.ts';

/**
* Custom editor operations.
Expand All @@ -12,7 +12,7 @@ const CustomEditor = {
},
toggleMark(editor: Editor, mark: string, fugue: Fugue) {
const isActive = CustomEditor.isMarkActive(editor, mark);
Transforms.setNodes(editor, { [mark]: !isActive }, { match: n => Editor.isBlock(editor, n) });
Editor.addMark(editor, mark, !isActive);

const [start, end] = getAbsoluteSelection(editor)!;
fugue.updateStyleLocal(start, end, !isActive, mark);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type RenderElementProps } from 'slate-react';
import { ElementRenderers, LeafRenderers } from './elements.tsx';
import { Paragraph } from './components/components.ts';
import { type CustomText } from '@editor/slate/model/types.ts';
import { type CustomText } from '@editor/slate.js/model/types.ts';
import { type ReactNode } from 'react';

export const getElementRenderer = (type: string, props: RenderElementProps) => {
Expand Down
4 changes: 2 additions & 2 deletions code/client/src/editor/slate.js/plugins/markdown/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type CustomElement } from '@editor/slate/model/types.ts';
import { type CustomElement } from '@editor/slate.js/model/types.ts';
import { BlockStyles, InlineStyles } from '@notespace/shared/crdt/types/styles';
import { type Editor, Element, Range, Text, Transforms } from 'slate';
import { createDescendant } from '@editor/slate/model/utils.ts';
import { createDescendant } from '@editor/slate.js/model/utils.ts';

const escapeRegExp = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

Expand Down
4 changes: 2 additions & 2 deletions code/client/src/editor/slate.js/toolbar/Toolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect } from 'react';
import { useFocused, useSlate } from 'slate-react';
import CustomEditor from '@editor/slate/model/CustomEditor';
import { isSelected } from '@editor/slate/utils/selection';
import CustomEditor from '@editor/slate.js/model/CustomEditor';
import { isSelected } from '@editor/slate.js/utils/selection';
import { FaBold, FaItalic, FaUnderline, FaStrikethrough, FaCode } from 'react-icons/fa';
import { type Fugue } from '@editor/crdt/fugue';

Expand Down
4 changes: 2 additions & 2 deletions code/client/src/editor/slate.js/utils/toSlate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Descendant } from 'slate';
import type { Style, BlockStyle } from '../../../../../shared/crdt/types/styles';
import type { CustomText } from '@editor/slate/model/types.ts';
import type { CustomText } from '@editor/slate.js/model/types.ts';
import { isEmpty, isEqual } from 'lodash';
import { createChildren, createDescendant } from '@editor/slate/model/utils.ts';
import { createChildren, createDescendant } from '@editor/slate.js/model/utils.ts';
import { Fugue } from '@editor/crdt/fugue.ts';

export function toSlate(fugue: Fugue): Descendant[] {
Expand Down
2 changes: 1 addition & 1 deletion code/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"babel-jest": "^29.7.0",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"knip": "^5.5.0",
"knip": "^5.6.0",
"prettier": "^3.2.5",
"socket.io-client": "^4.7.5",
"test-jest": "^1.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Operation } from '@notespace/shared/crdt/types/operations';

function onOperation(service: DocumentService) {
return (socket: Socket, operation: Operation) => {
console.log('operation', operation);
switch (operation.type) {
case 'insert': {
service.insertCharacter(operation);
Expand Down
2 changes: 1 addition & 1 deletion code/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"eslint": "^8.57.0",
"knip": "^5.5.0",
"knip": "^5.6.0",
"prettier": "^3.2.5",
"typescript": "^5.4.3"
},
Expand Down

0 comments on commit 61b97ff

Please sign in to comment.