import { useRef, useState } from "preact/hooks"; import { t } from "../../services/i18n"; import server from "../../services/server"; import toast from "../../services/toast"; import utils from "../../services/utils"; import Modal from "../react/Modal"; import Button from "../react/Button"; import { useTriliumEvent } from "../react/hooks"; import { CKEditorApi } from "../type_widgets/text/CKEditorWithWatchdog"; export interface MarkdownImportOpts { editorApi: CKEditorApi; } interface RenderMarkdownResponse { htmlContent: string; } export default function MarkdownImportDialog() { const markdownImportTextArea = useRef(null); const editorApiRef = useRef(null); const [ text, setText ] = useState(""); const [ shown, setShown ] = useState(false); useTriliumEvent("showPasteMarkdownDialog", ({ editorApi }) => { if (utils.isElectron()) { const { clipboard } = utils.dynamicRequire("electron"); const text = clipboard.readText(); convertMarkdownToHtml(text, editorApi); } else { editorApiRef.current = editorApi; setShown(true); } }); function submit() { setShown(false); if (editorApiRef.current && text) { convertMarkdownToHtml(text, editorApiRef.current); } } return ( } onShown={() => markdownImportTextArea.current?.focus()} onHidden={() => { setShown(false); setText(""); }} show={shown} >

{t("markdown_import.modal_body_text")}

) } async function convertMarkdownToHtml(markdownContent: string, textTypeWidget: CKEditorApi) { const { htmlContent } = await server.post("other/render-markdown", { markdownContent }); textTypeWidget.addHtmlToEditor(htmlContent); toast.showMessage(t("markdown_import.import_success")); }