Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

25.5 KB
Raw Blame
import { Editor } from "@tiptap/react"
import Link from "@tiptap/extension-link"
import React, { useState, useRef, useEffect } from "react"
import { EditorContent, useEditor, useEditorState } from "@tiptap/react"
import { Placeholder } from "@tiptap/extensions"
import { i18n } from "@lingui/core"
import { useAllowedProtocols } from "@fider/hooks"

import "./CommentEditor.scss"

// At the top of the file, add imports for your icons
import IconH2 from "@fider/assets/images/heroicons-h2.svg"
import IconH3 from "@fider/assets/images/heroicons-h3.svg"
import IconItalic from "@fider/assets/images/heroicons-italic.svg"
import IconBold from "@fider/assets/images/heroicons-bold.svg"
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 IconBulletList from "@fider/assets/images/heroicons-bulletlist.svg"
import IconPhotograph from "@fider/assets/images/heroicons-photograph.svg"
import IconLink from "@fider/assets/images/heroicons-link.svg"
import IconBlockquote from "@fider/assets/images/heroicons-blockquote.svg"
import { DisplayError, hasError, Icon, ValidationContext } from "@fider/components"
import { fileToBase64 } from "@fider/services"
import { generateBkey } from "@fider/services/bkey"
import { ImageUpload } from "@fider/models"
import { CustomImage } from "./CustomImage"
import { fiderStarterKit } from "./FiderStarterKit"
import { fiderMarkdown } from "./FiderMarkdown"
import { RawMarkdown } from "./RawMarkdown"

import suggestion from "./suggestion"
import { CustomMention } from "./CustomMention"
import LinkInsertModal from "./LinkInsertModal"
import { Trans } from "@lingui/react/macro"
import { classSet } from "@fider/services"

