trilium/src/public/javascripts/dialogs/note_source.js

60 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-12-27 07:15:29 +08:00
"use strict";
const noteSource = (function() {
const $showDialogButton = $("#show-source-button");
const $dialog = $("#note-source-dialog");
const $noteSource = $("#note-source");
2017-12-27 07:15:29 +08:00
function showDialog() {
glob.activeDialog = $dialog;
2017-12-27 07:15:29 +08:00
$dialog.dialog({
2017-12-27 07:15:29 +08:00
modal: true,
width: 800,
height: 500
2017-12-27 07:15:29 +08:00
});
2018-01-29 08:30:14 +08:00
const noteText = noteEditor.getCurrentNote().detail.content;
2017-12-27 07:15:29 +08:00
$noteSource.text(formatHtml(noteText));
2017-12-27 07:15:29 +08:00
}
function formatHtml(str) {
const div = document.createElement('div');
div.innerHTML = str.trim();
return formatNode(div, 0).innerHTML.trim();
}
function formatNode(node, level) {
const indentBefore = new Array(level++ + 1).join(' ');
const indentAfter = new Array(level - 1).join(' ');
let textNode;
for (let i = 0; i < node.children.length; i++) {
textNode = document.createTextNode('\n' + indentBefore);
node.insertBefore(textNode, node.children[i]);
formatNode(node.children[i], level);
if (node.lastElementChild === node.children[i]) {
textNode = document.createTextNode('\n' + indentAfter);
node.appendChild(textNode);
}
}
return node;
}
$(document).bind('keydown', 'ctrl+u', e => {
showDialog();
e.preventDefault();
});
$showDialogButton.click(showDialog);
2017-12-27 07:15:29 +08:00
return {
showDialog
};
})();