Open platform to collect and prioritize feedback
Bit mnessy, but this is a working mentions implementation - it's using HTML rather than markdown though, so let's look at that next.
Matt Roberts0aba26e5e61a
diff --git a/package-lock.json b/package-lock.jsonindex 3378d7e7a..7f60c96f3 100644--- a/package-lock.json+++ b/package-lock.json@@ -17,6 +17,7 @@"@tiptap/suggestion": "^2.11.5","dompurify": "^2.3.6","marked": "^4.0.15",+ "prosemirror-markdown": "^1.13.1","react": "^18.3.1","react-dom": "^18.3.1","react-textarea-autosize": "^8.5.4",diff --git a/package.json b/package.jsonindex d00aadc99..8b139a33b 100644--- a/package.json+++ b/package.json@@ -13,6 +13,7 @@"@tiptap/suggestion": "^2.11.5","dompurify": "^2.3.6","marked": "^4.0.15",+ "prosemirror-markdown": "^1.13.1","react": "^18.3.1","react-dom": "^18.3.1","react-textarea-autosize": "^8.5.4",diff --git a/public/components/common/form/CommentEditor2.tsx b/public/components/common/form/CommentEditor2.tsxindex 1d427294b..5b7cdb527 100644--- a/public/components/common/form/CommentEditor2.tsx+++ b/public/components/common/form/CommentEditor2.tsx@@ -21,12 +21,17 @@ import suggestion from "./suggestion"// define your extension arrayconst extensions = [StarterKit,- Markdown,+ Markdown.configure({+ html: true,+ }),Mention.configure({HTMLAttributes: {class: "mention",},suggestion,+ renderText: (props) => {+ return "mushrooms"+ },}),]@@ -87,8 +92,6 @@ interface CommentEditorProps2 {initialValue: string | nullplaceholder?: stringonChange?: (value: string) => void- // onFocus?: React.FocusEventHandler<HTMLDivElement>- // className?: string}const Tiptap: React.FunctionComponent<CommentEditorProps2> = (props) => {diff --git a/public/components/common/form/MentionList.tsx b/public/components/common/form/MentionList.tsxindex 87d514eb4..24eecf0a4 100644--- a/public/components/common/form/MentionList.tsx+++ b/public/components/common/form/MentionList.tsx@@ -1,22 +1,25 @@+import { MentionNodeAttrs } from "@tiptap/extension-mention"import "./MentionList.scss"import React, { forwardRef, useEffect, useImperativeHandle, useState } from "react"interface Props {- items: any[]- command?: (item: any) => void+ items: MentionNodeAttrs[]+ command?: (item: MentionNodeAttrs) => void}-export type Ref = any+export interface MentionListHandle {+ onKeyDown: (args: { event: KeyboardEvent }) => boolean+}-const MentionList = forwardRef<Ref, Props>((props, ref) => {+const MentionList = forwardRef<MentionListHandle, Props>((props, ref) => {const [selectedIndex, setSelectedIndex] = useState(0)const selectItem = (index: number) => {const item = props.items?.[index]if (item) {- props.command?.({ id: item })+ props.command?.(item)}}const upHandler = () => {@@ -59,7 +62,7 @@ const MentionList = forwardRef<Ref, Props>((props, ref) => {{props.items.length ? (props.items.map((item, index) => (<button className={`${index === selectedIndex ? "is-selected" : ""}`} key={index} onClick={() => selectItem(index)}>- {item}+ {item.label}</button>))) : (diff --git a/public/components/common/form/suggestion.ts b/public/components/common/form/suggestion.tsindex 7751ac14d..264c7a143 100644--- a/public/components/common/form/suggestion.ts+++ b/public/components/common/form/suggestion.ts@@ -1,47 +1,41 @@import { ReactRenderer } from "@tiptap/react"import tippy from "tippy.js"+import { actions } from "@fider/services"-import MentionList from "./MentionList"+import MentionList, { MentionListHandle } from "./MentionList"+import { MentionNodeAttrs } from "@tiptap/extension-mention"+interface MentionListProps {+ items: any[]+ command?: (item: MentionNodeAttrs) => void+}+// interface ReactRendererOptions {+// props: { editor: any; clientRect?: (() => DOMRect | null) | null }+// editor: any+// }++// Cache for storing users+let cachedUsers: MentionNodeAttrs[] = []export default {- items: ({ query }: { query: string }) => {- return [- "Lea Thompson",- "Cyndi Lauper",- "Tom Cruise",- "Madonna",- "Jerry Hall",- "Joan Collins",- "Winona Ryder",- "Christina Applegate",- "Alyssa Milano",- "Molly Ringwald",- "Ally Sheedy",- "Debbie Harry",- "Olivia Newton-John",- "Elton John",- "Michael J. Fox",- "Axl Rose",- "Emilio Estevez",- "Ralph Macchio",- "Rob Lowe",- "Jennifer Grey",- "Mickey Rourke",- "John Cusack",- "Matthew Broderick",- "Justine Bateman",- "Lisa Bonet",- ]- .filter((item) => item.toLowerCase().startsWith(query.toLowerCase()))- .slice(0, 5)+ items: async ({ query }: { query: string }) => {+ // If we don't have cached users yet, fetch them+ if (cachedUsers.length === 0) {+ const result = await actions.getTaggableUsers("")+ if (result.ok) {+ cachedUsers = result.data.map((user) => ({ id: user.id.toString(), label: user.name }))+ }+ }++ // Filter the cached users based on the query+ return cachedUsers.filter((item) => item.label?.toLowerCase().startsWith(query.toLowerCase())).slice(0, 5)},render: () => {- let component: ReactRenderer+ let reactRenderer: ReactRenderer<MentionListHandle, MentionListProps>let popup: anyreturn {onStart: (props: { editor: any; clientRect?: (() => DOMRect | null) | null }) => {- component = new ReactRenderer(MentionList, {+ reactRenderer = new ReactRenderer(MentionList, {props,editor: props.editor,})@@ -53,7 +47,7 @@ export default {popup = tippy(document.body, {getReferenceClientRect: props.clientRect as () => DOMRect,appendTo: () => document.body,- content: component.element,+ content: reactRenderer.element,showOnCreate: true,interactive: true,trigger: "manual",@@ -70,7 +64,7 @@ export default {return}- component.updateProps(props)+ reactRenderer.updateProps(props)popup.setProps({getReferenceClientRect: props.clientRect,@@ -84,13 +78,12 @@ export default {return true}- // Todo: need to call onKeyDown on the MentionList, but not sure how to get it.- return false+ return reactRenderer.ref?.onKeyDown(props) || false},onExit() {popup.destroy()- component.destroy()+ reactRenderer.destroy()},}},diff --git a/public/pages/ShowPost/components/CommentInput.tsx b/public/pages/ShowPost/components/CommentInput.tsxindex 56002c812..897cfcc6f 100644--- a/public/pages/ShowPost/components/CommentInput.tsx+++ b/public/pages/ShowPost/components/CommentInput.tsx@@ -40,7 +40,10 @@ export const CommentInput = (props: CommentInputProps) => {const submit = async () => {clearError()- const result = await actions.createComment(props.post.number, "hello", attachments)+ // Since the comment is being cached, we can save the content that's in the cache+ const content = getContentFromCache()++ const result = await actions.createComment(props.post.number, content || "", attachments)if (result.ok) {cache.session.remove(getCacheKey())location.reload()