Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Working tiptap editor component

Matt Robertsac36ede2d337
diff --git a/public/components/common/form/CommentEditor2.tsx b/public/components/common/form/CommentEditor2.tsx
index 5b7cdb527..9c8c6ac63 100644
--- a/public/components/common/form/CommentEditor2.tsx
+++ b/public/components/common/form/CommentEditor2.tsx
@@ -4,7 +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 Mention from "@tiptap/extension-mention"
import "./CommentEditor.scss"
@@ -17,6 +17,8 @@ import IconStrike from "@fider/assets/images/heroicons-strike.svg"
import { Icon } from "@fider/components"
import suggestion from "./suggestion"
+import { CustomMention } from "./CustomMention"
+// import CustomMarkdown from "./MentionMarkdown"
// define your extension array
const extensions = [
@@ -24,15 +26,18 @@ const extensions = [
Markdown.configure({
html: true,
}),
- Mention.configure({
+ CustomMention.configure({
HTMLAttributes: {
class: "mention",
},
suggestion,
- renderText: (props) => {
- return "mushrooms"
- },
}),
+ // CustomMarkdown,
+ // Mention.configure({
+ // HTMLAttributes: {
+ // class: "mention",
+ // },
+ // suggestion,
]
const MenuBar = ({ editor }: { editor: Editor | null }) => {
diff --git a/public/components/common/form/CustomMention.ts b/public/components/common/form/CustomMention.ts
new file mode 100644
index 000000000..c7b011491
--- /dev/null
+++ b/public/components/common/form/CustomMention.ts
@@ -0,0 +1,57 @@
+import Mention from "@tiptap/extension-mention"
+import * as MarkdownIt from "markdown-it"
+
+export const CustomMention = Mention.extend({
+ name: "mention",
+ addStorage() {
+ return {
+ markdown: {
+ serialize: (state: any, node: { attrs: { id: string; label: string } }, parent: any) => {
+ state.write(`@{"id":${node.attrs.id},"name":"${node.attrs.label}"}`)
+ },
+ parse: {
+ setup(markdownit: MarkdownIt) {
+ markdownit.renderer.rules.mention_open = (tokens, idx) => {
+ const token = tokens[idx]
+ const id = token.attrGet("id")
+ const label = token.attrGet("label")
+ return `<span data-type="mention" data-id="${id}" data-label="${label}" class="mention">`
+ }
+
+ markdownit.renderer.rules.mention_close = () => {
+ return "</span>"
+ }
+
+ markdownit.inline.ruler.before("text", "mention", (state: MarkdownIt.StateInline, silent: boolean) => {
+ const match = state.src.slice(state.pos).match(/^@\{([^}]+)\}/)
+ if (!match) return false
+ if (!silent) {
+ try {
+ const data = JSON.parse(`{${match[1]}}`)
+ const token = state.push("mention_open", "span", 1)
+ token.attrs = [
+ ["id", data.id],
+ ["label", data.name],
+ ]
+
+ // Add the text content token
+ const contentToken = state.push("text", "", 0)
+ contentToken.content = `@${data.name}`
+
+ // Close the mention span
+ state.push("mention_close", "span", -1)
+ } catch (error) {
+ console.error("Failed to parse mention:", match[1])
+ return false
+ }
+ }
+
+ state.pos += match[0].length
+ return true
+ })
+ },
+ },
+ },
+ }
+ },
+})
diff --git a/public/services/markdown.ts b/public/services/markdown.ts
index 5b28ca726..802d1d507 100644
--- a/public/services/markdown.ts
+++ b/public/services/markdown.ts
@@ -54,8 +54,8 @@ plainTextRenderer.html = (html) => html
plainTextRenderer.del = (text) => text
const entities: { [key: string]: string } = {
- "<": "&lt;",
- ">": "&gt;",
+ // "<": "&lt;",
+ // ">": "&gt;",
}
const encodeHTML = (s: string) => s.replace(/[<>]/g, (tag) => entities[tag] || tag)