diff --git a/src/components/ReactEditor.tsx b/src/components/ReactEditor.tsx index 29f1aed..42e91d4 100644 --- a/src/components/ReactEditor.tsx +++ b/src/components/ReactEditor.tsx @@ -1,102 +1,12 @@ import { memo, useEffect, useRef } from "react"; import EditorJS from "@editorjs/editorjs"; -import type { - BlockToolData, - OutputBlockData, - ToolConstructable, - OutputData, -} from "@editorjs/editorjs"; +import type { OutputData } from "@editorjs/editorjs"; import { EDITOR_JS_TOOLS } from "@/utils/tools"; // @ts-expect-error Undo is not a valid tool import Undo from "editorjs-undo"; // @ts-expect-error DragDrop is not a valid tool import DragDrop from "editorjs-drag-drop"; - -type ColumnData = { - columnsData: OutputBlockData[]; -}; - -type ColumnConfig = { - tools: ToolConstructable[] | any; -}; - -class ColumnTool { - private data: ColumnData; - private wrapper: HTMLElement | undefined; - private tools: { [name: symbol]: any }; - private editors: EditorJS[]; - - constructor({ data, config }: { data?: ColumnData; config?: ColumnConfig }) { - this.data = data ?? { columnsData: [] }; - this.wrapper = undefined; - this.tools = config?.tools ?? EDITOR_JS_TOOLS; - this.editors = []; - } - - static get toolbox() { - return { - title: "Column", - icon: ``, - }; - } - - static get enableLineBreaks() { - return true; - } - - createColumn() { - const column = document.createElement("div"); - column.classList.add( - "flex", - "flex-col", - "w-1/2", - "border", - "border-gray-300", - "rounded-md" - ); - return column; - } - - render() { - console.log(this.tools); - if (!this.wrapper) this.wrapper = document.createElement("div"); - - const child = document.createElement("div"); - child.classList.add("flex", "flex-row", "w-full", "space-x-4"); - - const blocksData: BlockToolData[] = []; - - for (let i = 0; i < 2; i++) { - const column = this.createColumn(); - const editor = new EditorJS({ - holder: column, - tools: this.tools, - async onChange(api) { - const data = await api.saver.save(); - blocksData[i] = data.blocks; - }, - }); - child.appendChild(column); - this.editors.push(editor); - } - this.wrapper.appendChild(child); - - this.data.columnsData = blocksData; - - return this.wrapper; - } - - save() { - return { - blocksData: this.data.columnsData, - }; - } - - destroy() { - this.editors.forEach((editor) => editor.destroy()); - this.wrapper?.remove(); - } -} +import ColumnTool from "@/utils/editor-tools/column"; type Props = { data?: OutputData; diff --git a/src/pages/test/editor.tsx b/src/pages/test/editor.tsx index ebafa09..2260b34 100644 --- a/src/pages/test/editor.tsx +++ b/src/pages/test/editor.tsx @@ -2,14 +2,15 @@ import dynamic from 'next/dynamic' import Layout from '@/layouts/Home' import { useState } from 'react' import { OutputData } from "@editorjs/editorjs"; +import { getPageBySlug } from '@/utils/pb'; import { api } from '@/utils/api'; const ReactEditorJS = dynamic(() => import('@/components/ReactEditor'), { ssr: false }) -export default function Editor() { - const [data, setData] = useState({blocks: [], time: 0, version: ''}) +export default function Editor({data: pbData}: {data: OutputData}) { + const [data, setData] = useState(pbData); const {mutate, isLoading, data: data2, isError} = api.pages.updatePage.useMutation(); if (data2) console.log(data2) @@ -42,3 +43,17 @@ export default function Editor() { ) } + +export async function getServerSideProps({query}: {query: {[key: string]: string}}) { + if (!query.page) return {notFound: true} + + const data = await getPageBySlug(query.page); + + if (!data.data) return {notFound: true}; + + return { + props: { + data: data.data.content + } + } +} diff --git a/src/utils/editor-tools/column.ts b/src/utils/editor-tools/column.ts new file mode 100644 index 0000000..c7e1692 --- /dev/null +++ b/src/utils/editor-tools/column.ts @@ -0,0 +1,90 @@ +import EditorJS, { BlockToolData, OutputBlockData, ToolConstructable } from "@editorjs/editorjs"; +import { EDITOR_JS_TOOLS } from "../tools"; + +type ColumnData = { + columnsData: OutputBlockData[]; +}; + +type ColumnConfig = { + tools: ToolConstructable[] | any; +}; + +class ColumnTool { + private data: ColumnData; + private wrapper: HTMLElement | undefined; + private tools: { [name: symbol]: any }; + private editors: EditorJS[]; + + constructor({ data, config }: { data?: ColumnData; config?: ColumnConfig }) { + this.data = data ?? { columnsData: [] }; + this.wrapper = undefined; + this.tools = config?.tools ?? EDITOR_JS_TOOLS; + this.editors = []; + } + + static get toolbox() { + return { + title: "Column", + icon: ``, + }; + } + + static get enableLineBreaks() { + return true; + } + + createColumn() { + const column = document.createElement("div"); + column.classList.add( + "flex", + "flex-col", + "w-1/2", + "border", + "border-gray-300", + "rounded-md" + ); + return column; + } + + render() { + console.log(this.tools); + if (!this.wrapper) this.wrapper = document.createElement("div"); + + const child = document.createElement("div"); + child.classList.add("flex", "flex-row", "w-full", "space-x-4"); + + const blocksData: BlockToolData[] = []; + + for (let i = 0; i < 2; i++) { + const column = this.createColumn(); + const editor = new EditorJS({ + holder: column, + tools: this.tools, + async onChange(api) { + const data = await api.saver.save(); + blocksData[i] = data.blocks; + }, + }); + child.appendChild(column); + this.editors.push(editor); + } + this.wrapper.appendChild(child); + + this.data.columnsData = blocksData; + + return this.wrapper; + } + + save() { + return { + blocksData: this.data.columnsData, + }; + } + + destroy() { + this.editors.forEach((editor) => editor.destroy()); + this.wrapper?.remove(); + } +} + +export default ColumnTool;