Skip to content

Commit e870d9e

Browse files
committed
feat: 에디터 content 자동 저장 구현
1 parent 5bb583f commit e870d9e

File tree

3 files changed

+8
-23
lines changed

3 files changed

+8
-23
lines changed

frontend/src/api/page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ export const deletePage = async (id: number) => {
3434
return res.data;
3535
};
3636

37-
export const updatePage = async (id: number, pageData: PageRequest) => {
37+
export const updatePage = async (id: number, pageData: JSONContent) => {
3838
const url = `/page/${id}`;
3939

40-
const res = await Patch<null, PageRequest>(url, pageData);
40+
const res = await Patch<null, JSONContent>(url, pageData);
4141
return res.data;
4242
};

frontend/src/components/editor/index.tsx

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { ColorSelector } from "./selectors/color-selector";
2424

2525
import { useDebouncedCallback } from "use-debounce";
2626

27+
import { useUpdatePage } from "@/hooks/usePages";
28+
2729
const extensions = [...defaultExtensions, slashCommand];
2830

2931
interface EditorProp {
@@ -41,30 +43,14 @@ const Editor = ({ pageId, initialValue }: EditorProp) => {
4143
const [openColor, setOpenColor] = useState(false);
4244
const [openLink, setOpenLink] = useState(false);
4345

44-
const highlightCodeblocks = (content: string) => {
45-
const doc = new DOMParser().parseFromString(content, "text/html");
46-
doc.querySelectorAll("pre code").forEach((el) => {
47-
// @ts-expect-error - highlightElement is not in the types
48-
// https://highlightjs.readthedocs.io/en/latest/api.html?highlight=highlightElement#highlightelement
49-
hljs.highlightElement(el);
50-
});
51-
return new XMLSerializer().serializeToString(doc);
52-
};
46+
const updatePageMutation = useUpdatePage();
5347

5448
const debouncedUpdates = useDebouncedCallback(
5549
async (editor: EditorInstance) => {
5650
if (pageId === undefined) return;
5751

5852
const json = editor.getJSON();
59-
window.localStorage.setItem(
60-
"html-content",
61-
highlightCodeblocks(editor.getHTML()),
62-
);
63-
window.localStorage.setItem(pageId.toString(), JSON.stringify(json));
64-
window.localStorage.setItem(
65-
"markdown",
66-
editor.storage.markdown.getMarkdown(),
67-
);
53+
updatePageMutation.mutate({ id: pageId, pageData: json });
6854
},
6955

7056
500,
@@ -75,8 +61,6 @@ const Editor = ({ pageId, initialValue }: EditorProp) => {
7561
if (content) setInitialContent(JSON.parse(content));
7662
}, [pageId]);
7763

78-
console.log(initialContent);
79-
8064
return (
8165
<EditorRoot>
8266
<EditorContent

frontend/src/hooks/usePages.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
updatePage,
77
type PageRequest,
88
} from "@/api/page";
9+
import { JSONContent } from "novel";
910

1011
export const usePages = () => {
1112
const { data, isError } = useQuery({
@@ -41,7 +42,7 @@ export const useDeletePage = () => {
4142

4243
export const useUpdatePage = () => {
4344
return useMutation({
44-
mutationFn: ({ id, pageData }: { id: number; pageData: PageRequest }) =>
45+
mutationFn: ({ id, pageData }: { id: number; pageData: JSONContent }) =>
4546
updatePage(id, pageData),
4647
});
4748
};

0 commit comments

Comments
 (0)