Unverified Commit 94d6a432 authored by Archana Agivale's avatar Archana Agivale Committed by GitHub

Examples:Updated with-slate example to App Router (#68060)

This PR updates the with-slate example to use the App Router. Here are
the changes that have been made:

Renamed the "pages" folder to the "app" folder.
Updated the routing for iindex.tsx files to align with the App Router.
Added the layout.tsx file as part of the App Router.
Updated API folder structure with app router.
Updated Slate component code accordingly new version of slate library
Updated the package.json file.

cc @samcx

---------
Co-authored-by: 's avatarsamcx <sam@vercel.com>
parent af6aa786
export async function POST(req: Request) {
const editorState = await req.json();
console.log("TODO: Save editorState on the server", editorState);
return Response.json({
status: "ok",
});
}
export const metadata = {
title: "Next.js",
description: "Generated by Next.js",
};
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
"use client";
import { useState } from "react"; import { useState } from "react";
import { createEditor, Descendant } from "slate"; import { createEditor, Descendant } from "slate";
import { Slate, Editable, withReact } from "slate-react"; import { Slate, Editable, withReact } from "slate-react";
import { withHistory } from "slate-history";
import { InferGetServerSidePropsType } from "next";
export async function getServerSideProps() { const initialValue: Descendant[] = [
return { {
props: { children: [
editorState: [ { text: "This is editable plain text, just like a <textarea>!" },
{ ],
children: [ },
{ text: "This is editable plain text, just like a <textarea>!" }, ];
],
},
],
},
};
}
async function saveEditorState(edtorState: Descendant[]) { async function saveEditorState(edtorState: Descendant[]) {
const response = await fetch("/api/editor-state", { const response = await fetch("/api/editor-state/", {
method: "POST", method: "POST",
body: JSON.stringify(edtorState), body: JSON.stringify(edtorState),
}); });
return response.json(); return response.json();
} }
export default function IndexPage({ export default function IndexPage() {
editorState, const [editor] = useState(() => withReact(createEditor()));
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
const [editor] = useState(() => withReact(withHistory(createEditor())));
return ( return (
<Slate <Slate
editor={editor} editor={editor}
value={editorState} initialValue={initialValue}
onChange={async (value) => { onChange={async (value) => {
const isAstChange = editor.operations.some( const isAstChange = editor.operations.some(
(op) => "set_selection" !== op.type, (op) => "set_selection" !== op.type,
......
...@@ -7,15 +7,16 @@ ...@@ -7,15 +7,16 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^18.2.0", "react": "^18.3.1",
"react-dom": "^18.2.0", "react-dom": "^18.3.1",
"slate": "^0.82.0", "slate": "^0.103.0",
"slate-history": "0.93.0", "slate-history": "0.100.0",
"slate-react": "^0.98.3" "slate-react": "^0.107.1"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "18.7.5", "@types/node": "^20.14.12",
"@types/react": "18.2.8", "@types/react": "^18.3.3",
"typescript": "4.7.4" "@types/react-dom": "^18.3.0",
"typescript": "^5.5.4"
} }
} }
import { NextApiRequest, NextApiResponse } from "next";
export default async function handleEditorStateChange(
req: NextApiRequest,
res: NextApiResponse,
) {
if (req.method !== "POST") {
return res
.setHeader("Allow", ["POST"])
.status(405)
.end(`Method ${req.method} Not Allowed`);
}
const editorState = JSON.parse(req.body);
console.log("TODO: Save editorState on the server", editorState);
res.json({
status: "ok",
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment