-
Notifications
You must be signed in to change notification settings - Fork 50
feat: markdown editor and markdown renderer #2140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e59e79b
feat: markdown editor and markdown renderer
kemuru e7971b8
fix: styling fixes, hack to make the text flicker appear
kemuru d82188a
feat: style improvements
kemuru e1a3d05
chore: comments from ai reviews
kemuru 9d4b286
feat: link popup warning, clean up codes
kemuru 4f35c9d
Merge branch 'dev' into feat/markdown-editor-and-markdown-renderer
kemuru e3dc195
Potential fix for code scanning alert no. 96: Bad HTML filtering regexp
kemuru 942fa9e
chore: dompurify in packagejson
kemuru 20ae3a4
Potential fix for code scanning alert no. 97: Incomplete URL scheme c…
kemuru e2d7c81
feat: max height and scrollable on mobile
kemuru 48abffb
Merge branch 'dev' into feat/markdown-editor-and-markdown-renderer
kemuru 859834b
feat: switch to reactmarkdown, remark gfm, rehype raw and rehype sani…
kemuru 56aaca8
chore: style impros resolver description
kemuru 94e4783
chore: slight transition and coloring
kemuru a8d32b8
chore: small styling issue
kemuru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import React, { useRef } from "react"; | ||
import styled from "styled-components"; | ||
|
||
import { | ||
MDXEditor, | ||
type MDXEditorMethods, | ||
type MDXEditorProps, | ||
headingsPlugin, | ||
listsPlugin, | ||
quotePlugin, | ||
thematicBreakPlugin, | ||
markdownShortcutPlugin, | ||
linkPlugin, | ||
linkDialogPlugin, | ||
tablePlugin, | ||
toolbarPlugin, | ||
UndoRedo, | ||
BoldItalicUnderlineToggles, | ||
CodeToggle, | ||
ListsToggle, | ||
CreateLink, | ||
InsertTable, | ||
BlockTypeSelect, | ||
Separator, | ||
} from "@mdxeditor/editor"; | ||
|
||
import InfoIcon from "svgs/icons/info-circle.svg"; | ||
|
||
import { sanitizeMarkdown } from "utils/markdownSanitization"; | ||
|
||
import { MDXEditorContainer, MDXEditorGlobalStyles } from "styles/mdxEditorTheme"; | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
import "@mdxeditor/editor/style.css"; | ||
|
||
const Container = styled(MDXEditorContainer)<{ isEmpty: boolean }>``; | ||
|
||
const MessageContainer = styled.div` | ||
display: flex; | ||
align-items: flex-start; | ||
gap: 8px; | ||
margin-top: 8px; | ||
`; | ||
|
||
const MessageText = styled.small` | ||
font-size: 14px; | ||
font-weight: 400; | ||
color: ${({ theme }) => theme.secondaryText}; | ||
hyphens: auto; | ||
line-height: 1.4; | ||
`; | ||
|
||
const StyledInfoIcon = styled(InfoIcon)` | ||
width: 16px; | ||
height: 16px; | ||
fill: ${({ theme }) => theme.secondaryText} !important; | ||
flex-shrink: 0; | ||
margin-top: 2px; | ||
path { | ||
fill: ${({ theme }) => theme.secondaryText} !important; | ||
} | ||
* { | ||
fill: ${({ theme }) => theme.secondaryText} !important; | ||
} | ||
`; | ||
|
||
interface IMarkdownEditor { | ||
value: string; | ||
onChange: (value: string) => void; | ||
placeholder?: string; | ||
showMessage?: boolean; | ||
} | ||
|
||
const MarkdownEditor: React.FC<IMarkdownEditor> = ({ | ||
value, | ||
onChange, | ||
placeholder = "Justify your vote...", | ||
showMessage = true, | ||
}) => { | ||
const editorRef = useRef<MDXEditorMethods>(null); | ||
|
||
const handleChange = (markdown: string) => { | ||
const cleanedMarkdown = markdown === "\u200B" ? "" : markdown.replace(/^\u200B/, ""); | ||
const sanitizedMarkdown = sanitizeMarkdown(cleanedMarkdown); | ||
onChange(sanitizedMarkdown); | ||
}; | ||
|
||
const handleContainerClick = () => { | ||
if (isEmpty && editorRef.current) { | ||
editorRef.current.setMarkdown("\u200B"); | ||
setTimeout(() => { | ||
if (editorRef.current) { | ||
editorRef.current.focus(); | ||
} | ||
}, 0); | ||
} | ||
}; | ||
|
||
const isEmpty = !value || value.trim() === ""; | ||
|
||
const editorProps: MDXEditorProps = { | ||
markdown: value, | ||
onChange: handleChange, | ||
placeholder, | ||
plugins: [ | ||
headingsPlugin(), | ||
listsPlugin(), | ||
quotePlugin(), | ||
thematicBreakPlugin(), | ||
markdownShortcutPlugin(), | ||
linkPlugin(), | ||
linkDialogPlugin(), | ||
tablePlugin(), | ||
toolbarPlugin({ | ||
toolbarContents: () => ( | ||
<> | ||
<UndoRedo /> | ||
<Separator /> | ||
<BoldItalicUnderlineToggles /> | ||
<CodeToggle /> | ||
<Separator /> | ||
<BlockTypeSelect /> | ||
<Separator /> | ||
<ListsToggle /> | ||
<Separator /> | ||
<CreateLink /> | ||
<InsertTable /> | ||
</> | ||
), | ||
}), | ||
], | ||
}; | ||
kemuru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return ( | ||
<> | ||
<MDXEditorGlobalStyles /> | ||
<Container isEmpty={isEmpty} onClick={handleContainerClick} role="region" aria-label="Markdown editor"> | ||
<MDXEditor ref={editorRef} {...editorProps} aria-label="Rich text editor for markdown content" /> | ||
{showMessage && ( | ||
<MessageContainer> | ||
<StyledInfoIcon /> | ||
<MessageText> | ||
Please provide a comprehensive justification for your decision. Quality explanations are essential for the | ||
parties involved and may be eligible for additional compensation in accordance with our justification | ||
policy. | ||
</MessageText> | ||
</MessageContainer> | ||
)} | ||
</Container> | ||
</> | ||
); | ||
}; | ||
|
||
export default MarkdownEditor; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from "react"; | ||
|
||
import { | ||
MDXEditor, | ||
type MDXEditorProps, | ||
headingsPlugin, | ||
listsPlugin, | ||
quotePlugin, | ||
thematicBreakPlugin, | ||
markdownShortcutPlugin, | ||
linkPlugin, | ||
tablePlugin, | ||
} from "@mdxeditor/editor"; | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
import { sanitizeMarkdown } from "utils/markdownSanitization"; | ||
|
||
import { MDXRendererContainer } from "styles/mdxEditorTheme"; | ||
|
||
import "@mdxeditor/editor/style.css"; | ||
|
||
interface IMarkdownRenderer { | ||
content: string; | ||
className?: string; | ||
} | ||
|
||
const MarkdownRenderer: React.FC<IMarkdownRenderer> = ({ content, className }) => { | ||
if (!content || content.trim() === "") { | ||
return null; | ||
} | ||
|
||
const sanitizedContent = sanitizeMarkdown(content); | ||
|
||
const editorProps: MDXEditorProps = { | ||
markdown: sanitizedContent, | ||
readOnly: true, | ||
plugins: [ | ||
headingsPlugin(), | ||
listsPlugin(), | ||
quotePlugin(), | ||
thematicBreakPlugin(), | ||
markdownShortcutPlugin(), | ||
linkPlugin(), | ||
tablePlugin(), | ||
], | ||
}; | ||
|
||
return ( | ||
<MDXRendererContainer className={className} role="region" aria-label="Markdown content"> | ||
<MDXEditor {...editorProps} aria-label="Rendered markdown content" /> | ||
</MDXRendererContainer> | ||
); | ||
}; | ||
|
||
export default MarkdownRenderer; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
web/src/pages/Cases/CaseDetails/Voting/JustificationArea.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.