-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrating react-markdown with miq-structured-list and adding a mark…
…down preview
- Loading branch information
Showing
62 changed files
with
1,944 additions
and
440 deletions.
There are no files selected for viewing
This file contains 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 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,8 @@ | ||
/** Configuration to idendify the usage of the Markdown component and the props needed. */ | ||
export const previewConfiguration = { | ||
CATALOG_EDIT_LONG_DESCRIPTION: { | ||
title: __('Long Description'), | ||
field: 'long_description', | ||
mode: 'htmlmixed', | ||
}, | ||
}; |
This file contains 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,98 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
import { Controlled as CodeMirror } from 'react-codemirror2'; | ||
import MiqMarkdown from '../MiqMarkdown'; | ||
import { previewConfiguration } from './helper'; | ||
|
||
/** Component to preview the markdown contents on-the-fly. */ | ||
const MarkdownPreview = ({ | ||
content, url, type, | ||
}) => { | ||
const miqCustomTabReducer = useSelector((state) => state.miqCustomTabReducer); | ||
const { title, mode, field } = previewConfiguration[type]; | ||
|
||
const [data, setData] = useState({ | ||
editorContent: content, | ||
oneTrans: 0, | ||
}); | ||
|
||
/** The textField present in the ruby form has to be updated when data in the CodeMirror is changed. */ | ||
useEffect(() => { | ||
const textArea = document.getElementById(field); | ||
textArea.value = data.editorContent; | ||
if (data.oneTrans === 1) { | ||
// To enable the form's save/cancel buttons (w.r.t pervious code-mirror implementation). | ||
window.miqSendOneTrans(url); | ||
} | ||
}, [data.editorContent]); | ||
|
||
/** The code-mirror component needs to be refreshed when the tab selection is changed. | ||
* If this is not used, then the code mirror will not load its default value. | ||
*/ | ||
useEffect(() => { | ||
window.miq_refresh_code_mirror(); | ||
}, [miqCustomTabReducer]); | ||
|
||
/** Function to render the title of editor and preview sections. */ | ||
const renderTitle = (type) => ( | ||
<div className="markdown-section-title"> | ||
{`${title} - ${type}`} | ||
</div> | ||
); | ||
|
||
/** Function to render the code-mirror editor. */ | ||
const renderEditor = () => ( | ||
<div className="markdown-section" id="editor"> | ||
{renderTitle(__('Editor'))} | ||
<div className="markdown-section-content"> | ||
<CodeMirror | ||
className="miq-codemirror miq-structured-list-code-mirror" | ||
options={{ | ||
mode, | ||
lineNumbers: true, | ||
matchBrackets: true, | ||
theme: 'eclipse', | ||
viewportMargin: Infinity, | ||
readOnly: false, | ||
}} | ||
onBeforeChange={(_editor, _data, value) => setData({ | ||
...data, | ||
editorContent: value, | ||
oneTrans: data.oneTrans + 1, | ||
})} | ||
value={data.editorContent} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
|
||
/** Function to render the preview of the data entered in code-mirror editor. */ | ||
const renderPreview = () => ( | ||
<div className="markdown-section" id="preview"> | ||
{renderTitle(__('Preview'))} | ||
<div className="markdown-section-content"> | ||
<MiqMarkdown content={data.editorContent} /> | ||
</div> | ||
</div> | ||
); | ||
|
||
return ( | ||
<div className="markdown-wrapper"> | ||
{renderEditor()} | ||
{renderPreview()} | ||
</div> | ||
); | ||
}; | ||
|
||
MarkdownPreview.propTypes = { | ||
content: PropTypes.string, | ||
url: PropTypes.string.isRequired, | ||
type: PropTypes.string.isRequired, | ||
}; | ||
|
||
MarkdownPreview.defaultProps = { | ||
content: undefined, | ||
}; | ||
|
||
export default MarkdownPreview; |
This file contains 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,16 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import ReactMarkdown from 'react-markdown'; | ||
|
||
/** Component to render the markdown contents. */ | ||
const MiqMarkdown = ({ content }) => <ReactMarkdown>{content}</ReactMarkdown>; | ||
|
||
MiqMarkdown.propTypes = { | ||
content: PropTypes.string, | ||
}; | ||
|
||
MiqMarkdown.defaultProps = { | ||
content: undefined, | ||
}; | ||
|
||
export default MiqMarkdown; |
This file contains 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 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 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 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,7 @@ | ||
const incrementClickCount = () => ({ | ||
type: 'INCREMENT_CLICK_COUNT', | ||
}); | ||
|
||
export const miqCustomTabActions = { | ||
incrementClickCount, | ||
}; |
This file contains 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,10 @@ | ||
const miqCustomTabReducer = (clickCount = 0, action) => { | ||
switch (action.type) { | ||
case 'INCREMENT_CLICK_COUNT': | ||
return clickCount + 1; | ||
default: | ||
return clickCount; | ||
} | ||
}; | ||
|
||
export default miqCustomTabReducer; |
This file contains 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 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 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 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 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 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
Oops, something went wrong.