accords-library.com/src/pages/editor.tsx

104 lines
3.1 KiB
TypeScript
Raw Normal View History

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";
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
interface EditorProps extends AppStaticProps {}
2022-02-17 16:55:44 +00:00
export default function Editor(props: EditorProps): JSX.Element {
const { langui } = props;
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';
});
`,
}}
/>
<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}
className="bg-mid rounded-xl p-8 w-full font-monospace"
value={markdown}
title="Input textarea"
/>
<h2 className="mt-4">Convert text to markdown</h2>
<textarea
readOnly
id="htmlMdTextArea"
title="Ouput textarea"
2022-02-17 16:55:44 +00:00
onPaste={(event) => {
const TurndownService = require("turndown").default;
const turndownService = new TurndownService({
headingStyle: "atx",
codeBlockStyle: "fenced",
bulletListMarker: "-",
emDelimiter: "*",
strongDelimiter: "**",
});
2022-02-17 16:55:44 +00:00
let paste = event.clipboardData.getData("text/html");
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();
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>
<div className="bg-mid rounded-xl p-8">
<Markdawn router={router} className="max-w-full" text={markdown} />
2022-02-17 16:55:44 +00:00
</div>
</div>
</div>
</ContentPanel>
);
return (
<AppLayout
2022-02-24 03:50:00 +00:00
navTitle="Markdawn Editor"
contentPanel={contentPanel}
{...props}
/>
);
2022-02-17 16:55:44 +00:00
}
export const getStaticProps: GetStaticProps = async (context) => {
const props: EditorProps = {
...(await getAppStaticProps(context)),
};
return {
props: props,
};
2022-02-17 16:55:44 +00:00
};