refactoring/cleanup

This commit is contained in:
zadam 2019-05-21 20:24:40 +02:00
parent ff5ea8b311
commit 3fe9218ea8
8 changed files with 47 additions and 63 deletions

View File

@ -257,12 +257,6 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, tabConte
*/
this.createNoteLink = linkService.createNoteLink;
/**
* @method
* @returns {string} content of active note (loaded into right pane)
*/
this.getActiveNoteContent = noteDetailService.getActiveNoteContent;
/**
* @method
* @returns {NoteFull} active note (loaded into right pane)

View File

@ -17,24 +17,6 @@ const $savedIndicator = $(".saved-indicator");
let detailLoadedListeners = [];
/** @return {NoteFull} */
function getActiveNote() {
const activeContext = getActiveTabContext();
return activeContext ? activeContext.note : null;
}
function getActiveNoteId() {
const activeNote = getActiveNote();
return activeNote ? activeNote.noteId : null;
}
function getActiveNoteType() {
const activeNote = getActiveNote();
return activeNote ? activeNote.type : null;
}
async function reload() {
// no saving here
@ -67,10 +49,6 @@ async function switchToNote(notePath) {
openTabsChanged();
}
function getActiveNoteContent() {
return getActiveTabContext().getComponent().getContent();
}
function onNoteChange(func) {
return getActiveTabContext().getComponent().onNoteChange(func);
}
@ -115,17 +93,40 @@ function getActiveTabContext() {
return tabContexts.find(tc => tc.tabId === tabId);
}
function isActive(tabContext) {
return tabContext.$tab[0] === tabRow.activateTab;
/** @return {NoteFull} */
function getActiveNote() {
const activeContext = getActiveTabContext();
return activeContext ? activeContext.note : null;
}
async function activateTabContext(tabContext) {
await tabRow.activateTab(tabContext.$tab[0]);
function getActiveNoteId() {
const activeNote = getActiveNote();
return activeNote ? activeNote.noteId : null;
}
/** @returns {TabContext} */
function getTabContext(tabId) {
return tabContexts.find(tc => tc.tabId === tabId);
function getActiveNoteType() {
const activeNote = getActiveNote();
return activeNote ? activeNote.type : null;
}
async function switchToTab(tabId, notePath) {
const tabContext = tabContexts.find(tc => tc.tabId === tabId);
if (!tabContext) {
await loadNoteDetail(notePath, {
newTab: true,
tabId: tabId,
activate: true
});
} else {
await tabContext.activate();
if (notePath && tabContext.notePath !== notePath) {
await loadNoteDetail(notePath);
}
}
}
async function showTab(tabId) {
@ -348,7 +349,13 @@ $tabContentsContainer.on("dragover", e => e.preventDefault());
$tabContentsContainer.on("dragleave", e => e.preventDefault());
$tabContentsContainer.on("drop", e => {
importDialog.uploadFiles(getActiveNoteId(), e.originalEvent.dataTransfer.files, {
const activeNote = getActiveNote();
if (!activeNote) {
return;
}
importDialog.uploadFiles(activeNote.noteId, e.originalEvent.dataTransfer.files, {
safeImport: true,
shrinkImages: true,
textImportedAsText: true,
@ -502,7 +509,6 @@ export default {
loadNote,
loadNoteDetail,
getActiveNote,
getActiveNoteContent,
getActiveNoteType,
getActiveNoteId,
focusOnTitle,
@ -510,12 +516,10 @@ export default {
saveNotesIfChanged,
onNoteChange,
addDetailLoadedListener,
getTabContext,
switchToTab,
getTabContexts,
getActiveTabContext,
getActiveEditor,
isActive,
activateTabContext,
clearOpenTabsTask,
filterTabs,
openEmptyTab,

View File

@ -65,7 +65,7 @@ class NoteDetailImage {
getFileUrl() {
// electron needs absolute URL so we extract current host, port, protocol
return utils.getHost() + "/api/notes/" + noteDetailService.getActiveNoteId() + "/download";
return utils.getHost() + `/api/notes/${this.ctx.note.noteId}/download`;
}
getContent() {}

View File

@ -114,7 +114,7 @@ function NoteTypeContext(ctx) {
}
function confirmChangeIfContent() {
if (!noteDetailService.getActiveNoteContent()) {
if (!self.ctx.getComponent().getContent()) {
return true;
}

View File

@ -70,7 +70,7 @@ async function enterProtectedSessionOnServer(password) {
}
async function protectNoteAndSendToServer() {
if (noteDetailService.getActiveNote().isProtected) {
if (!noteDetailService.getActiveNote() || noteDetailService.getActiveNote().isProtected) {
return;
}

View File

@ -192,13 +192,17 @@ class TabContext {
return this.components[type];
}
async activate() {
await this.tabRow.activateTab(this.$tab[0]);
}
async saveNote() {
if (this.note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
return;
}
this.note.title = this.$noteTitle.val();
this.note.content = noteDetailService.getActiveNoteContent();
this.note.content = this.getComponent().getContent();
// it's important to set the flag back to false immediatelly after retrieving title and content
// otherwise we might overwrite another change (especially async code)

File diff suppressed because one or more lines are too long

View File

@ -791,24 +791,7 @@ $(window).bind('hashchange', async function() {
console.debug(`Switching to ${notePath} on tab ${tabId} because of hash change`);
let tabContext = noteDetailService.getTabContext(tabId);
if (!tabContext) {
noteDetailService.loadNoteDetail(notePath, {
newTab: true,
tabId: tabId,
activate: true
});
}
else {
if (!noteDetailService.isActive(tabContext)) {
noteDetailService.activateTabContext(tabContext);
}
if (notePath && tabContext.notePath !== notePath) {
noteDetailService.loadNoteDetail(notePath);
}
}
noteDetailService.switchToTab(tabId, notePath);
}
});