|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Separator } from '@repo/design-system/components/ui/separator'; |
| 4 | +import { |
| 5 | + EditorCommand, |
| 6 | + EditorCommandEmpty, |
| 7 | + EditorCommandItem, |
| 8 | + EditorCommandList, |
| 9 | + EditorContent, |
| 10 | + type EditorInstance, |
| 11 | + EditorRoot, |
| 12 | + type JSONContent, |
| 13 | +} from 'novel'; |
| 14 | +import { ImageResizer, handleCommandNavigation } from 'novel'; |
| 15 | +import { useEffect, useState } from 'react'; |
| 16 | +import { defaultExtensions } from './extensions'; |
| 17 | +import { ColorSelector } from './selectors/color-selector'; |
| 18 | +import { LinkSelector } from './selectors/link-selector'; |
| 19 | +import { MathSelector } from './selectors/math-selector'; |
| 20 | +import { NodeSelector } from './selectors/node-selector'; |
| 21 | + |
| 22 | +import { defaultEditorContent } from '@/lib/content'; |
| 23 | +import { handleImageDrop, handleImagePaste } from 'novel'; |
| 24 | +import { useDebouncedCallback } from 'use-debounce'; |
| 25 | +import GenerativeMenuSwitch from './generative/generative-menu-switch'; |
| 26 | +import { uploadFn } from './image-upload'; |
| 27 | +import { TextButtons } from './selectors/text-buttons'; |
| 28 | +import { slashCommand, suggestionItems } from './slash-command'; |
| 29 | + |
| 30 | +const hljs = require('highlight.js'); |
| 31 | + |
| 32 | +export const AdvancedEditor = () => { |
| 33 | + const [initialContent, setInitialContent] = useState<null | JSONContent>( |
| 34 | + null |
| 35 | + ); |
| 36 | + const extensions = [...defaultExtensions, slashCommand]; |
| 37 | + const [charsCount, setCharsCount] = useState(); |
| 38 | + const [saveStatus, setSaveStatus] = useState('Saved'); |
| 39 | + const [openNode, setOpenNode] = useState(false); |
| 40 | + const [openColor, setOpenColor] = useState(false); |
| 41 | + const [openLink, setOpenLink] = useState(false); |
| 42 | + const [openAI, setOpenAI] = useState(false); |
| 43 | + |
| 44 | + //Apply Codeblock Highlighting on the HTML from editor.getHTML() |
| 45 | + const highlightCodeblocks = (content: string) => { |
| 46 | + const doc = new DOMParser().parseFromString(content, 'text/html'); |
| 47 | + doc.querySelectorAll('pre code').forEach((el) => { |
| 48 | + // @ts-ignore |
| 49 | + // https://highlightjs.readthedocs.io/en/latest/api.html?highlight=highlightElement#highlightelement |
| 50 | + hljs.highlightElement(el); |
| 51 | + }); |
| 52 | + return new XMLSerializer().serializeToString(doc); |
| 53 | + }; |
| 54 | + |
| 55 | + const debouncedUpdates = useDebouncedCallback( |
| 56 | + async (editor: EditorInstance) => { |
| 57 | + const json = editor.getJSON(); |
| 58 | + console.log('json', json); |
| 59 | + setCharsCount(editor.storage.characterCount.words()); |
| 60 | + window.localStorage.setItem( |
| 61 | + 'html-content', |
| 62 | + highlightCodeblocks(editor.getHTML()) |
| 63 | + ); |
| 64 | + window.localStorage.setItem('novel-content', JSON.stringify(json)); |
| 65 | + window.localStorage.setItem( |
| 66 | + 'markdown', |
| 67 | + editor.storage.markdown.getMarkdown() |
| 68 | + ); |
| 69 | + setSaveStatus('Saved'); |
| 70 | + }, |
| 71 | + 500 |
| 72 | + ); |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + const content = window.localStorage.getItem('novel-content'); |
| 76 | + if (content) setInitialContent(JSON.parse(content)); |
| 77 | + else setInitialContent(defaultEditorContent); |
| 78 | + }, []); |
| 79 | + |
| 80 | + if (!initialContent) return null; |
| 81 | + |
| 82 | + return ( |
| 83 | + <div className="relative w-full max-w-screen-lg"> |
| 84 | + <div className="absolute top-5 right-5 z-10 mb-5 flex gap-2"> |
| 85 | + <div |
| 86 | + className={ |
| 87 | + charsCount |
| 88 | + ? 'rounded-lg bg-accent px-2 py-1 text-muted-foreground text-sm' |
| 89 | + : 'hidden' |
| 90 | + } |
| 91 | + > |
| 92 | + {charsCount} Words |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + <EditorRoot> |
| 96 | + <EditorContent |
| 97 | + initialContent={initialContent} |
| 98 | + extensions={extensions} |
| 99 | + className="relative min-h-[500px] w-full p-24" |
| 100 | + editorProps={{ |
| 101 | + handleDOMEvents: { |
| 102 | + keydown: (_view, event) => handleCommandNavigation(event), |
| 103 | + }, |
| 104 | + handlePaste: (view, event) => |
| 105 | + handleImagePaste(view, event, uploadFn), |
| 106 | + handleDrop: (view, event, _slice, moved) => |
| 107 | + handleImageDrop(view, event, moved, uploadFn), |
| 108 | + attributes: { |
| 109 | + class: |
| 110 | + 'prose dark:prose-invert prose-headings:font-title font-default focus:outline-none max-w-full', |
| 111 | + }, |
| 112 | + }} |
| 113 | + onUpdate={({ editor }) => { |
| 114 | + debouncedUpdates(editor); |
| 115 | + setSaveStatus('Unsaved'); |
| 116 | + }} |
| 117 | + slotAfter={<ImageResizer />} |
| 118 | + immediatelyRender={false} |
| 119 | + > |
| 120 | + {/* <div className="absolute left-full ml-4"> |
| 121 | + <Threads /> |
| 122 | + </div> */} |
| 123 | + <EditorCommand className="z-50 h-auto max-h-[330px] overflow-y-auto rounded-md border border-muted bg-background px-1 py-2 shadow-md transition-all"> |
| 124 | + <EditorCommandEmpty className="px-2 text-muted-foreground"> |
| 125 | + No results |
| 126 | + </EditorCommandEmpty> |
| 127 | + <EditorCommandList> |
| 128 | + {suggestionItems.map((item) => ( |
| 129 | + <EditorCommandItem |
| 130 | + value={item.title} |
| 131 | + onCommand={(val) => { |
| 132 | + if (!item?.command) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + item.command(val); |
| 137 | + }} |
| 138 | + className="flex w-full items-center space-x-2 rounded-md px-2 py-1 text-left text-sm hover:bg-accent aria-selected:bg-accent" |
| 139 | + key={item.title} |
| 140 | + > |
| 141 | + <div className="flex h-10 w-10 items-center justify-center rounded-md border border-muted bg-background"> |
| 142 | + {item.icon} |
| 143 | + </div> |
| 144 | + <div> |
| 145 | + <p className="font-medium">{item.title}</p> |
| 146 | + <p className="text-muted-foreground text-xs"> |
| 147 | + {item.description} |
| 148 | + </p> |
| 149 | + </div> |
| 150 | + </EditorCommandItem> |
| 151 | + ))} |
| 152 | + </EditorCommandList> |
| 153 | + </EditorCommand> |
| 154 | + |
| 155 | + <GenerativeMenuSwitch open={openAI} onOpenChange={setOpenAI}> |
| 156 | + <Separator orientation="vertical" /> |
| 157 | + <NodeSelector open={openNode} onOpenChange={setOpenNode} /> |
| 158 | + <Separator orientation="vertical" /> |
| 159 | + <LinkSelector open={openLink} onOpenChange={setOpenLink} /> |
| 160 | + <Separator orientation="vertical" /> |
| 161 | + <MathSelector /> |
| 162 | + <Separator orientation="vertical" /> |
| 163 | + <TextButtons /> |
| 164 | + <Separator orientation="vertical" /> |
| 165 | + <ColorSelector open={openColor} onOpenChange={setOpenColor} /> |
| 166 | + <Separator orientation="vertical" /> |
| 167 | + </GenerativeMenuSwitch> |
| 168 | + </EditorContent> |
| 169 | + </EditorRoot> |
| 170 | + </div> |
| 171 | + ); |
| 172 | +}; |
0 commit comments