mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-12 08:54:00 +02:00
605 lines
26 KiB
HTML
605 lines
26 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<!--
|
|
|
|
!!! WARNING !!!
|
|
|
|
This file was auto-generated from readme/api/tutorials/toc_plugin.md and any manual change
|
|
made to it will be overwritten. To make a change to this file please modify
|
|
the source Markdown file:
|
|
|
|
https://github.com/laurent22/joplin/blob/dev/readme/api/tutorials/toc_plugin.md
|
|
|
|
-->
|
|
|
|
<head>
|
|
<!-- Donate button A/B testing -->
|
|
<script async src="https://www.googleoptimize.com/optimize.js?id=OPT-PW3ZPK3"></script> <meta
|
|
charset="utf-8"
|
|
http-equiv="X-UA-Compatible"
|
|
content="IE=edge,chrome=1"
|
|
/>
|
|
<link rel="icon" href="/images/favicon.png" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<meta name="theme-color" content="#000000" />
|
|
<meta name="description" content="Joplin, the open source note-taking application" />
|
|
<link
|
|
rel="stylesheet"
|
|
href="/css/bootstrap5.0.2.min.css"
|
|
as="style"
|
|
/>
|
|
<link rel="stylesheet" href="/css/fontawesome-all.min.css?t=1627922646989">
|
|
<link
|
|
href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700&display=swap"
|
|
rel="stylesheet"
|
|
as="style"
|
|
media="all"
|
|
onload="this.media='all'; this.onload = null"
|
|
/>
|
|
<link rel="stylesheet" href="/css/site.css?t=1627922646989" as="style" />
|
|
<title>Creating a table of content plugin | Joplin</title>
|
|
|
|
<script
|
|
src="/js/jquery-3.6.0.min.js"
|
|
rel="preload"
|
|
as="script"
|
|
></script>
|
|
</head>
|
|
<body class="website-env-prod">
|
|
<div class="container-fluid generic-template -page" id="main-container">
|
|
|
|
<div class=" navbar-main white-bg" id="nav-section">
|
|
<div class="container">
|
|
<div class="row">
|
|
<div class="col-3">
|
|
<a href="/">
|
|
<img
|
|
src="/images/logo-text-blue.svg"
|
|
alt=""
|
|
id="top-logo"
|
|
width="180"
|
|
/>
|
|
</a>
|
|
</div>
|
|
<div class="col-9 text-right d-none d-md-block">
|
|
<a href="/help/" class="fw500">Help</a>
|
|
<a href="https://discourse.joplinapp.org/" class="fw500">Forum</a>
|
|
<a href="/plans/" class="button-link btn-trans plans-button">Joplin Cloud</a><a class="button-link btn-blue sponsor-button" href="/donate">
|
|
<i class="fas fa-heart heart-full"></i><i class="far fa-heart heart-line"></i> Support us
|
|
</a> </div>
|
|
<div class="col-9 text-right d-block d-md-none">
|
|
<span class="pointer"
|
|
><img
|
|
src="/images/mobile-menu-black-open-icon.png"
|
|
id="open-menu-mobile"
|
|
alt=""
|
|
/></span>
|
|
|
|
|
|
<div id="menu-mobile">
|
|
<div>
|
|
<div class="text-right">
|
|
<img
|
|
src="/images/close-icon.png"
|
|
alt=""
|
|
class="pointer"
|
|
id="close-menu-mobile"
|
|
/>
|
|
</div>
|
|
|
|
<div class="text-center menu-mobile-top">
|
|
<a href="https://discourse.joplinapp.org/" class="fw500 mobile-menu-link">Forum</a>
|
|
<a href="/help/" class="fw500 mobile-menu-link">Help</a>
|
|
</div>
|
|
|
|
<div class="menu-mobile-buttons">
|
|
<a href="/plans/" class="button-link btn-trans plans-button">Joplin Cloud</a><a class="button-link btn-blue sponsor-button" href="/donate">
|
|
<i class="fas fa-heart heart-full"></i><i class="far fa-heart heart-line"></i> Support us
|
|
</a> </div>
|
|
</div>
|
|
|
|
<div id="toc-mobile"><div><ul>
|
|
<li>
|
|
<p>Applications</p>
|
|
<ul>
|
|
<li><a href="/desktop/">Desktop application</a></li>
|
|
<li><a href="/mobile/">Mobile applications</a></li>
|
|
<li><a href="/terminal/">Terminal application</a></li>
|
|
<li><a href="/clipper/">Web Clipper</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>Support</p>
|
|
<ul>
|
|
<li><a href="https://discourse.joplinapp.org">Joplin Forum</a></li>
|
|
<li><a href="/markdown/">Markdown Guide</a></li>
|
|
<li><a href="/e2ee/">How to enable end-to-end encryption</a></li>
|
|
<li><a href="/conflict/">What is a conflict?</a></li>
|
|
<li><a href="/debugging/">How to enable debug mode</a></li>
|
|
<li><a href="/rich_text_editor/">About the Rich Text editor limitations</a></li>
|
|
<li><a href="/faq/">FAQ</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>Joplin API - Get Started</p>
|
|
<ul>
|
|
<li><a href="/api/overview/">Joplin API Overview</a></li>
|
|
<li><a href="/api/get_started/plugins/">Plugin development</a></li>
|
|
<li><a href="/api/tutorials/toc_plugin/">Plugin tutorial</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>Joplin API - References</p>
|
|
<ul>
|
|
<li><a href="https://joplinapp.org/api/references/plugin_api/classes/joplin.html">Plugin API</a></li>
|
|
<li><a href="/api/references/rest_api/">Data API</a></li>
|
|
<li><a href="/api/references/plugin_manifest/">Plugin manifest</a></li>
|
|
<li><a href="/api/references/plugin_loading_rules/">Plugin loading rules</a></li>
|
|
<li><a href="/api/references/plugin_theming/">Plugin theming</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>Development</p>
|
|
<ul>
|
|
<li><a href="https://github.com/laurent22/joplin/blob/dev/BUILD.md">How to build the apps</a></li>
|
|
<li><a href="/spec/e2ee/">End-to-end encryption spec</a></li>
|
|
<li><a href="/spec/history/">Note History spec</a></li>
|
|
<li><a href="/spec/sync_lock/">Sync Lock spec</a></li>
|
|
<li><a href="/spec/plugins/">Plugin Architecture spec</a></li>
|
|
<li><a href="/spec/search_sorting/">Search Sorting spec</a></li>
|
|
<li><a href="/spec/server_file_url_format/">Server: File URL Format</a></li>
|
|
<li><a href="/spec/server_delta_sync/">Server: Delta Sync</a></li>
|
|
<li><a href="/spec/server_sharing/">Server: Sharing</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>Google Summer of Code 2021</p>
|
|
<ul>
|
|
<li><a href="/gsoc2021/index/">Google Summer of Code 2021</a></li>
|
|
<li><a href="/gsoc2021/pull_request_guidelines/">How to submit a GSoC pull request</a></li>
|
|
<li><a href="/gsoc2021/ideas/">Project Ideas</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>About</p>
|
|
<ul>
|
|
<li><a href="/changelog/">Changelog (Desktop App)</a></li>
|
|
<li><a href="/changelog_cli/">Changelog (CLI App)</a></li>
|
|
<li><a href="/changelog_server/">Changelog (Server)</a></li>
|
|
<li><a href="/stats/">Stats</a></li>
|
|
<li><a href="/donate/">Donate</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div></div>
|
|
|
|
<div>
|
|
<p class="light-blue mobile-menu-link-bottom text-center">
|
|
Copyright © 2016-2021 Laurent Cozic
|
|
<br/>
|
|
<a href="/privacy/" class="fw500">Privacy Policy</a>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="page-container page-toc_plugin">
|
|
<div class="container">
|
|
<div class="row content-wrapper">
|
|
<div id="toc"><div><ul>
|
|
<li>
|
|
<p>Applications</p>
|
|
<ul>
|
|
<li><a href="/desktop/">Desktop application</a></li>
|
|
<li><a href="/mobile/">Mobile applications</a></li>
|
|
<li><a href="/terminal/">Terminal application</a></li>
|
|
<li><a href="/clipper/">Web Clipper</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>Support</p>
|
|
<ul>
|
|
<li><a href="https://discourse.joplinapp.org">Joplin Forum</a></li>
|
|
<li><a href="/markdown/">Markdown Guide</a></li>
|
|
<li><a href="/e2ee/">How to enable end-to-end encryption</a></li>
|
|
<li><a href="/conflict/">What is a conflict?</a></li>
|
|
<li><a href="/debugging/">How to enable debug mode</a></li>
|
|
<li><a href="/rich_text_editor/">About the Rich Text editor limitations</a></li>
|
|
<li><a href="/faq/">FAQ</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>Joplin API - Get Started</p>
|
|
<ul>
|
|
<li><a href="/api/overview/">Joplin API Overview</a></li>
|
|
<li><a href="/api/get_started/plugins/">Plugin development</a></li>
|
|
<li><a href="/api/tutorials/toc_plugin/">Plugin tutorial</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>Joplin API - References</p>
|
|
<ul>
|
|
<li><a href="https://joplinapp.org/api/references/plugin_api/classes/joplin.html">Plugin API</a></li>
|
|
<li><a href="/api/references/rest_api/">Data API</a></li>
|
|
<li><a href="/api/references/plugin_manifest/">Plugin manifest</a></li>
|
|
<li><a href="/api/references/plugin_loading_rules/">Plugin loading rules</a></li>
|
|
<li><a href="/api/references/plugin_theming/">Plugin theming</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>Development</p>
|
|
<ul>
|
|
<li><a href="https://github.com/laurent22/joplin/blob/dev/BUILD.md">How to build the apps</a></li>
|
|
<li><a href="/spec/e2ee/">End-to-end encryption spec</a></li>
|
|
<li><a href="/spec/history/">Note History spec</a></li>
|
|
<li><a href="/spec/sync_lock/">Sync Lock spec</a></li>
|
|
<li><a href="/spec/plugins/">Plugin Architecture spec</a></li>
|
|
<li><a href="/spec/search_sorting/">Search Sorting spec</a></li>
|
|
<li><a href="/spec/server_file_url_format/">Server: File URL Format</a></li>
|
|
<li><a href="/spec/server_delta_sync/">Server: Delta Sync</a></li>
|
|
<li><a href="/spec/server_sharing/">Server: Sharing</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>Google Summer of Code 2021</p>
|
|
<ul>
|
|
<li><a href="/gsoc2021/index/">Google Summer of Code 2021</a></li>
|
|
<li><a href="/gsoc2021/pull_request_guidelines/">How to submit a GSoC pull request</a></li>
|
|
<li><a href="/gsoc2021/ideas/">Project Ideas</a></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<p>About</p>
|
|
<ul>
|
|
<li><a href="/changelog/">Changelog (Desktop App)</a></li>
|
|
<li><a href="/changelog_cli/">Changelog (CLI App)</a></li>
|
|
<li><a href="/changelog_server/">Changelog (Server)</a></li>
|
|
<li><a href="/stats/">Stats</a></li>
|
|
<li><a href="/donate/">Donate</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div></div>
|
|
|
|
<div class="main-content">
|
|
<div class="alert alert-danger alert-env-dev" role="alert">
|
|
Running in prod mode!
|
|
</div>
|
|
<div class="donate-links">
|
|
<p><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=E8JMYD2LQ8MMA&lc=GB&item_name=Joplin+Development&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"><img src="https://joplinapp.org/images/badges/Donate-PayPal-green.svg" alt="Donate using PayPal"></a> <a href="https://github.com/sponsors/laurent22/"><img src="https://joplinapp.org/images/badges/GitHub-Badge.svg" alt="Sponsor on GitHub"></a> <a href="https://www.patreon.com/joplin"><img src="https://joplinapp.org/images/badges/Patreon-Badge.svg" alt="Become a patron"></a> <a href="https://joplinapp.org/donate/#donations"><img src="https://joplinapp.org/images/badges/Donate-IBAN.svg" alt="Donate using IBAN"></a></p>
|
|
</div>
|
|
<h1>Creating a table of content plugin<a name="creating-a-table-of-content-plugin" href="#creating-a-table-of-content-plugin" class="heading-anchor">🔗</a></h1>
|
|
<p>This tutorial will guide you through the steps to create a table of content plugin for Joplin. It will display a view next to the current note that will contain links to the sections of a note. It will be possible to click on one of the header to jump to the relevant section.</p>
|
|
<p>Through this tutorial you will learn about several aspect of the Joplin API, including:</p>
|
|
<ul>
|
|
<li>The plugin API</li>
|
|
<li>How to create a webview</li>
|
|
<li>How to listen to changes in the user interface</li>
|
|
</ul>
|
|
<h2>Setting up your environment<a name="setting-up-your-environment" href="#setting-up-your-environment" class="heading-anchor">🔗</a></h2>
|
|
<p>Before getting any further, make sure your environment is setup correctly as described in the <a href="/api/get_started/plugins/">Get Started guide</a>.</p>
|
|
<h2>Registering the plugin<a name="registering-the-plugin" href="#registering-the-plugin" class="heading-anchor">🔗</a></h2>
|
|
<p>All plugins must <a href="https://joplinapp.org/api/references/plugin_api/classes/joplinplugins.html">register themselves</a> and declare what events they can handle. To do so, open <code>src/index.ts</code> and register the plugin as below. We'll also need to run some initialisation code when the plugin starts, so add the <code>onStart()</code> event handler too:</p>
|
|
<pre><code class="language-typescript">// Import the Joplin API
|
|
import joplin from 'api';
|
|
|
|
// Register the plugin
|
|
joplin.plugins.register({
|
|
|
|
// Run initialisation code in the onStart event handler
|
|
// Note that due to the plugin multi-process architecture, you should
|
|
// always assume that all function calls and event handlers are async.
|
|
onStart: async function() {
|
|
console.info('TOC plugin started!');
|
|
},
|
|
|
|
});
|
|
</code></pre>
|
|
<p>If you now build the plugin and try to run it in Joplin, you should see the message <code>TOC plugin started!</code> in the dev console.</p>
|
|
<h2>Getting the current note<a name="getting-the-current-note" href="#getting-the-current-note" class="heading-anchor">🔗</a></h2>
|
|
<p>In order to create the table of content, you will need to access the content of the currently selected note, and you will need to refresh the TOC every time the note changes. All this can be done using the <a href="https://joplinapp.org/api/references/plugin_api/classes/joplinworkspace.html">workspace API</a>, which provides information about the active content being edited.</p>
|
|
<p>So within the <code>onStart()</code> event handler, add the following:</p>
|
|
<pre><code class="language-typescript">joplin.plugins.register({
|
|
|
|
onStart: async function() {
|
|
|
|
// Later, this is where you'll want to update the TOC
|
|
async function updateTocView() {
|
|
// Get the current note from the workspace.
|
|
const note = await joplin.workspace.selectedNote();
|
|
|
|
// Keep in mind that it can be `null` if nothing is currently selected!
|
|
if (note) {
|
|
console.info('Note content has changed! New note is:', note);
|
|
} else {
|
|
console.info('No note is selected');
|
|
}
|
|
}
|
|
|
|
// This event will be triggered when the user selects a different note
|
|
await joplin.workspace.onNoteSelectionChange(() => {
|
|
updateTocView();
|
|
});
|
|
|
|
// This event will be triggered when the content of the note changes
|
|
// as you also want to update the TOC in this case.
|
|
await joplin.workspace.onNoteChange(() => {
|
|
updateTocView();
|
|
});
|
|
|
|
// Also update the TOC when the plugin starts
|
|
updateTocView();
|
|
},
|
|
|
|
});
|
|
</code></pre>
|
|
<p>Try the above and you should see in the console the event handler being called every time a new note is opened, or whenever the note content changes.</p>
|
|
<h2>Getting the note sections and slugs<a name="getting-the-note-sections-and-slugs" href="#getting-the-note-sections-and-slugs" class="heading-anchor">🔗</a></h2>
|
|
<p>Now that you have the current note, you'll need to extract the headers from that note in order to build the TOC from it. Since the note content is plain Markdown, there are several ways to do so, such as using a Markdown parser, but for now a quick and dirty solution is to get all the lines that start with any number of <code>#</code> followed by a space. Any such line should be a header.</p>
|
|
<p>The function below, which you can copy anywhere in your file, will use this method and return an array of headers, with the text and level (H1, H2, etc.) of header:</p>
|
|
<pre><code class="language-typescript">function noteHeaders(noteBody:string) {
|
|
const headers = [];
|
|
const lines = noteBody.split('\n');
|
|
for (const line of lines) {
|
|
const match = line.match(/^(#+)\s(.*)*/);
|
|
if (!match) continue;
|
|
headers.push({
|
|
level: match[1].length,
|
|
text: match[2],
|
|
});
|
|
}
|
|
return headers;
|
|
}
|
|
</code></pre>
|
|
<p>Then call this function from your event handler:</p>
|
|
<pre><code class="language-typescript">joplin.plugins.register({
|
|
|
|
onStart: async function() {
|
|
|
|
async function updateTocView() {
|
|
const note = await joplin.workspace.selectedNote();
|
|
|
|
if (note) {
|
|
const headers = noteHeaders(note.body);
|
|
console.info('The note has the following headers', headers);
|
|
} else {
|
|
console.info('No note is selected');
|
|
}
|
|
}
|
|
|
|
// ...
|
|
},
|
|
|
|
});
|
|
</code></pre>
|
|
<p>Later you will also need a way to generate the slug for each header. A slug is an identifier which is used to link to a particular header. Essentially a header text like "My Header" is converted to "my-header". And if there's already a slug with that name, a number is appended to it. Without going into too much details, you will need the "slug" package to generate this for you, so install it using <code>npm i -s 'git+https://github.com/laurent22/uslug.git#emoji-support'</code> from the root of your plugin directory (Note: you can also install the "uslug" package on its own, but it won't have emoji support).</p>
|
|
<p>Then this is the function you will need for Joplin, so copy it somewhere in your file:</p>
|
|
<pre><code class="language-typescript">const uslug = require('uslug');
|
|
|
|
let slugs = {};
|
|
|
|
function headerSlug(headerText) {
|
|
const s = uslug(headerText);
|
|
let num = slugs[s] ? slugs[s] : 1;
|
|
const output = [s];
|
|
if (num > 1) output.push(num);
|
|
slugs[s] = num + 1;
|
|
return output.join('-');
|
|
}
|
|
</code></pre>
|
|
<p>And you will need a utility function to escape HTML. There are many packages to do this but for now you can simply use this:</p>
|
|
<pre><code class="language-typescript">// From https://stackoverflow.com/a/6234804/561309
|
|
function escapeHtml(unsafe:string) {
|
|
return unsafe
|
|
.replace(/&/g, "&amp;")
|
|
.replace(/</g, "&lt;")
|
|
.replace(/>/g, "&gt;")
|
|
.replace(/"/g, "&quot;")
|
|
.replace(/'/g, "&#039;");
|
|
}
|
|
</code></pre>
|
|
<p>Again try to run the plugin and if you select a note with multiple headers, you should see the header list in the console.</p>
|
|
<h2>Creating a webview<a name="creating-a-webview" href="#creating-a-webview" class="heading-anchor">🔗</a></h2>
|
|
<p>In order to display the TOC in Joplin, you will need a <a href="https://joplinapp.org/api/references/plugin_api/classes/joplinviewspanels.html">webview panel</a>. Panels are a simple way to add custom content to the UI using HTML/CSS and JavaScript. First you would create the panel object and get back a view handler. Using this handler, you can set various properties such as the HTML content.</p>
|
|
<p>Here's how it could be done:</p>
|
|
<pre><code class="language-typescript">joplin.plugins.register({
|
|
|
|
onStart: async function() {
|
|
// Create the panel object
|
|
const panel = await joplin.views.panels.create('panel_1');
|
|
|
|
// Set some initial content while the TOC is being created
|
|
await joplin.views.panels.setHtml(panel, 'Loading...');
|
|
|
|
async function updateTocView() {
|
|
const note = await joplin.workspace.selectedNote();
|
|
slugs = {}; // Reset the slugs
|
|
|
|
if (note) {
|
|
const headers = noteHeaders(note.body);
|
|
|
|
// First create the HTML for each header:
|
|
const itemHtml = [];
|
|
for (const header of headers) {
|
|
const slug = headerSlug(header.text);
|
|
|
|
// - We indent each header based on header.level.
|
|
//
|
|
// - The slug will be needed later on once we implement clicking on a header.
|
|
// We assign it to a "data" attribute, which can then be easily retrieved from JavaScript
|
|
//
|
|
// - Also make sure you escape the text before inserting it in the HTML to avoid XSS attacks
|
|
// and rendering issues. For this use the `escapeHtml()` function you've added earlier.
|
|
itemHtml.push(`
|
|
<p class="toc-item" style="padding-left:${(header.level - 1) * 15}px">
|
|
<a class="toc-item-link" href="#" data-slug="${escapeHtml(slug)}">
|
|
${escapeHtml(header.text)}
|
|
</a>
|
|
</p>
|
|
`);
|
|
}
|
|
|
|
// Finally, insert all the headers in a container and set the webview HTML:
|
|
await joplin.views.panels.setHtml(panel, `
|
|
<div class="container">
|
|
${itemHtml.join('\n')}
|
|
</div>
|
|
`);
|
|
} else {
|
|
await joplin.views.panels.setHtml(panel, 'Please select a note to view the table of content');
|
|
}
|
|
}
|
|
|
|
// ...
|
|
},
|
|
|
|
});
|
|
</code></pre>
|
|
<p>Now run the plugin again and you should see the TOC dynamically updating as you change notes.</p>
|
|
<h2>Styling the view<a name="styling-the-view" href="#styling-the-view" class="heading-anchor">🔗</a></h2>
|
|
<p>In order to better integrate the TOC to Joplin, you might want to style it using CSS. To do so, first add a <code>webview.css</code> file next to <code>index.ts</code>, then you will need to let Joplin know about this file. This is done using the <code>addScript()</code> function (which is also used to add JavaScript files as we'll see later), like so:</p>
|
|
<pre><code class="language-typescript">const panel = await joplin.views.panels.create('panel_1');
|
|
// Add the CSS file to the view, right after it has been created:
|
|
await joplin.views.panels.addScript(panel, './webview.css');
|
|
</code></pre>
|
|
<p>This file is just a plain CSS file you can use to style your view. Additionally, you can access from there a number of theme variables, which you can use to better integrate the view to the UI. For example, using these variables you can use a dark background in dark mode, and a light one in light mode.</p>
|
|
<p>The CSS file below would give the view the correct font color and family, and the right background colour:</p>
|
|
<pre><code class="language-css">/* In webview.css */
|
|
|
|
.container {
|
|
background-color: var(--joplin-background-color);
|
|
color: var(--joplin-color);
|
|
font-size: var(--joplin-font-size);
|
|
font-family: var(--joplin-font-family);
|
|
}
|
|
|
|
.toc-item a {
|
|
color: var(--joplin-color);
|
|
text-decoration: none;
|
|
}
|
|
</code></pre>
|
|
<p>Try the plugin and the styling should be improved. You may also try to switch to dark or light mode and see the style being updated.</p>
|
|
<h2>Making the webview interactive<a name="making-the-webview-interactive" href="#making-the-webview-interactive" class="heading-anchor">🔗</a></h2>
|
|
<p>The next step is to make the TOC interactive so that when the user clicks on a link, the note is scrolled to right header. This can be done using an external JavaScript file that will handle the click events. As for the CSS file, create a <code>webview.js</code> file next to <code>index.ts</code>, then add the script to the webview:</p>
|
|
<pre><code class="language-typescript">// In index.ts
|
|
const panel = await joplin.views.panels.create('panel_1');
|
|
await joplin.views.panels.addScript(panel, './webview.css');
|
|
await joplin.views.panels.addScript(panel, './webview.js'); // Add the JS file
|
|
</code></pre>
|
|
<p>To check that everything's working, let's create a simple event handler that display the header slug when clicked:</p>
|
|
<pre><code class="language-javascript">// In webview.js
|
|
|
|
// There are many ways to listen to click events, you can even use
|
|
// something like jQuery or React. This is how it can be done using
|
|
// plain JavaScript:
|
|
document.addEventListener('click', event => {
|
|
const element = event.target;
|
|
// If a TOC header has been clicked:
|
|
if (element.className === 'toc-item-link') {
|
|
// Get the slug and display it:
|
|
const slug = element.dataset.slug;
|
|
console.info('Clicked header slug: ' + slug);
|
|
}
|
|
});
|
|
</code></pre>
|
|
<p>If everything works well, you should now see the slug in the console whenever you click on a header link. The next step will be to use that slug to scroll to the right header.</p>
|
|
<h2>Passing messages between the webview and the plugin<a name="passing-messages-between-the-webview-and-the-plugin" href="#passing-messages-between-the-webview-and-the-plugin" class="heading-anchor">🔗</a></h2>
|
|
<p>For security reason, webviews run within their own sandbox (iframe) and thus do not have access to the Joplin API. You can however send messages to and from the webview to the plugin, and you can call the Joplin API from the plugin.</p>
|
|
<p>From within a webview, you have access to the webviewApi object, which among others has a <code>postMessage()</code> function you can use to send a message to the plugin. Let's use this to post the slug info to the plugin:</p>
|
|
<p>Change <code>webview.js</code> like so:</p>
|
|
<pre><code class="language-javascript">document.addEventListener('click', event => {
|
|
const element = event.target;
|
|
if (element.className === 'toc-item-link') {
|
|
// Post the message and slug info back to the plugin:
|
|
webviewApi.postMessage({
|
|
name: 'scrollToHash',
|
|
hash: element.dataset.slug,
|
|
});
|
|
}
|
|
});
|
|
</code></pre>
|
|
<p>Then from the plugin, in <code>src/index.ts</code>, you can listen to this message using the <code>onMessage()</code> handler. Then from this handler, you can call the <code>scrollToHash</code> command and pass it the slug (or hash).</p>
|
|
<pre><code class="language-typescript">joplin.plugins.register({
|
|
onStart: async function() {
|
|
const panel = await joplin.views.panels.create('panel_1');
|
|
|
|
// ...
|
|
|
|
await joplin.views.panels.onMessage(panel, (message) => {
|
|
if (message.name === 'scrollToHash') {
|
|
// As the name says, the scrollToHash command makes the note scroll
|
|
// to the provided hash.
|
|
joplin.commands.execute('scrollToHash', message.hash)
|
|
}
|
|
});
|
|
|
|
// ...
|
|
}
|
|
|
|
// ...
|
|
</code></pre>
|
|
<p>And that's it! If you run this code you should now have a fully functional TOC. The full source code is available there:</p>
|
|
<p><a href="https://github.com/laurent22/joplin/tree/dev/packages/app-cli/tests/support/plugins/toc/">https://github.com/laurent22/joplin/tree/dev/packages/app-cli/tests/support/plugins/toc/</a></p>
|
|
<p>Various improvements can be made such as improving the styling, making the header collapsible, etc. but that tutorial should provide the basic building blocks to do so. You might also want to check the <a href="https://joplinapp.org/api/references/plugin_api/classes/joplin.html">plugin API</a> for further information or head to the <a href="https://discourse.joplinapp.org/c/development/6">development forum</a> for support.</p>
|
|
|
|
<div class="bottom-links">
|
|
<a href="https://github.com/laurent22/joplin/blob/dev/readme/api/tutorials/toc_plugin.md">
|
|
<i class="fab fa-github"></i> Improve this doc
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<footer class="darkblue-bg">
|
|
<div class="container">
|
|
<div class="row">
|
|
<div class="col-3 d-none d-md-block">
|
|
<img src="/images/logo-text.svg" alt="" width="150" />
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-12">
|
|
<hr />
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-12 col-md-6">
|
|
<img
|
|
src="/images/logo-text.svg"
|
|
width="120"
|
|
class="img-center d-block d-md-none"
|
|
alt=""
|
|
/>
|
|
<br class="d-block d-md-none" />
|
|
<p class="text-center-sm">Copyright © 2016-2021 Laurent Cozic</p>
|
|
</div>
|
|
<div class="col-12 col-md-6 right-links">
|
|
<p class="text-right text-center-sm">
|
|
<a href="https://github.com/laurent22/joplin/" class="github-link"><i class="fab fa-github"></i> GitHub Repository</a>
|
|
<a href="/privacy/">Privacy Policy</a>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</footer>
|
|
</div>
|
|
|
|
<script src="/js/script.js?t=1627922646989"></script>
|
|
|
|
<script>
|
|
if (window.location.hostname !== 'localhost') {
|
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
|
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
|
|
ga('create', 'UA-103586105-1', 'auto');
|
|
ga('send', 'pageview');
|
|
}
|
|
</script> </body>
|
|
</html>
|