Skip to content

Commit

Permalink
refactor: extract comment code #18
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 1, 2023
1 parent 08bda5c commit a9e2cb3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 29 deletions.
7 changes: 7 additions & 0 deletions editor/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"@types/node": "20.9.4",
"@types/react": "18.2.38",
"@types/react-dom": "^18",
"@types/uuid": "^9.0.7",
"autoprefixer": "^10.0.1",
"eslint": "^8",
"eslint-config-next": "14.0.3",
Expand Down
17 changes: 17 additions & 0 deletions editor/src/components/editor/comment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { v4 } from 'uuid'

export interface Comment {
id: string
content: string
replies: Comment[]
createdAt: Date
}

export const getNewComment = (content: string): Comment => {
return {
id: `a${v4()}a`,
content,
replies: [],
createdAt: new Date()
}
}
11 changes: 6 additions & 5 deletions editor/src/components/editor/intelli/advice-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import { Mark, mergeAttributes, Range } from "@tiptap/core";
import { Mark as PMMark } from "@tiptap/pm/model";
import { CommandProps } from "@tiptap/react";
import { Comment } from "../comment";

declare module "@tiptap/core" {
interface Commands<ReturnType> {
advice: {
setAdviceCommand: (advice: string) => ReturnType;
setAdviceCommand: (newComment: Comment) => ReturnType;
setAdvice: (commentId: string) => ReturnType;
unsetAdvice: (commentId: string) => ReturnType;
};
Expand All @@ -19,7 +20,7 @@ export interface MarkWithRange {
}

export interface CommentOptions {
setAdviceCommand: (advice: string) => void;
setAdviceCommand: (comment: Comment) => void;
HTMLAttributes: Record<string, any>;
onAdviceActivated: (commentId: string | null) => void;
}
Expand All @@ -36,7 +37,7 @@ export const AdviceExtension = Mark.create<CommentOptions, CommentStorage>({

addOptions() {
return {
setAdviceCommand: (advice: string) => {
setAdviceCommand: (comment: Comment) => {
},
HTMLAttributes: {},
onAdviceActivated: () => {
Expand Down Expand Up @@ -97,8 +98,8 @@ export const AdviceExtension = Mark.create<CommentOptions, CommentStorage>({
// @ts-ignore
addCommands() {
return {
setAdviceCommand: (advice: string) => ({ commands }: CommandProps) => {
this.options.setAdviceCommand(advice);
setAdviceCommand: (comment: Comment) => ({ commands }: CommandProps) => {
this.options.setAdviceCommand(comment);
},
setAdvice: (commentId) => ({ commands }) => {
if (!commentId) return false;
Expand Down
5 changes: 4 additions & 1 deletion editor/src/components/editor/intelli/menu/menu-bubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Editor } from "@tiptap/core";
import { CookieIcon } from "@radix-ui/react-icons";
import { ActionExecutor } from "@/components/editor/action/ActionExecutor";
import { Button, DropdownMenu } from "@radix-ui/themes";
import { getNewComment } from '../../comment';

export const MenuBubble = ({ editor }: {
editor: Editor
Expand Down Expand Up @@ -101,7 +102,9 @@ export const MenuBubble = ({ editor }: {
variant="outline"
key={index}
onClick={() => {
editor.commands?.setAdviceCommand("TODO, we are working on it")
const newComment = getNewComment("TODO, we are working on it")
editor.commands?.setAdvice(newComment.id)
editor.commands?.setAdviceCommand(newComment)
menu.action?.(editor)
}}
>
Expand Down
27 changes: 4 additions & 23 deletions editor/src/components/editor/live-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import TextStyle from '@tiptap/extension-text-style'
import StarterKit from '@tiptap/starter-kit'
import { CharacterCount } from "@tiptap/extension-character-count";
import { useTranslation } from "react-i18next";
import { v4 } from 'uuid'

import MarkdownIt from 'markdown-it'
import { useDebounce } from 'use-debounce';
Expand All @@ -24,26 +23,10 @@ import { CommandFunctions } from './action/command-functions'
import { Sidebar } from './sidebar'

import "./editor.css"
import { Comment } from "@/components/editor/comment";

const md = new MarkdownIt()


interface Comment {
id: string
content: string
replies: Comment[]
createdAt: Date
}

const getNewComment = (content: string): Comment => {
return {
id: `a${v4()}a`,
content,
replies: [],
createdAt: new Date()
}
}

const LiveEditor = () => {
const { t, i18n } = useTranslation();

Expand Down Expand Up @@ -72,11 +55,9 @@ const LiveEditor = () => {
HTMLAttributes: {
class: "my-advice",
},
setAdviceCommand: (advice: string) => {
const newComment = getNewComment(advice)
setComments([...comments, newComment])
editor?.commands.setAdvice(newComment.id)
setActiveCommentId(newComment.id)
setAdviceCommand: (comment: Comment) => {
setComments([...comments, comment])
setActiveCommentId(comment.id)
setTimeout(focusCommentWithActiveId)
},
onAdviceActivated: (commentId) => {
Expand Down

0 comments on commit a9e2cb3

Please sign in to comment.