Skip to content

Instantly share code, notes, and snippets.

@mvallebr
Created March 2, 2024 14:37
Show Gist options
  • Save mvallebr/1fc7a521d90ce70c9d3bdee572980ed0 to your computer and use it in GitHub Desktop.
Save mvallebr/1fc7a521d90ce70c9d3bdee572980ed0 to your computer and use it in GitHub Desktop.
import markdownit from 'markdown-it';
import React, { useState } from 'react';
import { default as MdEditor } from 'react-markdown-editor-lite';
import 'react-markdown-editor-lite/lib/index.css';
type Props = {
initialText: string,
onChange?: (html: string, text: string) => void,
viewOnly: boolean,
};
const MarkDownEditor: React.FC<Props> = ({ initialText, onChange, viewOnly }) => {
const [isFullScreen, setIsFullScreen] = useState(false);
const toggleFullScreen = () => {
setIsFullScreen(!isFullScreen);
// this.$refs.mdEditor.fullscreen(isFullScreen);
};
const mdParser = new markdownit(
/* Markdown-it options */
{ html: false }
);
function handleEditorChange({ html, text }: { html: string, text: string }) {
if (onChange) {
onChange(html, text);
}
}
const renderedOutput = mdParser.render(initialText);
return (
(!viewOnly)
? (
<MdEditor
style={{ width: '100%', height: '100%', textAlign: "left", zIndex: 2000 }}
renderHTML={text => mdParser.render(text)}
onChange={handleEditorChange}
value={initialText}
/>
) : (
<MdEditor
style={{ width: '100%', height: '100%', textAlign: "left", zIndex: 2000 }}
renderHTML={text => mdParser.render(text)}
view={{ menu: false, md: false, html: true }}
readOnly={true}
onChange={handleEditorChange}
value={initialText}
/>
)
);
};
export default MarkDownEditor;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment