unified/simplified protecting notes & subtree

This commit is contained in:
zadam
2020-02-26 16:37:17 +01:00
parent 3752cf8cba
commit 7bcae9981b
13 changed files with 84 additions and 90 deletions
@@ -305,9 +305,32 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, $contain
this.setupElementTooltip = noteTooltipService.setupElementTooltip;
/**
* @deprecated use protectNote and protectSubtree instead
* @method
*/
this.protectActiveNote = protectedSessionService.protectNoteAndSendToServer;
this.protectActiveNote = async () => {
const activeNote = appContext.tabManager.getActiveTabNote();
await protectedSessionService.protectNote(activeNote.noteId, true, false);
};
/**
* @method
* @param {string} noteId
* @param {boolean} protect - true to protect note, false to unprotect
*/
this.protectNote = async (noteId, protect) => {
await protectedSessionService.protectNote(noteId, protect, false);
};
/**
* @method
* @param {string} noteId
* @param {boolean} protect - true to protect subtree, false to unprotect
*/
this.protectSubTree = async (noteId, protect) => {
await protectedSessionService.protectNote(noteId, protect, true);
};
/**
* Returns date-note for today. If it doesn't exist, it is automatically created.
@@ -68,46 +68,10 @@ async function enterProtectedSessionOnServer(password) {
});
}
async function protectNoteAndSendToServer() {
if (!appContext.tabManager.getActiveTabNote() || appContext.tabManager.getActiveTabNote().isProtected) {
return;
}
async function protectNote(noteId, protect, includingSubtree) {
await enterProtectedSession();
const note = appContext.tabManager.getActiveTabNote();
note.isProtected = true;
await appContext.tabManager.getActiveTabContext().saveNote();
}
async function unprotectNoteAndSendToServer() {
const activeNote = appContext.tabManager.getActiveTabNote();
if (!activeNote.isProtected) {
toastService.showAndLogError(`Note ${activeNote.noteId} is not protected`);
return;
}
if (!protectedSessionHolder.isProtectedSessionAvailable()) {
console.log("Unprotecting notes outside of protected session is not allowed.");
// the reason is that it's not easy to handle even with enterProtectedSession,
// because we would first have to make sure the note is loaded and only then unprotect
// we used to have a bug where we would overwrite the previous note with unprotected content.
return;
}
activeNote.isProtected = false;
await appContext.tabManager.getActiveTabContext().saveNote();
}
async function protectSubtree(noteId, protect) {
await enterProtectedSession();
await server.put('notes/' + noteId + "/protect/" + (protect ? 1 : 0));
await server.put(`notes/${noteId}/protect/${protect ? 1 : 0}?subtree=${includingSubtree ? 1 : 0}`);
}
function makeToast(message, protectingLabel, text) {
@@ -140,10 +104,8 @@ ws.subscribeToMessages(async message => {
});
export default {
protectSubtree,
protectNote,
enterProtectedSession,
leaveProtectedSession,
protectNoteAndSendToServer,
unprotectNoteAndSendToServer,
setupProtectedSession
};
@@ -90,11 +90,12 @@ export default class TabManager extends Component {
await this.tabsUpdate.allowUpdateWithoutChange(async () => {
for (const tab of filteredTabs) {
const tabContext = this.openEmptyTab(tab.tabId);
await tabContext.setNote(tab.notePath);
if (tab.active) {
this.activateTab(tabContext.tabId);
}
await tabContext.setNote(tab.notePath);
}
});
}