Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

More nice functionality in the editor

Matt Roberts25f0088ca195
diff --git a/public/assets/styles/utility/spacing.scss b/public/assets/styles/utility/spacing.scss
index 22b13341d..fa305dc11 100644
--- a/public/assets/styles/utility/spacing.scss
+++ b/public/assets/styles/utility/spacing.scss
@@ -3,6 +3,10 @@
margin-left: auto;
}
+.ml-auto {
+ margin-left: auto;
+}
+
@for $i from 0 through 9 {
// Margin
.m-#{$i} {
diff --git a/public/components/common/form/CommentEditor.tsx b/public/components/common/form/CommentEditor.tsx
index c176fa291..e2fef8435 100644
--- a/public/components/common/form/CommentEditor.tsx
+++ b/public/components/common/form/CommentEditor.tsx
@@ -1,10 +1,13 @@
// src/Tiptap.tsx
import { Editor } from "@tiptap/react"
import StarterKit from "@tiptap/starter-kit"
-import React from "react"
+import React, { useState } from "react"
import { EditorContent, useEditor } from "@tiptap/react"
import { Markdown } from "tiptap-markdown"
import Placeholder from "@tiptap/extension-placeholder"
+import Document from "@tiptap/extension-document"
+import Paragraph from "@tiptap/extension-paragraph"
+import Text from "@tiptap/extension-text"
import "./CommentEditor.scss"
@@ -17,14 +20,16 @@ import IconStrike from "@fider/assets/images/heroicons-strike.svg"
import IconCode from "@fider/assets/images/heroicons-code.svg"
import IconAt from "@fider/assets/images/heroicons-at.svg"
import IconOrderedList from "@fider/assets/images/heroicons-orderedlist.svg"
+// import IconMarkdown from "@fider/assets/images/heroicons-h2.svg"
import IconBulletList from "@fider/assets/images/heroicons-bulletlist.svg"
import { Icon } from "@fider/components"
import suggestion from "./suggestion"
import { CustomMention } from "./CustomMention"
+import { Trans } from "@lingui/react/macro"
// define your extension array
-const MenuBar = ({ editor }: { editor: Editor | null }) => {
+const MenuBar = ({ editor, isMarkdownMode, toggleMarkdownMode }: { editor: Editor | null; isMarkdownMode: boolean; toggleMarkdownMode: () => void }) => {
if (!editor) {
return null
}
@@ -32,86 +37,100 @@ const MenuBar = ({ editor }: { editor: Editor | null }) => {
return (
<div className="c-editor-toolbar">
<div className="c-editor-button-group">
+ {/* Only show formatting buttons when not in markdown mode */}
+ {!isMarkdownMode && (
+ <>
+ <button
+ type="button"
+ title="Heading 2"
+ onClick={() => editor.chain().focus().toggleHeading({ level: 2 }).run()}
+ className={`c-editor-button ${editor.isActive("heading", { level: 2 }) ? "is-active" : ""}`}
+ >
+ <Icon sprite={IconH2} width="18" height="18" />
+ </button>
+ <button
+ type="button"
+ title="Heading 3"
+ onClick={() => editor.chain().focus().toggleHeading({ level: 3 }).run()}
+ className={`c-editor-button ${editor.isActive("heading", { level: 3 }) ? "is-active" : ""}`}
+ >
+ <Icon sprite={IconH3} />
+ </button>
+ <button
+ type="button"
+ title="Bold"
+ onClick={() => editor.chain().focus().toggleBold().run()}
+ className={`c-editor-button ${editor.isActive("bold") ? "is-active" : ""}`}
+ >
+ <Icon sprite={IconBold} />
+ </button>
+ <button
+ type="button"
+ title="Italic"
+ onClick={() => editor.chain().focus().toggleItalic().run()}
+ className={`c-editor-button ${editor.isActive("italic") ? "is-active" : ""}`}
+ >
+ <Icon sprite={IconItalic} />
+ </button>
+ <button
+ type="button"
+ title="Strikethrough"
+ onClick={() => editor.chain().focus().toggleStrike().run()}
+ className={`c-editor-button ${editor.isActive("strike") ? "is-active" : ""}`}
+ >
+ <Icon sprite={IconStrike} />
+ </button>
+ <button
+ type="button"
+ title="BulletList"
+ onClick={() => editor.chain().focus().toggleBulletList().run()}
+ className={`c-editor-button ${editor.isActive("bulletList") ? "is-active" : ""}`}
+ >
+ <Icon sprite={IconBulletList} />
+ </button>
+ <button
+ type="button"
+ title="OrderedList"
+ onClick={() => editor.chain().focus().toggleOrderedList().run()}
+ className={`c-editor-button ${editor.isActive("orderedList") ? "is-active" : ""}`}
+ >
+ <Icon sprite={IconOrderedList} />
+ </button>
+ <button
+ type="button"
+ title="Code"
+ onClick={() => editor.chain().focus().toggleCodeBlock().run()}
+ className={`c-editor-button ${editor.isActive("codeBlock") ? "is-active" : ""}`}
+ >
+ <Icon sprite={IconCode} />
+ </button>
+ <button
+ type="button"
+ title="Mention"
+ onClick={() => {
+ // Insert @ character at current position and trigger mention suggestion
+ editor.chain().focus().insertContent("@").run()
+ }}
+ className="c-editor-button"
+ >
+ <Icon sprite={IconAt} />
+ </button>
+ </>
+ )}
<button
type="button"
- title="Heading 2"
- onClick={() => editor.chain().focus().toggleHeading({ level: 2 }).run()}
- className={`c-editor-button ${editor.isActive("heading", { level: 2 }) ? "is-active" : ""}`}
+ title={isMarkdownMode ? "Rich Text Mode" : "Markdown Mode"}
+ onClick={toggleMarkdownMode}
+ className={`c-editor-button ${isMarkdownMode ? "is-active" : ""} ml-auto text-xs`}
>
- <Icon sprite={IconH2} width="18" height="18" />
- </button>
- <button
- type="button"
- title="Heading 3"
- onClick={() => editor.chain().focus().toggleHeading({ level: 3 }).run()}
- className={`c-editor-button ${editor.isActive("heading", { level: 3 }) ? "is-active" : ""}`}
- >
- <Icon sprite={IconH3} />
- </button>
- <button
- type="button"
- title="Bold"
- onClick={() => editor.chain().focus().toggleBold().run()}
- className={`c-editor-button ${editor.isActive("bold") ? "is-active" : ""}`}
- >
- <Icon sprite={IconBold} />
- </button>
- <button
- type="button"
- title="Italic"
- onClick={() => editor.chain().focus().toggleItalic().run()}
- className={`c-editor-button ${editor.isActive("italic") ? "is-active" : ""}`}
- >
- <Icon sprite={IconItalic} />
- </button>
- <button
- type="button"
- title="Strikethrough"
- onClick={() => editor.chain().focus().toggleStrike().run()}
- className={`c-editor-button ${editor.isActive("strike") ? "is-active" : ""}`}
- >
- <Icon sprite={IconStrike} />
- </button>
- <button
- type="button"
- title="BulletList"
- onClick={() => editor.chain().focus().toggleBulletList().run()}
- className={`c-editor-button ${editor.isActive("bulletList") ? "is-active" : ""}`}
- >
- <Icon sprite={IconBulletList} />
- </button>
- <button
- type="button"
- title="OrderedList"
- onClick={() => editor.chain().focus().toggleOrderedList().run()}
- className={`c-editor-button ${editor.isActive("orderedList") ? "is-active" : ""}`}
- >
- <Icon sprite={IconOrderedList} />
- </button>
- <button
- type="button"
- title="Code"
- onClick={() => editor.chain().focus().toggleCodeBlock().run()}
- className={`c-editor-button ${editor.isActive("codeBlock") ? "is-active" : ""}`}
- >
- <Icon sprite={IconCode} />
- </button>
- <button
- type="button"
- title="Mention"
- onClick={() => {
- // Insert @ character at current position and trigger mention suggestion
- editor.chain().focus().insertContent("@").run()
- }}
- className="c-editor-button"
- >
- <Icon sprite={IconAt} />
+ <span className="c-editor-button-text">
+ <Trans id="editor.markdownmode">Markdown editor</Trans>
+ </span>
</button>
</div>
</div>
)
}
-
interface CommentEditorProps {
initialValue: string | null
placeholder?: string
@@ -119,49 +138,97 @@ interface CommentEditorProps {
onFocus?: () => void
}
+const markdownToHtml = (markdownString: string) => {
+ return markdownString
+ .split("\n\n")
+ .map((line: string) => `<p>${line}</p>`)
+ .join("")
+}
+
const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {
+ const [isRawMarkdownMode, setIsRawMarkdownMode] = useState(false)
+
+ const getIntialContent = () => {
+ if (isRawMarkdownMode) {
+ return markdownToHtml(props.initialValue ?? "")
+ } else {
+ return props.initialValue ?? ""
+ }
+ }
+
+ const [editorContent, setEditorContent] = useState(getIntialContent())
+
+ const toggleMarkdownMode = () => {
+ if (editor) {
+ // Store current content before switching
+ let currentContent
+ if (isRawMarkdownMode) {
+ currentContent = editor.getText()
+ } else {
+ currentContent = markdownToHtml(editor.storage.markdown.getMarkdown())
+ }
+ // Destroy current editor
+ editor.destroy()
+ setIsRawMarkdownMode(!isRawMarkdownMode)
+ setEditorContent(currentContent)
+ }
+ }
+
const updated = ({ editor }: { editor: Editor; transaction: any }): void => {
- const markdown = editor.storage.markdown.getMarkdown()
+ const markdown = isRawMarkdownMode ? editor.getText() : editor.storage.markdown.getMarkdown()
props.onChange && props.onChange(markdown)
}
- const initialValue = props.initialValue ?? ""
-
- const extensions = [
- StarterKit,
- Markdown.configure({
- html: true,
- }),
- CustomMention.configure({
- HTMLAttributes: {
- class: "mention",
+
+ const extensions = isRawMarkdownMode
+ ? [
+ // Minimal extensions for markdown mode
+ Document,
+ Paragraph,
+ Text,
+ Placeholder.configure({
+ placeholder: props.placeholder ?? "Write your comment here...",
+ emptyEditorClass: "tiptap-is-empty",
+ }),
+ ]
+ : [
+ StarterKit,
+ Markdown.configure({
+ html: true,
+ }),
+ CustomMention.configure({
+ HTMLAttributes: {
+ class: "mention",
+ },
+ suggestion,
+ }),
+ Placeholder.configure({
+ placeholder: props.placeholder ?? "Write your comment here...",
+ emptyEditorClass: "tiptap-is-empty",
+ }),
+ ]
+
+ const editor = useEditor(
+ {
+ extensions,
+ content: editorContent,
+ onUpdate: updated,
+ onFocus: () => {
+ if (props.onFocus) {
+ props.onFocus()
+ }
},
- suggestion,
- }),
- Placeholder.configure({
- placeholder: props.placeholder ?? "Write your comment here...",
- emptyEditorClass: "tiptap-is-empty",
- }),
- ]
-
- const editor = useEditor({
- extensions,
- content: initialValue,
- onUpdate: updated,
- onFocus: () => {
- if (props.onFocus) {
- props.onFocus()
- }
- },
- editorProps: {
- attributes: {
- class: "no-focus",
+ editorProps: {
+ attributes: {
+ class: isRawMarkdownMode ? "markdown-mode no-focus" : "no-focus",
+ },
},
},
- })
+ [isRawMarkdownMode, editorContent]
+ ) // Re-initialize when mode changes
return (
<div className="fider-tiptap-editor">
- <MenuBar editor={editor} />
+ <MenuBar editor={editor} isMarkdownMode={isRawMarkdownMode} toggleMarkdownMode={toggleMarkdownMode} />
<EditorContent editor={editor} />
</div>
)