2018-03-28 10:42:46 +08:00
|
|
|
import libraryLoader from "./library_loader.js";
|
2018-03-27 12:22:02 +08:00
|
|
|
import noteDetailService from './note_detail.js';
|
2018-09-03 05:02:01 +08:00
|
|
|
import utils from "./utils.js";
|
|
|
|
import infoService from "./info.js";
|
2018-03-27 12:22:02 +08:00
|
|
|
|
|
|
|
const $noteDetailText = $('#note-detail-text');
|
|
|
|
|
2018-09-03 05:02:01 +08:00
|
|
|
const $markdownImportDialog = $('#markdown-import-dialog');
|
|
|
|
const $markdownImportTextarea = $('#markdown-import-textarea');
|
|
|
|
const $markdownImportButton = $('#markdown-import-button');
|
|
|
|
|
2018-03-27 12:22:02 +08:00
|
|
|
let textEditor = null;
|
|
|
|
|
2018-03-28 09:46:38 +08:00
|
|
|
async function show() {
|
2018-03-27 12:22:02 +08:00
|
|
|
if (!textEditor) {
|
2018-03-28 10:42:46 +08:00
|
|
|
await libraryLoader.requireLibrary(libraryLoader.CKEDITOR);
|
2018-03-27 12:22:02 +08:00
|
|
|
|
2018-08-28 21:03:23 +08:00
|
|
|
// textEditor might have been initialized during previous await so checking again
|
|
|
|
// looks like double initialization can freeze CKEditor pretty badly
|
|
|
|
if (!textEditor) {
|
2018-09-07 19:47:26 +08:00
|
|
|
textEditor = await BalloonEditor.create($noteDetailText[0]);
|
2018-03-27 12:22:02 +08:00
|
|
|
|
2018-09-03 22:05:28 +08:00
|
|
|
onNoteChange(noteDetailService.noteChanged);
|
2018-08-28 21:03:23 +08:00
|
|
|
}
|
2018-03-27 12:22:02 +08:00
|
|
|
}
|
|
|
|
|
2018-04-13 08:03:23 +08:00
|
|
|
textEditor.setData(noteDetailService.getCurrentNote().content);
|
2018-03-27 12:22:02 +08:00
|
|
|
|
|
|
|
$noteDetailText.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getContent() {
|
|
|
|
let content = textEditor.getData();
|
|
|
|
|
|
|
|
// if content is only tags/whitespace (typically <p> </p>), then just make it empty
|
|
|
|
// this is important when setting new note to code
|
|
|
|
if (jQuery(content).text().trim() === '' && !content.includes("<img")) {
|
|
|
|
content = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
function focus() {
|
|
|
|
$noteDetailText.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEditor() {
|
|
|
|
return textEditor;
|
|
|
|
}
|
|
|
|
|
2018-09-03 05:02:01 +08:00
|
|
|
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.dialog({
|
|
|
|
modal: true,
|
|
|
|
width: 700,
|
|
|
|
height: 500
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendMarkdownDialog() {
|
|
|
|
const text = $markdownImportTextarea.val();
|
|
|
|
|
|
|
|
$markdownImportDialog.dialog('close');
|
|
|
|
|
|
|
|
await convertMarkdownToHtml(text);
|
|
|
|
|
|
|
|
$markdownImportTextarea.val('');
|
|
|
|
}
|
|
|
|
|
2018-09-03 22:05:28 +08:00
|
|
|
function onNoteChange(func) {
|
|
|
|
textEditor.model.document.on('change:data', func);
|
|
|
|
}
|
|
|
|
|
2018-09-03 05:02:01 +08:00
|
|
|
$markdownImportButton.click(sendMarkdownDialog);
|
|
|
|
|
|
|
|
$markdownImportDialog.bind('keydown', 'ctrl+return', sendMarkdownDialog);
|
|
|
|
|
|
|
|
window.glob.importMarkdownInline = importMarkdownInline;
|
|
|
|
|
2018-03-27 12:22:02 +08:00
|
|
|
export default {
|
2018-03-28 09:46:38 +08:00
|
|
|
show,
|
2018-03-27 12:22:02 +08:00
|
|
|
getEditor,
|
|
|
|
getContent,
|
2018-09-03 22:05:28 +08:00
|
|
|
focus,
|
2018-10-31 19:29:01 +08:00
|
|
|
onNoteChange,
|
|
|
|
cleanup: () => {
|
|
|
|
if (textEditor) {
|
|
|
|
textEditor.setData('');
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 12:22:02 +08:00
|
|
|
}
|