add #toc label to control Table of Contents visibility per note, #2985

This commit is contained in:
zadam 2022-07-19 23:56:29 +02:00
parent 8902cb3117
commit f890e9917f
3 changed files with 19 additions and 6 deletions

View File

@ -227,7 +227,9 @@ const ATTR_HELP = {
<li><code>Log for \${now.format('YYYY-MM-DD HH:mm:ss')}</code></li> <li><code>Log for \${now.format('YYYY-MM-DD HH:mm:ss')}</code></li>
</ul> </ul>
See <a href="https://github.com/zadam/trilium/wiki/Default-note-title">wiki with details</a>, API docs for <a href="https://zadam.github.io/trilium/backend_api/Note.html">parentNote</a> and <a href="https://day.js.org/docs/en/display/format">now</a> for details.` See <a href="https://github.com/zadam/trilium/wiki/Default-note-title">wiki with details</a>, API docs for <a href="https://zadam.github.io/trilium/backend_api/Note.html">parentNote</a> and <a href="https://day.js.org/docs/en/display/format">now</a> for details.`,
"template": "This note will appear in the selection of available template when creating new note",
"toc": "<code>#toc</code> or <code>#toc=show</code> will force the Table of Contents to be shown, <code>#toc=hide</code> will force hiding it. If the label doesn't exist, the global setting is observed"
}, },
"relation": { "relation": {
"runOnNoteCreation": "executes when note is created on backend", "runOnNoteCreation": "executes when note is created on backend",

View File

@ -74,9 +74,7 @@ export default class TocWidget extends CollapsibleWidget {
} }
isEnabled() { isEnabled() {
return super.isEnabled() return super.isEnabled() && this.note.type === 'text';
&& this.note.type === 'text'
&& !this.note.hasLabel('noToc');
} }
async doRenderBody() { async doRenderBody() {
@ -85,6 +83,14 @@ export default class TocWidget extends CollapsibleWidget {
} }
async refreshWithNote(note) { async refreshWithNote(note) {
const tocLabel = note.getLabel('toc');
if (tocLabel?.value === 'hide') {
this.toggleInt(false);
this.triggerCommand("reevaluateIsEnabled");
return;
}
let $toc = "", headingCount = 0; let $toc = "", headingCount = 0;
// Check for type text unconditionally in case alwaysShowWidget is set // Check for type text unconditionally in case alwaysShowWidget is set
if (this.note.type === 'text') { if (this.note.type === 'text') {
@ -93,7 +99,11 @@ export default class TocWidget extends CollapsibleWidget {
} }
this.$toc.html($toc); this.$toc.html($toc);
this.toggleInt(headingCount >= options.getInt('minTocHeadings')); this.toggleInt(
["", "show"].includes(tocLabel?.value)
|| headingCount >= options.getInt('minTocHeadings')
);
this.triggerCommand("reevaluateIsEnabled"); this.triggerCommand("reevaluateIsEnabled");
} }
@ -241,7 +251,7 @@ export default class TocWidget extends CollapsibleWidget {
if (loadResults.isNoteContentReloaded(this.noteId)) { if (loadResults.isNoteContentReloaded(this.noteId)) {
await this.refresh(); await this.refresh();
} else if (loadResults.getAttributes().find(attr => attr.type === 'label' } else if (loadResults.getAttributes().find(attr => attr.type === 'label'
&& (attr.name.toLowerCase().includes('readonly') || attr.name === 'noToc') && (attr.name.toLowerCase().includes('readonly') || attr.name === 'toc')
&& attributeService.isAffecting(attr, this.note))) { && attributeService.isAffecting(attr, this.note))) {
await this.refresh(); await this.refresh();

View File

@ -52,6 +52,7 @@ module.exports = [
{ type: 'label', name: 'hideRelations' }, { type: 'label', name: 'hideRelations' },
{ type: 'label', name: 'titleTemplate', isDangerous: true }, { type: 'label', name: 'titleTemplate', isDangerous: true },
{ type: 'label', name: 'template' }, { type: 'label', name: 'template' },
{ type: 'label', name: 'toc' },
// relation names // relation names
{ type: 'relation', name: 'internalLink' }, { type: 'relation', name: 'internalLink' },