Writing a plugin for Bläck Studio
A plugin is a folder with a manifest.json and a script. Bläck Studio finds
plugins in its plugins folder:
| System |
Folder |
| Linux |
~/.local/share/black-studio/plugins/ (or $XDG_DATA_HOME/black-studio/plugins) |
| macOS |
~/Library/Application Support/black-studio/plugins/ |
| Windows |
%APPDATA%\black-studio\plugins\ |
Settings → Plugins opens this folder and turns plugins on and off.
Starting one
The SDK is the npm package @black-studio/plugin. Start a plugin with:
npm create @black-studio/plugin my-plugin # in the window, with React
npm create @black-studio/plugin my-plugin --sandboxed
npm create @black-studio/plugin my-plugin --computer # sandboxed, with files and a program
cd my-plugin
npm install
npm run dev
There are three starting points:
- In the window: a command with a hotkey, and an inspector tab drawn
with React.
--sandboxed: a command, marks in the text, and text in the status
bar, all running in a separate worker.
--computer: a sandboxed plugin with files:write and exec:pandoc.
It saves the scene the writer is in as a Word file. Pandoc makes the file
in the plugin's temporary folder, and the plugin then saves it in a folder
the writer chooses. It remembers that folder for next time. The template
shows how to ask for a folder, run a program, and tell the writer plainly
when the program isn't installed. To run another program, name it in the
manifest and in the code.
npm run dev builds the plugin into the plugins folder every time you save,
and Bläck Studio reloads it as soon as it lands. Turn it on once under
Settings → Plugins. npm run build makes dist/<id>/ to share, and a zip
with its SHA-256 for the plugin list.
A plugin in plain JavaScript needs no build at all. A main.js like this,
next to the manifest, is a complete plugin. A plain script asks for the SDK by
its older name, @black/plugin, which every version of Bläck Studio knows.
The build does the same for you.
var __blackPlugin = class extends __black.require('@black/plugin').Plugin {
onload() {
this.addCommand({ id: 'hello', name: 'Say hello', run: () => this.app.ui.notify('Hello') })
}
}
The manifest
{
"id": "word-of-the-day",
"name": "Word of the day",
"version": "1.0.0",
"description": "A word to use today, every morning.",
"author": "You",
"apiVersion": 1,
"main": "main.js",
"sandboxed": false,
"permissions": ["project:write", "network:api.example.com", "files:read", "exec:pandoc"],
"permissionReasons": {
"network:api.example.com": "fetch today's word from Example (api.example.com)"
},
"category": "writing",
"locales": {
"sv": { "name": "Dagens ord", "description": "Ett ord att använda i dag, varje morgon." }
}
}
-
id: lower-case letters, digits and dashes. It's also the folder name.
-
apiVersion: the plugin API version the plugin needs. It defaults to 1.
Set it to the version that added the newest feature you use:
- 2: the dashboard and the library
- 3: the microphone
- 4:
app.speech
- 5: dictation drafts, pauses and microphone choice
- 6: actions on marks
- 7: book importers
- 8:
permissionReasons and category in the manifest. A plugin that uses
only these two doesn't need version 8, because older versions of the app
ignore them.
- 9: quiet marks
- 10:
app.folder and project:folder, network:ask,
app.net.streamBytes, during, addVersionHistory, project.flush and
project.getProjectData
-
sandboxed: runs the plugin in a separate worker, apart from the
window (see below).
-
locales: the plugin's name and description in other languages, by
language code. Settings → Plugins, the plugin list and the permission
questions show them in the writer's language. You choose the plugin's other
strings (commands, settings, notices) yourself, using this.app.language.
When the writer changes the language, Bläck Studio reloads the plugin, so
onload runs again in the new language.
-
permissions: what the plugin needs beyond reading the book.
project:write lets the plugin change the book.
project:folder lets the plugin read and change every file in the open
project's folder, and nothing outside it (see The project's folder). It
needs "apiVersion": 10.
library:read lets the plugin see the writer's recent books and their
series (app.library), for example to build the welcome screen's
dashboard. It needs "apiVersion": 2.
microphone lets a plugin in the window record, with
navigator.mediaDevices.getUserMedia({ audio: true }). Bläck Studio gives
the page access to the microphone only while a plugin with this
permission is on, and never gives it a camera. It needs
"apiVersion": 3. Release the stream as soon as you're done, so the
system stops showing that it's listening.
With "apiVersion": 4 the plugin also gets app.speech, which turns
what the writer says into text on the device itself. Nothing is sent
anywhere. On a computer this uses Whisper, and Bläck Studio downloads its
model once, after asking the writer. On a phone or tablet it uses the
system's own recogniser, which gives you the words as they come. Call
status(language) first. If it isn't ready, prepare(language) gets
what's missing. Then call listen() and keep listening until stop().
onLevel reports how loud the sound is, so you can show that the plugin
is hearing something. The session can also pause() and resume().
Older versions of Bläck Studio have neither of these, so check that
they exist.
network:<host> lets the plugin reach a host. network:*.<host> also
covers that host's subdomains, and network:* lets the plugin reach
anywhere on the internet. It doesn't cover this computer or the network
it's on (a router, a NAS). The plugin can reach those only by name, for
example network:127.0.0.1.
files:read and files:write let the plugin use files and folders that
the writer chooses for it in the system's own dialog
(app.files.pickFolder, pickFile). It never gets the whole disk.
exec:<program> lets the plugin run that one program. The program is
found by name on the PATH, gets its arguments as you give them, and runs
with no shell. exec:pandoc runs Pandoc and nothing else.
Bläck Studio marks network:* and every exec: permission as reaching
beyond the app, with a warning. A shell, Python or Node gets a stronger
warning.
The writer is asked to allow these permissions when they turn the plugin
on. If a new version asks for more, it stays off until the writer allows
that too.
Running in Bläck Studio's window (without sandboxed) is also a permission
the writer is asked to allow, and it's shown as unrestricted: a plugin there
can do anything the window can.
-
optionalPermissions: permissions the plugin may ask for later with
app.permissions.request. Use it when the writer chooses something that
needs a permission (a service to send to, say), instead of asking when the
plugin is turned on. It takes the same kinds as permissions, and needs
"apiVersion": 5.
-
permissionReasons: why the plugin asks, by permission. Bläck Studio
shows each reason to the writer when it asks them to allow the permission,
with its own description of what the permission allows underneath. Say what
is sent where and what for, so that your text completes the sentence "it
needs to …". It can be one text, or one text per language code:
"permissionReasons": {
"network:api.example.com": {
"en": "fetch today's word from Example (api.example.com)",
"sv": "hämta dagens ord från Example (api.example.com)"
}
}
Each reason belongs to a permission in permissions or
optionalPermissions, and can be up to 300 characters. Bläck Studio reads
it from plugin API 8 on. Older versions leave it out, so your plugin doesn't
need to ask for API 8 to include it.
-
category: where Manage Plugins lists the plugin: writing,
planning, welcome (the welcome screen), import-export or other.
Bläck Studio reads it from plugin API 8 on, and older versions leave it
out, just like permissionReasons.
What a plugin can do
Everything is in index.d.ts. The plugins on Bläck Studio's
list are written this way too, and each shows a part of the API:
| Plugin |
Shows |
filter-words |
sandboxed: marks in the text, settings, a status bar item, events |
markdown-export |
sandboxed: a format in Compile |
name-generator |
in the window: a view, an inspector tab, a menu entry, a TipTap shortcut, changing the book |
pomodoro |
in the window: React in the status bar, a hotkey |
cast-suggest |
sandboxed: suggestions as you type, kept up to date as the navigator changes |
pandoc-export |
sandboxed: running a program in its temporary folder, with formats that follow its settings |
continue-writing, today, book-progress, writing-calendar, series-overview, recent-activity |
in the window: cards on the welcome screen's dashboard, built from the writer's library; starting a sprint |
dictation |
in the window: the microphone, speech turned into text on the device (app.speech), a toolbar button, a hotkey, a secret setting, a request to a service using the writer's own key |
In short:
| Method |
Adds |
addCommand |
a command in the palette (Ctrl+K) and the Plugins menu, with a hotkey if you like |
addView |
a view beside the corkboard and outliner (window only) |
addInspectorTab |
a tab in the inspector, for the items you choose (window only) |
addStatusBarItem |
an item in the status bar, drawn with React or as text you change |
addEditorMenuItem |
an entry in the text's right-click menu |
addBinderMenuItem |
an entry in the navigator's right-click menu |
addExporter |
a format in Compile |
addHighlighter |
marks in the text, found one paragraph at a time and never saved |
registerEditorExtension |
TipTap extensions: marks, nodes, input rules, shortcuts (window only) |
addSidebarPanel |
a tab beside the navigator (window only) |
addToolbarButton |
a button in the editor's toolbar |
addSuggester |
suggestions under the caret after a trigger: @ for characters, # for tags… |
addImporter |
a kind of file for File → Import Documents |
addBookImporter |
a whole book from another app's file or folder, in File → Import and on the welcome screen (API 7) |
addSettings |
settings that Bläck Studio draws for you in Settings → Plugins |
addSettingsPanel |
settings that you draw yourself (window only) |
on |
project-open, project-close, selection-change, doc-change, nodes-change, item-data-change and shared-data-change (API 11) |
loadData, saveData |
your plugin's data, stored on this computer |
You read settings declared with addSettings from this.settings. Bläck
Studio keeps only the values the writer changed. So if a new version of your
plugin has a better default, everyone who hadn't changed that setting gets it.
updateSettings({ key: undefined }) puts a setting back to its default.
this.app gives you the rest, each part described below:
app.project: the book.
app.editor: the selection, comments on it, typing at
the caret, and, in the window, the editor itself.
app.ui: notices and dialogs.
app.net: the internet, limited to the hosts you asked
for.
app.files: files and folders the writer chose.
app.exec: the programs you named.
If your plugin has a styles.css next to its manifest, Bläck Studio loads it
with the plugin.
In the window, react, react/jsx-runtime, @tiptap/core and
@tiptap/pm/* are Bläck Studio's copies. The build leaves them out of your
script. Your interface follows the theme if you use Bläck Studio's classes:
btn, input, insp-section, sb-btn and so on.
The book