Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

feat(ui): Adds insert link button to CommentEditor - Creates LinkInsertModal component - Adds insert link button to MenuBar component - Increases max width of ShareFeedback component to avoid wrapping

widal001613f2292a4e9
diff --git a/public/assets/images/heroicons-link.svg b/public/assets/images/heroicons-link.svg
new file mode 100644
index 000000000..faf5d4505
--- /dev/null
+++ b/public/assets/images/heroicons-link.svg
@@ -0,0 +1,3 @@
+<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
+ <path stroke-linecap="round" stroke-linejoin="round" d="M13.19 8.688a4.5 4.5 0 0 1 1.242 7.244l-4.5 4.5a4.5 4.5 0 0 1-6.364-6.364l1.757-1.757m13.35-.622 1.757-1.757a4.5 4.5 0 0 0-6.364-6.364l-4.5 4.5a4.5 4.5 0 0 0 1.242 7.244" />
+</svg>
diff --git a/public/components/common/form/CommentEditor.tsx b/public/components/common/form/CommentEditor.tsx
index b3394ac37..1f9903719 100644
--- a/public/components/common/form/CommentEditor.tsx
+++ b/public/components/common/form/CommentEditor.tsx
@@ -25,6 +25,7 @@ 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 { DisplayError, hasError, Icon, ValidationContext } from "@fider/components"
import { fileToBase64 } from "@fider/services"
import { generateBkey } from "@fider/services/bkey"
@@ -33,6 +34,7 @@ import { CustomImage } from "./CustomImage"
import suggestion from "./suggestion"
import { CustomMention } from "./CustomMention"
+import LinkInsertModal from "./LinkInsertModal"
import { Trans } from "@lingui/react/macro"
import { classSet } from "@fider/services"
@@ -42,19 +44,21 @@ const MenuBar = ({
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)
+
if (!editor) {
return null
}
- const fileInputRef = useRef<HTMLInputElement>(null)
-
const handleImageClick = () => {
if (fileInputRef.current) {
fileInputRef.current.click()
@@ -180,6 +184,20 @@ const MenuBar = ({
>
<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} />
</>
)}
@@ -227,6 +245,8 @@ const markdownToHtml = (markdownString: string) => {
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("")
// Use a ref instead of state for tracking document images
// This avoids the async state update issue and prevents unnecessary re-renders
@@ -414,6 +434,15 @@ const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {
}
}
+ const handleInsertLink = (text: string, url: string) => {
+ if (!editor) return
+
+ // Ensure URL has protocol
+ const urlWithProtocol = url.startsWith("http://") || url.startsWith("https://") ? url : `https://${url}`
+
+ editor.chain().focus().insertContent(`<a href="${urlWithProtocol}" target="_blank" rel="noopener nofollow">${text}</a>`).run()
+ }
+
const extensions = isRawMarkdownMode
? [
// Minimal extensions for markdown mode
@@ -573,10 +602,15 @@ const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {
isMarkdownMode={isRawMarkdownMode}
toggleMarkdownMode={toggleMarkdownMode}
onImageUpload={handleImageUpload}
+ onLinkClick={(text) => {
+ setSelectedText(text)
+ setIsLinkModalOpen(true)
+ }}
/>
<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} />
</div>
)}
</ValidationContext.Consumer>
diff --git a/public/components/common/form/LinkInsertModal.tsx b/public/components/common/form/LinkInsertModal.tsx
new file mode 100644
index 000000000..0a0ff6982
--- /dev/null
+++ b/public/components/common/form/LinkInsertModal.tsx
@@ -0,0 +1,145 @@
+import React, { useState, useEffect, useRef } from "react"
+
+interface LinkInsertModalProps {
+ isOpen: boolean
+ onClose: () => void
+ onInsertLink: (text: string, url: string) => void
+ selectedText?: string
+}
+
+const LinkInsertModal = ({ isOpen, onClose, onInsertLink, selectedText = "" }: LinkInsertModalProps) => {
+ const [text, setText] = useState("")
+ const [url, setUrl] = useState("")
+ const [errors, setErrors] = useState<{ text?: string; url?: string }>({})
+ const textInputRef = useRef<HTMLInputElement>(null)
+
+ const handleSubmit = (e: React.FormEvent) => {
+ e.preventDefault()
+
+ // Clear previous errors
+ setErrors({})
+
+ // Custom validation
+ const newErrors: { text?: string; url?: string } = {}
+
+ if (!text.trim()) {
+ newErrors.text = "Text is required"
+ }
+
+ if (!url.trim()) {
+ newErrors.url = "URL is required"
+ } else {
+ // Basic URL validation
+ try {
+ new URL(url.startsWith("http://") || url.startsWith("https://") ? url : `https://${url}`)
+ } catch {
+ newErrors.url = "Please enter a valid URL"
+ }
+ }
+
+ if (Object.keys(newErrors).length > 0) {
+ setErrors(newErrors)
+ return
+ }
+
+ // Form is valid, handle submission
+ const urlWithProtocol = url.startsWith("http://") || url.startsWith("https://") ? url : `https://${url}`
+ onInsertLink(text.trim(), urlWithProtocol)
+ setText("")
+ setUrl("")
+ onClose()
+ }
+
+ const handleClose = () => {
+ setText("")
+ setUrl("")
+ setErrors({})
+ onClose()
+ }
+
+ const handleKeyDown = (e: React.KeyboardEvent) => {
+ if (e.key === "Escape") {
+ handleClose()
+ }
+ }
+
+ // Focus management and prefill text
+ useEffect(() => {
+ if (isOpen) {
+ // Prefill with the highlighted text when the user clicked the link button
+ setText(selectedText)
+ if (textInputRef.current) {
+ textInputRef.current.focus()
+ }
+ }
+ }, [isOpen, selectedText])
+
+ if (!isOpen) return null
+
+ return (
+ <div className="c-modal-dimmer" role="dialog" aria-modal="true" aria-labelledby="link-modal-title" onKeyDown={handleKeyDown}>
+ <div className="c-modal-scroller">
+ <div className="c-modal-window c-modal-window--small">
+ <div className="c-modal-header">
+ <h3 id="link-modal-title">Insert Link</h3>
+ </div>
+ <div className="c-modal-content">
+ <form onSubmit={handleSubmit}>
+ <div className="c-form-field">
+ <label htmlFor="link-text">
+ Text to display <span style={{ color: "red" }}>*</span>
+ </label>
+ <input
+ ref={textInputRef}
+ id="link-text"
+ type="text"
+ value={text}
+ onChange={(e) => {
+ setText(e.target.value)
+ if (errors.text) {
+ setErrors((prev) => ({ ...prev, text: undefined }))
+ }
+ }}
+ placeholder="Enter link text"
+ className={`c-input ${errors.text ? "c-input--error" : ""}`}
+ />
+ {errors.text && <div className="text-red-600 text-sm mt-1">{errors.text}</div>}
+ </div>
+ <div className="c-form-field">
+ <label htmlFor="link-url">
+ URL <span style={{ color: "red" }}>*</span>
+ </label>
+ <input
+ id="link-url"
+ type="text"
+ value={url}
+ onChange={(e) => {
+ setUrl(e.target.value)
+ if (errors.url) {
+ setErrors((prev) => ({ ...prev, url: undefined }))
+ }
+ }}
+ placeholder="https://example.com"
+ className={`c-input ${errors.url ? "c-input--error" : ""}`}
+ />
+ {errors.url && <div className="text-red-600 text-sm mt-1">{errors.url}</div>}
+ </div>
+ </form>
+ </div>
+ <div className="c-modal-footer c-modal-footer--right">
+ <div style={{ display: "flex", gap: "8px" }}>
+ <button type="button" onClick={handleClose} className="c-button c-button--secondary">
+ Cancel
+ </button>
+ <button type="submit" onClick={handleSubmit} className="c-button c-button--primary">
+ Insert Link
+ </button>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ )
+}
+
+export default LinkInsertModal
diff --git a/public/pages/Home/components/ShareFeedback.scss b/public/pages/Home/components/ShareFeedback.scss
index cbdc84d76..44936000a 100644
--- a/public/pages/Home/components/ShareFeedback.scss
+++ b/public/pages/Home/components/ShareFeedback.scss
@@ -10,7 +10,7 @@
background-color: var(--colors-white);
padding: spacing(6);
border-radius: get("border.radius.large");
- max-width: 600px;
+ max-width: 720px;
margin: 0 auto spacing(4);
flex-shrink: 0;
}