submenu choice of template should override child: settings, fixes #3628

This commit is contained in:
zadam 2023-02-27 21:07:32 +01:00
parent b70699ce1c
commit ec8ed65feb

View File

@ -52,12 +52,22 @@ function deriveMime(type, mime) {
} }
function copyChildAttributes(parentNote, childNote) { function copyChildAttributes(parentNote, childNote) {
const hasAlreadyTemplate = childNote.hasRelation('template');
for (const attr of parentNote.getAttributes()) { for (const attr of parentNote.getAttributes()) {
if (attr.name.startsWith("child:")) { if (attr.name.startsWith("child:")) {
const name = attr.name.substr(6);
if (hasAlreadyTemplate && attr.type === 'relation' && name === 'template') {
// if the note already has a template, it means the template was chosen by the user explicitly
// in the menu. In that case we should override the default templates defined in the child: attrs
continue;
}
new BAttribute({ new BAttribute({
noteId: childNote.noteId, noteId: childNote.noteId,
type: attr.type, type: attr.type,
name: attr.name.substr(6), name: name,
value: attr.value, value: attr.value,
position: attr.position, position: attr.position,
isInheritable: attr.isInheritable isInheritable: attr.isInheritable
@ -191,21 +201,18 @@ function createNewNote(params) {
asyncPostProcessContent(note, params.content); asyncPostProcessContent(note, params.content);
copyChildAttributes(parentNote, note);
if (params.templateNoteId) { if (params.templateNoteId) {
if (!becca.getNote(params.templateNoteId)) { if (!becca.getNote(params.templateNoteId)) {
throw new Error(`Template note '${params.templateNoteId}' does not exist.`); throw new Error(`Template note '${params.templateNoteId}' does not exist.`);
} }
// could be already copied from the parent via `child:`, no need to have 2 note.addRelation('template', params.templateNoteId);
if (!note.hasOwnedRelation('template', params.templateNoteId)) {
note.addRelation('template', params.templateNoteId);
}
// no special handling for ~inherit since it doesn't matter if it's assigned with the note creation or later // no special handling for ~inherit since it doesn't matter if it's assigned with the note creation or later
} }
copyChildAttributes(parentNote, note);
triggerNoteTitleChanged(note); triggerNoteTitleChanged(note);
eventService.emit(eventService.ENTITY_CREATED, { eventService.emit(eventService.ENTITY_CREATED, {