feat: support comment cm

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2023-05-18 11:27:50 +08:00
parent 55df59877e
commit 38ecf47f8f
7 changed files with 207 additions and 109 deletions
+68 -17
View File
@@ -1,16 +1,20 @@
import { CharacterEntity, NoteEntity } from "crossbell.js"
import { useEffect } from "react"
import { useCallback, useEffect, useRef } from "react"
import { useForm } from "react-hook-form"
import { EditorView } from "@codemirror/view"
import { useAccountState } from "@crossbell/connect-kit"
import { Popover } from "@headlessui/react"
import { Avatar } from "~/components/ui/Avatar"
import { Button } from "~/components/ui/Button"
import { Input } from "~/components/ui/Input"
import { editorUpload } from "~/editor/Multimedia"
import { useUploadFile } from "~/hooks/useUploadFile"
import { useTranslation } from "~/lib/i18n/client"
import { useCommentPage, useUpdateComment } from "~/queries/page"
import { CodeMirrorEditor } from "../ui/CodeMirror"
import { Input } from "../ui/Input"
import { EmojiPicker } from "./EmojiPicker"
export const CommentInput: React.FC<{
@@ -76,6 +80,38 @@ export const CommentInput: React.FC<{
}
}, [commentPage.isSuccess, updateComment.isSuccess, form, onSubmitted])
const cmViewRef = useRef<EditorView>()
const onCreateEditor = useCallback((view: EditorView) => {
cmViewRef.current = view
}, [])
const uploadFile = useUploadFile()
const handleDropFile = useCallback(
async (file: File) => {
const view = cmViewRef.current
if (view) {
editorUpload(file, view)
}
},
[uploadFile],
)
const InputHolder = useCallback(
() => (
<Input
id="content"
isBlock
required={!!account?.character}
disabled={!account?.character}
multiline
maxLength={600}
className="mb-2"
placeholder={t("Write a comment on the blockchain") || ""}
{...form.register("content", {})}
/>
),
[account?.character, t, form],
)
return (
<div className="xlog-comment-input flex">
<Avatar
@@ -86,16 +122,17 @@ export const CommentInput: React.FC<{
/>
<form className="w-full" onSubmit={handleSubmit}>
<div>
<Input
id="content"
isBlock
required={!!account?.character}
disabled={!account?.character}
multiline
maxLength={600}
className="mb-2"
placeholder={t("Write a comment on the blockchain") || ""}
<CodeMirrorEditor
{...form.register("content", {})}
onChange={(val) => {
form.setValue("content", val)
}}
handleDropFile={handleDropFile}
className="mb-2 p-3 h-[74px] border focus-within:border-accent border-[var(--border-color)] rounded-lg outline-2 outline-transparent"
placeholder={t("Write a comment on the blockchain") || ""}
maxLength={600}
onCreateEditor={onCreateEditor}
LoadingComponent={InputHolder}
/>
</div>
<div className="flex justify-between">
@@ -107,12 +144,26 @@ export const CommentInput: React.FC<{
</Popover.Button>
<Popover.Panel className="absolute left-0 top-full z-10">
<EmojiPicker
onEmojiSelect={(e: any) =>
form.setValue(
"content",
form.getValues("content") + e.native,
)
}
onEmojiSelect={(e: any) => {
const emojiValue = e.native
const view = cmViewRef.current
if (!view) return
const state = view.state
const range = state.selection.ranges[0]
view.dispatch({
changes: {
from: range.from,
to: range.to,
insert: `${emojiValue}`,
},
selection: { anchor: range.from },
})
requestAnimationFrame(() => {
// console.log(view.state.doc.toString(), "statevalue")
form.setValue("content", view.state.doc.toString())
})
}}
/>
</Popover.Panel>
</>
+33 -16
View File
@@ -1,5 +1,8 @@
import {
CSSProperties,
FC,
Suspense,
createElement,
forwardRef,
useEffect,
useImperativeHandle,
@@ -22,7 +25,6 @@ import {
import { useIsDark } from "~/hooks/useDarkMode"
import { useGetState } from "~/hooks/useGetState"
import { useIsUnmounted } from "~/hooks/useLifecycle"
import { useIsMobileLayout } from "~/hooks/useMobileLayout"
const LoadingHolder = () => {
const { t } = useTranslation("common")
@@ -34,13 +36,18 @@ const LoadingHolder = () => {
}
interface XLogCodeMirrorEditorProps {
value: string
value?: string
maxLength?: number
placeholder?: string
className?: string
cmStyle?: Record<string, CSSProperties>
onChange?: (value: string, viewUpdate: ViewUpdate) => void
handleDropFile?: (file: File) => void
onScroll?: (scrollTop: number) => void
onUpdate?: (update: ViewUpdate) => void
onCreateEditor?: (view: EditorView, state: EditorState) => void
onMouseEnter?: () => void
LoadingComponent?: FC
}
export const CodeMirrorEditor = forwardRef<
@@ -48,7 +55,15 @@ export const CodeMirrorEditor = forwardRef<
XLogCodeMirrorEditorProps
>((props, ref) => {
return (
<Suspense fallback={<LoadingHolder />}>
<Suspense
fallback={
props.LoadingComponent ? (
createElement(props.LoadingComponent)
) : (
<LoadingHolder />
)
}
>
<LazyCodeMirrorEditor {...props} ref={ref} />
</Suspense>
)
@@ -64,13 +79,11 @@ const LazyCodeMirrorEditor = forwardRef<
const [loading, setLoading] = useState(true)
const editorElementRef = useRef<HTMLDivElement>(null)
const isUnmounted = useIsUnmounted()
const { t } = useTranslation("dashboard")
const isMobileLayout = useIsMobileLayout()
const [cmEditor, setCmEditor] = useState<EditorView | null>(null)
const isDark = useIsDark()
useCodeMirrorStyle(cmEditor)
useCodeMirrorStyle(cmEditor, props.cmStyle)
useCodeMirrorAutoToggleTheme(cmEditor, isDark)
useImperativeHandle(ref, () => cmEditor!)
@@ -79,7 +92,6 @@ const LazyCodeMirrorEditor = forwardRef<
const getHandleDropFile = useGetState(handleDropFile)
const getOnScroll = useGetState(onScroll)
const getValue = useGetState(value)
const getProps = useGetState(props)
useEffect(() => {
@@ -134,13 +146,13 @@ const LazyCodeMirrorEditor = forwardRef<
{ languages },
] = modules
const props = getProps()
const editorState = EditorState.create({
doc: getValue(),
doc: props.value || "",
extensions: [
placeholder(t("Start writing...") || ""),
placeholder(props.placeholder || ""),
EditorView.updateListener.of((vu) => {
const props = getProps()
const { onUpdate, onChange } = props
onUpdate?.(vu)
@@ -153,6 +165,10 @@ const LazyCodeMirrorEditor = forwardRef<
) {
const doc = vu.state.doc
const value = doc.toString()
if (props.maxLength && value.length > props.maxLength) {
return
}
onChange(value, vu)
}
}),
@@ -235,13 +251,14 @@ const LazyCodeMirrorEditor = forwardRef<
<div
ref={editorElementRef}
onMouseEnter={onMouseEnter}
className={
loading
? ""
: `h-full ${isMobileLayout ? "w-full" : "border-r w-1/2 px-5"}`
}
className={loading ? "" : props.className}
/>
{loading && <LoadingHolder />}
{loading &&
(props.LoadingComponent ? (
createElement(props.LoadingComponent)
) : (
<LoadingHolder />
))}
</>
)
})