init
This commit is contained in:
parent
99f3308e3d
commit
9429952505
37
.gitignore
vendored
37
.gitignore
vendored
|
@ -1 +1,38 @@
|
|||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
.yarn/install-state.gz
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# next.js
|
||||
/.next/
|
||||
/out/
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
*.pem
|
||||
|
||||
# debug
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# local env files
|
||||
.env*.local
|
||||
|
||||
# vercel
|
||||
.vercel
|
||||
|
||||
# typescript
|
||||
*.tsbuildinfo
|
||||
next-env.d.ts
|
||||
|
||||
pass
|
95
README.md
95
README.md
|
@ -1,2 +1,93 @@
|
|||
# paper-ai
|
||||
根据ai寻找到的论文自动生成引用
|
||||
<a href="https://demo-nextjs-with-supabase.vercel.app/">
|
||||
<img alt="Next.js and Supabase Starter Kit - the fastest way to build apps with Next.js and Supabase" src="https://demo-nextjs-with-supabase.vercel.app/opengraph-image.png">
|
||||
<h1 align="center">Next.js and Supabase Starter Kit</h1>
|
||||
</a>
|
||||
|
||||
<p align="center">
|
||||
The fastest way to build apps with Next.js and Supabase
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="#features"><strong>Features</strong></a> ·
|
||||
<a href="#demo"><strong>Demo</strong></a> ·
|
||||
<a href="#deploy-to-vercel"><strong>Deploy to Vercel</strong></a> ·
|
||||
<a href="#clone-and-run-locally"><strong>Clone and run locally</strong></a> ·
|
||||
<a href="#feedback-and-issues"><strong>Feedback and issues</strong></a>
|
||||
<a href="#more-supabase-examples"><strong>More Examples</strong></a>
|
||||
</p>
|
||||
<br/>
|
||||
|
||||
## Features
|
||||
|
||||
- Works across the entire [Next.js](https://nextjs.org) stack
|
||||
- App Router
|
||||
- Pages Router
|
||||
- Middleware
|
||||
- Client
|
||||
- Server
|
||||
- It just works!
|
||||
- supabase-ssr. A package to configure Supabase Auth to use cookies
|
||||
- Styling with [Tailwind CSS](https://tailwindcss.com)
|
||||
- Optional deployment with [Supabase Vercel Integration and Vercel deploy](#deploy-your-own)
|
||||
- Environment variables automatically assigned to Vercel project
|
||||
|
||||
## Demo
|
||||
|
||||
You can view a fully working demo at [demo-nextjs-with-supabase.vercel.app](https://demo-nextjs-with-supabase.vercel.app/).
|
||||
|
||||
## Deploy to Vercel
|
||||
|
||||
Vercel deployment will guide you through creating a Supabase account and project.
|
||||
|
||||
After installation of the Supabase integration, all relevant environment variables will be assigned to the project so the deployment is fully functioning.
|
||||
|
||||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fwith-supabase&project-name=nextjs-with-supabase&repository-name=nextjs-with-supabase&demo-title=nextjs-with-supabase&demo-description=This%20starter%20configures%20Supabase%20Auth%20to%20use%20cookies%2C%20making%20the%20user's%20session%20available%20throughout%20the%20entire%20Next.js%20app%20-%20Client%20Components%2C%20Server%20Components%2C%20Route%20Handlers%2C%20Server%20Actions%20and%20Middleware.&demo-url=https%3A%2F%2Fdemo-nextjs-with-supabase.vercel.app%2F&external-id=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fwith-supabase&demo-image=https%3A%2F%2Fdemo-nextjs-with-supabase.vercel.app%2Fopengraph-image.png&integration-ids=oac_VqOgBHqhEoFTPzGkPd7L0iH6)
|
||||
|
||||
The above will also clone the Starter kit to your GitHub, you can clone that locally and develop locally.
|
||||
|
||||
If you wish to just develop locally and not deploy to Vercel, [follow the steps below](#clone-and-run-locally).
|
||||
|
||||
## Clone and run locally
|
||||
|
||||
1. You'll first need a Supabase project which can be made [via the Supabase dashboard](https://database.new)
|
||||
|
||||
2. Create a Next.js app using the Supabase Starter template npx command
|
||||
|
||||
```bash
|
||||
npx create-next-app -e with-supabase
|
||||
```
|
||||
|
||||
3. Use `cd` to change into the app's directory
|
||||
|
||||
```bash
|
||||
cd name-of-new-app
|
||||
```
|
||||
|
||||
4. Rename `.env.local.example` to `.env.local` and update the following:
|
||||
|
||||
```
|
||||
NEXT_PUBLIC_SUPABASE_URL=[INSERT SUPABASE PROJECT URL]
|
||||
NEXT_PUBLIC_SUPABASE_ANON_KEY=[INSERT SUPABASE PROJECT API ANON KEY]
|
||||
```
|
||||
|
||||
Both `NEXT_PUBLIC_SUPABASE_URL` and `NEXT_PUBLIC_SUPABASE_ANON_KEY` can be found in [your Supabase project's API settings](https://app.supabase.com/project/_/settings/api)
|
||||
|
||||
5. You can now run the Next.js local development server:
|
||||
|
||||
```bash
|
||||
npm run dev
|
||||
```
|
||||
|
||||
The starter kit should now be running on [localhost:3000](http://localhost:3000/).
|
||||
|
||||
> Check out [the docs for Local Development](https://supabase.com/docs/guides/getting-started/local-development) to also run Supabase locally.
|
||||
|
||||
## Feedback and issues
|
||||
|
||||
Please file feedback and issues over on the [Supabase GitHub org](https://github.com/supabase/supabase/issues/new/choose).
|
||||
|
||||
## More Supabase examples
|
||||
|
||||
- [Next.js Subscription Payments Starter](https://github.com/vercel/nextjs-subscription-payments)
|
||||
- [Cookie-based Auth and the Next.js 13 App Router (free course)](https://youtube.com/playlist?list=PL5S4mPUpp4OtMhpnp93EFSo42iQ40XjbF)
|
||||
- [Supabase Auth and the Next.js App Router](https://github.com/supabase/supabase/tree/master/examples/auth/nextjs)
|
||||
|
|
20
app/auth/callback/route.ts
Normal file
20
app/auth/callback/route.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { createClient } from '@/utils/supabase/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { cookies } from 'next/headers'
|
||||
|
||||
export async function GET(request: Request) {
|
||||
// The `/auth/callback` route is required for the server-side auth flow implemented
|
||||
// by the Auth Helpers package. It exchanges an auth code for the user's session.
|
||||
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-sign-in-with-code-exchange
|
||||
const requestUrl = new URL(request.url)
|
||||
const code = requestUrl.searchParams.get('code')
|
||||
|
||||
if (code) {
|
||||
const cookieStore = cookies()
|
||||
const supabase = createClient(cookieStore)
|
||||
await supabase.auth.exchangeCodeForSession(code)
|
||||
}
|
||||
|
||||
// URL to redirect to after sign in process completes
|
||||
return NextResponse.redirect(requestUrl.origin)
|
||||
}
|
BIN
app/favicon.ico
Normal file
BIN
app/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 25 KiB |
42
app/globals.css
Normal file
42
app/globals.css
Normal file
|
@ -0,0 +1,42 @@
|
|||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
@layer base {
|
||||
:root {
|
||||
--background: 200 20% 98%;
|
||||
--btn-background: 200 10% 91%;
|
||||
--btn-background-hover: 200 10% 89%;
|
||||
--foreground: 200 50% 3%;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--background: 200 50% 3%;
|
||||
--btn-background: 200 10% 9%;
|
||||
--btn-background-hover: 200 10% 12%;
|
||||
--foreground: 200 20% 96%;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@layer base {
|
||||
* {
|
||||
@apply border-foreground/20;
|
||||
}
|
||||
}
|
||||
|
||||
.animate-in {
|
||||
animation: animateIn 0.3s ease 0.15s both;
|
||||
}
|
||||
|
||||
@keyframes animateIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(10px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
28
app/layout.tsx
Normal file
28
app/layout.tsx
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { GeistSans } from 'geist/font/sans'
|
||||
import './globals.css'
|
||||
|
||||
const defaultUrl = process.env.VERCEL_URL
|
||||
? `https://${process.env.VERCEL_URL}`
|
||||
: 'http://localhost:3000'
|
||||
|
||||
export const metadata = {
|
||||
metadataBase: new URL(defaultUrl),
|
||||
title: 'Next.js and Supabase Starter Kit',
|
||||
description: 'The fastest way to build apps with Next.js and Supabase',
|
||||
}
|
||||
|
||||
export default function RootLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
return (
|
||||
<html lang="en" className={GeistSans.className}>
|
||||
<body className="bg-background text-foreground">
|
||||
<main className="min-h-screen flex flex-col items-center">
|
||||
{children}
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
)
|
||||
}
|
118
app/login/page.tsx
Normal file
118
app/login/page.tsx
Normal file
|
@ -0,0 +1,118 @@
|
|||
import Link from 'next/link'
|
||||
import { headers, cookies } from 'next/headers'
|
||||
import { createClient } from '@/utils/supabase/server'
|
||||
import { redirect } from 'next/navigation'
|
||||
|
||||
export default function Login({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: { message: string }
|
||||
}) {
|
||||
const signIn = async (formData: FormData) => {
|
||||
'use server'
|
||||
|
||||
const email = formData.get('email') as string
|
||||
const password = formData.get('password') as string
|
||||
const cookieStore = cookies()
|
||||
const supabase = createClient(cookieStore)
|
||||
|
||||
const { error } = await supabase.auth.signInWithPassword({
|
||||
email,
|
||||
password,
|
||||
})
|
||||
|
||||
if (error) {
|
||||
return redirect('/login?message=Could not authenticate user')
|
||||
}
|
||||
|
||||
return redirect('/')
|
||||
}
|
||||
|
||||
const signUp = async (formData: FormData) => {
|
||||
'use server'
|
||||
|
||||
const origin = headers().get('origin')
|
||||
const email = formData.get('email') as string
|
||||
const password = formData.get('password') as string
|
||||
const cookieStore = cookies()
|
||||
const supabase = createClient(cookieStore)
|
||||
|
||||
const { error } = await supabase.auth.signUp({
|
||||
email,
|
||||
password,
|
||||
options: {
|
||||
emailRedirectTo: `${origin}/auth/callback`,
|
||||
},
|
||||
})
|
||||
|
||||
if (error) {
|
||||
return redirect('/login?message=Could not authenticate user')
|
||||
}
|
||||
|
||||
return redirect('/login?message=Check email to continue sign in process')
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex-1 flex flex-col w-full px-8 sm:max-w-md justify-center gap-2">
|
||||
<Link
|
||||
href="/"
|
||||
className="absolute left-8 top-8 py-2 px-4 rounded-md no-underline text-foreground bg-btn-background hover:bg-btn-background-hover flex items-center group text-sm"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="24"
|
||||
height="24"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
className="mr-2 h-4 w-4 transition-transform group-hover:-translate-x-1"
|
||||
>
|
||||
<polyline points="15 18 9 12 15 6" />
|
||||
</svg>{' '}
|
||||
Back
|
||||
</Link>
|
||||
|
||||
<form
|
||||
className="animate-in flex-1 flex flex-col w-full justify-center gap-2 text-foreground"
|
||||
action={signIn}
|
||||
>
|
||||
<label className="text-md" htmlFor="email">
|
||||
Email
|
||||
</label>
|
||||
<input
|
||||
className="rounded-md px-4 py-2 bg-inherit border mb-6"
|
||||
name="email"
|
||||
placeholder="you@example.com"
|
||||
required
|
||||
/>
|
||||
<label className="text-md" htmlFor="password">
|
||||
Password
|
||||
</label>
|
||||
<input
|
||||
className="rounded-md px-4 py-2 bg-inherit border mb-6"
|
||||
type="password"
|
||||
name="password"
|
||||
placeholder="••••••••"
|
||||
required
|
||||
/>
|
||||
<button className="bg-green-700 rounded-md px-4 py-2 text-foreground mb-2">
|
||||
Sign In
|
||||
</button>
|
||||
<button
|
||||
formAction={signUp}
|
||||
className="border border-foreground/20 rounded-md px-4 py-2 text-foreground mb-2"
|
||||
>
|
||||
Sign Up
|
||||
</button>
|
||||
{searchParams?.message && (
|
||||
<p className="mt-4 p-4 bg-foreground/10 text-foreground text-center">
|
||||
{searchParams.message}
|
||||
</p>
|
||||
)}
|
||||
</form>
|
||||
</div>
|
||||
)
|
||||
}
|
10
app/nodes/page.tsx
Normal file
10
app/nodes/page.tsx
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { createClient } from '@/utils/supabase/server';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
export default async function Notes() {
|
||||
const cookieStore = cookies()
|
||||
const supabase = createClient(cookieStore);
|
||||
const { data: notes } = await supabase.from("notes").select();
|
||||
|
||||
return <pre>{JSON.stringify(notes, null, 2)}</pre>
|
||||
}
|
BIN
app/opengraph-image.png
Normal file
BIN
app/opengraph-image.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 283 KiB |
70
app/page.tsx
Normal file
70
app/page.tsx
Normal file
|
@ -0,0 +1,70 @@
|
|||
import DeployButton from "../components/DeployButton";
|
||||
import AuthButton from "../components/AuthButton";
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
import ConnectSupabaseSteps from "@/components/ConnectSupabaseSteps";
|
||||
import SignUpUserSteps from "@/components/SignUpUserSteps";
|
||||
import Header from "@/components/Header";
|
||||
import { cookies } from "next/headers";
|
||||
import QEditor from "../components/QuillEditor";
|
||||
import TinyEditor from "../components/TinyEditor";
|
||||
import SEditor from "../components/SlateEditor";
|
||||
|
||||
export default async function Index() {
|
||||
const cookieStore = cookies();
|
||||
|
||||
const canInitSupabaseClient = () => {
|
||||
// This function is just for the interactive tutorial.
|
||||
// Feel free to remove it once you have Supabase connected.
|
||||
try {
|
||||
createClient(cookieStore);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const isSupabaseConnected = canInitSupabaseClient();
|
||||
|
||||
return (
|
||||
<div className="flex-1 w-full flex flex-col gap-20 items-center">
|
||||
<nav className="w-full flex justify-center border-b border-b-foreground/10 h-16">
|
||||
<div className="w-full max-w-4xl flex justify-between items-center p-3 text-sm">
|
||||
<DeployButton />
|
||||
{isSupabaseConnected && <AuthButton />}
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<QEditor />
|
||||
|
||||
<footer className="w-full border-t border-t-foreground/10 p-8 flex justify-center text-center text-xs">
|
||||
<p>
|
||||
Powered by{" "}
|
||||
<a
|
||||
href="https://supabase.com/?utm_source=create-next-app&utm_medium=template&utm_term=nextjs"
|
||||
target="_blank"
|
||||
className="font-bold hover:underline"
|
||||
rel="noreferrer"
|
||||
>
|
||||
Supabase
|
||||
</a>
|
||||
</p>
|
||||
</footer>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
/* <div className="animate-in flex-1 flex flex-col gap-20 opacity-0 max-w-4xl px-3">
|
||||
<Header />
|
||||
<main className="flex-1 flex flex-col gap-6">
|
||||
<h2 className="font-bold text-4xl mb-4">Next steps</h2>
|
||||
{isSupabaseConnected ? <SignUpUserSteps /> : <ConnectSupabaseSteps />}
|
||||
</main>
|
||||
</div> */
|
||||
}
|
||||
{
|
||||
/* <div className="animate-in flex-1 flex flex-col gap-20 opacity-0 max-w-4xl px-3"> */
|
||||
}
|
||||
{
|
||||
/*</div> */
|
||||
}
|
BIN
app/twitter-image.png
Normal file
BIN
app/twitter-image.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 283 KiB |
40
components/AuthButton.tsx
Normal file
40
components/AuthButton.tsx
Normal file
|
@ -0,0 +1,40 @@
|
|||
import { createClient } from '@/utils/supabase/server'
|
||||
import Link from 'next/link'
|
||||
import { cookies } from 'next/headers'
|
||||
import { redirect } from 'next/navigation'
|
||||
|
||||
export default async function AuthButton() {
|
||||
const cookieStore = cookies()
|
||||
const supabase = createClient(cookieStore)
|
||||
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser()
|
||||
|
||||
const signOut = async () => {
|
||||
'use server'
|
||||
|
||||
const cookieStore = cookies()
|
||||
const supabase = createClient(cookieStore)
|
||||
await supabase.auth.signOut()
|
||||
return redirect('/login')
|
||||
}
|
||||
|
||||
return user ? (
|
||||
<div className="flex items-center gap-4">
|
||||
Hey, {user.email}!
|
||||
<form action={signOut}>
|
||||
<button className="py-2 px-4 rounded-md no-underline bg-btn-background hover:bg-btn-background-hover">
|
||||
Logout
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
) : (
|
||||
<Link
|
||||
href="/login"
|
||||
className="py-2 px-3 flex rounded-md no-underline bg-btn-background hover:bg-btn-background-hover"
|
||||
>
|
||||
Login
|
||||
</Link>
|
||||
)
|
||||
}
|
58
components/Code.tsx
Normal file
58
components/Code.tsx
Normal file
|
@ -0,0 +1,58 @@
|
|||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
|
||||
const CopyIcon = () => (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
|
||||
<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
|
||||
</svg>
|
||||
)
|
||||
|
||||
const CheckIcon = () => (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<polyline points="20 6 9 17 4 12"></polyline>
|
||||
</svg>
|
||||
)
|
||||
|
||||
export default function Code({ code }: { code: string }) {
|
||||
const [icon, setIcon] = useState(CopyIcon)
|
||||
|
||||
const copy = async () => {
|
||||
await navigator?.clipboard?.writeText(code)
|
||||
setIcon(CheckIcon)
|
||||
setTimeout(() => setIcon(CopyIcon), 2000)
|
||||
}
|
||||
|
||||
return (
|
||||
<pre className="bg-foreground/5 rounded-md p-8 my-8 relative">
|
||||
<button
|
||||
onClick={copy}
|
||||
className="absolute top-4 right-4 p-2 rounded-md bg-foreground/5 hover:bg-foreground/10"
|
||||
>
|
||||
{icon}
|
||||
</button>
|
||||
<code>{code}</code>
|
||||
</pre>
|
||||
)
|
||||
}
|
62
components/ConnectSupabaseSteps.tsx
Normal file
62
components/ConnectSupabaseSteps.tsx
Normal file
|
@ -0,0 +1,62 @@
|
|||
import Step from './Step'
|
||||
|
||||
export default function ConnectSupabaseSteps() {
|
||||
return (
|
||||
<ol className="flex flex-col gap-6">
|
||||
<Step title="Create Supabase project">
|
||||
<p>
|
||||
Head over to{' '}
|
||||
<a
|
||||
href="https://app.supabase.com/project/_/settings/api"
|
||||
target="_blank"
|
||||
className="font-bold hover:underline text-foreground/80"
|
||||
rel="noreferrer"
|
||||
>
|
||||
database.new
|
||||
</a>{' '}
|
||||
and create a new Supabase project.
|
||||
</p>
|
||||
</Step>
|
||||
|
||||
<Step title="Declare environment variables">
|
||||
<p>
|
||||
Rename the{' '}
|
||||
<span className="px-2 py-1 rounded-md bg-foreground/20 text-foreground/80">
|
||||
.env.example
|
||||
</span>{' '}
|
||||
file in your Next.js app to{' '}
|
||||
<span className="px-2 py-1 rounded-md bg-foreground/20 text-foreground/80">
|
||||
.env.local
|
||||
</span>{' '}
|
||||
and populate with values from{' '}
|
||||
<a
|
||||
href="https://app.supabase.com/project/_/settings/api"
|
||||
target="_blank"
|
||||
className="font-bold hover:underline text-foreground/80"
|
||||
rel="noreferrer"
|
||||
>
|
||||
your Supabase project's API Settings
|
||||
</a>
|
||||
.
|
||||
</p>
|
||||
</Step>
|
||||
|
||||
<Step title="Restart your Next.js development server">
|
||||
<p>
|
||||
You may need to quit your Next.js development server and run{' '}
|
||||
<span className="px-2 py-1 rounded-md bg-foreground/20 text-foreground/80">
|
||||
npm run dev
|
||||
</span>{' '}
|
||||
again to load the new environment variables.
|
||||
</p>
|
||||
</Step>
|
||||
|
||||
<Step title="Refresh the page">
|
||||
<p>
|
||||
You may need to refresh the page for Next.js to load the new
|
||||
environment variables.
|
||||
</p>
|
||||
</Step>
|
||||
</ol>
|
||||
)
|
||||
}
|
23
components/DeployButton.tsx
Normal file
23
components/DeployButton.tsx
Normal file
|
@ -0,0 +1,23 @@
|
|||
export default function DeployButton() {
|
||||
return (
|
||||
<a
|
||||
className="py-2 px-3 flex rounded-md no-underline hover:bg-btn-background-hover border"
|
||||
href="https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fwith-supabase&project-name=nextjs-with-supabase&repository-name=nextjs-with-supabase&demo-title=nextjs-with-supabase&demo-description=This%20starter%20configures%20Supabase%20Auth%20to%20use%20cookies%2C%20making%20the%20user's%20session%20available%20throughout%20the%20entire%20Next.js%20app%20-%20Client%20Components%2C%20Server%20Components%2C%20Route%20Handlers%2C%20Server%20Actions%20and%20Middleware.&demo-url=https%3A%2F%2Fdemo-nextjs-with-supabase.vercel.app%2F&external-id=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fwith-supabase&demo-image=https%3A%2F%2Fdemo-nextjs-with-supabase.vercel.app%2Fopengraph-image.png&integration-ids=oac_VqOgBHqhEoFTPzGkPd7L0iH6"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
<svg
|
||||
aria-label="Vercel logomark"
|
||||
role="img"
|
||||
viewBox="0 0 74 64"
|
||||
className="h-4 w-4 mr-2"
|
||||
>
|
||||
<path
|
||||
d="M37.5896 0.25L74.5396 64.25H0.639648L37.5896 0.25Z"
|
||||
fill="currentColor"
|
||||
></path>
|
||||
</svg>
|
||||
Deploy to Vercel
|
||||
</a>
|
||||
)
|
||||
}
|
87
components/GetArxiv.tsx
Normal file
87
components/GetArxiv.tsx
Normal file
|
@ -0,0 +1,87 @@
|
|||
import axios, { AxiosError } from "axios";
|
||||
import { getRandomOffset } from "@/utils/others/quillutils";
|
||||
|
||||
const xml2js = require("xml2js");
|
||||
|
||||
interface ArxivFeed {
|
||||
feed: {
|
||||
xmlns: string;
|
||||
entry: ArxivEntry[];
|
||||
id: string[];
|
||||
link: Array<{ [key: string]: string }>;
|
||||
"opensearch:itemsPerPage": Array<{ [key: string]: string }>;
|
||||
"opensearch:startIndex": Array<{ [key: string]: string }>;
|
||||
"opensearch:totalResults": Array<{ [key: string]: string }>;
|
||||
title: Array<{ [key: string]: string }>;
|
||||
updated: string[];
|
||||
};
|
||||
}
|
||||
|
||||
interface ArxivEntry {
|
||||
"arxiv:comment": Array<{ [key: string]: string }>;
|
||||
"arxiv:primary_category": Array<{ [key: string]: string }>;
|
||||
author: Author[];
|
||||
category: Array<{ [key: string]: string }>;
|
||||
id: string[];
|
||||
link: Array<{ [key: string]: string }>;
|
||||
published: string[];
|
||||
summary: string[];
|
||||
title: string[];
|
||||
updated: string[];
|
||||
}
|
||||
|
||||
interface Author {
|
||||
name: string;
|
||||
affiliation?: string; // Assuming affiliation might be optional
|
||||
}
|
||||
|
||||
async function getArxivPapers(
|
||||
query: string,
|
||||
maxResults = 5,
|
||||
sortBy = "submittedDate",
|
||||
sortOrder = "descending"
|
||||
) {
|
||||
const maxOffset = 100 - maxResults; // 假设总记录数为 100
|
||||
const start = getRandomOffset(maxOffset);
|
||||
const url = `http://export.arxiv.org/api/query?search_query=${query}&start=${start}&max_results=${maxResults}&sortBy=${sortBy}&sortOrder=${sortOrder}`;
|
||||
|
||||
try {
|
||||
const response = await axios.get(url);
|
||||
let result = await xml2js.parseStringPromise(response.data);
|
||||
// 这里你将得到 JSON 格式的结果
|
||||
console.log(result);
|
||||
// 你可以在这里处理数据
|
||||
result = extractArxivData(result);
|
||||
return result;
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
// 请求已发送,但服务器响应的状态码不在 2xx 范围内
|
||||
console.error("Error fetching data: ", error.response.data);
|
||||
} else if (error.request) {
|
||||
// 请求已发送,但没有收到响应
|
||||
console.error("No response received: ", error.request);
|
||||
} else {
|
||||
// 发送请求时出现错误
|
||||
console.error("Error setting up the request: ", error.message);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function extractArxivData(data: ArxivFeed) {
|
||||
// const entries = data.feed.entry;
|
||||
const entries = data.feed.entry.slice(0, 2); // 只获取前两个条目
|
||||
|
||||
const extractedData = entries.map((entry: ArxivEntry) => {
|
||||
return {
|
||||
id: entry.id[0],
|
||||
published: entry.published[0],
|
||||
title: entry.title[0],
|
||||
summary: entry.summary[0],
|
||||
author: entry.author.map((author) => author.name[0]),
|
||||
};
|
||||
});
|
||||
return extractedData;
|
||||
}
|
||||
|
||||
export default getArxivPapers;
|
57
components/GetSemantic.tsx
Normal file
57
components/GetSemantic.tsx
Normal file
|
@ -0,0 +1,57 @@
|
|||
import axios from "axios";
|
||||
import {getRandomOffset} from "@/utils/others/quillutils"
|
||||
interface Author {
|
||||
authorId: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface Paper {
|
||||
paperId: string;
|
||||
title: string;
|
||||
abstract: string;
|
||||
year: number;
|
||||
authors: Author[];
|
||||
venue: string;
|
||||
}
|
||||
|
||||
async function getSemanticPapers(query: string, year: string, limit = 2) {
|
||||
try {
|
||||
const maxOffset = 100 - limit; // 假设总记录数为 100
|
||||
const offset = getRandomOffset(maxOffset);
|
||||
const url = `https://api.semanticscholar.org/graph/v1/paper/search`;
|
||||
const response = await axios.get(url, {
|
||||
headers: {
|
||||
'x-api-key': 'hEQvK6ARe84dzDPcMnpzX4n9jfoqztkMfaftPWnb',
|
||||
},
|
||||
params: {
|
||||
query: query,
|
||||
offset: offset,
|
||||
limit: 2,
|
||||
year: year,
|
||||
fields: "title,year,authors.name,abstract,venue",
|
||||
},
|
||||
});
|
||||
// 提取并处理论文数据
|
||||
const papers = response.data.data.map((paper:Paper) => {
|
||||
// 提取每篇论文的作者名字
|
||||
const authorNames = paper.authors.map((author) => author.name);
|
||||
|
||||
return {
|
||||
...paper,
|
||||
authors: authorNames, // 替换原有的authors字段为仅包含名字的数组
|
||||
};
|
||||
});
|
||||
return papers;
|
||||
} catch (error) {
|
||||
console.error("Error fetching data from Semantic Scholar API:", error);
|
||||
return null; // 或根据需要处理错误
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 调用函数示例
|
||||
// fetchSemanticPapers("covid", 50, 2, "2015-2023").then((data) => {
|
||||
// console.log(data);
|
||||
// });
|
||||
|
||||
export default getSemanticPapers;
|
44
components/Header.tsx
Normal file
44
components/Header.tsx
Normal file
|
@ -0,0 +1,44 @@
|
|||
import NextLogo from './NextLogo'
|
||||
import SupabaseLogo from './SupabaseLogo'
|
||||
|
||||
export default function Header() {
|
||||
return (
|
||||
<div className="flex flex-col gap-16 items-center">
|
||||
<div className="flex gap-8 justify-center items-center">
|
||||
<a
|
||||
href="https://supabase.com/?utm_source=create-next-app&utm_medium=template&utm_term=nextjs"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
<SupabaseLogo />
|
||||
</a>
|
||||
<span className="border-l rotate-45 h-6" />
|
||||
<a href="https://nextjs.org/" target="_blank" rel="noreferrer">
|
||||
<NextLogo />
|
||||
</a>
|
||||
</div>
|
||||
<h1 className="sr-only">Supabase and Next.js Starter Template</h1>
|
||||
<p className="text-3xl lg:text-4xl !leading-tight mx-auto max-w-xl text-center">
|
||||
The fastest way to build apps with{' '}
|
||||
<a
|
||||
href="https://supabase.com/?utm_source=create-next-app&utm_medium=template&utm_term=nextjs"
|
||||
target="_blank"
|
||||
className="font-bold hover:underline"
|
||||
rel="noreferrer"
|
||||
>
|
||||
Supabase
|
||||
</a>{' '}
|
||||
and{' '}
|
||||
<a
|
||||
href="https://nextjs.org/"
|
||||
target="_blank"
|
||||
className="font-bold hover:underline"
|
||||
rel="noreferrer"
|
||||
>
|
||||
Next.js
|
||||
</a>
|
||||
</p>
|
||||
<div className="w-full p-[1px] bg-gradient-to-r from-transparent via-foreground/10 to-transparent my-8" />
|
||||
</div>
|
||||
)
|
||||
}
|
46
components/NextLogo.tsx
Normal file
46
components/NextLogo.tsx
Normal file
|
@ -0,0 +1,46 @@
|
|||
export default function NextLogo() {
|
||||
return (
|
||||
<svg
|
||||
aria-label="Next.js logotype"
|
||||
height="68"
|
||||
role="img"
|
||||
viewBox="0 0 394 79"
|
||||
width="100"
|
||||
>
|
||||
<path
|
||||
d="M261.919 0.0330722H330.547V12.7H303.323V79.339H289.71V12.7H261.919V0.0330722Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M149.052 0.0330722V12.7H94.0421V33.0772H138.281V45.7441H94.0421V66.6721H149.052V79.339H80.43V12.7H80.4243V0.0330722H149.052Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M183.32 0.0661486H165.506L229.312 79.3721H247.178L215.271 39.7464L247.127 0.126654L229.312 0.154184L206.352 28.6697L183.32 0.0661486Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M201.6 56.7148L192.679 45.6229L165.455 79.4326H183.32L201.6 56.7148Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
clipRule="evenodd"
|
||||
d="M80.907 79.339L17.0151 0H0V79.3059H13.6121V16.9516L63.8067 79.339H80.907Z"
|
||||
fill="currentColor"
|
||||
fillRule="evenodd"
|
||||
/>
|
||||
<path
|
||||
d="M333.607 78.8546C332.61 78.8546 331.762 78.5093 331.052 77.8186C330.342 77.1279 329.991 76.2917 330 75.3011C329.991 74.3377 330.342 73.5106 331.052 72.8199C331.762 72.1292 332.61 71.7838 333.607 71.7838C334.566 71.7838 335.405 72.1292 336.115 72.8199C336.835 73.5106 337.194 74.3377 337.204 75.3011C337.194 75.9554 337.028 76.5552 336.696 77.0914C336.355 77.6368 335.922 78.064 335.377 78.373C334.842 78.6911 334.252 78.8546 333.607 78.8546Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M356.84 45.4453H362.872V68.6846C362.863 70.8204 362.401 72.6472 361.498 74.1832C360.585 75.7191 359.321 76.8914 357.698 77.7185C356.084 78.5364 354.193 78.9546 352.044 78.9546C350.079 78.9546 348.318 78.6001 346.75 77.9094C345.182 77.2187 343.937 76.1826 343.024 74.8193C342.101 73.456 341.649 71.7565 341.649 69.7207H347.691C347.7 70.6114 347.903 71.3838 348.29 72.0291C348.677 72.6744 349.212 73.1651 349.895 73.5105C350.586 73.8559 351.38 74.0286 352.274 74.0286C353.243 74.0286 354.073 73.8286 354.746 73.4196C355.419 73.0197 355.936 72.4199 356.296 71.6201C356.646 70.8295 356.831 69.8479 356.84 68.6846V45.4453Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M387.691 54.5338C387.544 53.1251 386.898 52.0254 385.773 51.2438C384.638 50.4531 383.172 50.0623 381.373 50.0623C380.11 50.0623 379.022 50.2532 378.118 50.6258C377.214 51.0075 376.513 51.5164 376.033 52.1617C375.554 52.807 375.314 53.5432 375.295 54.3703C375.295 55.061 375.461 55.6608 375.784 56.1607C376.107 56.6696 376.54 57.0968 377.103 57.4422C377.656 57.7966 378.274 58.0874 378.948 58.3237C379.63 58.56 380.313 58.76 380.995 58.9236L384.14 59.6961C385.404 59.9869 386.631 60.3778 387.802 60.8776C388.973 61.3684 390.034 61.9955 390.965 62.7498C391.897 63.5042 392.635 64.413 393.179 65.4764C393.723 66.5397 394 67.7848 394 69.2208C394 71.1566 393.502 72.8562 392.496 74.3285C391.491 75.7917 390.043 76.9369 388.143 77.764C386.252 78.582 383.965 79 381.272 79C378.671 79 376.402 78.6002 374.493 77.8004C372.575 77.0097 371.08 75.8463 370.001 74.3194C368.922 72.7926 368.341 70.9294 368.258 68.7391H374.235C374.318 69.8842 374.687 70.8386 375.314 71.6111C375.95 72.3745 376.78 72.938 377.795 73.3197C378.819 73.6923 379.962 73.8832 381.226 73.8832C382.545 73.8832 383.707 73.6832 384.712 73.2924C385.708 72.9016 386.492 72.3564 387.055 71.6475C387.627 70.9476 387.913 70.1206 387.922 69.1754C387.913 68.312 387.654 67.5939 387.156 67.0304C386.649 66.467 385.948 65.9944 385.053 65.6127C384.15 65.231 383.098 64.8856 381.899 64.5857L378.081 63.6223C375.323 62.9225 373.137 61.8592 371.541 60.4323C369.937 59.0054 369.143 57.115 369.143 54.7429C369.143 52.798 369.678 51.0894 370.758 49.6261C371.827 48.1629 373.294 47.0268 375.148 46.2179C377.011 45.4 379.114 45 381.456 45C383.836 45 385.92 45.4 387.719 46.2179C389.517 47.0268 390.929 48.1538 391.952 49.5897C392.976 51.0257 393.511 52.6707 393.539 54.5338H387.691Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
</svg>
|
||||
)
|
||||
}
|
218
components/QuillEditor.tsx
Normal file
218
components/QuillEditor.tsx
Normal file
|
@ -0,0 +1,218 @@
|
|||
"use client";
|
||||
|
||||
import React, { useState, useEffect, useRef } from "react";
|
||||
import Quill from "quill";
|
||||
import "quill/dist/quill.snow.css";
|
||||
|
||||
// 一些工具函数导入
|
||||
import getArxivPapers from "./GetArxiv";
|
||||
import getSemanticPapers from "./GetSemantic";
|
||||
import sendMessageToOpenAI from "./chatAI";
|
||||
import {
|
||||
getTextBeforeCursor,
|
||||
updateBracketNumbersInDelta,
|
||||
convertToSuperscript,
|
||||
} from "@/utils/others/quillutils";
|
||||
import ReferenceList from "./ReferenceList";
|
||||
//类型声明
|
||||
import { Reference } from "@/utils/global";
|
||||
|
||||
const toolbarOptions = [
|
||||
["bold", "italic", "underline", "strike"], // 加粗、斜体、下划线和删除线
|
||||
["blockquote", "code-block"], // 引用和代码块
|
||||
|
||||
[{ header: 1 }, { header: 2 }], // 标题
|
||||
[{ list: "ordered" }, { list: "bullet" }], // 列表
|
||||
[{ script: "sub" }, { script: "super" }], // 上标/下标
|
||||
[{ indent: "-1" }, { indent: "+1" }], // 缩进
|
||||
[{ direction: "rtl" }], // 文字方向
|
||||
|
||||
[{ size: ["small", false, "large", "huge"] }], // 字体大小
|
||||
[{ header: [1, 2, 3, 4, 5, 6, false] }],
|
||||
|
||||
[{ color: [] }, { background: [] }], // 字体颜色和背景色
|
||||
[{ font: [] }], // 字体
|
||||
[{ align: [] }], // 对齐方式
|
||||
|
||||
["clean"], // 清除格式按钮
|
||||
];
|
||||
|
||||
const QEditor = () => {
|
||||
const [quill, setQuill] = useState(null);
|
||||
//询问ai,用户输入
|
||||
const [userInput, setUserInput] = useState("");
|
||||
|
||||
// 初始化 Quill 编辑器
|
||||
const isMounted = useRef(false);
|
||||
const editor = useRef(null);
|
||||
// 选择论文来源
|
||||
const [selectedSource, setSelectedSource] = useState("semanticScholar"); // 默认选项
|
||||
|
||||
useEffect(() => {
|
||||
if (!isMounted.current) {
|
||||
editor.current = new Quill("#editor", {
|
||||
// modules: {
|
||||
// toolbar: toolbarOptions
|
||||
// },
|
||||
theme: "snow",
|
||||
});
|
||||
// 检查 localStorage 中是否有保存的内容
|
||||
const savedContent = localStorage.getItem("quillContent");
|
||||
if (savedContent) {
|
||||
// 设置编辑器的内容
|
||||
editor.current.root.innerHTML = savedContent;
|
||||
}
|
||||
|
||||
isMounted.current = true;
|
||||
setQuill(editor.current);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (quill) {
|
||||
// 设置监听器以处理内容变化
|
||||
quill.on("text-change", function (delta, oldDelta, source) {
|
||||
if (source === "user") {
|
||||
// 获取编辑器内容
|
||||
const content = quill.root.innerHTML; // 或 quill.getText(),或 quill.getContents()
|
||||
|
||||
// 保存到 localStorage
|
||||
localStorage.setItem("quillContent", content);
|
||||
setTimeout(() => {
|
||||
convertToSuperscript(quill);
|
||||
}, 0); // 延迟 0 毫秒,即将函数放入事件队列的下一个循环中执行,不然就会因为在改变文字触发整个函数时修改文本内容造成无法找到光标位置
|
||||
}
|
||||
});
|
||||
}
|
||||
}, [quill]);
|
||||
|
||||
//更新参考文献的部分
|
||||
const [references, setReferences] = useState<Reference[]>([]);
|
||||
|
||||
const addReference = (newReference: Reference) => {
|
||||
setReferences([...references, newReference]);
|
||||
};
|
||||
|
||||
const removeReference = (index: number) => {
|
||||
setReferences(references.filter((_, i) => i !== index));
|
||||
};
|
||||
// function updateBracketNumbers(text) {
|
||||
// let currentNumber = 1;
|
||||
// const updatedText = text.replace(/\[\d+\]/g, () => `[${currentNumber++}]`);
|
||||
// return updatedText;
|
||||
// }
|
||||
|
||||
// 处理用户输入变化
|
||||
const handleInputChange = (event) => {
|
||||
setUserInput(event.target.value);
|
||||
};
|
||||
|
||||
async function paper2AI(topic: string) {
|
||||
try {
|
||||
let rawData, dataString;
|
||||
if (selectedSource === "arxiv") {
|
||||
rawData = await getArxivPapers(topic);
|
||||
dataString = rawData
|
||||
.map((entry) => {
|
||||
addReference({
|
||||
url: entry.id,
|
||||
title: entry.title,
|
||||
year: entry.published,
|
||||
author: entry.author?.slice(0, 3).join(", "),
|
||||
});
|
||||
return `ID: ${entry.id}\nTime: ${entry.published}\nTitle: ${entry.title}\nSummary: ${entry.summary}\n\n`;
|
||||
})
|
||||
.join("");
|
||||
} else if (selectedSource === "semanticScholar") {
|
||||
rawData = await getSemanticPapers(topic, "2015-2023");
|
||||
dataString = rawData
|
||||
.map((entry) => {
|
||||
addReference({
|
||||
url: entry.paperId,
|
||||
title: entry.title,
|
||||
year: entry.published,
|
||||
author: entry.authors?.slice(0, 3).join(", "),
|
||||
venue: entry.venue,
|
||||
});
|
||||
return `Time: ${entry.year}\nTitle: ${entry.title}\nSummary: ${entry.abstract}\n\n`;
|
||||
})
|
||||
.join("");
|
||||
}
|
||||
// 其他数据源的处理
|
||||
|
||||
sendMessageToOpenAI(dataString, quill, getTextBeforeCursor(quill), topic);
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error);
|
||||
// 错误处理
|
||||
}
|
||||
}
|
||||
|
||||
// 插入论文信息
|
||||
const insertPapers = async (topic: string) => {
|
||||
const rawData = await getArxivPapers(topic);
|
||||
const dataString = rawData
|
||||
.map((entry) => {
|
||||
return `ID: ${entry.id}\nPublished: ${entry.published}\nTitle: ${entry.title}\nSummary: ${entry.summary}\n\n`;
|
||||
})
|
||||
.join("");
|
||||
quill.insertText(quill.getLength(), dataString);
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div id="Qtoolbar" className="space-y-2">
|
||||
<input
|
||||
type="text"
|
||||
value={userInput}
|
||||
onChange={handleInputChange}
|
||||
className="shadow appearance-none border rounded py-2 px-3 text-grey-darker"
|
||||
/>
|
||||
{/*<button
|
||||
onClick={handleAIClick}
|
||||
className="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Insert AI Text
|
||||
</button>*/}
|
||||
<button
|
||||
onClick={() => insertPapers(userInput || "robot")}
|
||||
className="bg-indigo-500 hover:bg-indigo-700 text-black font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Insert Papers
|
||||
</button>
|
||||
<button
|
||||
onClick={() => paper2AI(userInput || "robot")}
|
||||
className="bg-red-500 hover:bg-red-700 text-black font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Paper2AI
|
||||
</button>
|
||||
<select
|
||||
value={selectedSource}
|
||||
onChange={(e) => setSelectedSource(e.target.value)}
|
||||
>
|
||||
<option value="arxiv">arxiv</option>
|
||||
<option value="semanticScholar">semantic scholar</option>
|
||||
{/* 其他来源网站 */}
|
||||
</select>
|
||||
</div>
|
||||
<div
|
||||
id="editor"
|
||||
style={{
|
||||
height: "500px",
|
||||
width: "600px",
|
||||
minHeight: "150px", // 注意驼峰命名法
|
||||
maxHeight: "500px",
|
||||
overflowY: "auto", // overflow-y -> overflowY
|
||||
border: "1px solid #ccc",
|
||||
padding: "10px",
|
||||
}}
|
||||
></div>
|
||||
<ReferenceList
|
||||
references={references}
|
||||
addReference={addReference}
|
||||
removeReference={removeReference}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default QEditor;
|
132
components/ReactQuillEditor.tsx
Normal file
132
components/ReactQuillEditor.tsx
Normal file
|
@ -0,0 +1,132 @@
|
|||
"use client";
|
||||
|
||||
import React, { useState, useEffect } from "react";
|
||||
import ReactQuill from 'react-quill';
|
||||
import "quill/dist/quill.snow.css";
|
||||
|
||||
// 一些工具函数导入
|
||||
import getArxivPapers from "./GetArxiv";
|
||||
import sendMessageToOpenAI from "./chatAI";
|
||||
|
||||
|
||||
const QEditor = () => {
|
||||
const [quill, setQuill] = useState(null);
|
||||
const [userInput, setUserInput] = useState("");
|
||||
|
||||
const [content, setContent] = useState("");
|
||||
|
||||
// 处理内容变化
|
||||
const handleContentChange = (content) => {
|
||||
setContent(content);
|
||||
convertToSuperscript();
|
||||
};
|
||||
|
||||
|
||||
function convertToSuperscript() {
|
||||
const text = quill.getText();
|
||||
const regex = /\[\d+\]/g; // 正则表达式匹配 "[数字]" 格式
|
||||
let match;
|
||||
|
||||
while ((match = regex.exec(text)) !== null) {
|
||||
const startIndex = match.index;
|
||||
const length = match[0].length;
|
||||
|
||||
// 应用上标格式
|
||||
quill.formatText(startIndex, length, { script: "super" });
|
||||
// 重置格式(如果需要)
|
||||
if (startIndex + length < text.length) {
|
||||
quill.formatText(startIndex + length, 1, "script", false);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 处理按钮点击事件来插入文本
|
||||
const handleButtonClick = () => {
|
||||
if (quill) {
|
||||
quill.insertText(quill.getLength(), "Hello, World!");
|
||||
}
|
||||
};
|
||||
|
||||
// 处理用户输入变化
|
||||
const handleInputChange = (event) => {
|
||||
setUserInput(event.target.value);
|
||||
};
|
||||
|
||||
const paper2AI = (topic: string) => {
|
||||
getArxivPapers(topic).then((rawData) => {
|
||||
// 将每篇文章的信息转换为字符串
|
||||
const dataString = rawData
|
||||
.map((entry) => {
|
||||
return `ID: ${entry.id}\nPublished: ${entry.published}\nTitle: ${entry.title}\nSummary: ${entry.summary}\n\n`;
|
||||
})
|
||||
.join("");
|
||||
// 将处理后的字符串插入到编辑器中
|
||||
sendMessageToOpenAI(dataString, quill, quill.getText(), topic);
|
||||
});
|
||||
};
|
||||
|
||||
// 插入论文信息
|
||||
const insertPapers = async (topic: string) => {
|
||||
const rawData = await getArxivPapers(topic);
|
||||
const dataString = rawData
|
||||
.map((entry) => {
|
||||
return `ID: ${entry.id}\nPublished: ${entry.published}\nTitle: ${entry.title}\nSummary: ${entry.summary}\n\n`;
|
||||
})
|
||||
.join("");
|
||||
quill.insertText(quill.getLength(), dataString);
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="space-y-2">
|
||||
<button
|
||||
onClick={handleButtonClick}
|
||||
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Insert Text
|
||||
</button>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
value={userInput}
|
||||
onChange={handleInputChange}
|
||||
className="shadow appearance-none border rounded py-2 px-3 text-grey-darker"
|
||||
/>
|
||||
|
||||
{/*<button
|
||||
onClick={handleAIClick}
|
||||
className="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Insert AI Text
|
||||
</button>*/}
|
||||
|
||||
<button
|
||||
onClick={() => insertPapers("gnn")}
|
||||
className="bg-indigo-500 hover:bg-indigo-700 text-white font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Insert Papers
|
||||
</button>
|
||||
|
||||
<button
|
||||
onClick={() => paper2AI("gnn")}
|
||||
className="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Paper2AI
|
||||
</button>
|
||||
</div>
|
||||
<div
|
||||
id="editor"
|
||||
style={{
|
||||
height: "500px",
|
||||
width: "600px",
|
||||
minHeight: "150px", // 注意驼峰命名法
|
||||
maxHeight: "500px",
|
||||
overflowY: "auto", // overflow-y -> overflowY
|
||||
border: "1px solid #ccc",
|
||||
padding: "10px",
|
||||
}}
|
||||
></div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default QEditor;
|
84
components/ReferenceList.tsx
Normal file
84
components/ReferenceList.tsx
Normal file
|
@ -0,0 +1,84 @@
|
|||
import React, { useState } from "react";
|
||||
|
||||
import { Reference } from "@/utils/global";
|
||||
|
||||
type ReferenceListProps = {
|
||||
references: Reference[];
|
||||
addReference: (reference: Reference) => void;
|
||||
removeReference: (index: number) => void;
|
||||
};
|
||||
|
||||
function ReferenceList({
|
||||
references,
|
||||
addReference,
|
||||
removeReference,
|
||||
}: ReferenceListProps) {
|
||||
const [newTitle, setNewTitle] = useState("");
|
||||
const [newAuthor, setNewAuthor] = useState("");
|
||||
const [newYear, setNewYear] = useState(2020);
|
||||
const [newPublisher, setNewPublisher] = useState("");
|
||||
const [newUrl, setNewUrl] = useState("");
|
||||
return (
|
||||
<div>
|
||||
{/* 这里可以添加输入界面和添加按钮 */}
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
addReference({
|
||||
title: newTitle,
|
||||
author: newAuthor,
|
||||
year: newYear,
|
||||
publisher: newPublisher,
|
||||
url: newUrl,
|
||||
});
|
||||
setNewTitle("");
|
||||
setNewAuthor("");
|
||||
}}
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
value={newTitle}
|
||||
onChange={(e) => setNewTitle(e.target.value)}
|
||||
placeholder="Title"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={newAuthor}
|
||||
onChange={(e) => setNewAuthor(e.target.value)}
|
||||
placeholder="Author"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={newYear}
|
||||
onChange={(e) => setNewYear(e.target.value)}
|
||||
placeholder="Year"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={newPublisher}
|
||||
onChange={(e) => setNewPublisher(e.target.value)}
|
||||
placeholder="Publisher"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={newUrl}
|
||||
onChange={(e) => setNewUrl(e.target.value)}
|
||||
placeholder="URL"
|
||||
/>
|
||||
|
||||
<button type="submit">Add Reference</button>
|
||||
</form>
|
||||
{/* 这里可以添加显示界面 */}
|
||||
<ul>
|
||||
{references.map((reference, index) => (
|
||||
<li key={index}>
|
||||
{reference.title} by {reference.author}
|
||||
<button onClick={() => removeReference(index)}>X</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default ReferenceList;
|
116
components/SignUpUserSteps.tsx
Normal file
116
components/SignUpUserSteps.tsx
Normal file
|
@ -0,0 +1,116 @@
|
|||
import Link from 'next/link'
|
||||
import Step from './Step'
|
||||
import Code from '@/components/Code'
|
||||
|
||||
const create = `
|
||||
create table notes (
|
||||
id serial primary key,
|
||||
title text
|
||||
);
|
||||
|
||||
insert into notes(title)
|
||||
values
|
||||
('Today I created a Supabase project.'),
|
||||
('I added some data and queried it from Next.js.'),
|
||||
('It was awesome!');
|
||||
`.trim()
|
||||
|
||||
const server = `
|
||||
import { createClient } from '@/utils/supabase/server'
|
||||
import { cookies } from 'next/headers'
|
||||
|
||||
export default async function Page() {
|
||||
const cookieStore = cookies()
|
||||
const supabase = createClient(cookieStore)
|
||||
const { data: notes } = await supabase.from('notes').select()
|
||||
|
||||
return <pre>{JSON.stringify(notes, null, 2)}</pre>
|
||||
}
|
||||
`.trim()
|
||||
|
||||
const client = `
|
||||
'use client'
|
||||
|
||||
import { createClient } from '@/utils/supabase/client'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
export default function Page() {
|
||||
const [notes, setNotes] = useState<any[] | null>(null)
|
||||
const supabase = createClient()
|
||||
|
||||
useEffect(() => {
|
||||
const getData = async () => {
|
||||
const { data } = await supabase.from('notes').select()
|
||||
setNotes(data)
|
||||
}
|
||||
getData()
|
||||
}, [])
|
||||
|
||||
return <pre>{JSON.stringify(notes, null, 2)}</pre>
|
||||
}
|
||||
`.trim()
|
||||
|
||||
export default function SignUpUserSteps() {
|
||||
return (
|
||||
<ol className="flex flex-col gap-6">
|
||||
<Step title="Sign up your first user">
|
||||
<p>
|
||||
Head over to the{' '}
|
||||
<Link
|
||||
href="/login"
|
||||
className="font-bold hover:underline text-foreground/80"
|
||||
>
|
||||
Login
|
||||
</Link>{' '}
|
||||
page and sign up your first user. It's okay if this is just you for
|
||||
now. Your awesome idea will have plenty of users later!
|
||||
</p>
|
||||
</Step>
|
||||
|
||||
<Step title="Create some tables and insert some data">
|
||||
<p>
|
||||
Head over to the{' '}
|
||||
<a
|
||||
href="https://supabase.com/dashboard/project/_/editor"
|
||||
className="font-bold hover:underline text-foreground/80"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
Table Editor
|
||||
</a>{' '}
|
||||
for your Supabase project to create a table and insert some example
|
||||
data. If you're stuck for creativity, you can copy and paste the
|
||||
following into the{' '}
|
||||
<a
|
||||
href="https://supabase.com/dashboard/project/_/sql/new"
|
||||
className="font-bold hover:underline text-foreground/80"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
SQL Editor
|
||||
</a>{' '}
|
||||
and click RUN!
|
||||
</p>
|
||||
<Code code={create} />
|
||||
</Step>
|
||||
|
||||
<Step title="Query Supabase data from Next.js">
|
||||
<p>
|
||||
To create a Supabase client and query data from an Async Server
|
||||
Component, create a new page.tsx file at{' '}
|
||||
<span className="px-2 py-1 rounded-md bg-foreground/20 text-foreground/80">
|
||||
/app/notes/page.tsx
|
||||
</span>{' '}
|
||||
and add the following.
|
||||
</p>
|
||||
<Code code={server} />
|
||||
<p>Alternatively, you can use a Client Component.</p>
|
||||
<Code code={client} />
|
||||
</Step>
|
||||
|
||||
<Step title="Build in a weekend and scale to millions!">
|
||||
<p>You're ready to launch your product to the world! 🚀</p>
|
||||
</Step>
|
||||
</ol>
|
||||
)
|
||||
}
|
227
components/SlateEditor.tsx
Normal file
227
components/SlateEditor.tsx
Normal file
|
@ -0,0 +1,227 @@
|
|||
"use client";
|
||||
|
||||
import React, { useMemo, useState, useCallback } from "react";
|
||||
import {
|
||||
createEditor,
|
||||
Transforms,
|
||||
Editor,
|
||||
Node,
|
||||
Descendant,
|
||||
Element,
|
||||
Text,
|
||||
} from "slate";
|
||||
import { Slate, Editable, withReact, RenderLeafProps } from "slate-react";
|
||||
import getArxivPapers from "./GetArxiv";
|
||||
import sendMessageToOpenAI from "./chatAI";
|
||||
import isEqual from 'lodash/isEqual';
|
||||
|
||||
|
||||
const SEditor = () => {
|
||||
const [editor] = useState(() => withReact(createEditor()));
|
||||
const [userInput, setUserInput] = useState("");
|
||||
|
||||
const [editorValue, setEditorValue] = useState([
|
||||
{
|
||||
type: "paragraph",
|
||||
children: [{ text: "2.gnn的国内外研究状况\n" },
|
||||
{text: '[2]', superscript: true}],
|
||||
},
|
||||
]);
|
||||
|
||||
const handleButtonClick = () => {
|
||||
// 在当前光标位置插入文本
|
||||
Transforms.insertText(editor, "Hello, World!");
|
||||
};
|
||||
|
||||
const handleInputChange = (event) => {
|
||||
setUserInput(event.target.value);
|
||||
};
|
||||
|
||||
// const handleAIClick = () => {
|
||||
// sendMessageToOpenAI(userInput, editor, editorValue, topic);
|
||||
// };
|
||||
|
||||
const paper2AI = (topic: string) => {
|
||||
getArxivPapers(topic).then((rawData) => {
|
||||
// 将每篇文章的信息转换为字符串
|
||||
const dataString = rawData
|
||||
.map((entry) => {
|
||||
return `ID: ${entry.id}\nPublished: ${entry.published}\nTitle: ${entry.title}\nSummary: ${entry.summary}\n\n`;
|
||||
})
|
||||
.join("");
|
||||
// 将处理后的字符串插入到编辑器中
|
||||
sendMessageToOpenAI(dataString, editor, editorValue, topic);
|
||||
});
|
||||
};
|
||||
|
||||
const insertPapers = (topic: string) => {
|
||||
getArxivPapers(topic).then((rawData) => {
|
||||
// 将每篇文章的信息转换为字符串
|
||||
const dataString = rawData
|
||||
.map((entry) => {
|
||||
return `ID: ${entry.id}\nPublished: ${entry.published}\nTitle: ${entry.title}\nSummary: ${entry.summary}\n\n`;
|
||||
})
|
||||
.join("");
|
||||
|
||||
// 将处理后的字符串插入到编辑器中
|
||||
Transforms.insertText(editor, dataString);
|
||||
});
|
||||
};
|
||||
|
||||
// const handleTextChange = (value: Descendant[]) => {
|
||||
// // console.log("Original value:", value); // 打印初始值
|
||||
// // 如果值没有变化,不做任何操作
|
||||
// // if (isEqual(value, editor.children)) return;
|
||||
// const newValue = value.map((node: Node) => {
|
||||
// // console.log("Processing node:", node); // 打印当前处理的节点
|
||||
|
||||
// if (Element.isElement(node) && node.type === "paragraph") {
|
||||
// const newTexts = node.children
|
||||
// .map((child) => {
|
||||
// // console.log("Processing child:", child); // 打印子节点
|
||||
|
||||
// if (Text.isText(child)) {
|
||||
// const parts = child.text.split(/(\[\d+\])/).filter(Boolean);
|
||||
// // console.log("Text parts after split:", parts); // 打印拆分后的部分
|
||||
|
||||
// return parts.map((part) => {
|
||||
// // 检查部分是否为上标文本
|
||||
// if (/\[\d+\]/.test(part)) {
|
||||
// console.log("Superscript part found:", part); // 打印发现的上标部分
|
||||
// // 将方括号内的数字作为上标处理
|
||||
// return { text: part, superscript: true };
|
||||
// }
|
||||
// // 普通文本部分
|
||||
// return { text: part };
|
||||
// });
|
||||
// }
|
||||
// return child;
|
||||
// })
|
||||
// .flat();
|
||||
|
||||
// // console.log("New texts for node:", newTexts); // 打印节点的新文本
|
||||
|
||||
// return {
|
||||
// ...node,
|
||||
// children: newTexts,
|
||||
// };
|
||||
// }
|
||||
// return node;
|
||||
// });
|
||||
// // console.log("New value:", newValue); // 打印最终的新值
|
||||
// setEditorValue(newValue);
|
||||
// // 直接更新编辑器实例的子节点
|
||||
// editor.children = newValue;
|
||||
|
||||
// // 手动触发编辑器的 onChange 事件
|
||||
// editor.onChange();
|
||||
// console.log("New editorValue:", editorValue); // 打印最终的新值
|
||||
// };
|
||||
|
||||
const handleTextChange = (value: Descendant[]) => {
|
||||
// if (isEqual(value, editor.children)) return;
|
||||
|
||||
value.forEach((node, index) => {
|
||||
if (Element.isElement(node) && node.type === 'paragraph') {
|
||||
const path = [index]; // 获取当前节点的路径
|
||||
const newTexts = node.children.map((child) => {
|
||||
if (Text.isText(child)) {
|
||||
const parts = child.text.split(/(\[\d+\])/).filter(Boolean);
|
||||
return parts.map((part) => {
|
||||
if (/\[\d+\]/.test(part)) {
|
||||
// 处理上标文本
|
||||
return { text: part, superscript: true };
|
||||
}
|
||||
// 普通文本
|
||||
return { text: part };
|
||||
});
|
||||
}
|
||||
return child;
|
||||
}).flat();
|
||||
|
||||
// 使用 Transforms 更新节点
|
||||
Transforms.setNodes(editor, { children: newTexts }, { at: path });
|
||||
}
|
||||
});
|
||||
|
||||
// 由于已经使用 Transforms 更新了编辑器,因此不需要手动触发 onChange
|
||||
// editor.onChange(); // 这一行应该被移除
|
||||
};
|
||||
|
||||
const renderLeaf = useCallback((props: RenderLeafProps) => {
|
||||
// 打印被渲染的叶子节点的属性和子元素
|
||||
console.log("Rendering leaf:", props.leaf, "Children:", props.children);
|
||||
|
||||
if (props.leaf.superscript) {
|
||||
// 如果是上标文本,将渲染为 <sup> 元素
|
||||
console.log("Rendering a superscript leaf");
|
||||
return <sup {...props.attributes}>{props.children}</sup>;
|
||||
}
|
||||
|
||||
// 默认情况下,渲染为 <span> 元素
|
||||
console.log("Rendering a normal leaf");
|
||||
return <span {...props.attributes}>{props.children}</span>;
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="space-y-2">
|
||||
<button
|
||||
onClick={handleButtonClick}
|
||||
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Insert Text
|
||||
</button>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
value={userInput}
|
||||
onChange={handleInputChange}
|
||||
className="shadow appearance-none border rounded py-2 px-3 text-grey-darker"
|
||||
/>
|
||||
|
||||
{/*<button
|
||||
onClick={handleAIClick}
|
||||
className="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Insert AI Text
|
||||
</button>*/}
|
||||
|
||||
<button
|
||||
onClick={() => insertPapers("gnn")}
|
||||
className="bg-indigo-500 hover:bg-indigo-700 text-white font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Insert Papers
|
||||
</button>
|
||||
|
||||
<button
|
||||
onClick={() => paper2AI("gnn")}
|
||||
className="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Paper2AI
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<Slate
|
||||
editor={editor}
|
||||
initialValue={editorValue}
|
||||
onChange={handleTextChange}
|
||||
>
|
||||
<Editable
|
||||
renderLeaf={renderLeaf}
|
||||
style={{
|
||||
height: "500px",
|
||||
width: "600px",
|
||||
minHeight: "150px", // 注意驼峰命名法
|
||||
maxHeight: "500px",
|
||||
overflowY: "auto", // overflow-y -> overflowY
|
||||
border: "1px solid #ccc",
|
||||
padding: "10px",
|
||||
}}
|
||||
/>
|
||||
</Slate>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default SEditor;
|
24
components/Step.tsx
Normal file
24
components/Step.tsx
Normal file
|
@ -0,0 +1,24 @@
|
|||
export default function Step({
|
||||
title,
|
||||
children,
|
||||
}: {
|
||||
title: string
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
return (
|
||||
<li className="mx-4">
|
||||
<input type="checkbox" id={title} className={`mr-2 peer`} />
|
||||
<label
|
||||
htmlFor={title}
|
||||
className={`text-lg text-foreground/90 peer-checked:line-through font-semibold hover:cursor-pointer`}
|
||||
>
|
||||
{title}
|
||||
</label>
|
||||
<div
|
||||
className={`mx-6 text-foreground/80 text-sm peer-checked:line-through`}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
</li>
|
||||
)
|
||||
}
|
102
components/SupabaseLogo.tsx
Normal file
102
components/SupabaseLogo.tsx
Normal file
|
@ -0,0 +1,102 @@
|
|||
export default function SupabaseLogo() {
|
||||
return (
|
||||
<svg
|
||||
aria-label="Supabase logo"
|
||||
width="140"
|
||||
height="30"
|
||||
viewBox="0 0 115 23"
|
||||
fill="none"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<g clipPath="url(#clip0_4671_51136)">
|
||||
<g clipPath="url(#clip1_4671_51136)">
|
||||
<path
|
||||
d="M13.4028 21.8652C12.8424 22.5629 11.7063 22.1806 11.6928 21.2898L11.4954 8.25948H20.3564C21.9614 8.25948 22.8565 10.0924 21.8585 11.3353L13.4028 21.8652Z"
|
||||
fill="url(#paint0_linear_4671_51136)"
|
||||
/>
|
||||
<path
|
||||
d="M13.4028 21.8652C12.8424 22.5629 11.7063 22.1806 11.6928 21.2898L11.4954 8.25948H20.3564C21.9614 8.25948 22.8565 10.0924 21.8585 11.3353L13.4028 21.8652Z"
|
||||
fill="url(#paint1_linear_4671_51136)"
|
||||
fillOpacity="0.2"
|
||||
/>
|
||||
<path
|
||||
d="M9.79895 0.89838C10.3593 0.200591 11.4954 0.582929 11.5089 1.47383L11.5955 14.5041H2.84528C1.24026 14.5041 0.345103 12.6711 1.34316 11.4283L9.79895 0.89838Z"
|
||||
fill="#3ECF8E"
|
||||
/>
|
||||
</g>
|
||||
<path
|
||||
d="M30.5894 13.3913C30.7068 14.4766 31.7052 16.3371 34.6026 16.3371C37.1279 16.3371 38.3418 14.7479 38.3418 13.1976C38.3418 11.8022 37.3824 10.6588 35.4836 10.2712L34.1131 9.98049C33.5846 9.88359 33.2323 9.5929 33.2323 9.12777C33.2323 8.58512 33.7804 8.17818 34.4656 8.17818C35.5618 8.17818 35.9729 8.89521 36.0513 9.45725L38.2243 8.97275C38.1069 7.94561 37.1867 6.22083 34.446 6.22083C32.3709 6.22083 30.844 7.63555 30.844 9.34094C30.844 10.6781 31.6856 11.7828 33.5454 12.1898L34.8179 12.4805C35.5618 12.6355 35.8555 12.9844 35.8555 13.4107C35.8555 13.9146 35.4444 14.3603 34.583 14.3603C33.4476 14.3603 32.8797 13.6626 32.8212 12.9068L30.5894 13.3913Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M46.6623 16.0464H49.1486C49.1094 15.717 49.0506 15.0581 49.0506 14.3216V6.51154H46.4468V12.0542C46.4468 13.1588 45.7813 13.934 44.6263 13.934C43.4126 13.934 42.8643 13.0813 42.8643 12.0154V6.51154H40.2606V12.5387C40.2606 14.6123 41.5918 16.2984 43.9215 16.2984C44.9393 16.2984 46.0556 15.9108 46.5841 15.0193C46.5841 15.4069 46.6231 15.8526 46.6623 16.0464Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M54.433 19.7286V15.1162C54.9027 15.7558 55.8817 16.279 57.213 16.279C59.9341 16.279 61.7545 14.1472 61.7545 11.2596C61.7545 8.43021 60.1298 6.29842 57.3108 6.29842C55.8623 6.29842 54.7855 6.93792 54.3548 7.67439V6.51159H51.8295V19.7286H54.433ZM59.19 11.279C59.19 12.9845 58.133 13.9728 56.8017 13.9728C55.4708 13.9728 54.394 12.9651 54.394 11.279C54.394 9.59299 55.4708 8.6046 56.8017 8.6046C58.133 8.6046 59.19 9.59299 59.19 11.279Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M63.229 13.4495C63.229 14.9417 64.4818 16.3177 66.5375 16.3177C67.9662 16.3177 68.8865 15.6588 69.3758 14.9029C69.3758 15.2712 69.4149 15.7944 69.4737 16.0464H71.862C71.8033 15.7169 71.7449 15.0386 71.7449 14.5348V9.84482C71.7449 7.92622 70.6093 6.22083 67.5555 6.22083C64.9713 6.22083 63.5811 7.86807 63.4248 9.36033L65.7347 9.84482C65.8131 9.0115 66.4395 8.29445 67.5747 8.29445C68.6713 8.29445 69.1998 8.85646 69.1998 9.53475C69.1998 9.86421 69.0238 10.1355 68.4755 10.2131L66.1068 10.5619C64.5015 10.7945 63.229 11.744 63.229 13.4495ZM67.0854 14.3991C66.2438 14.3991 65.8325 13.8565 65.8325 13.2945C65.8325 12.558 66.361 12.1898 67.0268 12.0929L69.1998 11.7634V12.1898C69.1998 13.8759 68.1818 14.3991 67.0854 14.3991Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M76.895 16.0465V14.8837C77.4038 15.6976 78.4217 16.279 79.7531 16.279C82.4941 16.279 84.2951 14.1278 84.2951 11.2403C84.2951 8.4108 82.6701 6.25965 79.851 6.25965C78.4217 6.25965 77.3648 6.8798 76.934 7.55806V2.01546H74.3696V16.0465H76.895ZM81.6911 11.2596C81.6911 13.0038 80.6341 13.9728 79.3028 13.9728C77.9912 13.9728 76.895 12.9845 76.895 11.2596C76.895 9.51543 77.9912 8.56584 79.3028 8.56584C80.6341 8.56584 81.6911 9.51543 81.6911 11.2596Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M85.7692 13.4495C85.7692 14.9417 87.022 16.3177 89.0776 16.3177C90.5065 16.3177 91.4269 15.6588 91.916 14.9029C91.916 15.2712 91.9554 15.7944 92.014 16.0464H94.4023C94.3439 15.7169 94.2851 15.0386 94.2851 14.5348V9.84482C94.2851 7.92622 93.1495 6.22083 90.0955 6.22083C87.5115 6.22083 86.1216 7.86807 85.965 9.36033L88.2747 9.84482C88.3533 9.0115 88.9798 8.29445 90.1149 8.29445C91.2115 8.29445 91.74 8.85646 91.74 9.53475C91.74 9.86421 91.5638 10.1355 91.0156 10.2131L88.647 10.5619C87.0418 10.7945 85.7692 11.744 85.7692 13.4495ZM89.6258 14.3991C88.784 14.3991 88.3727 13.8565 88.3727 13.2945C88.3727 12.558 88.9012 12.1898 89.5671 12.0929L91.74 11.7634V12.1898C91.74 13.8759 90.722 14.3991 89.6258 14.3991Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M96.087 13.3913C96.2042 14.4766 97.2028 16.3371 100.1 16.3371C102.626 16.3371 103.839 14.7479 103.839 13.1976C103.839 11.8022 102.88 10.6588 100.981 10.2712L99.6105 9.98049C99.082 9.88359 98.7299 9.5929 98.7299 9.12777C98.7299 8.58512 99.2778 8.17818 99.963 8.17818C101.06 8.17818 101.471 8.89521 101.549 9.45725L103.722 8.97275C103.604 7.94561 102.684 6.22083 99.9436 6.22083C97.8683 6.22083 96.3416 7.63555 96.3416 9.34094C96.3416 10.6781 97.183 11.7828 99.043 12.1898L100.316 12.4805C101.06 12.6355 101.353 12.9844 101.353 13.4107C101.353 13.9146 100.942 14.3603 100.081 14.3603C98.9451 14.3603 98.3776 13.6626 98.3188 12.9068L96.087 13.3913Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M107.794 10.1937C107.852 9.32158 108.596 8.31381 109.947 8.31381C111.435 8.31381 112.062 9.24406 112.101 10.1937H107.794ZM112.355 12.6743C112.042 13.527 111.376 14.1278 110.163 14.1278C108.87 14.1278 107.794 13.2169 107.735 11.9573H114.626C114.626 11.9184 114.665 11.5309 114.665 11.1626C114.665 8.10064 112.884 6.22083 109.908 6.22083C107.441 6.22083 105.17 8.19753 105.17 11.2402C105.17 14.4572 107.5 16.3371 110.143 16.3371C112.512 16.3371 114.039 14.9611 114.528 13.3138L112.355 12.6743Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
</g>
|
||||
<defs>
|
||||
<linearGradient
|
||||
id="paint0_linear_4671_51136"
|
||||
x1="11.4954"
|
||||
y1="11.1486"
|
||||
x2="19.3439"
|
||||
y2="14.4777"
|
||||
gradientUnits="userSpaceOnUse"
|
||||
>
|
||||
<stop stopColor="#249361" />
|
||||
<stop offset="1" stopColor="#3ECF8E" />
|
||||
</linearGradient>
|
||||
<linearGradient
|
||||
id="paint1_linear_4671_51136"
|
||||
x1="8.00382"
|
||||
y1="6.42177"
|
||||
x2="11.5325"
|
||||
y2="13.1398"
|
||||
gradientUnits="userSpaceOnUse"
|
||||
>
|
||||
<stop />
|
||||
<stop offset="1" stopOpacity="0" />
|
||||
</linearGradient>
|
||||
<clipPath id="clip0_4671_51136">
|
||||
<rect
|
||||
width="113.85"
|
||||
height="21.8943"
|
||||
fill="currentColor"
|
||||
transform="translate(0.922119 0.456161)"
|
||||
/>
|
||||
</clipPath>
|
||||
<clipPath id="clip1_4671_51136">
|
||||
<rect
|
||||
width="21.3592"
|
||||
height="21.8943"
|
||||
fill="currentColor"
|
||||
transform="translate(0.919006 0.497101)"
|
||||
/>
|
||||
</clipPath>
|
||||
</defs>
|
||||
</svg>
|
||||
)
|
||||
}
|
43
components/TinyEditor.tsx
Normal file
43
components/TinyEditor.tsx
Normal file
|
@ -0,0 +1,43 @@
|
|||
"use client";
|
||||
|
||||
import { Editor } from "@tinymce/tinymce-react";
|
||||
import React, { useRef } from "react";
|
||||
|
||||
const TinyEditor = () => {
|
||||
const editorRef = useRef<Editor | null>(null);
|
||||
|
||||
const handleEditorChange = (content, editor) => {
|
||||
console.log("Content was updated:", content);
|
||||
};
|
||||
|
||||
// 添加一个函数来以编程的方式插入文本
|
||||
const insertTextAtCursor = (text) => {
|
||||
const editor = editorRef.current;
|
||||
if (editor) {
|
||||
editor.insertContent(text); // 使用 insertContent 方法插入文本
|
||||
}
|
||||
};
|
||||
return (
|
||||
<Editor
|
||||
initialValue="<p></p>"
|
||||
apiKey={process.env.NEXT_PUBLIC_TINYMCE_API_KEY}
|
||||
init={{
|
||||
height: 500,
|
||||
menubar: false,
|
||||
plugins: [
|
||||
"advlist autolink lists link image charmap print preview anchor",
|
||||
"searchreplace visualblocks code fullscreen",
|
||||
"insertdatetime media table paste code help wordcount",
|
||||
],
|
||||
toolbar:
|
||||
"undo redo | formatselect | " +
|
||||
"bold italic backcolor | alignleft aligncenter " +
|
||||
"alignright alignjustify | bullist numlist outdent indent | " +
|
||||
"removeformat | help",
|
||||
}}
|
||||
onEditorChange={handleEditorChange}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default TinyEditor;
|
180
components/chatAI.tsx
Normal file
180
components/chatAI.tsx
Normal file
|
@ -0,0 +1,180 @@
|
|||
import { Transforms } from "slate";
|
||||
import { Editor } from "slate";
|
||||
import { extractText } from "@/utils/others/slateutils";
|
||||
import {
|
||||
updateBracketNumbersInDeltaKeepSelection,
|
||||
convertToSuperscript,
|
||||
} from "@/utils/others/quillutils";
|
||||
interface ChatData {
|
||||
choices: Array<{
|
||||
delta: {
|
||||
content?: string;
|
||||
};
|
||||
}>;
|
||||
}
|
||||
|
||||
const sendMessageToOpenAI = async (
|
||||
userMessage: string,
|
||||
editor: Editor,
|
||||
editorValue: any,
|
||||
topic: string
|
||||
) => {
|
||||
// 确保 userMessage 不超过 2000 个字符
|
||||
const trimmedMessage =
|
||||
userMessage.length > 3000 ? userMessage.slice(0, 3000) : userMessage;
|
||||
//slate的方法
|
||||
// const content = `需要完成的论文主题:${topic}, 搜索到的论文内容:${trimmedMessage},之前已经完成的内容上下文:${extractText(
|
||||
// editorValue
|
||||
// )}`;
|
||||
const content = `需要完成的论文主题:${topic}, 搜索到的论文内容:${trimmedMessage},之前已经完成的内容上下文:${editorValue}`;
|
||||
// 设置API请求参数
|
||||
const requestOptions = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer " + process.env.NEXT_PUBLIC_OPENAI_API_KEY,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "gpt-3.5-turbo",
|
||||
stream: true,
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: `作为论文写作助手,您的主要任务是根据用户提供的研究主题和上下文,以及相关的研究论文,来撰写和完善学术论文。在撰写过程中,请注意以下要点:
|
||||
1.学术格式:请采用标准的学术论文格式进行写作,包括清晰的段落结构、逻辑严谨的论点展开,以及恰当的专业术语使用。
|
||||
2.文献引用:只引用与主题紧密相关的论文。在引用文献时,文末应使用方括号内的数字来标注引用来源,如 [1]。请确保每个引用在文章中都有其对应的编号,*无需在文章末尾提供参考文献列表*。只能对给出的文献进行引用,不能虚构文献。
|
||||
3.忽略无关文献:对于与主题无关的论文,请不要包含在您的写作中。只关注对理解和阐述主题有实质性帮助的资料。
|
||||
4.来源明确:在文章中,清楚地指出每个引用的具体来源。引用的信息应准确无误,确保读者能够追溯到原始文献。
|
||||
5.使用中文回答,不超过三百字
|
||||
举例:
|
||||
在某个方面,某论文实现了以下突破...[1],在另一篇论文中,研究了...[2]
|
||||
`,
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: content,
|
||||
},
|
||||
],
|
||||
}),
|
||||
};
|
||||
console.log("请求的内容\n", content);
|
||||
// 发送API请求
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
"https://api.openai.com/v1/chat/completions",
|
||||
requestOptions
|
||||
);
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
await processResult(reader, decoder, editor);
|
||||
|
||||
convertToSuperscript(editor);
|
||||
updateBracketNumbersInDeltaKeepSelection(editor);
|
||||
} catch (error) {
|
||||
console.error("Error:", error);
|
||||
}
|
||||
};
|
||||
|
||||
async function processResult(reader, decoder, editor) {
|
||||
let chunk = "";
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
console.log("Stream finished");
|
||||
break;
|
||||
}
|
||||
chunk += decoder.decode(value, { stream: true });
|
||||
|
||||
// 分割数据块为单独的数据对象
|
||||
const dataObjects = chunk
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((line) => {
|
||||
try {
|
||||
line = line.substring(6); // 移除前面的 "data: "
|
||||
if (line === "[DONE]") {
|
||||
console.log("stream finished");
|
||||
return null;
|
||||
}
|
||||
return JSON.parse(line);
|
||||
} catch (error) {
|
||||
console.error("Failed to parse line:", line);
|
||||
console.error("Error:", error);
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
if (dataObjects.length > 0) {
|
||||
// 处理每个数据对象
|
||||
dataObjects.forEach((dataObject) => {
|
||||
const content = dataObject.choices[0].delta.content;
|
||||
if (content) {
|
||||
// 在当前光标位置插入文本
|
||||
// Transforms.insertText(editor, content); //slate
|
||||
editor.insertText(editor.getSelection().index, content); //quill
|
||||
// console.log("成功插入:", content);
|
||||
}
|
||||
});
|
||||
chunk = ""; // 清空chunk以便读取新的数据
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default sendMessageToOpenAI;
|
||||
|
||||
// fetch("https://api.openai.com/v1/chat/completions", requestOptions)
|
||||
// .then((response) => {
|
||||
// // 获取响应的读取器
|
||||
// const reader = response.body!.getReader();
|
||||
// const decoder = new TextDecoder();
|
||||
// let chunk = "";
|
||||
|
||||
// // 处理流式响应
|
||||
// function processResult(result: any): Promise<void> {
|
||||
// // if (result.done) return;
|
||||
// chunk += decoder.decode(result.value, { stream: true });
|
||||
|
||||
// // 分割数据块为单独的数据对象
|
||||
// const dataObjects: ChatData[] = chunk
|
||||
// .split("\n")
|
||||
// .filter(Boolean)
|
||||
// .map((line) => {
|
||||
// try {
|
||||
// line = line.substring(6); // 移除前面的 "data: "
|
||||
// // console.log(line);
|
||||
// if (line === "[DONE]") {
|
||||
// console.log("stream finished");
|
||||
// return null;
|
||||
// }
|
||||
// return JSON.parse(line);
|
||||
// } catch (error) {
|
||||
// console.error("Failed to parse line:", line);
|
||||
// console.error("Error:", error);
|
||||
// return null;
|
||||
// }
|
||||
// })
|
||||
// .filter(Boolean);
|
||||
// if (dataObjects.length === 0) {
|
||||
// //如果这里不终止的话,会导致无限循环,程序崩溃
|
||||
// return Promise.resolve();
|
||||
// }
|
||||
// // const dataObjects = JSON.parse(chunk.data);
|
||||
// // 处理每个数据对象
|
||||
// dataObjects.forEach((dataObject) => {
|
||||
// const content = dataObject.choices[0].delta.content;
|
||||
// if (content) {
|
||||
// // 在当前光标位置插入文本
|
||||
// // Transforms.insertText(editor, content); //slate
|
||||
// editor.insertText(editor.getSelection().index, content); //quill
|
||||
// // console.log("成功插入:", content);
|
||||
// }
|
||||
// });
|
||||
// chunk = "";
|
||||
|
||||
// // 继续读取响应
|
||||
// return reader.read().then(processResult);
|
||||
// }
|
38
middleware.ts
Normal file
38
middleware.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import { NextResponse, type NextRequest } from 'next/server'
|
||||
import { createClient } from '@/utils/supabase/middleware'
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
try {
|
||||
// This `try/catch` block is only here for the interactive tutorial.
|
||||
// Feel free to remove once you have Supabase connected.
|
||||
const { supabase, response } = createClient(request)
|
||||
|
||||
// Refresh session if expired - required for Server Components
|
||||
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-session-with-middleware
|
||||
await supabase.auth.getSession()
|
||||
|
||||
return response
|
||||
} catch (e) {
|
||||
// If you are here, a Supabase client could not be created!
|
||||
// This is likely because you have not set up environment variables.
|
||||
// Check out http://localhost:3000 for Next Steps.
|
||||
return NextResponse.next({
|
||||
request: {
|
||||
headers: request.headers,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
/*
|
||||
* Match all request paths except for the ones starting with:
|
||||
* - _next/static (static files)
|
||||
* - _next/image (image optimization files)
|
||||
* - favicon.ico (favicon file)
|
||||
* Feel free to modify this pattern to include more paths.
|
||||
*/
|
||||
'/((?!_next/static|_next/image|favicon.ico).*)',
|
||||
],
|
||||
}
|
4
next.config.js
Normal file
4
next.config.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {}
|
||||
|
||||
module.exports = nextConfig
|
4629
package-lock.json
generated
Normal file
4629
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
38
package.json
Normal file
38
package.json
Normal file
|
@ -0,0 +1,38 @@
|
|||
{
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "next build",
|
||||
"start": "next start"
|
||||
},
|
||||
"dependencies": {
|
||||
"@juggle/resize-observer": "^3.4.0",
|
||||
"@supabase/ssr": "latest",
|
||||
"@supabase/supabase-js": "latest",
|
||||
"add": "^2.0.6",
|
||||
"autoprefixer": "10.4.15",
|
||||
"axios": "^1.6.5",
|
||||
"geist": "^1.0.0",
|
||||
"lodash": "^4.17.21",
|
||||
"next": "latest",
|
||||
"openai": "^4.24.3",
|
||||
"postcss": "8.4.29",
|
||||
"quill": "^1.3.7",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"react-quill": "^2.0.0",
|
||||
"slate": "^0.101.5",
|
||||
"slate-history": "^0.100.0",
|
||||
"slate-hyperscript": "^0.100.0",
|
||||
"slate-react": "^0.101.5",
|
||||
"tailwindcss": "3.3.3",
|
||||
"typescript": "5.1.3",
|
||||
"xml2js": "^0.6.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.3.1",
|
||||
"@types/react": "18.2.8",
|
||||
"@types/react-dom": "18.2.5",
|
||||
"encoding": "^0.1.13"
|
||||
}
|
||||
}
|
5
pages/api/hello.ts
Normal file
5
pages/api/hello.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
// pages/api/hello.js
|
||||
|
||||
export default function handler(req, res) {
|
||||
res.status(200).json({ message: "Hello from the API!" });
|
||||
}
|
28
pages/api/proxy.ts
Normal file
28
pages/api/proxy.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
// pages/api/proxy.js
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const upstreamUrl = "https://api.openai.com";
|
||||
|
||||
try {
|
||||
// 创建新 URL
|
||||
const url = new URL(upstreamUrl + req.url.replace('/api/proxy', ''));
|
||||
|
||||
// 创建新请求
|
||||
const newRequest = new Request(url, {
|
||||
headers: req.headers,
|
||||
method: req.method,
|
||||
body: req.method !== 'GET' ? req.body : undefined,
|
||||
});
|
||||
|
||||
// 转发请求到上游服务器
|
||||
const response = await fetch(newRequest);
|
||||
|
||||
// 将响应的内容发送回客户端
|
||||
const data = await response.text();
|
||||
res.status(response.status).send(data);
|
||||
} catch (error) {
|
||||
// 错误处理
|
||||
console.error(error);
|
||||
res.status(500).json({ error: 'Internal Server Error' });
|
||||
}
|
||||
}
|
6
postcss.config.js
Normal file
6
postcss.config.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
module.exports = {
|
||||
plugins: {
|
||||
tailwindcss: {},
|
||||
autoprefixer: {},
|
||||
},
|
||||
}
|
20
tailwind.config.js
Normal file
20
tailwind.config.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
/** @type {import('tailwindcss').Config} */
|
||||
module.exports = {
|
||||
content: [
|
||||
'./app/**/*.{js,ts,jsx,tsx,mdx}',
|
||||
'./components/**/*.{js,ts,jsx,tsx,mdx}',
|
||||
],
|
||||
theme: {
|
||||
extend: {
|
||||
colors: {
|
||||
background: 'hsl(var(--background))',
|
||||
foreground: 'hsl(var(--foreground))',
|
||||
btn: {
|
||||
background: 'hsl(var(--btn-background))',
|
||||
'background-hover': 'hsl(var(--btn-background-hover))',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
plugins: [],
|
||||
}
|
28
tsconfig.json
Normal file
28
tsconfig.json
Normal file
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noEmit": true,
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"incremental": true,
|
||||
"plugins": [
|
||||
{
|
||||
"name": "next"
|
||||
}
|
||||
],
|
||||
"paths": {
|
||||
"@/*": ["./*"]
|
||||
}
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
7
utils/global.d.ts
vendored
Normal file
7
utils/global.d.ts
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
export type Reference = {
|
||||
title: string;
|
||||
author: string;
|
||||
year: number;
|
||||
url: string;
|
||||
venue?: string;
|
||||
};
|
61
utils/others/quillutils.ts
Normal file
61
utils/others/quillutils.ts
Normal file
|
@ -0,0 +1,61 @@
|
|||
function getTextBeforeCursor(quill) {
|
||||
const cursorPosition = quill.getSelection().index;
|
||||
const start = Math.max(0, cursorPosition - 100); // 确保开始位置不是负数
|
||||
return quill.getText(start, cursorPosition - start);
|
||||
}
|
||||
|
||||
function updateBracketNumbersInDelta(delta) {
|
||||
let currentNumber = 1;
|
||||
|
||||
const updatedOps = delta.ops.map((op) => {
|
||||
if (typeof op.insert === "string") {
|
||||
return {
|
||||
...op,
|
||||
insert: op.insert.replace(/\[\d+\]/g, () => `[${currentNumber++}]`),
|
||||
};
|
||||
}
|
||||
return op;
|
||||
});
|
||||
|
||||
return { ops: updatedOps };
|
||||
}
|
||||
|
||||
function updateBracketNumbersInDeltaKeepSelection(quill) {
|
||||
const selection = quill.getSelection();
|
||||
const delta = quill.getContents();
|
||||
const updatedDelta = updateBracketNumbersInDelta(delta);
|
||||
quill.setContents(updatedDelta);
|
||||
if (selection) {
|
||||
quill.setSelection(selection.index, selection.length);
|
||||
}
|
||||
}
|
||||
|
||||
function convertToSuperscript(quill) {
|
||||
const text = quill.getText();
|
||||
const regex = /\[\d+\]/g; // 正则表达式匹配 "[数字]" 格式
|
||||
let match;
|
||||
|
||||
while ((match = regex.exec(text)) !== null) {
|
||||
const startIndex = match.index;
|
||||
const length = match[0].length;
|
||||
|
||||
// 应用上标格式
|
||||
quill.formatText(startIndex, length, { script: "super" });
|
||||
// 重置格式(如果需要)
|
||||
if (startIndex + length < text.length) {
|
||||
quill.formatText(startIndex + length, 1, "script", false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getRandomOffset(max: number) {
|
||||
return Math.floor(Math.random() * max);
|
||||
}
|
||||
|
||||
export {
|
||||
getTextBeforeCursor,
|
||||
updateBracketNumbersInDelta,
|
||||
updateBracketNumbersInDeltaKeepSelection,
|
||||
convertToSuperscript,
|
||||
getRandomOffset
|
||||
};
|
21
utils/others/slateutils.ts
Normal file
21
utils/others/slateutils.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
import { Node } from "slate";
|
||||
|
||||
const extractText = (nodes: Node[]): string => {
|
||||
return nodes
|
||||
.map((node: any) => {
|
||||
// 如果节点是文本节点
|
||||
if ("text" in node) {
|
||||
return node.text;
|
||||
}
|
||||
|
||||
// 如果节点是元素且包含子节点
|
||||
if (Array.isArray(node.children)) {
|
||||
return extractText(node.children);
|
||||
}
|
||||
|
||||
return "";
|
||||
})
|
||||
.join("");
|
||||
};
|
||||
|
||||
export { extractText };
|
7
utils/supabase/client.ts
Normal file
7
utils/supabase/client.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { createBrowserClient } from '@supabase/ssr'
|
||||
|
||||
export const createClient = () =>
|
||||
createBrowserClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
|
||||
)
|
61
utils/supabase/middleware.ts
Normal file
61
utils/supabase/middleware.ts
Normal file
|
@ -0,0 +1,61 @@
|
|||
import { createServerClient, type CookieOptions } from '@supabase/ssr'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
|
||||
export const createClient = (request: NextRequest) => {
|
||||
// Create an unmodified response
|
||||
let response = NextResponse.next({
|
||||
request: {
|
||||
headers: request.headers,
|
||||
},
|
||||
})
|
||||
|
||||
const supabase = createServerClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
||||
{
|
||||
cookies: {
|
||||
get(name: string) {
|
||||
return request.cookies.get(name)?.value
|
||||
},
|
||||
set(name: string, value: string, options: CookieOptions) {
|
||||
// If the cookie is updated, update the cookies for the request and response
|
||||
request.cookies.set({
|
||||
name,
|
||||
value,
|
||||
...options,
|
||||
})
|
||||
response = NextResponse.next({
|
||||
request: {
|
||||
headers: request.headers,
|
||||
},
|
||||
})
|
||||
response.cookies.set({
|
||||
name,
|
||||
value,
|
||||
...options,
|
||||
})
|
||||
},
|
||||
remove(name: string, options: CookieOptions) {
|
||||
// If the cookie is removed, update the cookies for the request and response
|
||||
request.cookies.set({
|
||||
name,
|
||||
value: '',
|
||||
...options,
|
||||
})
|
||||
response = NextResponse.next({
|
||||
request: {
|
||||
headers: request.headers,
|
||||
},
|
||||
})
|
||||
response.cookies.set({
|
||||
name,
|
||||
value: '',
|
||||
...options,
|
||||
})
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
return { supabase, response }
|
||||
}
|
34
utils/supabase/server.ts
Normal file
34
utils/supabase/server.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { createServerClient, type CookieOptions } from '@supabase/ssr'
|
||||
import { cookies } from 'next/headers'
|
||||
|
||||
export const createClient = (cookieStore: ReturnType<typeof cookies>) => {
|
||||
return createServerClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
||||
{
|
||||
cookies: {
|
||||
get(name: string) {
|
||||
return cookieStore.get(name)?.value
|
||||
},
|
||||
set(name: string, value: string, options: CookieOptions) {
|
||||
try {
|
||||
cookieStore.set({ name, value, ...options })
|
||||
} catch (error) {
|
||||
// The `set` method was called from a Server Component.
|
||||
// This can be ignored if you have middleware refreshing
|
||||
// user sessions.
|
||||
}
|
||||
},
|
||||
remove(name: string, options: CookieOptions) {
|
||||
try {
|
||||
cookieStore.set({ name, value: '', ...options })
|
||||
} catch (error) {
|
||||
// The `delete` method was called from a Server Component.
|
||||
// This can be ignored if you have middleware refreshing
|
||||
// user sessions.
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue
Block a user