mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	refactored import markdown dialog into separate file, fixes #206
This commit is contained in:
		
							
								
								
									
										63
									
								
								src/public/javascripts/dialogs/markdown_import.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								src/public/javascripts/dialogs/markdown_import.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | |||||||
|  | import libraryLoader from "../services/library_loader.js"; | ||||||
|  | import infoService from "../services/info.js"; | ||||||
|  | import utils from "../services/utils.js"; | ||||||
|  | import noteDetailTextService from "../services/note_detail_text.js"; | ||||||
|  |  | ||||||
|  | const $markdownImportDialog = $('#markdown-import-dialog'); | ||||||
|  | const $markdownImportTextarea = $('#markdown-import-textarea'); | ||||||
|  | const $markdownImportButton = $('#markdown-import-button'); | ||||||
|  |  | ||||||
|  | async function convertMarkdownToHtml(text) { | ||||||
|  |     await libraryLoader.requireLibrary(libraryLoader.COMMONMARK); | ||||||
|  |  | ||||||
|  |     const reader = new commonmark.Parser(); | ||||||
|  |     const writer = new commonmark.HtmlRenderer(); | ||||||
|  |     const parsed = reader.parse(text); | ||||||
|  |  | ||||||
|  |     const result = writer.render(parsed); | ||||||
|  |  | ||||||
|  |     const textEditor = noteDetailTextService.getEditor(); | ||||||
|  |     const viewFragment = textEditor.data.processor.toView(result); | ||||||
|  |     const modelFragment = textEditor.data.toModel(viewFragment); | ||||||
|  |  | ||||||
|  |     textEditor.model.insertContent(modelFragment, textEditor.model.document.selection); | ||||||
|  |  | ||||||
|  |     infoService.showMessage("Markdown content has been imported into the document."); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | async function importMarkdownInline() { | ||||||
|  |     if (utils.isElectron()) { | ||||||
|  |         const {clipboard} = require('electron'); | ||||||
|  |         const text = clipboard.readText(); | ||||||
|  |  | ||||||
|  |         convertMarkdownToHtml(text); | ||||||
|  |     } | ||||||
|  |     else { | ||||||
|  |         $("input[name='search-text']").focus(); | ||||||
|  |  | ||||||
|  |         glob.activeDialog = $markdownImportDialog; | ||||||
|  |  | ||||||
|  |         $markdownImportDialog.modal(); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | async function sendForm() { | ||||||
|  |     const text = $markdownImportTextarea.val(); | ||||||
|  |  | ||||||
|  |     $markdownImportDialog.modal('hide'); | ||||||
|  |  | ||||||
|  |     await convertMarkdownToHtml(text); | ||||||
|  |  | ||||||
|  |     $markdownImportTextarea.val(''); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | $markdownImportButton.click(sendForm); | ||||||
|  |  | ||||||
|  | $markdownImportDialog.bind('keydown', 'ctrl+return', sendForm); | ||||||
|  |  | ||||||
|  | // for CKEditor integration (button on block toolbar) | ||||||
|  | window.glob.importMarkdownInline = importMarkdownInline; | ||||||
|  |  | ||||||
|  | export default { | ||||||
|  |     importMarkdownInline | ||||||
|  | }; | ||||||
							
								
								
									
										1
									
								
								src/public/javascripts/services/bootstrap.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								src/public/javascripts/services/bootstrap.js
									
									
									
									
										vendored
									
									
								
							| @@ -6,6 +6,7 @@ import noteSourceDialog from '../dialogs/note_source.js'; | |||||||
| import recentChangesDialog from '../dialogs/recent_changes.js'; | import recentChangesDialog from '../dialogs/recent_changes.js'; | ||||||
| import optionsDialog from '../dialogs/options.js'; | import optionsDialog from '../dialogs/options.js'; | ||||||
| import sqlConsoleDialog from '../dialogs/sql_console.js'; | import sqlConsoleDialog from '../dialogs/sql_console.js'; | ||||||
|  | import markdownImportDialog from '../dialogs/markdown_import.js'; | ||||||
|  |  | ||||||
| import cloning from './cloning.js'; | import cloning from './cloning.js'; | ||||||
| import contextMenu from './tree_context_menu.js'; | import contextMenu from './tree_context_menu.js'; | ||||||
|   | |||||||
| @@ -1,14 +1,8 @@ | |||||||
| import libraryLoader from "./library_loader.js"; | import libraryLoader from "./library_loader.js"; | ||||||
| import noteDetailService from './note_detail.js'; | import noteDetailService from './note_detail.js'; | ||||||
| import utils from "./utils.js"; |  | ||||||
| import infoService from "./info.js"; |  | ||||||
|  |  | ||||||
| const $component = $('#note-detail-text'); | const $component = $('#note-detail-text'); | ||||||
|  |  | ||||||
| const $markdownImportDialog = $('#markdown-import-dialog'); |  | ||||||
| const $markdownImportTextarea = $('#markdown-import-textarea'); |  | ||||||
| const $markdownImportButton = $('#markdown-import-button'); |  | ||||||
|  |  | ||||||
| let textEditor = null; | let textEditor = null; | ||||||
|  |  | ||||||
| async function show() { | async function show() { | ||||||
| @@ -49,59 +43,10 @@ function getEditor() { | |||||||
|     return textEditor; |     return textEditor; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function convertMarkdownToHtml(text) { |  | ||||||
|     await libraryLoader.requireLibrary(libraryLoader.COMMONMARK); |  | ||||||
|  |  | ||||||
|     const reader = new commonmark.Parser(); |  | ||||||
|     const writer = new commonmark.HtmlRenderer(); |  | ||||||
|     const parsed = reader.parse(text); |  | ||||||
|  |  | ||||||
|     const result = writer.render(parsed); |  | ||||||
|  |  | ||||||
|     const viewFragment = textEditor.data.processor.toView(result); |  | ||||||
|     const modelFragment = textEditor.data.toModel(viewFragment); |  | ||||||
|  |  | ||||||
|     textEditor.model.insertContent(modelFragment, textEditor.model.document.selection); |  | ||||||
|  |  | ||||||
|     infoService.showMessage("Markdown content has been imported into the document."); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function importMarkdownInline() { |  | ||||||
|     if (utils.isElectron()) { |  | ||||||
|         const {clipboard} = require('electron'); |  | ||||||
|         const text = clipboard.readText(); |  | ||||||
|  |  | ||||||
|         convertMarkdownToHtml(text); |  | ||||||
|     } |  | ||||||
|     else { |  | ||||||
|         $("input[name='search-text']").focus(); |  | ||||||
|  |  | ||||||
|         glob.activeDialog = $markdownImportDialog; |  | ||||||
|  |  | ||||||
|         $markdownImportDialog.modal(); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function sendMarkdownDialog() { |  | ||||||
|     const text = $markdownImportTextarea.val(); |  | ||||||
|  |  | ||||||
|     $markdownImportDialog.modal('hide'); |  | ||||||
|  |  | ||||||
|     await convertMarkdownToHtml(text); |  | ||||||
|  |  | ||||||
|     $markdownImportTextarea.val(''); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| function onNoteChange(func) { | function onNoteChange(func) { | ||||||
|     textEditor.model.document.on('change:data', func); |     textEditor.model.document.on('change:data', func); | ||||||
| } | } | ||||||
|  |  | ||||||
| $markdownImportButton.click(sendMarkdownDialog); |  | ||||||
|  |  | ||||||
| $markdownImportDialog.bind('keydown', 'ctrl+return', sendMarkdownDialog); |  | ||||||
|  |  | ||||||
| window.glob.importMarkdownInline = importMarkdownInline; |  | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|     show, |     show, | ||||||
|     getEditor, |     getEditor, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user