Skip to content

Commit

Permalink
fix: add rich text editor (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirily11 authored Dec 8, 2022
1 parent cf885c8 commit e2b07d1
Show file tree
Hide file tree
Showing 43 changed files with 2,255 additions and 72 deletions.
1 change: 0 additions & 1 deletion components/Auth/UserAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export function UserAvatar(props: Props) {
popupId: "userAvatar",
});
const router = useRouter();
console.log(session);

const user = useMemo(() => {
return session.data?.user as any;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
"eslint": "8.29.0",
"eslint-config-next": "13.0.6",
"formik": "^2.2.9",
"lexical": "^0.6.5",
"material-ui-popup-state": "^4.1.0",
"next": "13.0.6",
"next-auth": "^4.18.0",
"notistack": "^2.0.8",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-qr-code": "^2.0.8",
"typescript": "4.9.3"
"typescript": "4.9.3",
"editor": "workspace:editor"
},
"devDependencies": {
"autoprefixer": "^10.4.13",
Expand Down
31 changes: 31 additions & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "editor",
"version": "1.0.0",
"description": "",
"main": "src/index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@lexical/clipboard": "^0.6.5",
"@lexical/code": "^0.6.5",
"@lexical/link": "^0.6.5",
"@lexical/list": "^0.6.5",
"@lexical/markdown": "^0.6.5",
"@lexical/react": "^0.6.5",
"@lexical/rich-text": "^0.6.5",
"@lexical/selection": "^0.6.5",
"@lexical/table": "^0.6.5",
"@lexical/utils": "^0.6.5",
"lexical": "^0.6.5",
"react": "18.2.0",
"react-dom": "18.2.0",
"yjs": "^13.5.43"
},
"devDependencies": {
"typescript": "4.9.3"
}
}
93 changes: 93 additions & 0 deletions packages/editor/src/Editor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import Theme from "./themes/Theme";
import {
InitialConfigType,
LexicalComposer,
} from "@lexical/react/LexicalComposer";
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
import { AutoFocusPlugin } from "@lexical/react/LexicalAutoFocusPlugin";
import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";
import ToolbarPlugin from "./plugins/ToolbarPlugin";
import { HeadingNode, QuoteNode } from "@lexical/rich-text";
import { TableCellNode, TableNode, TableRowNode } from "@lexical/table";
import { ListItemNode, ListNode } from "@lexical/list";
import { CodeHighlightNode, CodeNode } from "@lexical/code";
import { AutoLinkNode, LinkNode } from "@lexical/link";
import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
import { ListPlugin } from "@lexical/react/LexicalListPlugin";
import { MarkdownShortcutPlugin } from "@lexical/react/LexicalMarkdownShortcutPlugin";
import { TRANSFORMERS } from "@lexical/markdown";
import React, { useMemo } from "react";
import {
LexicalComposerContext,
useLexicalComposerContext,
} from "@lexical/react/LexicalComposerContext";
import { SaveAndLoadPlugin } from "./plugins/SaveAndLoadPlugin";

function Placeholder() {
return <div className="editor-placeholder">Enter some rich text...</div>;
}

type Props = {
initialValue?: string;
onChange?: (value: string) => void;
};

export function Editor({ initialValue, onChange }: Props) {
const editorConfig: InitialConfigType = useMemo(() => {
return {
// The editor theme
theme: Theme,
// Handling of errors during update
onError(error: any) {
throw error;
},
namespace: "editor",
// Any custom nodes go here
nodes: [
HeadingNode,
ListNode,
ListItemNode,
QuoteNode,
CodeNode,
CodeHighlightNode,
TableNode,
TableCellNode,
TableRowNode,
AutoLinkNode,
LinkNode,
],
};
}, [initialValue]);

return (
<LexicalComposer initialConfig={editorConfig}>
<div className="editor-container">
<ToolbarPlugin />
<div className="editor-inner">
<RichTextPlugin
contentEditable={<ContentEditable className="editor-input" />}
placeholder={<Placeholder />}
ErrorBoundary={LexicalErrorBoundary}
/>
<HistoryPlugin />
<AutoFocusPlugin />
<ListPlugin />
<LinkPlugin />
<MarkdownShortcutPlugin transformers={TRANSFORMERS} />
<SaveAndLoadPlugin
value={initialValue}
onChange={(value) => {
if (onChange) {
onChange(value);
}
}}
/>
</div>
</div>
</LexicalComposer>
);
}

export default Editor;
Binary file added packages/editor/src/images/emoji/1F600.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/editor/src/images/emoji/1F641.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/editor/src/images/emoji/1F642.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/editor/src/images/emoji/2764.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/editor/src/images/emoji/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
OpenMoji
https://openmoji.org

Licensed under Attribution-ShareAlike 4.0 International
https://creativecommons.org/licenses/by-sa/4.0/
5 changes: 5 additions & 0 deletions packages/editor/src/images/icons/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bootstrap Icons
https://icons.getbootstrap.com

Licensed under MIT license
https://github.com/twbs/icons/blob/main/LICENSE.md
4 changes: 4 additions & 0 deletions packages/editor/src/images/icons/arrow-clockwise.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions packages/editor/src/images/icons/arrow-counterclockwise.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions packages/editor/src/images/icons/chat-square-quote.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/chevron-down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/code.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/editor/src/images/icons/journal-code.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/editor/src/images/icons/journal-text.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/justify.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions packages/editor/src/images/icons/link.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions packages/editor/src/images/icons/list-ol.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/list-ul.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/pencil-fill.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/text-center.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/text-left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/text-paragraph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/text-right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/type-bold.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/type-h1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/type-h2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/type-h3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/type-italic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/type-strikethrough.svg
3 changes: 3 additions & 0 deletions packages/editor/src/images/icons/type-underline.svg
1 change: 1 addition & 0 deletions packages/editor/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Editor } from "./Editor";
29 changes: 29 additions & 0 deletions packages/editor/src/plugins/SaveAndLoadPlugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @flow
import * as React from "react";
import { OnChangePlugin } from "@lexical/react/LexicalOnChangePlugin";
import { useEffect } from "react";
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";

type Props = {
value?: string;
onChange: (value: string) => void;
};

export function SaveAndLoadPlugin(props: Props) {
const [editor] = useLexicalComposerContext();

useEffect(() => {
if (props.value) {
const state = editor.parseEditorState(props.value);
editor.setEditorState(state);
}
}, [editor]);

return (
<OnChangePlugin
onChange={(editorState, editor) => {
props.onChange(JSON.stringify(editorState));
}}
/>
);
}
Loading

1 comment on commit e2b07d1

@vercel
Copy link

@vercel vercel bot commented on e2b07d1 Dec 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.