Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Looknig at mentions - contrived example working

Matt Roberts4ccac5304a25
diff --git a/package-lock.json b/package-lock.json
index 1478bd892..3378d7e7a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -10,9 +10,11 @@
"dependencies": {
"@lingui/format-json": "^5.1.2",
"@lingui/react": "^5.1.2",
+ "@tiptap/extension-mention": "^2.11.5",
"@tiptap/pm": "^2.11.5",
"@tiptap/react": "^2.11.5",
"@tiptap/starter-kit": "^2.11.5",
+ "@tiptap/suggestion": "^2.11.5",
"dompurify": "^2.3.6",
"marked": "^4.0.15",
"react": "^18.3.1",
@@ -22,6 +24,7 @@
"slate": "^0.110.2",
"slate-history": "^0.110.3",
"slate-react": "^0.111.0",
+ "tippy.js": "^6.3.7",
"tiptap-markdown": "^0.8.10"
},
"devDependencies": {
@@ -4683,6 +4686,21 @@
"@tiptap/core": "^2.7.0"
}
},
+ "node_modules/@tiptap/extension-mention": {
+ "version": "2.11.5",
+ "resolved": "https://registry.npmjs.org/@tiptap/extension-mention/-/extension-mention-2.11.5.tgz",
+ "integrity": "sha512-xj0/P4WSQWiDHzQLSIqdPUEu8LlC+ptSYA+y9IDChG51j1jVqcmolnS4sxpyrfr/t0ug0smNmJ4PDjQtXaG63A==",
+ "license": "MIT",
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/ueberdosis"
+ },
+ "peerDependencies": {
+ "@tiptap/core": "^2.7.0",
+ "@tiptap/pm": "^2.7.0",
+ "@tiptap/suggestion": "^2.7.0"
+ }
+ },
"node_modules/@tiptap/extension-ordered-list": {
"version": "2.11.5",
"resolved": "https://registry.npmjs.org/@tiptap/extension-ordered-list/-/extension-ordered-list-2.11.5.tgz",
@@ -4834,6 +4852,20 @@
"url": "https://github.com/sponsors/ueberdosis"
}
},
+ "node_modules/@tiptap/suggestion": {
+ "version": "2.11.5",
+ "resolved": "https://registry.npmjs.org/@tiptap/suggestion/-/suggestion-2.11.5.tgz",
+ "integrity": "sha512-uafwGgB5YuKX/xLRjnt2H5eA21I8HcNXpdbH4Du2gg3KM71RpUbkyjaV7KEMA/5qwCEo+sddlpuErj4wBycZ5Q==",
+ "license": "MIT",
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/ueberdosis"
+ },
+ "peerDependencies": {
+ "@tiptap/core": "^2.7.0",
+ "@tiptap/pm": "^2.7.0"
+ }
+ },
"node_modules/@tootallnate/once": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz",
diff --git a/package.json b/package.json
index 28395beda..d00aadc99 100644
--- a/package.json
+++ b/package.json
@@ -6,9 +6,11 @@
"dependencies": {
"@lingui/format-json": "^5.1.2",
"@lingui/react": "^5.1.2",
+ "@tiptap/extension-mention": "^2.11.5",
"@tiptap/pm": "^2.11.5",
"@tiptap/react": "^2.11.5",
"@tiptap/starter-kit": "^2.11.5",
+ "@tiptap/suggestion": "^2.11.5",
"dompurify": "^2.3.6",
"marked": "^4.0.15",
"react": "^18.3.1",
@@ -18,6 +20,7 @@
"slate": "^0.110.2",
"slate-history": "^0.110.3",
"slate-react": "^0.111.0",
+ "tippy.js": "^6.3.7",
"tiptap-markdown": "^0.8.10"
},
"devDependencies": {
diff --git a/public/components/common/form/CommentEditor.scss b/public/components/common/form/CommentEditor.scss
index da7873d79..8ae4dc9d4 100644
--- a/public/components/common/form/CommentEditor.scss
+++ b/public/components/common/form/CommentEditor.scss
@@ -146,3 +146,22 @@
}
}
}
+
+// These are hacked in for mentions
+/* Basic editor styles */
+.tiptap {
+ :first-child {
+ margin-top: 0;
+ }
+
+ .mention {
+ background-color: get("colors.primary.light");
+ border-radius: get("border.radius.medium");
+ box-decoration-break: clone;
+ color: get("colors.primary.base");
+ padding: spacing(0.1) spacing(0.3);
+ &::after {
+ content: "\200B";
+ }
+ }
+}
diff --git a/public/components/common/form/CommentEditor2.tsx b/public/components/common/form/CommentEditor2.tsx
index af4dd9831..1d427294b 100644
--- a/public/components/common/form/CommentEditor2.tsx
+++ b/public/components/common/form/CommentEditor2.tsx
@@ -4,6 +4,7 @@ import StarterKit from "@tiptap/starter-kit"
import React from "react"
import { EditorContent, useEditor } from "@tiptap/react"
import { Markdown } from "tiptap-markdown"
+import Mention from "@tiptap/extension-mention"
import "./CommentEditor.scss"
@@ -15,8 +16,19 @@ import IconBold from "@fider/assets/images/heroicons-bold.svg"
import IconStrike from "@fider/assets/images/heroicons-strike.svg"
import { Icon } from "@fider/components"
+import suggestion from "./suggestion"
+
// define your extension array
-const extensions = [StarterKit, Markdown]
+const extensions = [
+ StarterKit,
+ Markdown,
+ Mention.configure({
+ HTMLAttributes: {
+ class: "mention",
+ },
+ suggestion,
+ }),
+]
const MenuBar = ({ editor }: { editor: Editor | null }) => {
if (!editor) {
diff --git a/public/components/common/form/MentionList.scss b/public/components/common/form/MentionList.scss
new file mode 100644
index 000000000..b904b2c29
--- /dev/null
+++ b/public/components/common/form/MentionList.scss
@@ -0,0 +1,39 @@
+@import "~@fider/assets/styles/variables.scss";
+
+/* Dropdown menu */
+.dropdown-menu {
+ background: get("colors.white");
+ border: 1px solid get("colors.gray.200");
+ border-radius: get("border.radius.large");
+ box-shadow: rgba(0, 0, 0, 0) 0px 0px 0px 0px, rgba(0, 0, 0, 0) 0px 0px 0px 0px, rgba(0, 0, 0, 0.1) 0px 1px 3px 0px, rgba(0, 0, 0, 0.06) 0px 1px 2px 0px;
+ display: flex;
+ flex-direction: column;
+ gap: 0.1rem;
+ overflow: auto;
+ padding: spacing(1);
+ position: relative;
+
+ button {
+ align-items: center;
+ background-color: transparent;
+ border: none;
+ display: flex;
+ gap: spacing(0.5);
+ text-align: left;
+ padding: 8px 16px;
+ font-weight: 500;
+ width: 100%;
+ color: get("colors.gray.900");
+ background-color: transparent;
+
+ &:hover,
+ &:hover.is-selected {
+ text-decoration: none;
+ background-color: get("colors.gray.300");
+ }
+
+ &.is-selected {
+ background-color: get("colors.gray.200");
+ }
+ }
+}
diff --git a/public/components/common/form/MentionList.tsx b/public/components/common/form/MentionList.tsx
new file mode 100644
index 000000000..87d514eb4
--- /dev/null
+++ b/public/components/common/form/MentionList.tsx
@@ -0,0 +1,74 @@
+import "./MentionList.scss"
+
+import React, { forwardRef, useEffect, useImperativeHandle, useState } from "react"
+
+interface Props {
+ items: any[]
+ command?: (item: any) => void
+}
+
+export type Ref = any
+
+const MentionList = forwardRef<Ref, Props>((props, ref) => {
+ const [selectedIndex, setSelectedIndex] = useState(0)
+
+ const selectItem = (index: number) => {
+ const item = props.items?.[index]
+
+ if (item) {
+ props.command?.({ id: item })
+ }
+ }
+ const upHandler = () => {
+ setSelectedIndex((selectedIndex + props.items.length - 1) % props.items.length)
+ }
+
+ const downHandler = () => {
+ setSelectedIndex((selectedIndex + 1) % props.items.length)
+ }
+
+ const enterHandler = () => {
+ selectItem(selectedIndex)
+ }
+
+ useEffect(() => setSelectedIndex(0), [props.items])
+
+ useImperativeHandle(ref, () => ({
+ onKeyDown: ({ event }: { event: KeyboardEvent }): boolean => {
+ if (event.key === "ArrowUp") {
+ upHandler()
+ return true
+ }
+
+ if (event.key === "ArrowDown") {
+ downHandler()
+ return true
+ }
+
+ if (event.key === "Enter") {
+ enterHandler()
+ return true
+ }
+
+ return false
+ },
+ }))
+
+ return (
+ <div className="dropdown-menu">
+ {props.items.length ? (
+ props.items.map((item, index) => (
+ <button className={`${index === selectedIndex ? "is-selected" : ""}`} key={index} onClick={() => selectItem(index)}>
+ {item}
+ </button>
+ ))
+ ) : (
+ <div className="item">No result</div>
+ )}
+ </div>
+ )
+})
+
+MentionList.displayName = "MentionList"
+
+export default MentionList
diff --git a/public/components/common/form/suggestion.ts b/public/components/common/form/suggestion.ts
new file mode 100644
index 000000000..7751ac14d
--- /dev/null
+++ b/public/components/common/form/suggestion.ts
@@ -0,0 +1,97 @@
+import { ReactRenderer } from "@tiptap/react"
+import tippy from "tippy.js"
+
+import MentionList from "./MentionList"
+
+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)
+ },
+ render: () => {
+ let component: ReactRenderer
+ let popup: any
+
+ return {
+ onStart: (props: { editor: any; clientRect?: (() => DOMRect | null) | null }) => {
+ component = new ReactRenderer(MentionList, {
+ props,
+ editor: props.editor,
+ })
+
+ if (!props.clientRect) {
+ return
+ }
+
+ popup = tippy(document.body, {
+ getReferenceClientRect: props.clientRect as () => DOMRect,
+ appendTo: () => document.body,
+ content: component.element,
+ showOnCreate: true,
+ interactive: true,
+ trigger: "manual",
+ placement: "bottom-start",
+ })
+ },
+ onUpdate(props: { clientRect?: (() => DOMRect | null) | null | undefined }) {
+ if (!props.clientRect) {
+ return
+ }
+
+ const rect = props.clientRect()
+ if (!rect) {
+ return
+ }
+
+ component.updateProps(props)
+
+ popup.setProps({
+ getReferenceClientRect: props.clientRect,
+ })
+ },
+
+ onKeyDown(props: { event: KeyboardEvent }) {
+ if (props.event.key === "Escape") {
+ popup.hide()
+
+ return true
+ }
+
+ // Todo: need to call onKeyDown on the MentionList, but not sure how to get it.
+ return false
+ },
+
+ onExit() {
+ popup.destroy()
+ component.destroy()
+ },
+ }
+ },
+}