-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate editor to prosekit (#4888)
- Loading branch information
Showing
61 changed files
with
2,358 additions
and
1,494 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
apps/web/src/components/Composer/Actions/DraftSettings/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...rc/components/Composer/Actions/OpenActionSettings/Config/RentableBillboard/CostConfig.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...rc/components/Composer/Actions/OpenActionSettings/Config/RentableBillboard/TimeConfig.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...c/components/Composer/Actions/OpenActionSettings/Config/RentableBillboard/TokenConfig.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
apps/web/src/components/Composer/Actions/OpenActionSettings/Config/Swap/TokenConfig.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { FC } from 'react'; | ||
|
||
import { defineEditorExtension } from '@helpers/prosekit/extension'; | ||
import { htmlFromMarkdown } from '@helpers/prosekit/markdown'; | ||
import getAvatar from '@hey/helpers/getAvatar'; | ||
import { Image } from '@hey/ui'; | ||
import dynamic from 'next/dynamic'; | ||
import 'prosekit/basic/style.css'; | ||
import { createEditor } from 'prosekit/core'; | ||
import { ProseKit } from 'prosekit/react'; | ||
import { useMemo, useRef } from 'react'; | ||
import useContentChange from 'src/hooks/prosekit/useContentChange'; | ||
import { usePaste } from 'src/hooks/prosekit/usePaste'; | ||
import { usePublicationStore } from 'src/store/non-persisted/publication/usePublicationStore'; | ||
import { useProfileStore } from 'src/store/persisted/useProfileStore'; | ||
|
||
import { useEditorHandle } from './EditorHandle'; | ||
|
||
// Lazy load EditorMenus to reduce bundle size | ||
const EditorMenus = dynamic(() => import('./EditorMenus'), { ssr: false }); | ||
|
||
const Editor: FC = () => { | ||
const { currentProfile } = useProfileStore(); | ||
const { publicationContent } = usePublicationStore(); | ||
const defaultMarkdownRef = useRef(publicationContent); | ||
|
||
const defaultHTML = useMemo(() => { | ||
const markdown = defaultMarkdownRef.current; | ||
return markdown ? htmlFromMarkdown(markdown) : undefined; | ||
}, []); | ||
|
||
const editor = useMemo(() => { | ||
const extension = defineEditorExtension(); | ||
return createEditor({ defaultHTML, extension }); | ||
}, [defaultHTML]); | ||
|
||
useContentChange(editor); | ||
usePaste(editor); | ||
useEditorHandle(editor); | ||
|
||
return ( | ||
<ProseKit editor={editor}> | ||
<div className="box-border flex h-full w-full justify-stretch overflow-y-auto overflow-x-hidden px-5 py-4"> | ||
<Image | ||
alt={currentProfile?.id} | ||
className="mr-3 size-11 rounded-full border bg-gray-200 dark:border-gray-700" | ||
src={getAvatar(currentProfile)} | ||
/> | ||
<div className="flex flex-1 flex-col overflow-x-hidden"> | ||
<EditorMenus /> | ||
<div | ||
className="relative mt-[8.5px] box-border h-full min-h-[80px] flex-1 overflow-auto leading-6 outline-0 sm:leading-[26px]" | ||
ref={editor.mount} | ||
/> | ||
</div> | ||
</div> | ||
</ProseKit> | ||
); | ||
}; | ||
|
||
export default Editor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import type { EditorExtension } from '@helpers/prosekit/extension'; | ||
import type { Editor } from 'prosekit/core'; | ||
import type { FC } from 'react'; | ||
|
||
import { setMarkdownContent } from '@helpers/prosekit/markdownContent'; | ||
import { createContext, useContext, useEffect, useState } from 'react'; | ||
|
||
interface EditorHandle { | ||
insertText: (text: string) => void; | ||
setMarkdown: (markdown: string) => void; | ||
} | ||
|
||
const HandleContext = createContext<EditorHandle | null>(null); | ||
const SetHandleContext = createContext<((handle: EditorHandle) => void) | null>( | ||
null | ||
); | ||
|
||
const Provider = ({ children }: { children: React.ReactNode }) => { | ||
const [handle, setHandle] = useState<EditorHandle | null>(null); | ||
|
||
return ( | ||
<HandleContext.Provider value={handle}> | ||
<SetHandleContext.Provider value={setHandle}> | ||
{children} | ||
</SetHandleContext.Provider> | ||
</HandleContext.Provider> | ||
); | ||
}; | ||
|
||
/** | ||
* A hook for accessing the text editor handle. | ||
*/ | ||
export const useEditorContext = (): EditorHandle | null => { | ||
return useContext(HandleContext); | ||
}; | ||
|
||
/** | ||
* A hook to register the text editor handle. | ||
*/ | ||
export const useEditorHandle = (editor: Editor<EditorExtension>) => { | ||
const setHandle = useContext(SetHandleContext); | ||
|
||
useEffect(() => { | ||
const handle: EditorHandle = { | ||
insertText: (text: string): void => { | ||
if (!editor.mounted) { | ||
return; | ||
} | ||
|
||
editor.commands.insertText({ text }); | ||
}, | ||
setMarkdown: (markdown: string): void => { | ||
setMarkdownContent(editor, markdown); | ||
} | ||
}; | ||
|
||
setHandle?.(handle); | ||
}, [setHandle, editor]); | ||
}; | ||
|
||
/** | ||
* A higher-order component for providing the text editor handle. | ||
*/ | ||
export const withEditorContext = <Props extends object>( | ||
Component: FC<Props> | ||
): FC<Props> => { | ||
const WithEditorContext: FC<Props> = (props: Props) => { | ||
return ( | ||
<Provider> | ||
<Component {...props} /> | ||
</Provider> | ||
); | ||
}; | ||
|
||
return WithEditorContext; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { FC } from 'react'; | ||
|
||
import EmojiPicker from './EmojiPicker'; | ||
import InlineMenu from './InlineMenu'; | ||
import MentionPicker from './MentionPicker'; | ||
|
||
const EditorMenus: FC = () => { | ||
return ( | ||
<> | ||
<InlineMenu /> | ||
<MentionPicker /> | ||
<EmojiPicker /> | ||
</> | ||
); | ||
}; | ||
|
||
export default EditorMenus; |
Oops, something went wrong.
8f3c6f5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
web – ./apps/web
heyxyz.vercel.app
hey.xyz
web-git-main-heyxyz.vercel.app
web-heyxyz.vercel.app