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

View File

@@ -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.

View File

@@ -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
};

View File

@@ -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);
}
});
}

View File

@@ -151,16 +151,14 @@ export default class NoteDetailWidget extends TabAwareWidget {
}
async getWidgetType() {
const note = this.note;
if (!note) {
if (!this.note) {
return "empty";
}
let type = note.type;
let type = this.note.type;
if (type === 'text' && !this.tabContext.autoBookDisabled
&& note.hasChildren()
&& this.note.hasChildren()
&& utils.isDesktop()) {
const noteComplement = await this.tabContext.getNoteComplement();
@@ -170,7 +168,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
}
}
if (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
if (this.note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
type = 'protected-session';
}

View File

@@ -804,11 +804,11 @@ export default class NoteTreeWidget extends TabAwareWidget {
}
protectSubtreeCommand({node}) {
protectedSessionService.protectSubtree(node.data.noteId, true);
protectedSessionService.protectNote(node.data.noteId, true, true);
}
unprotectSubtreeCommand({node}) {
protectedSessionService.protectSubtree(node.data.noteId, false);
protectedSessionService.protectNote(node.data.noteId, false, true);
}
duplicateNoteCommand({node}) {

View File

@@ -21,10 +21,10 @@ export default class ProtectedNoteSwitchWidget extends TabAwareWidget {
this.$widget = $(TPL);
this.$protectButton = this.$widget.find(".protect-button");
this.$protectButton.on('click', protectedSessionService.protectNoteAndSendToServer);
this.$protectButton.on('click', () => protectedSessionService.protectNote(this.noteId, true, false));
this.$unprotectButton = this.$widget.find(".unprotect-button");
this.$unprotectButton.on('click', protectedSessionService.unprotectNoteAndSendToServer);
this.$unprotectButton.on('click', () => protectedSessionService.protectNote(this.noteId, false, false));
return this.$widget;
}
@@ -33,6 +33,12 @@ export default class ProtectedNoteSwitchWidget extends TabAwareWidget {
this.$protectButton.toggleClass("active", note.isProtected);
this.$protectButton.prop("disabled", note.isProtected);
this.$unprotectButton.toggleClass("active", !note.isProtected);
this.$unprotectButton.prop("disabled", !note.isProtected || !protectedSessionHolder.isProtectedSessionAvailable());
this.$unprotectButton.prop("disabled", !note.isProtected);
}
async entitiesReloadedEvent({loadResults}) {
if (loadResults.isNoteReloaded(this.noteId)) {
this.refresh();
}
}
}

View File

@@ -32,7 +32,7 @@ export default class RenderTypeWidget extends TypeWidget {
const renderNotesFound = await renderService.render(note, this.$noteDetailRenderContent);
console.log("render", this.$noteDetailRenderContent);
console.trace("render");
if (!renderNotesFound) {
this.$noteDetailRenderHelp.show();

View File

@@ -16,10 +16,11 @@ export default class TypeWidget extends TabAwareWidget {
*/
doRefresh(note) {}
refresh() {
const widgetType = this.constructor.getType();
async refresh() {
const thisWidgetType = this.constructor.getType();
const noteWidgetType = await this.noteDetailWidget.getWidgetType();
if (widgetType !== this.noteDetailWidget.type) {
if (thisWidgetType !== noteWidgetType) {
this.toggle(false);
this.cleanup();