Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

テキストエディタの実装 #16

Merged
merged 24 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@
"stylelint": "stylelint \"./src/**/*.css\" --fix"
},
"dependencies": {
"@emoji-mart/data": "^1.2.1",
"@emoji-mart/react": "^1.1.1",
"@mantine/core": "^7.15.1",
"@mantine/hooks": "^7.15.1",
"@mantine/modals": "^7.15.1",
"@mantine/spotlight": "^7.15.1",
"@mantine/tiptap": "^7.15.1",
"@prisma/client": "^6.1.0",
"@tabler/icons-react": "^3.24.0",
"@tiptap/extension-link": "^2.10.3",
"@tiptap/pm": "^2.10.3",
"@tiptap/react": "^2.10.3",
"@tiptap/starter-kit": "^2.10.3",
"emoji-mart": "^5.6.0",
"next": "15.1.0",
"react": "^19.0.0",
"react-dom": "^19.0.0"
Expand Down
912 changes: 771 additions & 141 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion src/app/_components/AppShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { useDisclosure } from "@mantine/hooks";

import Sidebar from "./Sidebar";

import TextEditor from "#/components/TextEditor";

type AppShellProps = {
children: React.ReactNode;
};
Expand Down Expand Up @@ -36,7 +38,17 @@ const AppShell: React.FC<AppShellProps> = ({ children }) => {
<Sidebar />
<MantineAppShell.Main>{children}</MantineAppShell.Main>
<MantineAppShell.Aside p="md">Aside</MantineAppShell.Aside>
<MantineAppShell.Footer p="md">Footer</MantineAppShell.Footer>
<MantineAppShell.Footer p="xs" h="auto" withBorder={false}>
<TextEditor
value="aaa"
onChange={() => {
return 0;
}}
onSend={() => {
return 0;
}}
/>
</MantineAppShell.Footer>
</MantineAppShell>
);
};
Expand Down
1 change: 1 addition & 0 deletions src/app/api/fetch-ogp/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export async function GET(request: Request) {
try {
new URL(url);
} catch (e) {
console.log(e);
return NextResponse.json(
{
status: "error",
Expand Down
3 changes: 2 additions & 1 deletion src/app/lab/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import LinkCard from "#/components/LinkCard";
import { Stack, Title } from "@mantine/core";

import LinkCard from "#/components/LinkCard";

const LabPage = () => {
return (
<div>
Expand Down
28 changes: 28 additions & 0 deletions src/components/EmojiPicker.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.PopoverDropdown {
--popover-shadow: none;

padding: 0;
background: none;
border: none;
}

.DrawerContent {
height: 435px;
padding: 0;
background: none;

em-emoji-picker {
--border-radius: 0;

width: 100%;
border-radius: 0;
}

section {
border-radius: 0;
}
}

.DrawerBody {
padding: 0;
}
80 changes: 80 additions & 0 deletions src/components/EmojiPickerControl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { useState } from "react";

import data from "@emoji-mart/data";
import Picker from "@emoji-mart/react";
import { Drawer, Popover } from "@mantine/core";
import { useMediaQuery } from "@mantine/hooks";
import {
RichTextEditorControl,
useRichTextEditorContext,
} from "@mantine/tiptap";
import { IconMoodSmile } from "@tabler/icons-react";

import styles from "./EmojiPicker.module.css";

import { EmojiData } from "#/types/emoji";

const EmojiPickerControl = () => {
const { editor } = useRichTextEditorContext();
const isDesktop = useMediaQuery("(min-width: 768px)");
const [drawerOpened, setDrawerOpened] = useState(false);

const handleEmojiSelect = (emoji: EmojiData) => {
editor?.chain().focus().insertContent(emoji.native).run();
if (isDesktop) {
setDrawerOpened(false);
}
};

if (isDesktop) {
return (
<Popover position="bottom">
<Popover.Target>
<RichTextEditorControl
title="絵文字を入力する"
aria-label="絵文字を入力する"
>
<IconMoodSmile stroke={1.5} size="1rem" />
</RichTextEditorControl>
</Popover.Target>
<Popover.Dropdown className={styles.PopoverDropdown}>
<Picker theme="light" data={data} onEmojiSelect={handleEmojiSelect} />
</Popover.Dropdown>
</Popover>
);
} else {
return (
<>
<RichTextEditorControl
title="Insert emoji"
aria-label="Insert emoji"
onClick={() => setDrawerOpened(true)}
>
<IconMoodSmile stroke={1.5} size="1rem" />
</RichTextEditorControl>
<Drawer
classNames={{
content: styles.DrawerContent,
body: styles.DrawerBody,
}}
withCloseButton={false}
position="bottom"
opened={drawerOpened}
onClose={() => setDrawerOpened(false)}
>
<Picker
style={{
width: "100%",
}}
dynamicWidth
theme="light"
data={data}
onEmojiSelect={handleEmojiSelect}
/>
</Drawer>
</>
);
}
};

export default EmojiPickerControl;
29 changes: 29 additions & 0 deletions src/components/FileUploadControl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ActionIcon, FileButton } from "@mantine/core";
import { IconCloudUpload } from "@tabler/icons-react";

/*
- アップロード可能なフォーマットはDiscordを参考にした
- https://www.reddit.com/r/discordapp/comments/f2kt5r/guide_file_formats_discord_can_embed/
- これに`image/webp`, `image/avif`を追加
*/

const FileUploadControl = () => {
const setFile = (payload: File[]) => {
console.log(payload);
};
return (
<FileButton
onChange={setFile}
accept="image/jpeg,image/png,image/gif,video/webm,video/mp4,audio/wav,audio/mp3,audio/ogg,image/webp,image/avif"
multiple
>
{(props) => (
<ActionIcon variant="light" color="gray" {...props}>
<IconCloudUpload stroke={1.5} />
</ActionIcon>
)}
</FileButton>
);
};

export default FileUploadControl;
6 changes: 4 additions & 2 deletions src/components/LinkCard.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"use client";

import { Card, Image, Skeleton, Text } from "@mantine/core";
import { useEffect, useState } from "react";

import { fetchOGPData } from "#/libs/fetch-ogp";
import { Card, Image, Skeleton, Text } from "@mantine/core";

import styles from "./LinkCard.module.css";

import { fetchOGPData } from "#/libs/fetch-ogp";

type OGPData = {
title: string;
description: string;
Expand Down
3 changes: 3 additions & 0 deletions src/components/TextEditor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.ProseMirror {
padding: 0 !important;
}
26 changes: 26 additions & 0 deletions src/components/TextEditor.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.Editor {
margin-top: 0;
border: 2px solid var(--mantine-color-gray-3);

&:hover {
border-color: var(--mantine-color-gray-4);
}

&:focus-within {
border-color: var(--mantine-color-gray-5);
}
}

.Content {
max-height: 50vh;
padding: 0.5rem;
padding-bottom: 0;
overflow-y: scroll;
}

.Toolbar {
display: flex;
justify-content: space-between;
padding: 0.5rem;
border: none;
}
62 changes: 62 additions & 0 deletions src/components/TextEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use client";

import React from "react";

import { ActionIcon } from "@mantine/core";
import { Link, RichTextEditor } from "@mantine/tiptap";
import { IconSend2 } from "@tabler/icons-react";
import { useEditor } from "@tiptap/react";
import StarterKit from "@tiptap/starter-kit";

import "@mantine/tiptap/styles.css";
import EmojiPickerControl from "./EmojiPickerControl";
import FileUploadControl from "./FileUploadControl";
import "./TextEditor.css";
import styles from "./TextEditor.module.css";

type TextEditorProps = {
value: string;
onChange: (value: string) => void;
onSend: () => void;
};

const TextEditor: React.FC<TextEditorProps> = ({ value, onChange }) => {
const editor = useEditor({
extensions: [StarterKit, Link],
content: value,
onUpdate: ({ editor }) => {
onChange(editor.getHTML());
},
immediatelyRender: false,
});

return (
<RichTextEditor editor={editor} className={styles.Editor}>
<RichTextEditor.Toolbar className={styles.Toolbar}>
<RichTextEditor.ControlsGroup>
<RichTextEditor.Bold />
<RichTextEditor.Italic />
<RichTextEditor.H1 />
<RichTextEditor.H2 />
<RichTextEditor.H3 />
<RichTextEditor.BulletList />
<RichTextEditor.OrderedList />
<RichTextEditor.Strikethrough />
<RichTextEditor.Code />
<EmojiPickerControl />
</RichTextEditor.ControlsGroup>
</RichTextEditor.Toolbar>
<RichTextEditor.Content className={styles.Content} />
<RichTextEditor.Toolbar className={styles.Toolbar}>
<RichTextEditor.ControlsGroup>
<FileUploadControl />
</RichTextEditor.ControlsGroup>
<ActionIcon w="3rem" disabled={value.trim() === ""}>
<IconSend2 />
</ActionIcon>
</RichTextEditor.Toolbar>
</RichTextEditor>
);
};

export default TextEditor;
13 changes: 13 additions & 0 deletions src/types/emoji.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
@types/emoji-martは更新が行われておらず、型定義エラーが発生するため、独自に型定義を記述
*/
export interface EmojiData {
id: string;
name: string;
native: string;
unified: string;
keywords: string[];
shortcodes: string;
skin: number;
aliases: string[];
}
20 changes: 16 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
Expand All @@ -19,9 +23,17 @@
}
],
"paths": {
"#/*": ["./src/*"]
"#/*": [
"./src/*"
]
}
},
"include": ["next-env.d.ts", "src/**/*"],
"exclude": ["node_modules"]
"include": [
"next-env.d.ts",
"src/**/*",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Loading