Skip to content

Commit

Permalink
feat: add markown editor preview (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchiarulli committed Jun 26, 2024
1 parent 2681a97 commit caacd67
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 28 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@tauri-apps/plugin-os": "^2.0.0-beta.2",
"@tauri-apps/plugin-shell": "^2.0.0-beta.2",
"@uiw/codemirror-themes": "^4.21.24",
"@uiw/react-markdown-preview": "^5.1.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"cmdk": "^1.0.0",
Expand All @@ -50,6 +51,7 @@
"react-hook-form": "^7.52.0",
"react-intersection-observer": "^9.8.2",
"react-resizable-panels": "^2.0.13",
"rehype-sanitize": "^6.0.0",
"react-router-dom": "^6.24.0",
"sonner": "^1.4.32",
"tailwind-merge": "^2.3.0",
Expand Down
21 changes: 4 additions & 17 deletions src/components/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { useCM6Editor } from "~/hooks/useCM6Editor";
import { useAppContext } from "~/store";
import { type InfiniteQueryData, type Note } from "~/types";

import EditorControls from "./EditorControls";
import TagInput from "./TagInput";

export const Editor = () => {
const { currentNote, setCurrentNote, currentTrashedNote } = useAppContext();

Expand Down Expand Up @@ -45,20 +42,10 @@ export const Editor = () => {
});

return (
<>
{(currentNote ?? currentTrashedNote) && (
<div className="flex h-full flex-col">
<div
className="editor-container h-full w-full overflow-y-auto"
ref={editorRef}
/>
<div className="flex items-center border-t border-muted">
<TagInput />
<EditorControls />
</div>
</div>
)}
</>
<div
className="editor-container h-full w-full overflow-y-auto"
ref={editorRef}
/>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/EditorControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function EditorControls() {
}

return (
<div className="flex gap-y-2 p-2">
<div className="flex gap-y-2 py-2 pl-2 pr-5">
<Button onClick={handleSendNote} variant="outline" size="icon">
<SendIcon className="h-[1.2rem] w-[1.2rem]" />
</Button>
Expand Down
47 changes: 47 additions & 0 deletions src/components/editor/EditorWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useState } from "react";

import { Button } from "~/components/ui/button";
import { useAppContext } from "~/store";
import { Eye } from "lucide-react";

import Editor from "./Editor";
import EditorControls from "./EditorControls";
import Preview from "./Preview";
import TagInput from "./TagInput";

export const EditorWrapper = () => {
const { currentNote, currentTrashedNote } = useAppContext();
const [showPreview, setShowPreview] = useState(false);

return (
<>
{(currentNote ?? currentTrashedNote) && (
<div className="flex h-full flex-col">
{!showPreview && <Editor />}
{showPreview && <Preview />}
<div className="fixed bottom-[3.75rem] right-2.5 p-2">
<Button
id="editor-preview-btn"
name="editor-preview-btn"
type="button"
variant="ghost"
size="icon"
onClick={() =>
setShowPreview((prevShowPreview) => !prevShowPreview)
}
className="rounded-full text-muted-foreground"
>
<Eye className="h-[1.2rem] w-[1.2rem]" />
</Button>
</div>
<div className="flex items-center border-t border-muted">
<TagInput />
<EditorControls />
</div>
</div>
)}
</>
);
};

export default EditorWrapper;
23 changes: 23 additions & 0 deletions src/components/editor/Preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import MarkdownPreview from "@uiw/react-markdown-preview";
import { useAppContext } from "~/store";
import rehypeSanitize from "rehype-sanitize";

const rehypePlugins = [rehypeSanitize];

export const Preview = () => {
const { currentNote, currentTrashedNote } = useAppContext();

return (
<div className="editor-container h-full w-full overflow-y-auto">
<div className="h-full w-full">
<MarkdownPreview
source={currentNote?.content ?? currentTrashedNote?.content ?? ""}
className="pb-40 pl-[22px] pr-[18px] pt-4"
rehypePlugins={rehypePlugins}
/>
</div>
</div>
);
};

export default Preview;
4 changes: 2 additions & 2 deletions src/components/editor/TagInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function TagInput() {

if (noteId === undefined) return;

if (currentNote?.tags.some(tag => tag.name === tagName)) return;
if (currentNote?.tags.some((tag) => tag.name === tagName)) return;

const getTagResponse = await getTag({ name: tagName });
const existingTag = getTagResponse.data;
Expand Down Expand Up @@ -68,7 +68,7 @@ export default function TagInput() {
};

return (
<div className="w-full px-2">
<div className="w-full pl-4 pr-2">
<div className="flex items-center gap-x-2">
{currentNote?.tags?.map((tag, tagIndex) => {
return <NoteTag key={tagIndex} tag={tag} />;
Expand Down
2 changes: 0 additions & 2 deletions src/components/notes/NoteCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export default function NoteCard({ note }: Props) {
note.tags = currentNoteTags;
setCurrentNote(note);

console.log("currentNote", currentNote);

if (activePage === "settings") {
setActivePage("editor");
}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ContextSidebar from "~/components/context/ContextSidebar";
import Editor from "~/components/editor/Editor";
import EditorWrapper from "~/components/editor/EditorWrapper";
import NoteFeed from "~/components/notes/NoteFeed";
import NoteFeedHeader from "~/components/notes/NoteFeedHeader";
import SearchFeed from "~/components/notes/SearchFeed";
Expand Down Expand Up @@ -33,7 +33,7 @@ export default function HomePage() {
<ResizableHandle />
<ResizablePanel className="border-sky-500" minSize={10}>
{activePage === "settings" && <Settings />}
{activePage === "editor" && <Editor />}
{activePage === "editor" && <EditorWrapper />}
</ResizablePanel>
</ResizablePanelGroup>
</div>
Expand Down
15 changes: 11 additions & 4 deletions src/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@
@apply bg-background text-foreground;
}

.editor-container .cm-scroller {
overflow: auto;
}

.editor-container .cm-editor {
width: 100%;
height: 100%;
}

.editor-container .cm-scroller {
overflow: auto;
}

.editor-container .cm-content {
padding-right: 1rem;
padding-left: 1rem;
Expand All @@ -92,6 +92,13 @@
}
}

.wmde-markdown {
font-family: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji",
"Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji" !important;
@apply !text-foreground;
@apply !bg-background;
}

/* TODO: fix this so that the shadcn scroll area bar can be used */

/* Scrollbar background */
Expand Down

0 comments on commit caacd67

Please sign in to comment.