mirror of
https://github.com/zadam/trilium.git
synced 2024-12-25 00:34:08 +08:00
Page:
Custom widget
Pages
Advanced showcases
Anonymized database
Apache proxy setup
Archived notes
Attachments
Attribute inheritance
Attributes
Backup
Book note
Bookmarks
Canvas note
Cloning notes
Code notes
Custom request handler
Custom widget
Data directory
Day notes
Default note title
Desktop installation
Docker server installation
Document
ETAPI
Error logs
Events
Evernote import
FAQ
Frontend Basics
Home
Images
Keyboard shortcuts
Kubernetes server installation
Link map
Links
Manual server installation
Markdown
Mobile frontend
Nginx proxy setup
NixOS server installation
Note hoisting
Note map
Note navigation
Note revisions
Note
Onenote
Packaged server installation
Patterns of personal knowledge base
Privacy policy
Promoted attributes
Protected notes
Read only note
Relation map
Release notes v0.48
Saved search
Screenshot tour
Script API
Scripts
Search
Server installation
Sharing
Sorting
Synchronization
TLS configuration
Task manager
Template
Text notes
Theme Gallery
Themes
Tree concepts
Tree manipulation
Troubleshooting
Upgrading Trilium
Web clipper
Weight tracker
Widget Basics
Workspace
No results
5
Custom widget
zadam edited this page 2023-09-25 23:07:11 +02:00
Table of Contents
It's possible to create custom widget in three possible locations where you can display your custom content.
Positions are:
left-pane
center-pane
note-detail-pane
- located withincenter-pane
, but specific to note (split)right-pane
Example - word count widget
Create a code note of type JS frontend and give it a #widget
label.
/*
* This defines a custom widget which displays number of words and characters in a current text note.
* To be activated for a given note, add label 'wordCount' to the note, you can also make it inheritable and thus activate it for the whole subtree.
*
* See it in action in "Books" and its subtree.
*/
const TPL = `<div style="padding: 10px; border-top: 1px solid var(--main-border-color); contain: none;">
<strong>Word count: </strong>
<span class="word-count"></span>
<strong>Character count: </strong>
<span class="character-count"></span>
</div>`;
class WordCountWidget extends api.NoteContextAwareWidget {
static get parentWidget() { return 'center-pane'; }
get position() { return 100; } // higher value means position towards the bottom/right
isEnabled() {
return super.isEnabled()
&& this.note.type === 'text'
&& this.note.hasLabel('wordCount');
}
doRender() {
this.$widget = $(TPL);
this.$wordCount = this.$widget.find('.word-count');
this.$characterCount = this.$widget.find('.character-count');
return this.$widget;
}
async refreshWithNote(note) {
const {content} = await note.getNoteComplement();
const text = $(content).text(); // get plain text only
const counts = this.getCounts(text);
this.$wordCount.text(counts.words);
this.$characterCount.text(counts.characters);
}
getCounts(text) {
const chunks = text
.split(/[\s-+:,/\\]+/)
.filter(chunk => chunk !== '');
let words;
if (chunks.length === 1 && chunks[0] === '') {
words = 0;
}
else {
words = chunks.length;
}
const characters = chunks.join('').length;
return {words, characters};
}
async entitiesReloadedEvent({loadResults}) {
if (loadResults.isNoteContentReloaded(this.noteId)) {
this.refresh();
}
}
}
module.exports = WordCountWidget;
After you make changes it is necessary to restart Trilium so that the layout can be rebuilt.
Example screenshot
On the bottom you can see the resulting widget:
- Screenshot tour
- Basic concepts
- Installation & setup
- Advanced usage
- Developer guides
- FAQ
- Troubleshooting
Back to Overview