@@ -7,46 +7,42 @@ import utils from "../../services/utils";
77import Modal from "../react/Modal" ;
88import Button from "../react/Button" ;
99import { useTriliumEvent } from "../react/hooks" ;
10+ import EditableTextTypeWidget from "../type_widgets/editable_text" ;
1011
1112interface RenderMarkdownResponse {
1213 htmlContent : string ;
1314}
1415
1516export default function MarkdownImportDialog ( ) {
1617 const markdownImportTextArea = useRef < HTMLTextAreaElement > ( null ) ;
18+ const [ textTypeWidget , setTextTypeWidget ] = useState < EditableTextTypeWidget > ( ) ;
1719 const [ text , setText ] = useState ( "" ) ;
1820 const [ shown , setShown ] = useState ( false ) ;
1921
20- const triggerImport = useCallback ( ( ) => {
21- if ( appContext . tabManager . getActiveContextNoteType ( ) !== "text" ) {
22- return ;
23- }
24-
22+ useTriliumEvent ( "showPasteMarkdownDialog" , ( { textTypeWidget } ) => {
23+ setTextTypeWidget ( textTypeWidget ) ;
2524 if ( utils . isElectron ( ) ) {
2625 const { clipboard } = utils . dynamicRequire ( "electron" ) ;
2726 const text = clipboard . readText ( ) ;
2827
29- convertMarkdownToHtml ( text ) ;
28+ convertMarkdownToHtml ( text , textTypeWidget ) ;
3029 } else {
3130 setShown ( true ) ;
3231 }
33- } , [ ] ) ;
34-
35- useTriliumEvent ( "importMarkdownInline" , triggerImport ) ;
36- useTriliumEvent ( "pasteMarkdownIntoText" , triggerImport ) ;
37-
38- async function sendForm ( ) {
39- await convertMarkdownToHtml ( text ) ;
40- setText ( "" ) ;
41- setShown ( false ) ;
42- }
32+ } ) ;
4333
4434 return (
4535 < Modal
4636 className = "markdown-import-dialog" title = { t ( "markdown_import.dialog_title" ) } size = "lg"
47- footer = { < Button className = "markdown-import-button" text = { t ( "markdown_import.import_button" ) } onClick = { sendForm } keyboardShortcut = "Ctrl+Space " /> }
37+ footer = { < Button className = "markdown-import-button" text = { t ( "markdown_import.import_button" ) } onClick = { ( ) => setShown ( false ) } keyboardShortcut = "Ctrl+Enter " /> }
4838 onShown = { ( ) => markdownImportTextArea . current ?. focus ( ) }
49- onHidden = { ( ) => setShown ( false ) }
39+ onHidden = { async ( ) => {
40+ if ( textTypeWidget ) {
41+ await convertMarkdownToHtml ( text , textTypeWidget ) ;
42+ }
43+ setShown ( false ) ;
44+ setText ( "" ) ;
45+ } }
5046 show = { shown }
5147 >
5248 < p > { t ( "markdown_import.modal_body_text" ) } </ p >
@@ -56,26 +52,17 @@ export default function MarkdownImportDialog() {
5652 onKeyDown = { ( e ) => {
5753 if ( e . key === "Enter" && e . ctrlKey ) {
5854 e . preventDefault ( ) ;
59- sendForm ( ) ;
55+ setShown ( false ) ;
6056 }
6157 } } > </ textarea >
6258 </ Modal >
6359 )
6460}
6561
66- async function convertMarkdownToHtml ( markdownContent : string ) {
62+ async function convertMarkdownToHtml ( markdownContent : string , textTypeWidget : EditableTextTypeWidget ) {
6763 const { htmlContent } = await server . post < RenderMarkdownResponse > ( "other/render-markdown" , { markdownContent } ) ;
6864
69- const textEditor = await appContext . tabManager . getActiveContext ( ) ?. getTextEditor ( ) ;
70- if ( ! textEditor ) {
71- return ;
72- }
73-
74- const viewFragment = textEditor . data . processor . toView ( htmlContent ) ;
75- const modelFragment = textEditor . data . toModel ( viewFragment ) ;
76-
77- textEditor . model . insertContent ( modelFragment , textEditor . model . document . selection ) ;
78- textEditor . editing . view . focus ( ) ;
79-
65+ await textTypeWidget . addHtmlToEditor ( htmlContent ) ;
66+
8067 toast . showMessage ( t ( "markdown_import.import_success" ) ) ;
8168}
0 commit comments