From ab25396404664e68d0b020083ae33346bc803262 Mon Sep 17 00:00:00 2001 From: ubbn Date: Sun, 12 May 2024 12:02:36 +0200 Subject: [PATCH] Improve blog feature --- frontend/src/common/editor/editor.tsx | 19 +++-- frontend/src/components/blog/Post.tsx | 83 ++++++++++++++----- frontend/src/components/blog/index.tsx | 46 ++++++---- frontend/src/components/layout/menu/index.tsx | 3 + 4 files changed, 108 insertions(+), 43 deletions(-) diff --git a/frontend/src/common/editor/editor.tsx b/frontend/src/common/editor/editor.tsx index 01f6c94..652344f 100644 --- a/frontend/src/common/editor/editor.tsx +++ b/frontend/src/common/editor/editor.tsx @@ -20,7 +20,7 @@ import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin"; import { HeadingNode, QuoteNode } from "@lexical/rich-text"; import { TableCellNode, TableNode, TableRowNode } from "@lexical/table"; import { $getRoot } from "lexical"; -import { useEffect } from "react"; +import React from "react"; import AutoLinkPlugin from "./plugins/AutoLinkPlugin"; import CodeHighlightPlugin from "./plugins/CodeHighlightPlugin"; import ListMaxIndentLevelPlugin from "./plugins/ListMaxIndentLevelPlugin"; @@ -32,7 +32,7 @@ function Placeholder() { return
Enter some rich text...
; } -const editorConfig = (value: string, editable: boolean): any => ({ +const editorConfig = (value: string): any => ({ // The editor theme theme: ExampleTheme, // Handling of errors during update @@ -54,13 +54,12 @@ const editorConfig = (value: string, editable: boolean): any => ({ AutoLinkNode, LinkNode, ], - editable, }); -function MyOnChangePlugin({ onChange, value }: { onChange: any, value: any }) { +function MyOnChangePlugin({ onChange, value, editable }: { onChange: any, value: any, editable: boolean }) { const [editor] = useLexicalComposerContext(); - useEffect(() => { + React.useEffect(() => { return editor.registerUpdateListener(({ editorState }) => { // onChange(editorState); @@ -80,7 +79,7 @@ function MyOnChangePlugin({ onChange, value }: { onChange: any, value: any }) { }); }, [editor, onChange]); - useEffect(() => { + React.useEffect(() => { editor.update(() => { // Get the RootNode from the EditorState const rootNode = $getRoot(); @@ -90,6 +89,10 @@ function MyOnChangePlugin({ onChange, value }: { onChange: any, value: any }) { }); }, [value]) + React.useEffect(() => { + editor.setEditable(editable) + }, [editor, editable]) + return null; } @@ -122,7 +125,7 @@ export default function Editor({ } return ( - +
{!hideToolbar && }
@@ -136,7 +139,7 @@ export default function Editor({ ignoreHistoryMergeTagChange ignoreSelectionChange /> - + diff --git a/frontend/src/components/blog/Post.tsx b/frontend/src/components/blog/Post.tsx index 0a9f6e5..f5fc10c 100644 --- a/frontend/src/components/blog/Post.tsx +++ b/frontend/src/components/blog/Post.tsx @@ -1,19 +1,26 @@ -import { useEffect, useState } from "react"; +import { EditOutlined, EyeOutlined, SaveOutlined } from "@ant-design/icons"; +import { Button } from "antd"; +import React from "react"; +import { useSelector } from "react-redux"; import { useParams } from "react-router-dom"; +import { styled } from "styled-components"; +import { FlexRow } from "../../common"; +import Editor from "../../common/editor/editor"; import { setNeuron, thunkGetNeuron } from "../../redux/neuronSlice"; import { RootState, useAppDispatch } from "../../redux/store"; -import { useSelector } from "react-redux"; -import Editor from "../../common/editor/editor"; -import { styled } from "styled-components"; const Post = () => { - const [item, setItem] = useState() - const { id } = useParams(); - const dispatch = useAppDispatch(); + const [initial, setInitial] = React.useState() + const [item, setItem] = React.useState() + const [editMode, setEditMode] = React.useState(false) + const [pristine, setPristine] = React.useState(true); const { selected } = useSelector((v: RootState) => v.neuron); const { loading } = useSelector((v: RootState) => v.main); - useEffect(() => { + const { id } = useParams(); + const dispatch = useAppDispatch(); + + React.useEffect(() => { if (id) { setItem(undefined) dispatch(thunkGetNeuron(id)) @@ -23,24 +30,57 @@ const Post = () => { }) }, [id]); - useEffect(() => { + React.useEffect(() => { if (selected) { + setInitial(selected) setItem(selected) } }, [selected]) + const saveNeuron = () => { + // onSave(item); + setPristine(true); + setInitial(item) + } + + const onView = () => { + setEditMode(false) + } + + const onEdit = () => { + setEditMode(true) + } + if (loading) { - return <>Loading... + return Loading... } return ( - - {item?.title} + + + {item?.title} + {editMode ? +
+
+ : + {currentPage} of {totalPage}
} -
+ ); }; export default Blog; + +const StyledCart = styled.div` + border: 1px solid lightgrey; + padding: 25px; + margin: 20px 0; + border-radius: 6px; + cursor: pointer; + box-shadow: 3px 3px 7px lightgray; + &:hover { + box-shadow: 7px 7px 13px lightgray; + } +` diff --git a/frontend/src/components/layout/menu/index.tsx b/frontend/src/components/layout/menu/index.tsx index 8e118b2..fca2b4d 100644 --- a/frontend/src/components/layout/menu/index.tsx +++ b/frontend/src/components/layout/menu/index.tsx @@ -60,4 +60,7 @@ const Text = styled.div<{ selected: boolean }>` const Separator = styled.span` margin: 0 20px; + @media (max-width: 600px) { + margin: 0 13px; + } `;