Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Much better "raw markdown mode" implementation.

Matt Roberts0c1006ccd487
diff --git a/public/components/common/form/CommentEditor.scss b/public/components/common/form/CommentEditor.scss
index ae33d3b62..8b786b716 100644
--- a/public/components/common/form/CommentEditor.scss
+++ b/public/components/common/form/CommentEditor.scss
@@ -79,6 +79,27 @@
border-color: var(--colors-red-600);
}
+ .c-markdown-textarea {
+ width: 100%;
+ min-height: 120px;
+ padding: 12px;
+ border: none;
+ outline: none;
+ resize: vertical;
+ font-family: monospace;
+ font-size: 14px;
+ line-height: 1.5;
+ background-color: var(--colors-white);
+
+ &:focus {
+ outline: none;
+ }
+
+ &::placeholder {
+ color: var(--colors-gray-700);
+ }
+ }
+
.tiptap {
:first-child {
margin-top: 0;
diff --git a/public/components/common/form/CommentEditor.tsx b/public/components/common/form/CommentEditor.tsx
index 3cba1be12..bd273673c 100644
--- a/public/components/common/form/CommentEditor.tsx
+++ b/public/components/common/form/CommentEditor.tsx
@@ -233,57 +233,34 @@ interface CommentEditorProps {
maxImageSizeKB?: number
}
-const markdownToHtml = (markdownString: string) => {
- const converted = markdownString
- .split("\n\n")
- .map((line: string) => `<p>${line}</p>`)
- .join("")
- .replace(/\\\n/g, "<br>")
- .replace(/\n/g, "<br>")
-
- console.log("markdownToHtml done: ", converted)
-
- return converted
-}
-
const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {
const [isRawMarkdownMode, setIsRawMarkdownMode] = useState(false)
const [imageUploads, setImageUploads] = useState<ImageUpload[]>([])
const [isLinkModalOpen, setIsLinkModalOpen] = useState(false)
const [selectedText, setSelectedText] = useState("")
+ const [markdownText, setMarkdownText] = useState(props.initialValue ?? "")
const allowedProtocols = useAllowedProtocols()
// Use a ref instead of state for tracking document images
// This avoids the async state update issue and prevents unnecessary re-renders
const documentImagesRef = useRef<Map<string, boolean>>(new Map())
- const getIntialContent = () => {
- if (isRawMarkdownMode) {
- return markdownToHtml(props.initialValue ?? "")
- } else {
- return props.initialValue ?? ""
- }
- }
-
- const [editorContent, setEditorContent] = useState("")
+ const [editorContent, setEditorContent] = useState(props.initialValue ?? "")
const toggleMarkdownMode = () => {
- if (editor) {
- // Store current content before switching
- let currentContent
- if (isRawMarkdownMode) {
- console.log("Getting text from markdown editor")
- currentContent = editor.getText()
- } else {
- console.log("Getting markdown from rich text editor")
- currentContent = editor.storage.markdown.getMarkdown()
+ if (isRawMarkdownMode) {
+ // Switching FROM markdown TO rich text
+ // Set the editor content to the markdown text
+ setEditorContent(markdownText)
+ } else {
+ // Switching FROM rich text TO markdown
+ // Get the markdown from the editor and store it
+ if (editor) {
+ const markdown = editor.storage.markdown.getMarkdown()
+ setMarkdownText(markdown)
}
- console.log("Current content before toggle:", JSON.stringify(currentContent))
- // Destroy current editor
- editor.destroy()
- setIsRawMarkdownMode(!isRawMarkdownMode)
- setEditorContent(currentContent)
}
+ setIsRawMarkdownMode(!isRawMarkdownMode)
}
// Handle image deletion
@@ -463,69 +440,58 @@ const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {
}
}
- const extensions = isRawMarkdownMode
- ? [
- // Minimal extensions for markdown mode
- Document,
- Paragraph,
- Text,
- HardBreak,
- Placeholder.configure({
- placeholder: props.placeholder ?? "Write your comment here...",
- emptyEditorClass: "tiptap-is-empty",
- }),
- ]
- : [
- StarterKit,
- Link.configure({
- openOnClick: true,
- autolink: true,
- defaultProtocol: "https",
- protocols: allowedProtocols,
- HTMLAttributes: {
- class: "text-link",
- target: "_blank",
- rel: "noopener nofollow",
- },
- }),
- Markdown.configure({
- html: false,
- breaks: true,
- }),
- CustomMention.configure({
- HTMLAttributes: {
- class: "mention",
- },
- suggestion,
- }),
- CustomImage.configure({
- HTMLAttributes: {},
- allowBase64: true,
- onImageUpload: (upload) => {
- if (props.onImageUploaded) {
- // Initialize other required properties
- props.onImageUploaded(upload)
- }
- },
- onImageRemove: (id) => {
- // This is called when an image is removed from the editor
- handleImageRemove(id)
- },
- onGetImageSrc: (bkey) => {
- if (props.onGetImageSrc) {
- const imageSrc = props.onGetImageSrc(bkey)
- if (imageSrc) {
- return "data:image/jpeg;base64," + imageSrc
- }
- }
- return ""
- },
- }),
- Placeholder.configure({
- placeholder: props.placeholder ?? "Write your comment here...",
- emptyEditorClass: "tiptap-is-empty",
- }),
- ]
+ // Only use extensions for rich text mode
+ const extensions = [
+ StarterKit,
+ Link.configure({
+ openOnClick: true,
+ autolink: true,
+ defaultProtocol: "https",
+ protocols: allowedProtocols,
+ HTMLAttributes: {
+ class: "text-link",
+ target: "_blank",
+ rel: "noopener nofollow",
+ },
+ }),
+ Markdown.configure({
+ html: false,
+ breaks: true,
+ }),
+ CustomMention.configure({
+ HTMLAttributes: {
+ class: "mention",
+ },
+ suggestion,
+ }),
+ CustomImage.configure({
+ HTMLAttributes: {},
+ allowBase64: true,
+ onImageUpload: (upload) => {
+ if (props.onImageUploaded) {
+ // Initialize other required properties
+ props.onImageUploaded(upload)
+ }
+ },
+ onImageRemove: (id) => {
+ // This is called when an image is removed from the editor
+ handleImageRemove(id)
+ },
+ onGetImageSrc: (bkey) => {
+ if (props.onGetImageSrc) {
+ const imageSrc = props.onGetImageSrc(bkey)
+ if (imageSrc) {
+ return "data:image/jpeg;base64," + imageSrc
+ }
+ }
+ return ""
+ },
+ }),
+ Placeholder.configure({
+ placeholder: props.placeholder ?? "Write your comment here...",
+ emptyEditorClass: "tiptap-is-empty",
+ }),
+ ]
const editor = useEditor(
{
@@ -539,7 +505,7 @@ const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {
},
editorProps: {
attributes: {
- class: isRawMarkdownMode ? "markdown-mode no-focus" : "no-focus",
+ class: "no-focus",
},
handlePaste: (view, event) => {
// Check if the clipboard has files
@@ -597,8 +563,8 @@ const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {
},
},
},
- [isRawMarkdownMode, editorContent]
- ) // Re-initialize when mode changes
+ [editorContent]
+ )
// Initialize document images when editor is ready
useEffect(() => {
@@ -609,7 +575,7 @@ const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {
// Add keyboard event listener for shortcuts
useEffect(() => {
- if (editor) {
+ if (editor && !isRawMarkdownMode) {
const editorElement = editor.view.dom
editorElement.addEventListener("keydown", handleKeyDown)
@@ -617,7 +583,18 @@ const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {
editorElement.removeEventListener("keydown", handleKeyDown)
}
}
- }, [editor])
+ }, [editor, isRawMarkdownMode])
+
+ // Handle textarea changes in markdown mode
+ const handleTextareaChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
+ const newValue = e.target.value
+ setMarkdownText(newValue)
+
+ // Notify parent component
+ if (props.onChange) {
+ props.onChange(newValue)
+ }
+ }
return (
<ValidationContext.Consumer>
@@ -640,7 +617,19 @@ const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {
setIsLinkModalOpen(true)
}}
/>
- <EditorContent editor={editor} data-testid="tiptap-editor" />
+ {isRawMarkdownMode ? (
+ <textarea
+ className="c-markdown-textarea no-focus"
+ value={markdownText}
+ onChange={handleTextareaChange}
+ onFocus={props.onFocus}
+ disabled={props.disabled}
+ placeholder={props.placeholder ?? "Write your comment here..."}
+ data-testid="markdown-textarea"
+ />
+ ) : (
+ <EditorContent editor={editor} data-testid="tiptap-editor" />
+ )}
</div>
<DisplayError fields={[props.field]} error={ctx.error} />
<LinkInsertModal isOpen={isLinkModalOpen} onClose={() => setIsLinkModalOpen(false)} onInsertLink={handleInsertLink} selectedText={selectedText} />