Skip to content

Commit

Permalink
standardize naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Aug 31, 2023
1 parent e4eba50 commit 5a91037
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 113 deletions.
38 changes: 0 additions & 38 deletions apps/web/lib/hooks/use-window-size.ts

This file was deleted.

38 changes: 0 additions & 38 deletions packages/novel/src/lib/hooks/use-window-size.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
StrikethroughIcon,
CodeIcon,
} from "lucide-react";

import { NodeSelector } from "./node-selector";
import { ColorSelector } from "./color-selector";
import { LinkSelector } from "./link-selector";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "lucide-react";
import * as Popover from "@radix-ui/react-popover";
import { Dispatch, FC, SetStateAction } from "react";
import { BubbleMenuItem } from "./bubble-menu";
import { BubbleMenuItem } from ".";

interface NodeSelectorProps {
editor: Editor;
Expand Down
4 changes: 1 addition & 3 deletions packages/novel/src/ui/editor/default-content.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const DEFAULT_EDITOR_CONTENT = {
export const defaultEditorContent = {
type: "doc",
content: [
{
Expand Down Expand Up @@ -199,5 +199,3 @@ const DEFAULT_EDITOR_CONTENT = {
},
],
};

export default DEFAULT_EDITOR_CONTENT;
16 changes: 7 additions & 9 deletions packages/novel/src/ui/editor/extensions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ import { InputRule } from "@tiptap/core";
import UploadImagesPlugin from "@/ui/editor/plugins/upload-images";
import UpdatedImage from "./updated-image";

const CustomImage = TiptapImage.extend({
addProseMirrorPlugins() {
return [UploadImagesPlugin()];
},
});

export const TiptapExtensions = [
export const defaultExtensions = [
StarterKit.configure({
bulletList: {
HTMLAttributes: {
Expand Down Expand Up @@ -78,7 +72,7 @@ export const TiptapExtensions = [

tr.insert(start - 1, this.type.create(attributes)).delete(
tr.mapping.map(start),
tr.mapping.map(end),
tr.mapping.map(end)
);
},
}),
Expand All @@ -95,7 +89,11 @@ export const TiptapExtensions = [
"text-stone-400 underline underline-offset-[3px] hover:text-stone-600 transition-colors cursor-pointer",
},
}),
CustomImage.configure({
TiptapImage.extend({
addProseMirrorPlugins() {
return [UploadImagesPlugin()];
},
}).configure({
allowBase64: true,
HTMLAttributes: {
class: "rounded-lg border border-stone-200",
Expand Down
46 changes: 25 additions & 21 deletions packages/novel/src/ui/editor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,61 @@
"use client";

import { useEffect, useRef, useState } from "react";
import { useEditor, EditorContent, JSONContent } from "@tiptap/react";
import { TiptapEditorProps } from "./props";
import { TiptapExtensions } from "./extensions";
import {
useEditor,
EditorContent,
JSONContent,
Extension,
} from "@tiptap/react";
import { defaultEditorProps } from "./props";
import { defaultExtensions } from "./extensions";
import useLocalStorage from "@/lib/hooks/use-local-storage";
import { useDebouncedCallback } from "use-debounce";
import { useCompletion } from "ai/react";
import { toast } from "sonner";
import va from "@vercel/analytics";
import DEFAULT_EDITOR_CONTENT from "./default-content";
import { EditorBubbleMenu } from "./components/bubble-menu";
import { defaultEditorContent } from "./default-content";
import { EditorBubbleMenu } from "./bubble-menu";
import { getPrevText } from "@/lib/editor";
import { ImageResizer } from "./components/image-resizer";
import { ImageResizer } from "./extensions/image-resizer";
import { EditorProps } from "@tiptap/pm/view";

export default function Editor({
defaultValue = DEFAULT_EDITOR_CONTENT,
apiRoute = "/api/generate",
defaultValue = defaultEditorContent,
extensions = [],
editorProps = {},
onUpdate = () => {},
onDebouncedUpdate = () => {},
debounceDuration = 750,
}: {
apiRoute?: string;
defaultValue?: JSONContent;
extensions?: Extension[];
editorProps?: EditorProps;
// eslint-disable-next-line no-unused-vars
onUpdate?: (content: JSONContent) => void;
// eslint-disable-next-line no-unused-vars
onDebouncedUpdate?: (content: JSONContent) => void;
debounceDuration?: number;
}) {
const [content, setContent] = useLocalStorage("content", defaultValue);
const [saveStatus, setSaveStatus] = useState("Saved");

const [hydrated, setHydrated] = useState(false);

const debouncedUpdates = useDebouncedCallback(async ({ editor }) => {
const json = editor.getJSON();
setSaveStatus("Saving...");
setContent(json);
// Simulate a delay in saving.
setTimeout(() => {
setSaveStatus("Saved");
}, 500);

onDebouncedUpdate(json);
}, debounceDuration);

const editor = useEditor({
extensions: TiptapExtensions,
editorProps: TiptapEditorProps,
extensions: [...defaultExtensions, ...extensions],
editorProps: {
...defaultEditorProps,
...editorProps,
},
onUpdate: (e) => {
setSaveStatus("Unsaved");
const selection = e.editor.state.selection;
const lastTwo = getPrevText(e.editor, {
chars: 2,
Expand All @@ -75,7 +82,7 @@ export default function Editor({

const { complete, completion, isLoading, stop } = useCompletion({
id: "novel",
api: "/api/generate",
api: apiRoute,
onFinish: (_prompt, completion) => {
editor?.commands.setTextSelection({
from: editor.state.selection.from - completion.length,
Expand Down Expand Up @@ -150,9 +157,6 @@ export default function Editor({
}}
className="relative min-h-[500px] w-full max-w-screen-lg border-stone-200 bg-white p-12 px-8 sm:mb-[calc(20vh)] sm:rounded-lg sm:border sm:px-12 sm:shadow-lg"
>
<div className="absolute right-5 top-5 mb-5 rounded-lg bg-stone-100 px-2 py-1 text-sm text-stone-400">
{saveStatus}
</div>
{editor && <EditorBubbleMenu editor={editor} />}
{editor?.isActive("image") && <ImageResizer editor={editor} />}
<EditorContent editor={editor} />
Expand Down
2 changes: 1 addition & 1 deletion packages/novel/src/ui/editor/props.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EditorProps } from "@tiptap/pm/view";
import { startImageUpload } from "@/ui/editor/plugins/upload-images";

export const TiptapEditorProps: EditorProps = {
export const defaultEditorProps: EditorProps = {
attributes: {
class: `prose-lg prose-stone dark:prose-invert prose-headings:font-display font-default focus:outline-none max-w-full`,
},
Expand Down
1 change: 0 additions & 1 deletion packages/tailwind-config/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = {
"./pages/**/*.{js,ts,jsx,tsx}",
"./ui/**/*.{js,ts,jsx,tsx}",
`src/**/*.{js,ts,jsx,tsx}`,

"../../packages/novel/src/*.{js,ts,jsx,tsx}",
],
theme: {
Expand Down

0 comments on commit 5a91037

Please sign in to comment.