2017-11-05 07:38:50 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-11-05 05:54:27 +08:00
|
|
|
const noteEditor = (function() {
|
2018-02-15 12:31:20 +08:00
|
|
|
const $noteTitle = $("#note-title");
|
|
|
|
|
|
|
|
const $noteDetail = $('#note-detail');
|
|
|
|
const $noteDetailCode = $('#note-detail-code');
|
|
|
|
const $noteDetailRender = $('#note-detail-render');
|
|
|
|
const $noteDetailAttachment = $('#note-detail-attachment');
|
|
|
|
|
|
|
|
const $protectButton = $("#protect-button");
|
|
|
|
const $unprotectButton = $("#unprotect-button");
|
|
|
|
const $noteDetailWrapper = $("#note-detail-wrapper");
|
|
|
|
const $noteIdDisplay = $("#note-id-display");
|
|
|
|
const $attributeList = $("#attribute-list");
|
|
|
|
const $attributeListInner = $("#attribute-list-inner");
|
2018-02-19 10:28:24 +08:00
|
|
|
const $attachmentFileName = $("#attachment-filename");
|
|
|
|
const $attachmentFileType = $("#attachment-filetype");
|
|
|
|
const $attachmentFileSize = $("#attachment-filesize");
|
|
|
|
const $attachmentDownload = $("#attachment-download");
|
2018-02-19 11:19:07 +08:00
|
|
|
const $attachmentOpen = $("#attachment-open");
|
2017-12-25 22:46:11 +08:00
|
|
|
|
2017-12-02 23:37:12 +08:00
|
|
|
let editor = null;
|
2018-01-21 10:56:03 +08:00
|
|
|
let codeEditor = null;
|
2017-11-05 05:54:27 +08:00
|
|
|
|
|
|
|
let currentNote = null;
|
|
|
|
|
|
|
|
let noteChangeDisabled = false;
|
|
|
|
|
|
|
|
let isNoteChanged = false;
|
|
|
|
|
|
|
|
function getCurrentNote() {
|
|
|
|
return currentNote;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentNoteId() {
|
2018-01-29 08:30:14 +08:00
|
|
|
return currentNote ? currentNote.detail.noteId : null;
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function noteChanged() {
|
|
|
|
if (noteChangeDisabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isNoteChanged = true;
|
|
|
|
}
|
|
|
|
|
2017-11-05 23:06:49 +08:00
|
|
|
async function reload() {
|
|
|
|
// no saving here
|
|
|
|
|
|
|
|
await loadNoteToEditor(getCurrentNoteId());
|
|
|
|
}
|
|
|
|
|
|
|
|
async function switchToNote(noteId) {
|
|
|
|
if (getCurrentNoteId() !== noteId) {
|
|
|
|
await saveNoteIfChanged();
|
|
|
|
|
|
|
|
await loadNoteToEditor(noteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 05:54:27 +08:00
|
|
|
async function saveNoteIfChanged() {
|
|
|
|
if (!isNoteChanged) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const note = noteEditor.getCurrentNote();
|
|
|
|
|
|
|
|
updateNoteFromInputs(note);
|
|
|
|
|
|
|
|
await saveNoteToServer(note);
|
2017-11-15 11:50:56 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (note.detail.isProtected) {
|
2017-11-15 11:50:56 +08:00
|
|
|
protected_session.touchProtectedSession();
|
|
|
|
}
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateNoteFromInputs(note) {
|
2018-01-21 23:33:32 +08:00
|
|
|
if (note.detail.type === 'text') {
|
2018-01-29 08:30:14 +08:00
|
|
|
note.detail.content = editor.getData();
|
2018-01-27 08:54:27 +08:00
|
|
|
|
|
|
|
// if content is only tags/whitespace (typically <p> </p>), then just make it empty
|
|
|
|
// this is important when setting new note to code
|
2018-01-29 08:30:14 +08:00
|
|
|
if (jQuery(note.detail.content).text().trim() === '') {
|
|
|
|
note.detail.content = ''
|
2018-01-27 08:54:27 +08:00
|
|
|
}
|
2018-01-21 23:33:32 +08:00
|
|
|
}
|
|
|
|
else if (note.detail.type === 'code') {
|
2018-01-29 08:30:14 +08:00
|
|
|
note.detail.content = codeEditor.getValue();
|
2018-01-21 23:33:32 +08:00
|
|
|
}
|
2018-02-19 10:28:24 +08:00
|
|
|
else if (note.detail.type === 'render' || note.detail.type === 'file') {
|
2018-01-24 12:41:22 +08:00
|
|
|
// nothing
|
|
|
|
}
|
2018-01-21 23:33:32 +08:00
|
|
|
else {
|
|
|
|
throwError("Unrecognized type: " + note.detail.type);
|
|
|
|
}
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
const title = $noteTitle.val();
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
note.detail.title = title;
|
2017-11-23 08:58:56 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
noteTree.setNoteTitle(note.detail.noteId, title);
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function saveNoteToServer(note) {
|
2018-01-29 08:30:14 +08:00
|
|
|
await server.put('notes/' + note.detail.noteId, note);
|
2017-11-05 05:54:27 +08:00
|
|
|
|
|
|
|
isNoteChanged = false;
|
|
|
|
|
2017-11-09 11:33:08 +08:00
|
|
|
showMessage("Saved!");
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
2017-11-15 11:21:56 +08:00
|
|
|
function setNoteBackgroundIfProtected(note) {
|
2018-01-29 08:30:14 +08:00
|
|
|
const isProtected = !!note.detail.isProtected;
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteDetailWrapper.toggleClass("protected", isProtected);
|
|
|
|
$protectButton.toggle(!isProtected);
|
|
|
|
$unprotectButton.toggle(isProtected);
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 08:40:47 +08:00
|
|
|
let isNewNoteCreated = false;
|
|
|
|
|
|
|
|
function newNoteCreated() {
|
|
|
|
isNewNoteCreated = true;
|
|
|
|
}
|
|
|
|
|
2018-02-20 11:02:03 +08:00
|
|
|
async function setContent(content) {
|
2018-02-13 12:53:00 +08:00
|
|
|
if (currentNote.detail.type === 'text') {
|
2018-02-20 11:02:03 +08:00
|
|
|
if (!editor) {
|
|
|
|
await requireLibrary(CKEDITOR);
|
|
|
|
|
|
|
|
editor = await BalloonEditor.create($noteDetail[0], {});
|
|
|
|
|
|
|
|
editor.document.on('change', noteChanged);
|
|
|
|
}
|
|
|
|
|
2018-02-13 12:53:00 +08:00
|
|
|
// temporary workaround for https://github.com/ckeditor/ckeditor5-enter/issues/49
|
|
|
|
editor.setData(content ? content : "<p></p>");
|
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteDetail.show();
|
2018-02-13 12:53:00 +08:00
|
|
|
}
|
|
|
|
else if (currentNote.detail.type === 'code') {
|
2018-02-20 11:02:03 +08:00
|
|
|
if (!codeEditor) {
|
|
|
|
await requireLibrary(CODE_MIRROR);
|
|
|
|
|
|
|
|
CodeMirror.keyMap.default["Shift-Tab"] = "indentLess";
|
|
|
|
CodeMirror.keyMap.default["Tab"] = "indentMore";
|
|
|
|
|
|
|
|
CodeMirror.modeURL = 'libraries/codemirror/mode/%N/%N.js';
|
|
|
|
|
|
|
|
codeEditor = CodeMirror($("#note-detail-code")[0], {
|
|
|
|
value: "",
|
|
|
|
viewportMargin: Infinity,
|
|
|
|
indentUnit: 4,
|
|
|
|
matchBrackets: true,
|
|
|
|
matchTags: { bothTags: true },
|
2018-02-21 12:24:55 +08:00
|
|
|
highlightSelectionMatches: { showToken: /\w/, annotateScrollbar: false },
|
|
|
|
lint: true,
|
|
|
|
gutters: ["CodeMirror-lint-markers"],
|
|
|
|
lineNumbers: true
|
2018-02-20 11:02:03 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
codeEditor.on('change', noteChanged);
|
|
|
|
}
|
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteDetailCode.show();
|
2018-02-13 12:53:00 +08:00
|
|
|
|
|
|
|
// this needs to happen after the element is shown, otherwise the editor won't be refresheds
|
|
|
|
codeEditor.setValue(content);
|
|
|
|
|
|
|
|
const info = CodeMirror.findModeByMIME(currentNote.detail.mime);
|
|
|
|
|
|
|
|
if (info) {
|
|
|
|
codeEditor.setOption("mode", info.mime);
|
|
|
|
CodeMirror.autoLoadMode(codeEditor, info.mode);
|
|
|
|
}
|
2018-02-25 03:42:52 +08:00
|
|
|
|
|
|
|
codeEditor.refresh();
|
2018-02-13 12:53:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 05:54:27 +08:00
|
|
|
async function loadNoteToEditor(noteId) {
|
2017-12-11 01:56:59 +08:00
|
|
|
currentNote = await loadNote(noteId);
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2017-11-24 08:40:47 +08:00
|
|
|
if (isNewNoteCreated) {
|
|
|
|
isNewNoteCreated = false;
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteTitle.focus().select();
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteIdDisplay.html(noteId);
|
2017-12-25 22:46:11 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await protected_session.ensureProtectedSession(currentNote.detail.isProtected, false);
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (currentNote.detail.isProtected) {
|
2017-11-15 11:50:56 +08:00
|
|
|
protected_session.touchProtectedSession();
|
|
|
|
}
|
|
|
|
|
2017-11-15 13:10:11 +08:00
|
|
|
// this might be important if we focused on protected note when not in protected note and we got a dialog
|
|
|
|
// to login, but we chose instead to come to another node - at that point the dialog is still visible and this will close it.
|
|
|
|
protected_session.ensureDialogIsClosed();
|
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteDetailWrapper.show();
|
2017-11-05 05:54:27 +08:00
|
|
|
|
|
|
|
noteChangeDisabled = true;
|
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteTitle.val(currentNote.detail.title);
|
2017-11-30 10:13:12 +08:00
|
|
|
|
2018-01-22 12:36:09 +08:00
|
|
|
noteType.setNoteType(currentNote.detail.type);
|
|
|
|
noteType.setNoteMime(currentNote.detail.mime);
|
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteDetail.hide();
|
|
|
|
$noteDetailCode.hide();
|
|
|
|
$noteDetailRender.html('').hide();
|
|
|
|
$noteDetailAttachment.hide();
|
|
|
|
|
2018-02-13 12:53:00 +08:00
|
|
|
if (currentNote.detail.type === 'render') {
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteDetailRender.show();
|
2018-01-24 12:41:22 +08:00
|
|
|
|
|
|
|
const subTree = await server.get('script/subtree/' + getCurrentNoteId());
|
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteDetailRender.html(subTree);
|
|
|
|
}
|
|
|
|
else if (currentNote.detail.type === 'file') {
|
|
|
|
$noteDetailAttachment.show();
|
2018-02-19 10:28:24 +08:00
|
|
|
|
|
|
|
$attachmentFileName.text(currentNote.attributes.original_file_name);
|
|
|
|
$attachmentFileSize.text(currentNote.attributes.file_size + " bytes");
|
|
|
|
$attachmentFileType.text(currentNote.detail.mime);
|
2018-01-24 12:41:22 +08:00
|
|
|
}
|
2018-01-21 10:56:03 +08:00
|
|
|
else {
|
2018-02-20 11:02:03 +08:00
|
|
|
await setContent(currentNote.detail.content);
|
2018-01-21 10:56:03 +08:00
|
|
|
}
|
2017-11-05 05:54:27 +08:00
|
|
|
|
|
|
|
noteChangeDisabled = false;
|
|
|
|
|
2017-11-15 11:21:56 +08:00
|
|
|
setNoteBackgroundIfProtected(currentNote);
|
2017-12-25 22:30:37 +08:00
|
|
|
noteTree.setNoteTreeBackgroundBasedOnProtectedStatus(noteId);
|
2017-11-07 11:21:11 +08:00
|
|
|
|
2017-12-29 09:13:54 +08:00
|
|
|
// after loading new note make sure editor is scrolled to the top
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteDetailWrapper.scrollTop(0);
|
2018-02-05 09:23:30 +08:00
|
|
|
|
|
|
|
loadAttributeList();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadAttributeList() {
|
|
|
|
const noteId = getCurrentNoteId();
|
|
|
|
|
|
|
|
const attributes = await server.get('notes/' + noteId + '/attributes');
|
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$attributeListInner.html('');
|
2018-02-05 09:23:30 +08:00
|
|
|
|
|
|
|
if (attributes.length > 0) {
|
|
|
|
for (const attr of attributes) {
|
2018-02-15 12:31:20 +08:00
|
|
|
$attributeListInner.append(formatAttribute(attr) + " ");
|
2018-02-05 09:23:30 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$attributeList.show();
|
2018-02-05 09:23:30 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-02-15 12:31:20 +08:00
|
|
|
$attributeList.hide();
|
2018-02-05 09:23:30 +08:00
|
|
|
}
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function loadNote(noteId) {
|
2017-11-29 09:52:38 +08:00
|
|
|
return await server.get('notes/' + noteId);
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
2017-12-03 02:54:16 +08:00
|
|
|
function getEditor() {
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
2018-01-23 11:14:03 +08:00
|
|
|
function focus() {
|
|
|
|
const note = getCurrentNote();
|
|
|
|
|
|
|
|
if (note.detail.type === 'text') {
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteDetail.focus();
|
2018-01-23 11:14:03 +08:00
|
|
|
}
|
|
|
|
else if (note.detail.type === 'code') {
|
|
|
|
codeEditor.focus();
|
|
|
|
}
|
2018-02-19 10:28:24 +08:00
|
|
|
else if (note.detail.type === 'render' || note.detail.type === 'file') {
|
2018-01-24 12:41:22 +08:00
|
|
|
// do nothing
|
|
|
|
}
|
2018-01-23 11:14:03 +08:00
|
|
|
else {
|
|
|
|
throwError('Unrecognized type: ' + note.detail.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 09:14:10 +08:00
|
|
|
function getCurrentNoteType() {
|
|
|
|
const currentNote = getCurrentNote();
|
|
|
|
|
|
|
|
return currentNote ? currentNote.detail.type : null;
|
|
|
|
}
|
|
|
|
|
2018-01-26 12:49:03 +08:00
|
|
|
async function executeCurrentNote() {
|
2018-01-24 09:45:34 +08:00
|
|
|
if (getCurrentNoteType() === 'code') {
|
2018-01-26 12:22:19 +08:00
|
|
|
// make sure note is saved so we load latest changes
|
|
|
|
await saveNoteIfChanged();
|
|
|
|
|
2018-01-26 12:49:03 +08:00
|
|
|
const script = await server.get('script/subtree/' + getCurrentNoteId());
|
2018-01-24 09:45:34 +08:00
|
|
|
|
2018-01-26 12:49:03 +08:00
|
|
|
executeScript(script);
|
2018-01-24 09:45:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 23:55:21 +08:00
|
|
|
$attachmentDownload.click(() => download(getAttachmentUrl()));
|
2018-02-19 11:19:07 +08:00
|
|
|
|
|
|
|
$attachmentOpen.click(() => {
|
|
|
|
if (isElectron()) {
|
|
|
|
const open = require("open");
|
|
|
|
|
|
|
|
open(getAttachmentUrl());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.location.href = getAttachmentUrl();
|
|
|
|
}
|
2018-02-19 10:28:24 +08:00
|
|
|
});
|
|
|
|
|
2018-02-19 11:19:07 +08:00
|
|
|
function getAttachmentUrl() {
|
|
|
|
// electron needs absolute URL so we extract current host, port, protocol
|
2018-02-25 23:55:21 +08:00
|
|
|
return getHost() + "/api/attachments/download/" + getCurrentNoteId()
|
2018-02-24 11:58:24 +08:00
|
|
|
+ "?protectedSessionId=" + encodeURIComponent(protected_session.getProtectedSessionId());
|
2018-02-19 11:19:07 +08:00
|
|
|
}
|
|
|
|
|
2017-11-05 05:54:27 +08:00
|
|
|
$(document).ready(() => {
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteTitle.on('input', () => {
|
2017-11-30 10:13:12 +08:00
|
|
|
noteChanged();
|
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
const title = $noteTitle.val();
|
2017-11-30 10:13:12 +08:00
|
|
|
|
|
|
|
noteTree.setNoteTitle(getCurrentNoteId(), title);
|
|
|
|
});
|
2017-11-05 05:54:27 +08:00
|
|
|
|
|
|
|
// so that tab jumps from note title (which has tabindex 1)
|
2018-02-15 12:31:20 +08:00
|
|
|
$noteDetail.attr("tabindex", 2);
|
2017-11-05 05:54:27 +08:00
|
|
|
});
|
|
|
|
|
2018-01-26 12:49:03 +08:00
|
|
|
$(document).bind('keydown', "ctrl+return", executeCurrentNote);
|
2018-01-24 09:45:34 +08:00
|
|
|
|
2017-11-05 05:54:27 +08:00
|
|
|
setInterval(saveNoteIfChanged, 5000);
|
|
|
|
|
|
|
|
return {
|
2017-11-05 23:06:49 +08:00
|
|
|
reload,
|
|
|
|
switchToNote,
|
2017-11-05 05:54:27 +08:00
|
|
|
saveNoteIfChanged,
|
|
|
|
updateNoteFromInputs,
|
|
|
|
saveNoteToServer,
|
2017-11-15 11:21:56 +08:00
|
|
|
setNoteBackgroundIfProtected,
|
2017-11-05 05:54:27 +08:00
|
|
|
loadNote,
|
|
|
|
getCurrentNote,
|
2018-01-24 09:14:10 +08:00
|
|
|
getCurrentNoteType,
|
2017-11-05 05:54:27 +08:00
|
|
|
getCurrentNoteId,
|
2017-12-03 02:54:16 +08:00
|
|
|
newNoteCreated,
|
2018-01-23 11:14:03 +08:00
|
|
|
getEditor,
|
2018-01-24 09:45:34 +08:00
|
|
|
focus,
|
2018-02-05 09:23:30 +08:00
|
|
|
executeCurrentNote,
|
2018-02-13 12:53:00 +08:00
|
|
|
loadAttributeList,
|
|
|
|
setContent
|
2017-11-05 05:54:27 +08:00
|
|
|
};
|
|
|
|
})();
|