@@ -7,44 +7,43 @@ 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 ( "showMarkdownIntoTextDialog" , ( { 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 ) ;
32+ } ) ;
3733
3834 async function sendForm ( ) {
39- await convertMarkdownToHtml ( text ) ;
35+ if ( textTypeWidget ) {
36+ await convertMarkdownToHtml ( text , textTypeWidget ) ;
37+ }
38+
4039 setText ( "" ) ;
4140 setShown ( false ) ;
4241 }
4342
4443 return (
4544 < Modal
4645 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 " /> }
46+ footer = { < Button className = "markdown-import-button" text = { t ( "markdown_import.import_button" ) } onClick = { sendForm } keyboardShortcut = "Ctrl+Enter " /> }
4847 onShown = { ( ) => markdownImportTextArea . current ?. focus ( ) }
4948 onHidden = { ( ) => setShown ( false ) }
5049 show = { shown }
@@ -63,19 +62,10 @@ export default function MarkdownImportDialog() {
6362 )
6463}
6564
66- async function convertMarkdownToHtml ( markdownContent : string ) {
65+ async function convertMarkdownToHtml ( markdownContent : string , textTypeWidget : EditableTextTypeWidget ) {
6766 const { htmlContent } = await server . post < RenderMarkdownResponse > ( "other/render-markdown" , { markdownContent } ) ;
6867
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-
68+ await textTypeWidget . addHtmlToEditor ( htmlContent ) ;
69+
8070 toast . showMessage ( t ( "markdown_import.import_success" ) ) ;
8171}
0 commit comments