const MenuBar = ({
  editor,
  isMarkdownMode,
  toggleMarkdownMode,
  disabled,
  onImageUpload,
  onLinkClick,
}: {
  editor: Editor | null
  isMarkdownMode: boolean
  disabled: boolean
  toggleMarkdownMode: () => void
  onImageUpload: (file: File) => Promise<void>
  onLinkClick: (selectedText: string) => void
}) => {
  const fileInputRef = useRef<HTMLInputElement>(null)

  // tiptap v3 doesn't re-render on every transaction, so subscribe to the button states. Toggling a
  // mark with just a cursor (or moving the cursor) doesn't change the document, so nothing else
  // would re-render the toolbar until the next keystroke.
  const active = useEditorState({
    editor,
    selector: ({ editor: e }) => ({
      h2: !!e?.isActive("heading", { level: 2 }),
      h3: !!e?.isActive("heading", { level: 3 }),
      bold: !!e?.isActive("bold"),
      italic: !!e?.isActive("italic"),
      strike: !!e?.isActive("strike"),
      bulletList: !!e?.isActive("bulletList"),
      orderedList: !!e?.isActive("orderedList"),
      codeBlock: !!e?.isActive("codeBlock"),
      blockquote: !!e?.isActive("blockquote"),
    }),
  })

  if (!editor || !active) {
    return null
  }

  const handleImageClick = () => {
    if (fileInputRef.current) {
      fileInputRef.current.click()
    }
  }

  const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
    if (e.target.files && e.target.files[0]) {
      const file = e.target.files[0]
      await onImageUpload(file)

      // Reset the input value so the same file can be selected again
      if (fileInputRef.current) {
        fileInputRef.current.value = ""
      }
    }
  }

  return (
    <div className="c-editor-toolbar">
      <div className="c-editor-button-group">
        {/* Only show formatting buttons when not in markdown mode */}
        {!isMarkdownMode && (
          <>
            <button
              disabled={disabled}
              type="button"
              title="Heading 2"
              onClick={() => editor.chain().focus().toggleHeading({ level: 2 }).run()}
              className={`c-editor-button ${active.h2 ? "is-active" : ""} ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconH2} width="18" height="18" />
            </button>
            <button
              disabled={disabled}
              type="button"
              title="Heading 3"
              onClick={() => editor.chain().focus().toggleHeading({ level: 3 }).run()}
              className={`c-editor-button ${active.h3 ? "is-active" : ""} ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconH3} />
            </button>
            <button
              disabled={disabled}
              type="button"
              title="Bold"
              onClick={() => editor.chain().focus().toggleBold().run()}
              className={`c-editor-button ${active.bold ? "is-active" : ""} ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconBold} />
            </button>
            <button
              disabled={disabled}
              type="button"
              title="Italic"
              onClick={() => editor.chain().focus().toggleItalic().run()}
              className={`c-editor-button ${active.italic ? "is-active" : ""} ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconItalic} />
            </button>
            <button
              disabled={disabled}
              type="button"
              title="Strikethrough"
              onClick={() => editor.chain().focus().toggleStrike().run()}
              className={`c-editor-button ${active.strike ? "is-active" : ""} ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconStrike} />
            </button>
            <button
              disabled={disabled}
              type="button"
              title="BulletList"
              onClick={() => editor.chain().focus().toggleBulletList().run()}
              className={`c-editor-button ${active.bulletList ? "is-active" : ""} ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconBulletList} />
            </button>
            <button
              disabled={disabled}
              type="button"
              title="OrderedList"
              onClick={() => editor.chain().focus().toggleOrderedList().run()}
              className={`c-editor-button ${active.orderedList ? "is-active" : ""} ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconOrderedList} />
            </button>
            <button
              disabled={disabled}
              type="button"
              title="Code"
              onClick={() => editor.chain().focus().toggleCodeBlock().run()}
              className={`c-editor-button ${active.codeBlock ? "is-active" : ""} ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconCode} />
            </button>
            <button
              disabled={disabled}
              type="button"
              title="Quote"
              onClick={() => editor.chain().focus().toggleBlockquote().run()}
              className={`c-editor-button ${active.blockquote ? "is-active" : ""} ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconBlockquote} />
            </button>
            <button
              disabled={disabled}
              type="button"
              title="Mention"
              onClick={() => {
                // Get the current cursor position
                const { from } = editor.state.selection
                // Get the character before the cursor
                const textBefore = editor.state.doc.textBetween(Math.max(0, from - 1), from)
                // Insert space before @ only if the previous character isn't a space or the cursor is at the beginning
                if (from === 0 || textBefore === " " || textBefore === "\n") {
                  editor.chain().focus().insertContent("@").run()
                } else {
                  editor.chain().focus().insertContent(" @").run()
                }
              }}
              className={`c-editor-button ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconAt} />
            </button>
            <button
              disabled={disabled}
              type="button"
              title="Insert Image"
              onClick={handleImageClick}
              className={`c-editor-button ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconPhotograph} />
            </button>
            <button
              disabled={disabled}
              type="button"
              title="Insert Link"
              onClick={() => {
                // Get the text that was highlighted when the user clicked the link button
                const { from, to } = editor.state.selection
                const selectedText = editor.state.doc.textBetween(from, to)
                onLinkClick(selectedText)
              }}
              className={`c-editor-button ${disabled ? "is-disabled" : ""}`}
            >
              <Icon sprite={IconLink} />
            </button>
            <input ref={fileInputRef} type="file" accept="image/*" style={{ display: "none" }} onChange={handleFileChange} />
          </>
        )}
        <button
          disabled={disabled}
          type="button"
          title={isMarkdownMode ? "Rich Text Mode" : "Markdown Mode"}
          onClick={toggleMarkdownMode}
          className={`no-focus c-markdown-toggle ${isMarkdownMode ? "is-active" : ""} ${disabled ? "is-disabled" : ""} ml-auto text-xs`}
        >
          <span className="c-editor-button-text">
            {isMarkdownMode ? (
              <Trans id="editor.richtextmode">Switch to rich text editor</Trans>
            ) : (
              <Trans id="editor.markdownmode">Switch to markdown editor</Trans>
            )}
          </span>
        </button>
      </div>
    </div>
  )
}
interface CommentEditorProps {
  initialValue: string | null
  placeholder?: string
  onChange?: (value: string, plainText?: string) => void
  onFocus?: () => void
  disabled: boolean
  field: string
  onImageUploaded?: (upload: ImageUpload) => void
  onGetImageSrc?: (bkey: string) => string
  maxAttachments?: number
  maxImageSizeKB?: number
  maxLength?: number
}

const DEFAULT_MAX_LENGTH = 4000

const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {
  const maxLength = props.maxLength ?? DEFAULT_MAX_LENGTH
  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 [contentLength, setContentLength] = useState((props.initialValue ?? "").length)
  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())

  // data: URLs of images attached in this editor session that aren't saved yet, by bkey. The
  // markdown only holds ![](fider-image:<bkey>), so when it's loaded again (switching back from
  // markdown mode) these are needed to show the image; the server doesn't have it yet.
  const pendingImageSrcRef = useRef<Map<string, string>>(new Map())

  // Content the editor is created with. Captured once — the editor instance is created a
  // single time (useEditor deps below are empty) and lives for the component's lifetime, so
  // switching editor modes must update content via commands rather than by recreating it.
  const initialContentRef = useRef(props.initialValue ?? "")

  const toggleMarkdownMode = () => {
    if (isRawMarkdownMode) {
      // Switching FROM markdown TO rich text
      // Load the edited markdown back into the existing editor instance.
      // tiptap v3 emits an update on setContent by default; keep v2 semantics (no onChange).
      if (editor) {
        editor.commands.setContent(markdownText, { emitUpdate: false, contentType: "markdown" })
      }
    } else {
      // Switching FROM rich text TO markdown
      // Get the markdown from the editor and store it
      if (editor) {
        const markdown = editor.getMarkdown().trim()
        setMarkdownText(markdown)
      }
    }
    setIsRawMarkdownMode(!isRawMarkdownMode)
  }

  // Handle image deletion
  const handleImageRemove = async (bkey: string) => {
    // Create an ImageUpload object with remove flag set to true
    const removeUpload: ImageUpload = {
      bkey,
      remove: true,
    }

    // Call the parent component's onImageUploaded prop with the removeUpload object
    if (props.onImageUploaded) {
      props.onImageUploaded(removeUpload)
    }
  }

  // Track all images in the document using the ref
  const trackDocumentImages = (editor: Editor) => {
    if (!editor) return

    // Create a new map for the current state
    const newImages = new Map<string, boolean>()

    // Find all image nodes in the document
    editor.state.doc.descendants((node) => {
      if (node.type.name === "customImage" || node.type.name === "image") {
        const bkey = node.attrs.bkey
        if (bkey) {
          newImages.set(bkey, true)
        }
      }
      return true
    })

    // Store the previous images for comparison
    const prevImages = new Map(documentImagesRef.current)

    // Update the ref with the new images
    documentImagesRef.current = newImages

    // Check for removed images
    checkForRemovedImages(prevImages, newImages)
  }

  // Check for removed images by comparing previous and current document images
  const checkForRemovedImages = (prevImages: Map<string, boolean>, currentImages: Map<string, boolean>) => {
    // Find images that were in the previous state but not in the current state
    prevImages.forEach((_, bkey) => {
      if (!currentImages.has(bkey)) {
        // This image was removed
        handleImageRemove(bkey)
      }
    })
  }

  const updated = ({ editor }: { editor: Editor; transaction: any }): void => {
    // Get the current markdown content
    const markdown = isRawMarkdownMode ? editor.getText() : editor.getMarkdown().trim()

    setContentLength(markdown.length)

    // Pass markdown to parent (plain text will be generated in ShareFeedback if needed)
    props.onChange && props.onChange(markdown)

    // Track the current state of images in the document
    // This will also check for removed images
    trackDocumentImages(editor)

    // Also pass any image uploads to the parent component
    if (props.onImageUploaded && imageUploads.length > 0) {
      imageUploads.forEach((upload) => {
        props.onImageUploaded && props.onImageUploaded(upload)
      })
      setImageUploads([])
    }
  }

  const validateImageUpload = (file: File): string => {
    // Default max size is 5MB (5 * 1024 KB)
    const maxSizeKB = props.maxImageSizeKB || 5 * 1024

    // Check file size
    if (file.size > maxSizeKB * 1024) {
      return i18n._({
        id: "validation.custom.maximagesize",
        values: { kilobytes: maxSizeKB },
        message: "The image size must be smaller than {kilobytes}KB.",
      })
    }

    // Check max attachments if specified
    if (props.maxAttachments) {
      if (documentImagesRef.current.size >= props.maxAttachments) {
        return i18n._({
          id: "validation.custom.maxattachments",
          values: { number: props.maxAttachments },
          message: "A maximum of {number} attachments are allowed.",
        })
      }
    }

    return "" // No error
  }

  const handleImageUpload = async (file: File) => {
    // Validate the image upload
    const errorMessage = validateImageUpload(file)
    if (errorMessage) {
      alert(errorMessage)
      return
    }

    try {
      const base64 = await fileToBase64(file)

      // Generate a bkey for this image that matches the server-side format
      const bkey = generateBkey(file.name)

      // Create an ImageUpload object to be sent to the server
      const newUpload: ImageUpload = {
        bkey,
        upload: {
          fileName: file.name,
          content: base64,
          contentType: file.type,
        },
        remove: false,
      }

      // Insert the image into the editor with the server-provided bkey.
      // Guard against a missing/destroyed editor: inserting into a destroyed ProseMirror
      // view silently no-ops (the transaction never applies and onUpdate never fires),
      // which would leave the markdown reference and the uploaded blob out of sync. Never
      // register the blob unless the node actually made it into the document.
      if (!editor || editor.isDestroyed) {
        console.error("Cannot insert pasted image: editor is not available")
        return
      }

      // ProseMirror documents are immutable, so an applied transaction always yields a new
      // doc object. If the doc reference is unchanged after the insert, the transaction was
      // dropped (e.g. dispatched into a destroyed view) and the node did not land.
      const docBefore = editor.state.doc
      const dataUrl = `data:${file.type};base64,${base64}`
      pendingImageSrcRef.current.set(bkey, dataUrl)
      editor
        .chain()
        .focus()
        .setImage({
          src: dataUrl,
          alt: file.name,
          ...({ bkey } as any),
        })
        .run()

      // Confirm the node was actually inserted before treating the upload as committed.
      if (editor.state.doc === docBefore) {
        console.error("Cannot insert pasted image: editor did not apply the change")
        return
      }

      // Add the bkey to the upload object for the parent component
      newUpload.bkey = bkey

      // Pass the upload to the parent component. The onUpdate handler that fired from the
      // insert above already committed the markdown reference, so registering the blob here
      // keeps the reference and the attachment in sync.
      if (props.onImageUploaded) {
        props.onImageUploaded(newUpload)
      }

      // Update the document images ref
      documentImagesRef.current.set(bkey, true)
    } catch (error) {
      console.error("Error uploading image:", error)
    }
  }

  const handleInsertLink = (text: string, url: string) => {
    if (!editor) return

    editor
      .chain()
      .focus()
      .insertContent({
        type: "text",
        text: text,
        marks: [{ type: "link", attrs: { href: url, target: "_blank", rel: "noopener nofollow" } }],
      })
      .unsetMark("link")
      .run()
  }

  // Handle keyboard shortcuts
  const handleKeyDown = (event: KeyboardEvent) => {
    if (!editor) return

    const isMac = navigator.userAgent.toUpperCase().indexOf("MAC") >= 0
    const isCmdK = (isMac && event.metaKey && event.key === "k") || (!isMac && event.ctrlKey && event.key === "k")

    if (isCmdK) {
      event.preventDefault()
      // Get the text that was highlighted when the user pressed Cmd+K
      const { from, to } = editor.state.selection
      const selectedText = editor.state.doc.textBetween(from, to)
      setSelectedText(selectedText)
      setIsLinkModalOpen(true)
    }
  }

  // Only use extensions for rich text mode
  const extensions = [
    // StarterKit without its Link (configured below) or Underline (no markdown form)
    ...fiderStarterKit(),
    Link.configure({
      openOnClick: true,
      autolink: true,
      defaultProtocol: "https",
      protocols: allowedProtocols,
      HTMLAttributes: {
        class: "text-link",
        target: "_blank",
        rel: "noopener nofollow",
      },
    }),
    fiderMarkdown(),
    RawMarkdown,
    CustomMention.configure({
      HTMLAttributes: {
        class: "mention",
      },
      suggestion,
    }),
    CustomImage.configure({
      HTMLAttributes: {},
      allowBase64: true,
      onGetImageSrc: (bkey) => {
        // Attached in this session...
        const pendingSrc = pendingImageSrcRef.current.get(bkey)
        if (pendingSrc) {
          return pendingSrc
        }
        // ...or in a restored draft (the parent's cached attachments)
        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(
    {
      extensions,
      content: initialContentRef.current,
      contentType: "markdown",
      onUpdate: updated,
      onFocus: () => {
        if (props.onFocus) {
          props.onFocus()
        }
      },
      editorProps: {
        attributes: {
          class: "no-focus",
        },
        handlePaste: (view, event) => {
          // Check if the clipboard has files
          if (event.clipboardData && event.clipboardData.files && event.clipboardData.files.length > 0) {
            // Get the first file (assuming it's an image)
            const file = event.clipboardData.files[0]

            // Check if it's an image
            if (file.type.startsWith("image/")) {
              // Prevent the default paste behavior
              event.preventDefault()

              // Validate and upload the image
              const errorMessage = validateImageUpload(file)
              if (errorMessage) {
                alert(errorMessage)
                return true
              }

              // Upload the image
              handleImageUpload(file)
              return true
            }
          }

          // Check for pasted image data URLs
          if (event.clipboardData) {
            const items = event.clipboardData.items

            for (let i = 0; i < items.length; i++) {
              if (items[i].type.indexOf("image") !== -1) {
                // Get the image as a blob
                const blob = items[i].getAsFile()

                if (blob) {
                  // Prevent the default paste behavior
                  event.preventDefault()

                  // Validate and upload the image
                  const errorMessage = validateImageUpload(blob)
                  if (errorMessage) {
                    alert(errorMessage)
                    return true
                  }

                  // Upload the image
                  handleImageUpload(blob)
                  return true
                }
              }
            }
          }

          return false
        },
      },
    },
    // Empty deps: create the editor once and keep it for the component's lifetime. tiptap
    // re-syncs editorProps/handlers (handlePaste, onUpdate, …) onto the live instance on
    // every render via setOptions, which preserves the current document. A non-empty deps
    // array here caused tiptap to destroy & recreate the instance on mount churn while
    // leaving the paste handler bound to the destroyed one, silently dropping pasted images.
    []
  )

  // Initialize document images when editor is ready
  useEffect(() => {
    if (editor) {
      trackDocumentImages(editor)
    }
  }, [editor])

  // Add keyboard event listener for shortcuts
  useEffect(() => {
    if (editor && !isRawMarkdownMode) {
      const editorElement = editor.view.dom
      editorElement.addEventListener("keydown", handleKeyDown)

      return () => {
        editorElement.removeEventListener("keydown", handleKeyDown)
      }
    }
  }, [editor, isRawMarkdownMode])

  // Handle textarea changes in markdown mode
  const handleTextareaChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
    const newValue = e.target.value
    setMarkdownText(newValue)
    setContentLength(newValue.length)

    // Notify parent component
    if (props.onChange) {
      props.onChange(newValue)
    }
  }

  return (
    <ValidationContext.Consumer>
      {(ctx) => (
        <div>
          <div
            className={classSet({
              "fider-tiptap-editor": true,
              "m-error": hasError(props.field, ctx.error),
            })}
          >
            <MenuBar
              disabled={props.disabled}
              editor={editor}
              isMarkdownMode={isRawMarkdownMode}
              toggleMarkdownMode={toggleMarkdownMode}
              onImageUpload={handleImageUpload}
              onLinkClick={(text) => {
                setSelectedText(text)
                setIsLinkModalOpen(true)
              }}
            />
            {isRawMarkdownMode ? (
              <textarea
                className="c-markdown-textarea no-focus"
                value={markdownText}
                onChange={handleTextareaChange}
                onFocus={props.onFocus}
                disabled={props.disabled}
                maxLength={maxLength}
                placeholder={props.placeholder ?? "Write your comment here..."}
                data-testid="markdown-textarea"
              />
            ) : (
              <EditorContent editor={editor} data-testid="tiptap-editor" />
            )}
          </div>
          {contentLength >= maxLength * 0.8 && (
            <div
              className={classSet({
                "c-comment-editor-counter": true,
                "c-comment-editor-counter--over": contentLength > maxLength,
              })}
              data-testid="comment-editor-counter"
            >
              {contentLength} / {maxLength}
            </div>
          )}
          <DisplayError fields={[props.field]} error={ctx.error} />
          <LinkInsertModal isOpen={isLinkModalOpen} onClose={() => setIsLinkModalOpen(false)} onInsertLink={handleInsertLink} selectedText={selectedText} />
        </div>
      )}
    </ValidationContext.Consumer>
  )
}

const CommentEditor = React.memo(Tiptap, (prevProps, nextProps) => {
  return prevProps.placeholder === nextProps.placeholder
})

export default CommentEditor