Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions apps/web/__tests__/components/Editor.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Editor } from "@app/components/editor";
import { render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";

describe("Editor", () => {
it("hides formatting controls when editing is disabled", () => {
render(
<Editor
defaultValue={{
type: "doc",
content: [
{
type: "paragraph",
content: [{ type: "text", text: "Read only content" }],
},
],
}}
editorProps={{
editable: () => false,
}}
/>
);

expect(screen.queryAllByRole("button")).toHaveLength(0);
expect(screen.getByText("Read only content")).toBeTruthy();
});
});
17 changes: 12 additions & 5 deletions apps/web/components/editor/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,36 @@ import { cn } from "@app/ui/lib/utils";
import { MenuBar } from "./MenuBar";
import { defaultContent } from "./constants";
import { extensions } from "./extentions";
import { EditorContent, useEditor } from "@tiptap/react";
import { EditorContent, JSONContent, EditorOptions, useEditor } from "@tiptap/react";
import React, { forwardRef } from "react";

interface Props {
defaultValue?: string;
defaultValue?: string | JSONContent;
className?: string;
contentClassName?: string;
onChange?: (e: React.FormEvent<HTMLDivElement>) => void;
editorProps?: EditorOptions["editorProps"];
editable?: EditorOptions["editable"];
}

export const Editor: React.FC<Props> = forwardRef<HTMLDivElement, Props>(
({ defaultValue, className, contentClassName, onChange }, ref) => {
(
{ defaultValue, className, contentClassName, onChange, editorProps, editable },
ref
) => {
const editor = useEditor({
extensions: extensions,
extensions,
content: defaultValue || defaultContent,
editorProps,
editable,
});

return (
<section
ref={ref}
className={cn("border-2 border-gray-700 rounded-sm", className)}
>
<MenuBar editor={editor} />
{editor?.isEditable && <MenuBar editor={editor} />}
<EditorContent
editor={editor}
className={cn("p-3", contentClassName)}
Expand Down
2 changes: 1 addition & 1 deletion apps/web/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
6 changes: 2 additions & 4 deletions apps/web/pages/events/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { useMutation } from "@tanstack/react-query";
import { NextPageWithLayout } from "next";
import Image from "next/image";
import { useRouter } from "next/router";
import { Editor } from "novel";
import { RiLoaderFill } from "react-icons/ri";
import { toast } from "sonner";
import { Error } from "@app/components/Error";
import { Loader } from "@app/components/preloaders";
import { Editor } from "@app/components/editor";

const Event: NextPageWithLayout = () => {
const {
Expand Down Expand Up @@ -94,9 +94,7 @@ const Event: NextPageWithLayout = () => {
<Editor
className="w-full"
defaultValue={JSON.parse(data.data?.description as string)}
editorProps={{
editable: () => false,
}}
editable={false}
/>
<div className="flex flex-col rounded-md">
<Image
Expand Down
9 changes: 2 additions & 7 deletions apps/web/views/tasks/components/TaskPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
SelectTrigger,
SelectValue,
} from "@app/ui/components/select";
import { Editor } from "@app/components/editor";
import { useMutation } from "@tanstack/react-query";
import { useRouter } from "next/router";
import { Editor } from "novel";
import { useRef } from "react";
import Drawer from "react-modern-drawer";
import "react-modern-drawer/dist/index.css";
Expand Down Expand Up @@ -104,12 +104,7 @@ export const TaskPane = ({ open, onClose, boardId }: Props) => {
</Select>
</article>
<article className="border-2 rounded-sm mt-3 min-h-[700px]">
<Editor
className="w-full"
completionApi={`/api/generate`}
storageKey="kanban-editor"
defaultValue={defaultEditorContent}
/>
<Editor className="w-full" defaultValue={defaultEditorContent} />
</article>
<Button className="mt-3" onClick={handleSave} isLoading={isLoading}>
Save
Expand Down
9 changes: 3 additions & 6 deletions apps/web/views/tasks/components/TaskPreviewPane.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Editor } from "@app/components/editor";
import { Button } from "@app/components/ui/Button";
import { Comments } from "@app/types";
import { Editor } from "novel";
import Drawer from "react-modern-drawer";
import "react-modern-drawer/dist/index.css";

Expand All @@ -9,15 +9,13 @@ type Props = {
onClose: () => void;
title: string;
description: Comments[];
storageKey: string;
};

export const TaskPreviewPane = ({
open,
onClose,
title,
description,
storageKey,
}: Props) => {
const handleClose = () => {
localStorage.clear();
Expand All @@ -40,14 +38,13 @@ export const TaskPreviewPane = ({
<Editor
key={desc.id}
className="w-full"
completionApi={`/api/generate`}
storageKey={desc.id}
defaultValue={JSON.parse(desc.data) || ""}
editable={false}
/>
))}
</article>
<article className="border-2 rounded-sm mt-3 p-2">
<Editor className="w-full" storageKey={storageKey} defaultValue="" />
<Editor className="w-full" defaultValue="" />
</article>
<Button className="mt-3">Save</Button>
</div>
Expand Down
Loading
Loading