@black-studio/plugin 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +954 -2
- package/build.mjs +118 -0
- package/cli.mjs +28 -0
- package/index.d.ts +1021 -0
- package/package.json +18 -4
package/README.md
CHANGED
|
@@ -1,3 +1,955 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Writing a plugin for Bläck Studio
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A plugin is a folder with a `manifest.json` and a script. Bläck Studio finds
|
|
4
|
+
plugins in its plugins folder:
|
|
5
|
+
|
|
6
|
+
| System | Folder |
|
|
7
|
+
| ------- | ---------------------------------------------- |
|
|
8
|
+
| Linux | `~/.local/share/black-studio/plugins/` (or `$XDG_DATA_HOME/black-studio/plugins`) |
|
|
9
|
+
| macOS | `~/Library/Application Support/black-studio/plugins/` |
|
|
10
|
+
| Windows | `%APPDATA%\black-studio\plugins\` |
|
|
11
|
+
|
|
12
|
+
Settings → Plugins opens this folder and turns plugins on and off.
|
|
13
|
+
|
|
14
|
+
## Starting one
|
|
15
|
+
|
|
16
|
+
The SDK is the npm package `@black-studio/plugin`. Start a plugin with:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm create @black-studio/plugin my-plugin # in the window, with React
|
|
20
|
+
npm create @black-studio/plugin my-plugin --sandboxed
|
|
21
|
+
npm create @black-studio/plugin my-plugin --computer # sandboxed, with files and a program
|
|
22
|
+
cd my-plugin
|
|
23
|
+
npm install
|
|
24
|
+
npm run dev
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
There are three starting points:
|
|
28
|
+
- **In the window:** a command with a hotkey, and an inspector tab drawn
|
|
29
|
+
with React.
|
|
30
|
+
- **`--sandboxed`:** a command, marks in the text, and text in the status
|
|
31
|
+
bar, all running in a separate worker.
|
|
32
|
+
- **`--computer`:** a sandboxed plugin with `files:write` and `exec:pandoc`.
|
|
33
|
+
It saves the scene the writer is in as a Word file. Pandoc makes the file
|
|
34
|
+
in the plugin's temporary folder, and the plugin then saves it in a folder
|
|
35
|
+
the writer chooses. It remembers that folder for next time. The template
|
|
36
|
+
shows how to ask for a folder, run a program, and tell the writer plainly
|
|
37
|
+
when the program isn't installed. To run another program, name it in the
|
|
38
|
+
manifest and in the code.
|
|
39
|
+
|
|
40
|
+
`npm run dev` builds the plugin into the plugins folder every time you save,
|
|
41
|
+
and Bläck Studio reloads it as soon as it lands. Turn it on once under
|
|
42
|
+
Settings → Plugins. `npm run build` makes `dist/<id>/` to share, and a zip
|
|
43
|
+
with its SHA-256 for the plugin list.
|
|
44
|
+
|
|
45
|
+
A plugin in plain JavaScript needs no build at all. A `main.js` like this,
|
|
46
|
+
next to the manifest, is a complete plugin. A plain script asks for the SDK by
|
|
47
|
+
its older name, `@black/plugin`, which every version of Bläck Studio knows.
|
|
48
|
+
The build does the same for you.
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
var __blackPlugin = class extends __black.require('@black/plugin').Plugin {
|
|
52
|
+
onload() {
|
|
53
|
+
this.addCommand({ id: 'hello', name: 'Say hello', run: () => this.app.ui.notify('Hello') })
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## The manifest
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"id": "word-of-the-day",
|
|
63
|
+
"name": "Word of the day",
|
|
64
|
+
"version": "1.0.0",
|
|
65
|
+
"description": "A word to use today, every morning.",
|
|
66
|
+
"author": "You",
|
|
67
|
+
"apiVersion": 1,
|
|
68
|
+
"main": "main.js",
|
|
69
|
+
"sandboxed": false,
|
|
70
|
+
"permissions": ["project:write", "network:api.example.com", "files:read", "exec:pandoc"],
|
|
71
|
+
"permissionReasons": {
|
|
72
|
+
"network:api.example.com": "fetch today's word from Example (api.example.com)"
|
|
73
|
+
},
|
|
74
|
+
"category": "writing",
|
|
75
|
+
"locales": {
|
|
76
|
+
"sv": { "name": "Dagens ord", "description": "Ett ord att använda i dag, varje morgon." }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
- **`id`:** lower-case letters, digits and dashes. It's also the folder name.
|
|
82
|
+
- **`apiVersion`:** the plugin API version the plugin needs. It defaults to 1.
|
|
83
|
+
Set it to the version that added the newest feature you use:
|
|
84
|
+
- 2: the dashboard and the library
|
|
85
|
+
- 3: the microphone
|
|
86
|
+
- 4: `app.speech`
|
|
87
|
+
- 5: dictation drafts, pauses and microphone choice
|
|
88
|
+
- 6: actions on marks
|
|
89
|
+
- 7: book importers
|
|
90
|
+
- 8: `permissionReasons` and `category` in the manifest. A plugin that uses
|
|
91
|
+
only these two doesn't need version 8, because older versions of the app
|
|
92
|
+
ignore them.
|
|
93
|
+
- 9: quiet marks
|
|
94
|
+
- 10: `app.folder` and `project:folder`, `network:ask`,
|
|
95
|
+
`app.net.streamBytes`, `during`, `addVersionHistory`, `project.flush` and
|
|
96
|
+
`project.getProjectData`
|
|
97
|
+
- **`sandboxed`:** runs the plugin in a separate worker, apart from the
|
|
98
|
+
window (see below).
|
|
99
|
+
- **`locales`:** the plugin's name and description in other languages, by
|
|
100
|
+
language code. Settings → Plugins, the plugin list and the permission
|
|
101
|
+
questions show them in the writer's language. You choose the plugin's other
|
|
102
|
+
strings (commands, settings, notices) yourself, using `this.app.language`.
|
|
103
|
+
When the writer changes the language, Bläck Studio reloads the plugin, so
|
|
104
|
+
`onload` runs again in the new language.
|
|
105
|
+
- **`permissions`:** what the plugin needs beyond reading the book.
|
|
106
|
+
- `project:write` lets the plugin change the book.
|
|
107
|
+
- `project:folder` lets the plugin read and change every file in the open
|
|
108
|
+
project's folder, and nothing outside it (see The project's folder). It
|
|
109
|
+
needs `"apiVersion": 10`.
|
|
110
|
+
- `library:read` lets the plugin see the writer's recent books and their
|
|
111
|
+
series (`app.library`), for example to build the welcome screen's
|
|
112
|
+
dashboard. It needs `"apiVersion": 2`.
|
|
113
|
+
- `microphone` lets a plugin in the window record, with
|
|
114
|
+
`navigator.mediaDevices.getUserMedia({ audio: true })`. Bläck Studio gives
|
|
115
|
+
the page access to the microphone only while a plugin with this
|
|
116
|
+
permission is on, and never gives it a camera. It needs
|
|
117
|
+
`"apiVersion": 3`. Release the stream as soon as you're done, so the
|
|
118
|
+
system stops showing that it's listening.
|
|
119
|
+
With `"apiVersion": 4` the plugin also gets `app.speech`, which turns
|
|
120
|
+
what the writer says into text on the device itself. Nothing is sent
|
|
121
|
+
anywhere. On a computer this uses Whisper, and Bläck Studio downloads its
|
|
122
|
+
model once, after asking the writer. On a phone or tablet it uses the
|
|
123
|
+
system's own recogniser, which gives you the words as they come. Call
|
|
124
|
+
`status(language)` first. If it isn't `ready`, `prepare(language)` gets
|
|
125
|
+
what's missing. Then call `listen()` and keep listening until `stop()`.
|
|
126
|
+
`onLevel` reports how loud the sound is, so you can show that the plugin
|
|
127
|
+
is hearing something. The session can also `pause()` and `resume()`.
|
|
128
|
+
Older versions of Bläck Studio have neither of these, so check that
|
|
129
|
+
they exist.
|
|
130
|
+
- `network:<host>` lets the plugin reach a host. `network:*.<host>` also
|
|
131
|
+
covers that host's subdomains, and `network:*` lets the plugin reach
|
|
132
|
+
anywhere on the internet. It doesn't cover this computer or the network
|
|
133
|
+
it's on (a router, a NAS). The plugin can reach those only by name, for
|
|
134
|
+
example `network:127.0.0.1`.
|
|
135
|
+
- `files:read` and `files:write` let the plugin use files and folders that
|
|
136
|
+
the writer chooses for it in the system's own dialog
|
|
137
|
+
(`app.files.pickFolder`, `pickFile`). It never gets the whole disk.
|
|
138
|
+
- `exec:<program>` lets the plugin run that one program. The program is
|
|
139
|
+
found by name on the PATH, gets its arguments as you give them, and runs
|
|
140
|
+
with no shell. `exec:pandoc` runs Pandoc and nothing else.
|
|
141
|
+
|
|
142
|
+
Bläck Studio marks `network:*` and every `exec:` permission as reaching
|
|
143
|
+
beyond the app, with a warning. A shell, Python or Node gets a stronger
|
|
144
|
+
warning.
|
|
145
|
+
|
|
146
|
+
The writer is asked to allow these permissions when they turn the plugin
|
|
147
|
+
on. If a new version asks for more, it stays off until the writer allows
|
|
148
|
+
that too.
|
|
149
|
+
|
|
150
|
+
Running in Bläck Studio's window (without `sandboxed`) is also a permission
|
|
151
|
+
the writer is asked to allow, and it's shown as unrestricted: a plugin there
|
|
152
|
+
can do anything the window can.
|
|
153
|
+
- **`optionalPermissions`:** permissions the plugin may ask for later with
|
|
154
|
+
`app.permissions.request`. Use it when the writer chooses something that
|
|
155
|
+
needs a permission (a service to send to, say), instead of asking when the
|
|
156
|
+
plugin is turned on. It takes the same kinds as `permissions`, and needs
|
|
157
|
+
`"apiVersion": 5`.
|
|
158
|
+
- **`permissionReasons`:** why the plugin asks, by permission. Bläck Studio
|
|
159
|
+
shows each reason to the writer when it asks them to allow the permission,
|
|
160
|
+
with its own description of what the permission allows underneath. Say what
|
|
161
|
+
is sent where and what for, so that your text completes the sentence "it
|
|
162
|
+
needs to …". It can be one text, or one text per language code:
|
|
163
|
+
|
|
164
|
+
```json
|
|
165
|
+
"permissionReasons": {
|
|
166
|
+
"network:api.example.com": {
|
|
167
|
+
"en": "fetch today's word from Example (api.example.com)",
|
|
168
|
+
"sv": "hämta dagens ord från Example (api.example.com)"
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Each reason belongs to a permission in `permissions` or
|
|
174
|
+
`optionalPermissions`, and can be up to 300 characters. Bläck Studio reads
|
|
175
|
+
it from plugin API 8 on. Older versions leave it out, so your plugin doesn't
|
|
176
|
+
need to ask for API 8 to include it.
|
|
177
|
+
- **`category`:** where Manage Plugins lists the plugin: `writing`,
|
|
178
|
+
`planning`, `welcome` (the welcome screen), `import-export` or `other`.
|
|
179
|
+
Bläck Studio reads it from plugin API 8 on, and older versions leave it
|
|
180
|
+
out, just like `permissionReasons`.
|
|
181
|
+
|
|
182
|
+
## What a plugin can do
|
|
183
|
+
|
|
184
|
+
Everything is in [`index.d.ts`](index.d.ts). The plugins on Bläck Studio's
|
|
185
|
+
list are written this way too, and each shows a part of the API:
|
|
186
|
+
|
|
187
|
+
| Plugin | Shows |
|
|
188
|
+
| --- | --- |
|
|
189
|
+
| `filter-words` | sandboxed: marks in the text, settings, a status bar item, events |
|
|
190
|
+
| `markdown-export` | sandboxed: a format in Compile |
|
|
191
|
+
| `name-generator` | in the window: a view, an inspector tab, a menu entry, a TipTap shortcut, changing the book |
|
|
192
|
+
| `pomodoro` | in the window: React in the status bar, a hotkey |
|
|
193
|
+
| `cast-suggest` | sandboxed: suggestions as you type, kept up to date as the navigator changes |
|
|
194
|
+
| `pandoc-export` | sandboxed: running a program in its temporary folder, with formats that follow its settings |
|
|
195
|
+
| `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 |
|
|
196
|
+
| `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 |
|
|
197
|
+
|
|
198
|
+
In short:
|
|
199
|
+
|
|
200
|
+
| Method | Adds |
|
|
201
|
+
| --- | --- |
|
|
202
|
+
| `addCommand` | a command in the palette (Ctrl+K) and the Plugins menu, with a hotkey if you like |
|
|
203
|
+
| `addView` | a view beside the corkboard and outliner (window only) |
|
|
204
|
+
| `addInspectorTab` | a tab in the inspector, for the items you choose (window only) |
|
|
205
|
+
| `addStatusBarItem` | an item in the status bar, drawn with React or as text you change |
|
|
206
|
+
| `addEditorMenuItem` | an entry in the text's right-click menu |
|
|
207
|
+
| `addBinderMenuItem` | an entry in the navigator's right-click menu |
|
|
208
|
+
| `addExporter` | a format in Compile |
|
|
209
|
+
| `addHighlighter` | marks in the text, found one paragraph at a time and never saved |
|
|
210
|
+
| `registerEditorExtension` | TipTap extensions: marks, nodes, input rules, shortcuts (window only) |
|
|
211
|
+
| `addSidebarPanel` | a tab beside the navigator (window only) |
|
|
212
|
+
| `addToolbarButton` | a button in the editor's toolbar |
|
|
213
|
+
| `addSuggester` | suggestions under the caret after a trigger: `@` for characters, `#` for tags… |
|
|
214
|
+
| `addImporter` | a kind of file for File → Import Documents |
|
|
215
|
+
| `addBookImporter` | a whole book from another app's file or folder, in File → Import and on the welcome screen (API 7) |
|
|
216
|
+
| `addSettings` | settings that Bläck Studio draws for you in Settings → Plugins |
|
|
217
|
+
| `addSettingsPanel` | settings that you draw yourself (window only) |
|
|
218
|
+
| `on` | `project-open`, `project-close`, `selection-change`, `doc-change`, `nodes-change`, `item-data-change` and `shared-data-change` (API 11) |
|
|
219
|
+
| `loadData`, `saveData` | your plugin's data, stored on this computer |
|
|
220
|
+
|
|
221
|
+
You read settings declared with `addSettings` from `this.settings`. Bläck
|
|
222
|
+
Studio keeps only the values the writer changed. So if a new version of your
|
|
223
|
+
plugin has a better default, everyone who hadn't changed that setting gets it.
|
|
224
|
+
`updateSettings({ key: undefined })` puts a setting back to its default.
|
|
225
|
+
|
|
226
|
+
`this.app` gives you the rest, each part described below:
|
|
227
|
+
- **[`app.project`](#the-book):** the book.
|
|
228
|
+
- **[`app.editor`](#the-editor):** the selection, comments on it, typing at
|
|
229
|
+
the caret, and, in the window, the editor itself.
|
|
230
|
+
- **[`app.ui`](#dialogs):** notices and dialogs.
|
|
231
|
+
- **[`app.net`](#the-network):** the internet, limited to the hosts you asked
|
|
232
|
+
for.
|
|
233
|
+
- **[`app.files`](#files-and-folders):** files and folders the writer chose.
|
|
234
|
+
- **[`app.exec`](#programs):** the programs you named.
|
|
235
|
+
|
|
236
|
+
If your plugin has a `styles.css` next to its manifest, Bläck Studio loads it
|
|
237
|
+
with the plugin.
|
|
238
|
+
|
|
239
|
+
In the window, `react`, `react/jsx-runtime`, `@tiptap/core` and
|
|
240
|
+
`@tiptap/pm/*` are Bläck Studio's copies. The build leaves them out of your
|
|
241
|
+
script. Your interface follows the theme if you use Bläck Studio's classes:
|
|
242
|
+
`btn`, `input`, `insp-section`, `sb-btn` and so on.
|
|
243
|
+
|
|
244
|
+
## The book
|
|
245
|
+
|
|
246
|
+
`this.app.project` reads the open book and, with `project:write`, changes it.
|
|
247
|
+
Every method returns a promise. You refer to items by id. You get ids from
|
|
248
|
+
`nodes()`, `manuscript()` and `selected()`.
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
const p = this.app.project
|
|
252
|
+
for (const id of await p.manuscript()) { // the scenes, in reading order
|
|
253
|
+
const text = await p.readText(id) // or read(id) for its HTML
|
|
254
|
+
const meta = await p.meta(id) // synopsis, notes, keywords, fields, when, words
|
|
255
|
+
if (meta && text.length > 20_000) await p.setMeta(id, { keywords: [...meta.keywords, 'long'] })
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
These methods read the book: `info()`, `nodes()`, `node(id)`, `manuscript()`,
|
|
260
|
+
`selected()`, `read`, `readText`, `meta`, `labels()`, `statuses()`,
|
|
261
|
+
`comments(id)`, `snapshots(id)`, `search(query, { wholeWord, caseSensitive,
|
|
262
|
+
regex })` (which works like Find in project) and `series()`.
|
|
263
|
+
|
|
264
|
+
With `project:write`, these methods change the book:
|
|
265
|
+
- **Text:** `write(id, html)`.
|
|
266
|
+
- **The navigator:** `create(type, title, parentId?)`, `rename`,
|
|
267
|
+
`move(id, folderId, index?)`, and `trash`, which moves an item to the
|
|
268
|
+
trash and never deletes it for good.
|
|
269
|
+
- **Cards and comments:** `setMeta(id, patch)` (synopsis, notes, keywords,
|
|
270
|
+
fields, when), `setLabel`, `setStatus` (by id; `''` for none), and
|
|
271
|
+
`resolveComment`.
|
|
272
|
+
|
|
273
|
+
Some things are always safe:
|
|
274
|
+
- **Snapshots:** before a plugin first rewrites a document, Bläck Studio
|
|
275
|
+
keeps a snapshot of it under the plugin's name. `snapshot(id, label)` keeps
|
|
276
|
+
one yourself, and needs no permission.
|
|
277
|
+
- **Undo:** every change is saved, can be undone, and is committed like the
|
|
278
|
+
writer's own changes.
|
|
279
|
+
- **Settings → Plugins** lists what each plugin changed recently.
|
|
280
|
+
- **Ids:** a method that takes an item's id throws if the book has no such
|
|
281
|
+
item. An id therefore always refers to an item in the book.
|
|
282
|
+
|
|
283
|
+
To find out when the navigator changes (an item is created, renamed, moved or
|
|
284
|
+
trashed), listen for `nodes-change`: `this.on('nodes-change', () => …)`.
|
|
285
|
+
|
|
286
|
+
## Your plugin's data
|
|
287
|
+
|
|
288
|
+
A plugin can keep data in five places. Choose by who it's for and where it should go:
|
|
289
|
+
|
|
290
|
+
| For | Use | Where it's kept | Shared on Bläck Sync |
|
|
291
|
+
| --- | --- | --- | --- |
|
|
292
|
+
| Settings of this computer | `loadData` / `saveData` | This computer | No |
|
|
293
|
+
| This computer's settings for the project | `getProjectData` / `setProjectData` | `project.json` | No |
|
|
294
|
+
| The whole project, such as a world | `getSharedData` / `setSharedData` | `project.json` | Yes |
|
|
295
|
+
| One book | `getData` / `setData` | `book.json` | Yes |
|
|
296
|
+
| One item | `getItemData` / `setItemData` | `book.json`, with the item | Yes |
|
|
297
|
+
|
|
298
|
+
`getData()` and `setData(value)` keep your plugin's data in the book, saved
|
|
299
|
+
with it and shared on Bläck Sync. Data that belongs to the plugin rather than
|
|
300
|
+
to one book goes in `loadData` and `saveData`.
|
|
301
|
+
|
|
302
|
+
- **Plain JSON only:** objects, lists, strings, finite numbers, booleans and
|
|
303
|
+
`null`. A `Date`, a `Map` or a class instance throws, because it would arrive
|
|
304
|
+
on other devices changed or empty. Store a date as a string.
|
|
305
|
+
- **At most 1 MB,** as JSON. Everything in it goes to every device the book is
|
|
306
|
+
on, and to everyone it's shared with.
|
|
307
|
+
- **When the book can't be changed** on this device (a reader or commenter of
|
|
308
|
+
a shared book, or after the trial), `setData` throws an error whose `name` is
|
|
309
|
+
`"ReadOnly"`. Catch it if your plugin writes as the writer works.
|
|
310
|
+
- **From API 11, `setData` needs `project:write`.** A plugin that declares an
|
|
311
|
+
older API keeps writing without it.
|
|
312
|
+
|
|
313
|
+
Data that belongs to one item goes with that item (API 11):
|
|
314
|
+
`getItemData(id)`, `setItemData(id, value)` and `itemsWithData()`. It moves
|
|
315
|
+
with the item, goes to the Trash with it, is copied when the item is
|
|
316
|
+
duplicated, and is deleted with it, so you never key book data by item id
|
|
317
|
+
yourself. The same rules apply, at most 64 KB per item, and it needs
|
|
318
|
+
`project:write`. Listen for `item-data-change` (its argument is the item's
|
|
319
|
+
id) to follow changes made on another device or by a co-author: read the
|
|
320
|
+
item's data again rather than writing back a copy you kept.
|
|
321
|
+
|
|
322
|
+
Data for the whole project, every book of a series, goes in
|
|
323
|
+
`getSharedData()` and `setSharedData(value)` (API 11). It's kept in the
|
|
324
|
+
project's `project.json` and shared on Bläck Sync. The same rules apply, and
|
|
325
|
+
`shared-data-change` tells you when it changed elsewhere.
|
|
326
|
+
|
|
327
|
+
The first time your plugin stores anything in a project (book data, item
|
|
328
|
+
data, shared data, text fields or a kind), the project lists it in
|
|
329
|
+
`project.json` as `plugins`, with the list it was installed from and its
|
|
330
|
+
version. When the project opens on a device without your plugin, or with it
|
|
331
|
+
turned off, Bläck Studio offers to install it from that list or turn it on.
|
|
332
|
+
|
|
333
|
+
### Writing at the same time as others (API 11)
|
|
334
|
+
|
|
335
|
+
On Bläck Sync, plugin data is merged key by key, and a value is replaced
|
|
336
|
+
whole: if two people change the same description at once, one change is lost.
|
|
337
|
+
For text that people write, call `textFields` when a book opens, with the
|
|
338
|
+
paths of those texts in your data:
|
|
339
|
+
|
|
340
|
+
```js
|
|
341
|
+
this.on('project-open', () => this.app.project.textFields(['description', 'chapters/*/summary']))
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
Those are then merged letter by letter, in the book's, each item's and the
|
|
345
|
+
shared data. `*` matches any key or list item.
|
|
346
|
+
|
|
347
|
+
From API 11 a write is refused if the data changed since your plugin last
|
|
348
|
+
read or wrote it, here or on another device: `setData`, `setItemData` and
|
|
349
|
+
`setSharedData` throw an error whose `name` is `"Stale"`. Read it again, apply
|
|
350
|
+
your change to what you read, and write that. Listen for `item-data-change`
|
|
351
|
+
and `shared-data-change` to take in changes as they come, so you never write
|
|
352
|
+
back an older copy:
|
|
353
|
+
|
|
354
|
+
```js
|
|
355
|
+
async rename(id, name) {
|
|
356
|
+
const entry = (await this.app.project.getItemData(id)) ?? {}
|
|
357
|
+
await this.app.project.setItemData(id, { ...entry, name })
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
## Kinds of entry (API 12)
|
|
363
|
+
|
|
364
|
+
A plugin can add kinds of entry of its own, such as factions or cultures.
|
|
365
|
+
An entry is a sheet like a character's or a place's: a title, a picture,
|
|
366
|
+
fields and a text. Define each kind when a book opens. It needs
|
|
367
|
+
`project:write`:
|
|
368
|
+
|
|
369
|
+
```js
|
|
370
|
+
this.on('project-open', async () => {
|
|
371
|
+
await this.app.project.defineKind({
|
|
372
|
+
id: 'faction',
|
|
373
|
+
name: 'Faction',
|
|
374
|
+
plural: 'Factions',
|
|
375
|
+
icon: 'banner',
|
|
376
|
+
fields: ['Leader', 'Seat', 'Goals'],
|
|
377
|
+
section: { id: 'world', name: 'World' },
|
|
378
|
+
})
|
|
379
|
+
})
|
|
380
|
+
const id = await this.app.project.create({ kind: 'faction' }, 'The Guild')
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
- **The book keeps the kind,** so its entries show with their name, icon and
|
|
384
|
+
fields even where your plugin isn't installed. Defining it again with
|
|
385
|
+
changes updates the book: a new version of your plugin can add a field.
|
|
386
|
+
A field it drops stays on the entries that have it.
|
|
387
|
+
- **Entries live in your section,** a folder at the top of the navigator
|
|
388
|
+
named by `section`, where each kind has a folder of its own named by its
|
|
389
|
+
`plural` ("Factions"). New entries go into their kind's folder. Entries
|
|
390
|
+
can't be moved out of the section, and neither the section nor a kind's
|
|
391
|
+
folder can be moved or put in the Trash. Several kinds can share a
|
|
392
|
+
section.
|
|
393
|
+
- **Ids** of kinds and sections are lower-case letters, digits and `-`.
|
|
394
|
+
`nodes()` and `node(id)` give an entry's `kind` as `"<plugin id>:<kind id>"`.
|
|
395
|
+
- **Icons** are names from the app's own set: `KindIcon` in the types lists
|
|
396
|
+
them.
|
|
397
|
+
- **A kind can be turned off** by defining it again with `hidden: true`, for
|
|
398
|
+
a writer who doesn't want it. Its entries aren't shown and none can be
|
|
399
|
+
made, but they're kept, and come back when you define it without `hidden`.
|
|
400
|
+
- **Writers can add fields** of their own for each kind in Templates. New
|
|
401
|
+
entries start with your fields and then theirs.
|
|
402
|
+
- **Where your plugin isn't there** (not installed, turned off, or unable
|
|
403
|
+
to run on a phone), its entries aren't shown. Your section becomes one
|
|
404
|
+
line that offers to get the plugin. Nothing about the entries changes:
|
|
405
|
+
they stay in the book, sync, and keep their links, and emptying the Trash
|
|
406
|
+
leaves them alone.
|
|
407
|
+
|
|
408
|
+
To show entries across a series, list them in your shared data as
|
|
409
|
+
`entries`. The series bible shows them under each kind's plural, beside the
|
|
410
|
+
characters and places, but only your plugin changes them:
|
|
411
|
+
|
|
412
|
+
```js
|
|
413
|
+
await this.app.project.setSharedData({
|
|
414
|
+
...shared,
|
|
415
|
+
entries: [{ id: 'guild', kind: 'faction', title: 'The Guild', synopsis: 'Merchants', fields: { Leader: 'Ama' } }],
|
|
416
|
+
})
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
Names in a world are often words no spelling dictionary knows. With
|
|
420
|
+
`project:write`, `addToDictionary(['Gillet', 'Ödemark'])` adds single words
|
|
421
|
+
to the book's dictionary, so they aren't marked. The writer can remove them
|
|
422
|
+
there.
|
|
423
|
+
|
|
424
|
+
To show an item's picture, such as a map, `picture(id)` gives an address
|
|
425
|
+
for an `<img>`. `storyTime(id)` gives a scene's story time as the timeline
|
|
426
|
+
reads it, as a day number, a date, or both, so a plugin can show it in a
|
|
427
|
+
calendar of its own. Your own checks go in the series' Continuity tab with
|
|
428
|
+
`addContinuityCheck({ id, name, run })`: `run` returns findings, each with a
|
|
429
|
+
`key`, a `message` and the `itemId` it's about, and the writer can open each
|
|
430
|
+
one or mark it as handled.
|
|
431
|
+
|
|
432
|
+
A book importer can bring entries too: return `kinds` (as you'd give them to
|
|
433
|
+
`defineKind`), and give an item `type: 'entry'` and the `kind` id. An
|
|
434
|
+
entry of a kind that isn't listed is read as an ordinary item.
|
|
435
|
+
|
|
436
|
+
## The editor
|
|
437
|
+
|
|
438
|
+
`this.app.editor` works with the editor the writer is in:
|
|
439
|
+
- `selection()` gives the selected text and where it is.
|
|
440
|
+
- `insertText(text)` types at the caret, replacing the selection.
|
|
441
|
+
- `comment(text)` puts a margin comment on the selection.
|
|
442
|
+
|
|
443
|
+
The last two need `project:write`. In the window, `active()` gives you the
|
|
444
|
+
TipTap editor itself, for anything else you need. In a sandboxed plugin it
|
|
445
|
+
returns `null`.
|
|
446
|
+
|
|
447
|
+
To change how the editor works (new marks or nodes, input rules, keyboard
|
|
448
|
+
shortcuts), register a TipTap extension with `registerEditorExtension`
|
|
449
|
+
(window only). If a mark only shows something, like a word to look at again,
|
|
450
|
+
a highlighter (`addHighlighter`) is a better fit. Bläck Studio draws
|
|
451
|
+
highlights and never saves them, and they work in sandboxed plugins too.
|
|
452
|
+
|
|
453
|
+
## Dialogs
|
|
454
|
+
|
|
455
|
+
`this.app.ui` asks the writer questions in dialogs that Bläck Studio draws:
|
|
456
|
+
|
|
457
|
+
```ts
|
|
458
|
+
const name = await this.app.ui.prompt({ title: 'Name the scene', label: 'Title', value: 'Untitled' })
|
|
459
|
+
if (name === null) return // cancelled
|
|
460
|
+
|
|
461
|
+
const kind = await this.app.ui.choose({
|
|
462
|
+
title: 'Export as',
|
|
463
|
+
options: [
|
|
464
|
+
{ value: 'odt', label: 'OpenDocument', detail: '.odt' },
|
|
465
|
+
{ value: 'tex', label: 'LaTeX', detail: '.tex' },
|
|
466
|
+
],
|
|
467
|
+
})
|
|
468
|
+
|
|
469
|
+
if (await this.app.ui.confirm('Replace every em dash?', 'In every scene.', 'Replace')) { … }
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
- **`prompt` and `choose`** resolve with the writer's answer, or with `null`
|
|
473
|
+
if the writer cancels.
|
|
474
|
+
- **`confirm`** asks a yes or no question and resolves with `true` or
|
|
475
|
+
`false`. The dialog says that your plugin is asking.
|
|
476
|
+
- **`notify(message, action?)`** shows a notice, with a button if you pass
|
|
477
|
+
an action.
|
|
478
|
+
|
|
479
|
+
All of these work in sandboxed plugins too, except that a sandboxed plugin's
|
|
480
|
+
notice can't have a button.
|
|
481
|
+
|
|
482
|
+
`openModal({ title, render, wide?, onClose? })` shows a dialog that you draw
|
|
483
|
+
yourself (window only). Bläck Studio passes `render` a `close` function, and
|
|
484
|
+
`openModal` returns one too. `onClose` tells you why the dialog went away:
|
|
485
|
+
`'closed'` if the writer closed it (Escape, ✕) or you called `close`, or
|
|
486
|
+
`'replaced'` if another dialog opened over it.
|
|
487
|
+
|
|
488
|
+
```tsx
|
|
489
|
+
const close = this.app.ui.openModal({
|
|
490
|
+
title: 'Word of the day',
|
|
491
|
+
render: (close) => (
|
|
492
|
+
<div>
|
|
493
|
+
<p className="insp-hint">{word}</p>
|
|
494
|
+
<button className="btn primary" onClick={close}>Thanks</button>
|
|
495
|
+
</div>
|
|
496
|
+
),
|
|
497
|
+
})
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
## Suggestions as you type
|
|
501
|
+
|
|
502
|
+
`addSuggester` shows a list of suggestions under the caret. The list appears
|
|
503
|
+
when the writer types a trigger character at the start of a word: `@` for
|
|
504
|
+
people, `#` for tags, `:` for emoji…
|
|
505
|
+
|
|
506
|
+
```ts
|
|
507
|
+
this.addSuggester({
|
|
508
|
+
id: 'tags',
|
|
509
|
+
trigger: '#',
|
|
510
|
+
find: (query) =>
|
|
511
|
+
this.tags
|
|
512
|
+
.filter((tag) => tag.startsWith(query.toLowerCase()))
|
|
513
|
+
.map((tag) => ({ label: tag, insert: `#${tag}`, detail: 'tag' })),
|
|
514
|
+
})
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
This is how it behaves:
|
|
518
|
+
- **What `find` gets:** the text typed after the trigger so far. Bläck Studio
|
|
519
|
+
calls `find` again as the writer types, after a 60 ms pause. Keep it quick,
|
|
520
|
+
and prepare whatever it searches beforehand. `find` may be async. If its
|
|
521
|
+
answer arrives after the writer has typed more, Bläck Studio drops it.
|
|
522
|
+
- **What it returns:** suggestions, each with a `label`, an optional `detail`
|
|
523
|
+
shown beside it, and an optional `insert`. When the writer picks a
|
|
524
|
+
suggestion, its `insert` (or its `label`, if there is no `insert`) replaces
|
|
525
|
+
the trigger and everything typed after it.
|
|
526
|
+
- **The keys:** up and down choose a suggestion, Enter or Tab takes it, and
|
|
527
|
+
Escape hides the list until the writer moves on.
|
|
528
|
+
- **Limits:** Bläck Studio shows at most 12 suggestions, counting every
|
|
529
|
+
suggester with the same trigger. A trigger in the middle of a word
|
|
530
|
+
(`e@mail`) offers nothing.
|
|
531
|
+
|
|
532
|
+
Suggesters work in sandboxed plugins too.
|
|
533
|
+
|
|
534
|
+
## More of the interface
|
|
535
|
+
|
|
536
|
+
- **`addSidebarPanel({ id, name, render })`:** a tab beside the navigator,
|
|
537
|
+
drawn with React (window only).
|
|
538
|
+
- **`addToolbarButton({ id, title, icon, active?, run })`:** a button in the
|
|
539
|
+
editor's toolbar. In the window the icon is a component. In either mode it
|
|
540
|
+
can also be a few characters (`'★'`). `active` shows the button as pressed.
|
|
541
|
+
- **`addImporter({ id, name, extensions, import })`:** a kind of file for
|
|
542
|
+
File → Import Documents. Bläck Studio offers it in the file dialog and
|
|
543
|
+
passes `import` the file (`name`, `ext`, `data` as bytes, `text`). You
|
|
544
|
+
return the documents to create: `[{ title, html }]`.
|
|
545
|
+
- **`addBookImporter({ id, name, from?, extensions, folder?, import })`:**
|
|
546
|
+
imports a whole book from another app's file or folder. Use `folder: true`
|
|
547
|
+
for a folder, as with a Scrivener project's .scriv. On a phone, the writer
|
|
548
|
+
chooses a zip of it. The importer appears in File → Import as `name`, and on
|
|
549
|
+
the welcome screen as "Import from `from`". Bläck Studio asks the writer for
|
|
550
|
+
the file and passes it to `import`. For a file, that's `name`, `ext`,
|
|
551
|
+
`data` and `text`. For a folder, it's `files` (every file in the folder,
|
|
552
|
+
by its path there), plus `read(path)` and `readText(path)` to read one of
|
|
553
|
+
them. You return the book:
|
|
554
|
+
- its `title`, labels, statuses and storylines;
|
|
555
|
+
- the items in each part of the navigator (`manuscript`, `characters`,
|
|
556
|
+
`places`, `notes`, `research`, `trash`), each with its text as HTML,
|
|
557
|
+
synopsis, notes, keywords, label and status;
|
|
558
|
+
- beats on the storylines.
|
|
559
|
+
|
|
560
|
+
Items refer to each other (a scene's cast and place, a beat's scene) by ids
|
|
561
|
+
that you make up for the import. Bläck Studio shows the writer what it
|
|
562
|
+
created, and either makes it a new book or adds it to the open one. To say
|
|
563
|
+
why a file can't be read, throw an Error. Book importers need no
|
|
564
|
+
permission and work in sandboxed plugins. They need `"apiVersion": 7`. The
|
|
565
|
+
Scrivener and Plottr imports on the list are built this way.
|
|
566
|
+
- **`styles.css`:** if it's next to the manifest, Bläck Studio loads it with
|
|
567
|
+
the plugin and removes it when the plugin is removed. Put your plugin's id
|
|
568
|
+
in your class names, so they don't clash with Bläck Studio's or another
|
|
569
|
+
plugin's.
|
|
570
|
+
|
|
571
|
+
## The dashboard
|
|
572
|
+
|
|
573
|
+
When no book is open, the welcome screen is the writer's dashboard. It shows
|
|
574
|
+
cards from plugins instead of its quote. If a plugin is turned off, its card
|
|
575
|
+
goes away, and when no plugin has a card, the quote comes back. This needs
|
|
576
|
+
`"apiVersion": 2`. A Bläck Studio that supports only API 1 tells the writer
|
|
577
|
+
that the plugin needs a newer version.
|
|
578
|
+
|
|
579
|
+
```tsx
|
|
580
|
+
export default class WordsToday extends Plugin {
|
|
581
|
+
onload() {
|
|
582
|
+
this.addDashboardWidget({ id: 'today', name: 'Today', size: 'half', order: 20, render: () => <Today app={this.app} /> })
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
- **`addDashboardWidget({ id, name, size?, order?, render })`:** adds a card.
|
|
588
|
+
`size` is `'half'` (half the dashboard's width, the default) or `'full'`.
|
|
589
|
+
Cards are sorted by `order`, lowest first (100 if you don't give one), and
|
|
590
|
+
then by name. Window only. These values only set where a card starts. The
|
|
591
|
+
writer can drag it elsewhere and make it wider or narrower, and Bläck Studio
|
|
592
|
+
remembers that. A card the writer hasn't placed yet (one just turned on)
|
|
593
|
+
goes after the others.
|
|
594
|
+
- **`app.library`**, with the `library:read` permission:
|
|
595
|
+
- `books()`: the books on the recent list and every book in their series.
|
|
596
|
+
Each book is summarised from its files: title, words, goal and deadline,
|
|
597
|
+
the daily goal, the words written each day (`history`), scenes by status,
|
|
598
|
+
its series and its place in it, and the scene written in most recently,
|
|
599
|
+
with the end of its text. Every card asks for this as the screen needs it, but
|
|
600
|
+
Bläck Studio reads the files only once for all of them.
|
|
601
|
+
- `activity()`: the latest saved versions (git), zip backups, and each
|
|
602
|
+
book's last push of its history, whether it failed or not.
|
|
603
|
+
- `open(path, { select? })`: opens one of these books, at one of its
|
|
604
|
+
documents.
|
|
605
|
+
- **`app.ui.startSprint(minutes)`**: starts a writing sprint in the open book.
|
|
606
|
+
|
|
607
|
+
Draw cards with Bläck Studio's classes, so they look like the rest of the
|
|
608
|
+
dashboard:
|
|
609
|
+
- `dash-big`: a large number.
|
|
610
|
+
- `dash-muted`: a quiet line.
|
|
611
|
+
- `dash-rows` and `dash-row`: a list of rows, with a name at the start and a
|
|
612
|
+
number at the end.
|
|
613
|
+
- `dash-bar`: progress, with a `<span style="width: 40%">` inside it. Add
|
|
614
|
+
`done` to make it green.
|
|
615
|
+
- `dash-quote`: a passage of the book.
|
|
616
|
+
- `dash-warn`: a warning.
|
|
617
|
+
- `btn`, `btn primary` and `btn ghost sm` for buttons.
|
|
618
|
+
|
|
619
|
+
## Settings that are secrets
|
|
620
|
+
|
|
621
|
+
A setting with `type: 'secret'` is text shown as dots, for an API key, say.
|
|
622
|
+
Bläck Studio keeps it apart from the plugin's other settings, sealed with the
|
|
623
|
+
system's keychain on a computer and in its keystore on a phone. On a computer
|
|
624
|
+
with no keychain (Linux without one), it stays in a file only the writer can read (API 3).
|
|
625
|
+
|
|
626
|
+
## The project's folder
|
|
627
|
+
|
|
628
|
+
A plugin that asks for `"project:folder"` can read and change every file in the open
|
|
629
|
+
project's folder, including hidden ones such as a version history, and nothing
|
|
630
|
+
outside it. The writer is told exactly that when asked to allow it. It works the
|
|
631
|
+
same on a computer and a phone, sandboxed or in the window, and needs `"apiVersion": 10`.
|
|
632
|
+
|
|
633
|
+
```ts
|
|
634
|
+
const here = await this.app.folder.current() // { key, name, book: 'books/<id>' }, or null
|
|
635
|
+
const bytes = await this.app.folder.read(here.key, 'project.json') // null if it isn't there
|
|
636
|
+
await this.app.folder.write(here.key, 'notes/today.txt', 'Hello')
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
- Paths are relative to the project's folder and use `/` as the separator. A path is refused if
|
|
640
|
+
it's absolute, contains `..`, goes through a symbolic link, or names `.sync` (at any level) or
|
|
641
|
+
`.git` (below the top level).
|
|
642
|
+
- Each call names the project by its key, not by a path, so work started on one project can't end
|
|
643
|
+
up in the next one opened. A key stops working two minutes after its project closes.
|
|
644
|
+
- `folder.arrived(key, paths)` tells the app that files were changed from outside it: they're
|
|
645
|
+
marked as downloaded, and the open book reloads them. `folder.create(title, name)` asks the
|
|
646
|
+
writer where to create a new project, for a plugin that brings one in from elsewhere, and
|
|
647
|
+
`project.open(key)` opens it.
|
|
648
|
+
|
|
649
|
+
## Versions, and the moments the app waits for
|
|
650
|
+
|
|
651
|
+
A plugin can keep versions of the project. The app draws the list, so this works in a sandboxed
|
|
652
|
+
plugin too:
|
|
653
|
+
|
|
654
|
+
```ts
|
|
655
|
+
this.addVersionHistory({
|
|
656
|
+
id: 'versions',
|
|
657
|
+
list: async (file) => [{ id: 'v2', label: 'Chapter one done', at: '2026-11-07T10:00:00Z' }],
|
|
658
|
+
read: async (version, file) => '<p>The text then.</p>',
|
|
659
|
+
save: async (message) => true, // false if nothing has changed since the last one
|
|
660
|
+
status: async () => ({ text: 'The last backup failed.', problem: true }), // or null
|
|
661
|
+
})
|
|
662
|
+
this.versionsChanged() // the app reloads the list
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
The list shows in Book → Versions & Backups and in a document's history. `file` is a path in the project's folder. There's no
|
|
666
|
+
call for restoring. The writer restores a document from the list, and the app reads its text with
|
|
667
|
+
`read`, keeps a snapshot of the document first, and then replaces it.
|
|
668
|
+
|
|
669
|
+
`this.during(event, handler)` registers a handler for a moment when the app waits for plugins, up
|
|
670
|
+
to a time limit:
|
|
671
|
+
- `'copy-due'`: the writing timer is due and something has changed, a new book was created, or
|
|
672
|
+
the writer is about to remove a plugin's data from the project (`info.reason` says which:
|
|
673
|
+
`'timer'`, `'new-book'` or `'before-removal'`). 30 seconds.
|
|
674
|
+
- `'project-closing'`: a book is closing, or the app is quitting (then `info.quitting` is set).
|
|
675
|
+
30 seconds.
|
|
676
|
+
- `'session-end'`: a writing session ended, after 30 minutes without writing or because the book is
|
|
677
|
+
closing. 10 seconds.
|
|
678
|
+
|
|
679
|
+
The handler receives `{ words, changed }`. After the limit the app carries on, so a plugin can't
|
|
680
|
+
stop anything from closing. A plugin has one handler per event: calling `during` again for the
|
|
681
|
+
same event replaces it. `app.project.flush()` resolves once the project's files are all saved, and
|
|
682
|
+
`app.project.getProjectData()` and `setProjectData(value)` store data with the project, on this device.
|
|
683
|
+
|
|
684
|
+
## On a phone or tablet
|
|
685
|
+
|
|
686
|
+
Bläck Studio for Android runs plugins too, from the same lists and under the
|
|
687
|
+
same rules. There are two differences:
|
|
688
|
+
- A plugin can't ask the writer to choose files, and can't run programs.
|
|
689
|
+
`files:*` and `exec:*` aren't granted there. A plugin that asks for them
|
|
690
|
+
stays off, and the writer is told it works on a desktop.
|
|
691
|
+
- `app.net.stream` and `app.net.streamBytes` give the whole answer as one piece.
|
|
692
|
+
|
|
693
|
+
Everything else works as it does on a desktop, sandboxed or in the window.
|
|
694
|
+
|
|
695
|
+
## The network
|
|
696
|
+
|
|
697
|
+
A plugin reaches the internet only through `this.app.net`, and only the hosts
|
|
698
|
+
that its manifest names (`network:api.example.com`) and the writer allowed.
|
|
699
|
+
Requests go over https. Plain http is allowed only to this computer
|
|
700
|
+
(`localhost`, `127.0.0.1`), so you can try a plugin against a server you run
|
|
701
|
+
yourself. Redirects aren't followed.
|
|
702
|
+
|
|
703
|
+
**`app.net.fetch(url, init?)`** gets the whole answer, up to 20 MB, within 30
|
|
704
|
+
seconds:
|
|
705
|
+
|
|
706
|
+
```ts
|
|
707
|
+
const res = await this.app.net.fetch('https://api.example.com/words', {
|
|
708
|
+
method: 'POST',
|
|
709
|
+
headers: { 'Content-Type': 'application/json' },
|
|
710
|
+
body: JSON.stringify({ text }),
|
|
711
|
+
})
|
|
712
|
+
if (res.ok) console.log(res.json()) // also res.text(), res.bytes(), res.status, res.headers
|
|
713
|
+
```
|
|
714
|
+
|
|
715
|
+
**`app.net.stream(url, init, onChunk)`** passes the answer on as it arrives,
|
|
716
|
+
giving each piece to `onChunk` as text. This suits an AI's reply, word by
|
|
717
|
+
word. When the answer is complete, `stream` resolves with its status and
|
|
718
|
+
headers. It can run for up to ten minutes and receive up to 200 MB.
|
|
719
|
+
|
|
720
|
+
**`app.net.streamBytes(url, init, onChunk)`** does the same with each piece as bytes, for
|
|
721
|
+
what isn't text (API 10).
|
|
722
|
+
|
|
723
|
+
**An address the writer enters**, such as a repository's, can't be named in the manifest. List
|
|
724
|
+
`"network:ask"` in `optionalPermissions`, and ask for the exact address when it's needed:
|
|
725
|
+
`await this.app.permissions.request('address:https://git.example.com/me/books')`. The writer
|
|
726
|
+
is asked about that address, in Bläck Studio's own window. The plugin can then reach that address
|
|
727
|
+
and what's under it, and no other (API 10).
|
|
728
|
+
|
|
729
|
+
```ts
|
|
730
|
+
const status = this.addStatusBarItem({ id: 'reply', text: '' })
|
|
731
|
+
let reply = ''
|
|
732
|
+
const res = await this.app.net.stream(
|
|
733
|
+
'https://api.example.com/v1/chat',
|
|
734
|
+
{ method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prompt, stream: true }) },
|
|
735
|
+
(text) => {
|
|
736
|
+
reply += text
|
|
737
|
+
status.setText(reply.slice(-40))
|
|
738
|
+
},
|
|
739
|
+
)
|
|
740
|
+
if (!res.ok) this.app.ui.notify(`The service answered ${res.status}`)
|
|
741
|
+
```
|
|
742
|
+
|
|
743
|
+
Each piece is the text as it arrived, not a whole line or message. The
|
|
744
|
+
`data:` lines of an event stream can be split across pieces, so collect them
|
|
745
|
+
before you read them.
|
|
746
|
+
|
|
747
|
+
There is one catch with a `text/plain` answer. Unless it has the header
|
|
748
|
+
`X-Content-Type-Options: nosniff`, the first 512 bytes may arrive as one
|
|
749
|
+
piece, because Chromium reads that much to guess the type of the answer.
|
|
750
|
+
Event streams (`text/event-stream`, which AI services send) and JSON arrive as
|
|
751
|
+
they're sent.
|
|
752
|
+
|
|
753
|
+
Both work the same way in a sandboxed plugin. A plugin can't open a
|
|
754
|
+
WebSocket.
|
|
755
|
+
|
|
756
|
+
## Files and folders
|
|
757
|
+
|
|
758
|
+
With `files:read` or `files:write`, a plugin can use what the writer chooses
|
|
759
|
+
for it in the system's own dialog (its title starts with the plugin's name),
|
|
760
|
+
and nothing else:
|
|
761
|
+
|
|
762
|
+
```ts
|
|
763
|
+
const folder = await this.app.files.pickFolder({ write: true, title: 'Where should the notes go?' })
|
|
764
|
+
if (!folder) return // cancelled
|
|
765
|
+
await this.app.files.write(`${folder}/notes.md`, '# Notes\n')
|
|
766
|
+
for (const entry of await this.app.files.list(folder)) console.log(entry.name, entry.folder, entry.size)
|
|
767
|
+
|
|
768
|
+
const file = await this.app.files.pickFile({ extensions: ['csv'] }) // one file, to read
|
|
769
|
+
const target = await this.app.files.pickFile({ save: true, name: 'cast.csv' }) // where to save one
|
|
770
|
+
```
|
|
771
|
+
|
|
772
|
+
Choosing:
|
|
773
|
+
- **`pickFolder`** gives the plugin a folder and everything in it.
|
|
774
|
+
`write: true` lets the plugin change what's there, if it has
|
|
775
|
+
`files:write`. With only `files:read`, it can read what's there and nothing
|
|
776
|
+
more.
|
|
777
|
+
- **`pickFile`** gives the plugin one file, to read it or, with
|
|
778
|
+
`save: true`, to write it.
|
|
779
|
+
|
|
780
|
+
Reading and writing (paths are full paths):
|
|
781
|
+
- **`read`** returns bytes, up to 100 MB. **`readText`** returns text.
|
|
782
|
+
- **`write(path, text or bytes)`** creates any missing folders on the way.
|
|
783
|
+
- **`list`**, **`stat`** and **`mkdir`** do what their names say.
|
|
784
|
+
- **`trash`** moves a file to the system's trash, where the writer can get it
|
|
785
|
+
back.
|
|
786
|
+
|
|
787
|
+
Bläck Studio checks every path in the main process, including any links. A
|
|
788
|
+
link inside a chosen folder that leads outside it leads nowhere. Bläck Studio
|
|
789
|
+
remembers what the writer chose until they take it back in Settings →
|
|
790
|
+
Plugins, or turn the plugin off. `granted()` lists these choices.
|
|
791
|
+
|
|
792
|
+
`temp()` gives the plugin a private folder among Bläck Studio's data, which
|
|
793
|
+
only the writer's account can open. The plugin can always use it, with any
|
|
794
|
+
`files:` or `exec:` permission, for passing files to a program.
|
|
795
|
+
|
|
796
|
+
## Programs
|
|
797
|
+
|
|
798
|
+
With `exec:<program>` in its manifest, a plugin can run that program and no
|
|
799
|
+
other:
|
|
800
|
+
|
|
801
|
+
```ts
|
|
802
|
+
const dir = await this.app.files.temp()
|
|
803
|
+
await this.app.files.write(`${dir}/book.html`, book.html)
|
|
804
|
+
const res = await this.app.exec.run('pandoc', ['book.html', '-o', 'book.odt'], { cwd: dir, timeout: 120_000 })
|
|
805
|
+
if (res.code !== 0) throw new Error(res.stderr || 'Pandoc failed')
|
|
806
|
+
const odt = await this.app.files.read(`${dir}/book.odt`)
|
|
807
|
+
```
|
|
808
|
+
|
|
809
|
+
This is how it runs:
|
|
810
|
+
- **The program:** Bläck Studio finds it by name on the PATH, as the system
|
|
811
|
+
would. A path isn't accepted. If the program isn't installed, you get an
|
|
812
|
+
error that says so.
|
|
813
|
+
- **Arguments:** they go to the program as you give them, with no shell.
|
|
814
|
+
`$HOME`, `;`, `|` and `*` are just characters.
|
|
815
|
+
- **`input`:** goes to the program's standard input.
|
|
816
|
+
- **`cwd`:** a folder the writer chose for the plugin, or the plugin's
|
|
817
|
+
temporary folder (the default).
|
|
818
|
+
- **Time and output:** Bläck Studio stops the program after `timeout` (a
|
|
819
|
+
minute unless you set it, ten at most; `timedOut` tells you this happened),
|
|
820
|
+
and at 50 MB of output. Anything the program started is stopped with it.
|
|
821
|
+
- **Its environment:** only what a program needs to find its way: `PATH`,
|
|
822
|
+
`HOME`, the language and the like. It doesn't get Bläck Studio's own
|
|
823
|
+
environment, which may hold keys and tokens.
|
|
824
|
+
- **The result:** `code`, `stdout` as bytes (`text()` for text), `stderr` and
|
|
825
|
+
`timedOut`.
|
|
826
|
+
|
|
827
|
+
When the writer allows a program, they trust the plugin with whatever that
|
|
828
|
+
program can be made to do with the arguments the plugin gives it. Pandoc
|
|
829
|
+
converts documents, but it also runs Lua filters it's given, and git runs
|
|
830
|
+
hooks. That's why every `exec:` permission comes with a warning, and a shell,
|
|
831
|
+
Python or Node with a stronger one. Ask only for the programs you need, and
|
|
832
|
+
say in your description what you run them for.
|
|
833
|
+
|
|
834
|
+
## In the window, or sandboxed
|
|
835
|
+
|
|
836
|
+
A plugin **in the window** runs in Bläck Studio's page. It can draw its own
|
|
837
|
+
interface and use the editor directly.
|
|
838
|
+
|
|
839
|
+
A **sandboxed** plugin runs in a separate worker:
|
|
840
|
+
- **No page:** it can't reach the page, the editor or other plugins.
|
|
841
|
+
- **No network access of its own:** it has no `fetch` and no way to load
|
|
842
|
+
code.
|
|
843
|
+
- **Everything through Bläck Studio:** it asks for what it needs through
|
|
844
|
+
`this.app`, and Bläck Studio answers according to the plugin's permissions.
|
|
845
|
+
- **The same methods:** it has everything described above, except what draws
|
|
846
|
+
an interface, `editor.active()`, `editor.anchor()` and `app.speech`. Its status bar items are text, and its menu entries use
|
|
847
|
+
`show` to say whether they're for a selection.
|
|
848
|
+
|
|
849
|
+
The plugin list shows when a plugin is sandboxed. If your plugin doesn't need
|
|
850
|
+
to draw anything, make it sandboxed.
|
|
851
|
+
|
|
852
|
+
## What protects the writer
|
|
853
|
+
|
|
854
|
+
- **The disk:** plugins reach the books only through Bläck Studio, and can't
|
|
855
|
+
reach the disk at all. Bläck Studio's main process works only in the books
|
|
856
|
+
and series the writer chose in the app's own dialogs, whatever the page
|
|
857
|
+
asks. It copies into a book only files the writer picked or dropped.
|
|
858
|
+
- **Plugins' changes:** they go through Bläck Studio's own actions, so they
|
|
859
|
+
can be undone, and they are saved like the writer's own
|
|
860
|
+
changes. Bläck Studio snapshots a document under the plugin's name before
|
|
861
|
+
the plugin first rewrites it.
|
|
862
|
+
- **Deleting:** nothing a plugin does deletes for good. `trash` moves an item
|
|
863
|
+
to the trash.
|
|
864
|
+
- **The network:** the page and its plugins can't send requests themselves.
|
|
865
|
+
`app.net.fetch` goes through Bläck Studio, which allows only https, only to
|
|
866
|
+
the hosts in the manifest or addresses the writer entered, and only to those the writer
|
|
867
|
+
allowed. The page also can't open an address in the browser.
|
|
868
|
+
- **The project's folder:** a plugin with `project:folder` reaches that folder and nothing outside
|
|
869
|
+
it. The checks on each path are in the main process, including any links.
|
|
870
|
+
- **Files:** a plugin reads and changes only what the writer chose for it.
|
|
871
|
+
The main process checks this, including any links. Settings → Plugins lists
|
|
872
|
+
what each plugin was given, so the writer can take it back. "Deleting" a
|
|
873
|
+
file moves it to the system's trash.
|
|
874
|
+
- **Programs:** a plugin can run only the programs named in its manifest, by
|
|
875
|
+
name, without a shell, and Bläck Studio stops them after a time limit.
|
|
876
|
+
- **Permissions:** the writer allows permissions in a small Bläck Studio
|
|
877
|
+
window, in the app's theme, that opens over the main one. No plugin runs
|
|
878
|
+
there, and the main process takes an answer only from that window, so
|
|
879
|
+
nothing in the page can answer for the writer. Cancel has the focus, and
|
|
880
|
+
Allow becomes active after a short delay, so a key pressed just as the
|
|
881
|
+
window appears allows nothing. Bläck Studio asks the same way before
|
|
882
|
+
uninstalling a plugin and before adding a list.
|
|
883
|
+
- **Separate tokens:** each plugin's calls carry its own token. Bläck Studio
|
|
884
|
+
makes the tokens from a secret that it gives the page's plugin host once,
|
|
885
|
+
before any plugin loads, and it takes the means of making tokens away
|
|
886
|
+
before any plugin runs. So a sandboxed plugin can't use what another plugin
|
|
887
|
+
was allowed.
|
|
888
|
+
- **What still depends on trust:** a plugin in the window shares the page with Bläck
|
|
889
|
+
Studio. In principle it could interfere with the app or with other plugins,
|
|
890
|
+
as in Obsidian. It could change the books that are open, or get at what
|
|
891
|
+
another plugin in the page was allowed. It still can't reach the rest of
|
|
892
|
+
the disk, or the network beyond what plugins were allowed. A sandboxed
|
|
893
|
+
plugin can do none of this.
|
|
894
|
+
- **A crash:** when a plugin throws, Bläck Studio shows the error where it
|
|
895
|
+
happened and in Settings → Plugins, and the rest of the app carries on.
|
|
896
|
+
- **Safe mode:** Plugins → Restart Without Plugins starts Bläck Studio
|
|
897
|
+
without plugins. `--safe-mode` on the command line does the same.
|
|
898
|
+
|
|
899
|
+
## The plugin list
|
|
900
|
+
|
|
901
|
+
Settings → Plugins → Find plugins reads a list from
|
|
902
|
+
`https://blackstudio.app/plugins/plugins.json`
|
|
903
|
+
(set `BLACK_PLUGIN_INDEX` to use another):
|
|
904
|
+
|
|
905
|
+
```json
|
|
906
|
+
{
|
|
907
|
+
"plugins": [
|
|
908
|
+
{
|
|
909
|
+
"id": "word-of-the-day",
|
|
910
|
+
"name": "Word of the day",
|
|
911
|
+
"description": "A word to use today, every morning.",
|
|
912
|
+
"author": "You",
|
|
913
|
+
"version": "1.0.0",
|
|
914
|
+
"apiVersion": 1,
|
|
915
|
+
"sandboxed": true,
|
|
916
|
+
"permissions": ["network:api.example.com"],
|
|
917
|
+
"download": "https://example.com/word-of-the-day-1.0.0.zip",
|
|
918
|
+
"sha256": "…the SHA-256 that npm run build prints…",
|
|
919
|
+
"homepage": "https://example.com/word-of-the-day"
|
|
920
|
+
}
|
|
921
|
+
]
|
|
922
|
+
}
|
|
923
|
+
```
|
|
924
|
+
|
|
925
|
+
Bläck Studio installs plugins only from a list:
|
|
926
|
+
- **The download:** it fetches the zip that the list names, and refuses it
|
|
927
|
+
unless the zip's SHA-256 matches the one in the list.
|
|
928
|
+
- **The contents:** before unpacking, it checks that the zip holds the plugin
|
|
929
|
+
the list names, with nothing outside the plugin's folder.
|
|
930
|
+
|
|
931
|
+
To add a plugin, add its entry to the list's repository.
|
|
932
|
+
|
|
933
|
+
### Lists of your own
|
|
934
|
+
|
|
935
|
+
Anyone can keep a list: a `plugins.json` like the one above, at an https
|
|
936
|
+
address. A list can name itself with `"name"` next to `"plugins"`.
|
|
937
|
+
|
|
938
|
+
Bläck Studio also offers a community list by default, next to the official
|
|
939
|
+
one: `https://raw.githubusercontent.com/black-community/plugins/main/plugins.json`.
|
|
940
|
+
Its plugins are marked as not official. The writer can remove the list.
|
|
941
|
+
|
|
942
|
+
The writer adds a list under Settings → Plugins → Find plugins → Plugin
|
|
943
|
+
lists, by its address:
|
|
944
|
+
- **Asked first:** Bläck Studio reads the list, then asks in its own window
|
|
945
|
+
whether to add it. The window says that the list's plugins aren't Bläck
|
|
946
|
+
Studio's.
|
|
947
|
+
- **Marked as unofficial:** its plugins appear next to the official ones,
|
|
948
|
+
each labelled with the list it's from.
|
|
949
|
+
- **Checked the same way:** Bläck Studio checks a download against its list
|
|
950
|
+
exactly as it does for the official list.
|
|
951
|
+
- **Updates:** a plugin installed from a list is updated only from that list.
|
|
952
|
+
Bläck Studio won't replace a plugin from one list with one from another
|
|
953
|
+
list, and won't replace one you're writing in the plugins folder.
|
|
954
|
+
- **Removing a list:** its plugins stay installed. They just aren't offered
|
|
955
|
+
any more.
|