2022-03-26 16:09:42 +00:00
|
|
|
import AppLayout from "components/AppLayout";
|
|
|
|
import Markdawn from "components/Markdown/Markdawn";
|
2022-02-17 16:55:44 +00:00
|
|
|
import ContentPanel, {
|
|
|
|
ContentPanelWidthSizes,
|
|
|
|
} from "components/Panels/ContentPanel";
|
|
|
|
import { GetStaticProps } from "next";
|
2022-03-26 16:09:42 +00:00
|
|
|
import { useRouter } from "next/router";
|
2022-02-17 16:55:44 +00:00
|
|
|
import Script from "next/script";
|
2022-03-07 14:50:00 +00:00
|
|
|
import { AppStaticProps, getAppStaticProps } from "queries/getAppStaticProps";
|
2022-03-26 16:09:42 +00:00
|
|
|
import { useCallback, useState } from "react";
|
2022-02-17 16:55:44 +00:00
|
|
|
|
2022-03-07 14:50:00 +00:00
|
|
|
interface EditorProps extends AppStaticProps {}
|
2022-02-17 16:55:44 +00:00
|
|
|
|
|
|
|
export default function Editor(props: EditorProps): JSX.Element {
|
2022-03-07 14:50:00 +00:00
|
|
|
const { langui } = props;
|
2022-03-19 16:35:17 +00:00
|
|
|
const router = useRouter();
|
2022-02-17 16:55:44 +00:00
|
|
|
|
|
|
|
const handleInput = useCallback((e) => {
|
|
|
|
setMarkdown(e.target.value);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const [markdown, setMarkdown] = useState("");
|
|
|
|
|
|
|
|
const contentPanel = (
|
|
|
|
<ContentPanel width={ContentPanelWidthSizes.large}>
|
|
|
|
<Script
|
|
|
|
id="autoFitTextArea"
|
|
|
|
strategy="afterInteractive"
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: `
|
|
|
|
const el = document.querySelector("#editorTextArea")
|
|
|
|
el.addEventListener('input', function() {
|
|
|
|
this.style.height = (this.scrollHeight) + 'px';
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
2022-02-17 18:49:28 +00:00
|
|
|
<div className="grid grid-cols-2 gap-8">
|
2022-02-17 16:55:44 +00:00
|
|
|
<div>
|
|
|
|
<h2>Editor</h2>
|
|
|
|
<textarea
|
|
|
|
id="editorTextArea"
|
|
|
|
onInput={handleInput}
|
2022-02-24 01:06:25 +00:00
|
|
|
className="bg-mid rounded-xl p-8 w-full font-monospace"
|
2022-02-17 18:49:28 +00:00
|
|
|
value={markdown}
|
2022-03-07 14:50:00 +00:00
|
|
|
title="Input textarea"
|
2022-02-17 18:49:28 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<h2 className="mt-4">Convert text to markdown</h2>
|
|
|
|
<textarea
|
|
|
|
readOnly
|
|
|
|
id="htmlMdTextArea"
|
2022-03-07 14:50:00 +00:00
|
|
|
title="Ouput textarea"
|
2022-02-17 16:55:44 +00:00
|
|
|
onPaste={(event) => {
|
2022-02-17 18:49:28 +00:00
|
|
|
const TurndownService = require("turndown").default;
|
|
|
|
const turndownService = new TurndownService({
|
|
|
|
headingStyle: "atx",
|
|
|
|
codeBlockStyle: "fenced",
|
|
|
|
bulletListMarker: "-",
|
|
|
|
emDelimiter: "*",
|
2022-02-21 00:01:27 +00:00
|
|
|
strongDelimiter: "**",
|
2022-02-17 18:49:28 +00:00
|
|
|
});
|
|
|
|
|
2022-02-17 16:55:44 +00:00
|
|
|
let paste = event.clipboardData.getData("text/html");
|
2022-02-17 18:49:28 +00:00
|
|
|
paste = paste.replace(/<\!--.*?-->/g, "");
|
|
|
|
paste = turndownService.turndown(paste);
|
|
|
|
paste = paste.replace(/<\!--.*?-->/g, "");
|
2022-02-17 22:36:24 +00:00
|
|
|
|
|
|
|
const target = event.target as HTMLTextAreaElement;
|
|
|
|
target.value = paste;
|
|
|
|
target.select();
|
2022-02-17 18:49:28 +00:00
|
|
|
event.preventDefault();
|
2022-02-17 16:55:44 +00:00
|
|
|
}}
|
2022-03-23 15:19:23 +00:00
|
|
|
className="font-monospace"
|
2022-02-17 16:55:44 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<h2>Preview</h2>
|
2022-02-24 01:06:25 +00:00
|
|
|
<div className="bg-mid rounded-xl p-8">
|
2022-03-19 16:35:17 +00:00
|
|
|
<Markdawn router={router} className="max-w-full" text={markdown} />
|
2022-02-17 16:55:44 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ContentPanel>
|
|
|
|
);
|
2022-02-17 18:49:28 +00:00
|
|
|
return (
|
|
|
|
<AppLayout
|
2022-02-24 03:50:00 +00:00
|
|
|
navTitle="Markdawn Editor"
|
2022-02-17 18:49:28 +00:00
|
|
|
contentPanel={contentPanel}
|
2022-03-07 14:50:00 +00:00
|
|
|
{...props}
|
2022-02-17 18:49:28 +00:00
|
|
|
/>
|
|
|
|
);
|
2022-02-17 16:55:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getStaticProps: GetStaticProps = async (context) => {
|
2022-03-07 14:50:00 +00:00
|
|
|
const props: EditorProps = {
|
|
|
|
...(await getAppStaticProps(context)),
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
props: props,
|
|
|
|
};
|
2022-02-17 16:55:44 +00:00
|
|
|
};
|