@inkbar/core 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +242 -2
- package/dist/document-Cywnv8Sz.d.mts +1183 -0
- package/dist/document-Cywnv8Sz.d.mts.map +1 -0
- package/dist/dom.d.mts +610 -0
- package/dist/dom.d.mts.map +1 -0
- package/dist/dom.mjs +3364 -0
- package/dist/dom.mjs.map +1 -0
- package/dist/index.d.mts +99 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +136 -0
- package/dist/index.mjs.map +1 -0
- package/dist/threads-BtvtrAhL.mjs +1878 -0
- package/dist/threads-BtvtrAhL.mjs.map +1 -0
- package/dist/vite/client.d.ts +4 -0
- package/dist/vite.d.mts +10 -0
- package/dist/vite.d.mts.map +1 -0
- package/dist/vite.mjs +123 -0
- package/dist/vite.mjs.map +1 -0
- package/package.json +75 -3
- package/schema.json +619 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Redactr Platforms Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,243 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/inkbar/inkbar/main/.github/assets/inkbar-mark-on-dark.svg">
|
|
4
|
+
<img src="https://raw.githubusercontent.com/inkbar/inkbar/main/.github/assets/inkbar-mark-on-light.svg" alt="Inkbar" width="72" height="72">
|
|
5
|
+
</picture>
|
|
6
|
+
</p>
|
|
2
7
|
|
|
3
|
-
|
|
8
|
+
<h1 align="center">@inkbar/core</h1>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
The engine under Inkbar: documents, rendering, annotations and tools, with no framework dependency.
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
<a href="https://inkbar.dev">Website</a> ·
|
|
16
|
+
<a href="https://inkbar.dev/docs/">Docs</a> ·
|
|
17
|
+
<a href="https://sandbox.inkbar.dev">Sandbox</a> ·
|
|
18
|
+
<a href="https://github.com/inkbar/inkbar">GitHub</a>
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
`@inkbar/core` opens PDFs, renders pages with PDF.js and keeps annotations as typed data. The Vue, React and Svelte packages build on it. You can also use it on its own: `createViewer` puts a complete viewer, with note threads, redaction review and controls to build your toolbars from, into any element in a plain TypeScript app.
|
|
24
|
+
|
|
25
|
+
Every coordinate is stored in PDF user space (points, origin bottom-left, before `/Rotate`), so annotations stay put whatever the zoom, rotation or screen.
|
|
26
|
+
|
|
27
|
+
- `@inkbar/core` holds the document model and annotation store. It is safe to import during server rendering.
|
|
28
|
+
- `@inkbar/core/dom` renders pages, text layers and annotation layers into the DOM.
|
|
29
|
+
- `@inkbar/core/vite` is a Vite plugin that serves the PDF.js assets.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
pnpm add @inkbar/core
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { openDocument } from '@inkbar/core'
|
|
41
|
+
import { assets } from 'virtual:inkbar-assets' // from the Vite plugin, see below
|
|
42
|
+
|
|
43
|
+
const doc = await openDocument(file, { assets })
|
|
44
|
+
await doc.ready
|
|
45
|
+
|
|
46
|
+
doc.annotations.add({ type: 'highlight', page: 0, quads })
|
|
47
|
+
const json = doc.exportAnnotations('json')
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`file` can be a `File`, `Blob`, `Uint8Array`, a URL string, or `{ url, key }` when the URL is signed and changes between loads. `quads` is a list of quads in PDF user space.
|
|
51
|
+
|
|
52
|
+
Writing annotations back into the PDF needs `@inkbar/engine`, which is not released yet. Until then `save()` rejects with `EngineUnavailable`. Persist `exportAnnotations('json')` instead and re-import it on the next open.
|
|
53
|
+
|
|
54
|
+
The DOM layer (`@inkbar/core/dom`) provides the viewport, canvas renderer, text layer, overlay and tools for hosts without a framework package. Vue hosts should use [`@inkbar/vue`](https://github.com/inkbar/inkbar/tree/main/packages/vue), React hosts [`@inkbar/react`](https://github.com/inkbar/inkbar/tree/main/packages/react), Svelte hosts [`@inkbar/svelte`](https://github.com/inkbar/inkbar/tree/main/packages/svelte).
|
|
55
|
+
|
|
56
|
+
### Without a framework
|
|
57
|
+
|
|
58
|
+
`createViewer` opens a document and renders it into an element, with a note thread and a suggested-redaction card. The toolbars are yours: build them from the controls below, which use the same markup as the framework packages, so `@inkbar/theme` styles them.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import {
|
|
62
|
+
createViewer,
|
|
63
|
+
createToolbar,
|
|
64
|
+
createToolButton,
|
|
65
|
+
createPageInput,
|
|
66
|
+
createPageCount,
|
|
67
|
+
createZoomOut,
|
|
68
|
+
createZoomSelect,
|
|
69
|
+
createZoomIn,
|
|
70
|
+
} from '@inkbar/core/dom'
|
|
71
|
+
import '@inkbar/theme/theme.css'
|
|
72
|
+
|
|
73
|
+
const viewer = createViewer(el, { source: file, assets, zoom: 'fit-width' })
|
|
74
|
+
|
|
75
|
+
const tools = createToolbar()
|
|
76
|
+
tools.append(createToolButton(viewer, 'select'), createToolButton(viewer, 'highlight'))
|
|
77
|
+
|
|
78
|
+
const undo = document.createElement('button')
|
|
79
|
+
undo.textContent = 'Undo'
|
|
80
|
+
undo.onclick = () => viewer.document.get()?.history.undo()
|
|
81
|
+
tools.append(undo) // buttons join the toolbar's arrow-key order
|
|
82
|
+
|
|
83
|
+
const pages = createToolbar({ label: 'Page and zoom' })
|
|
84
|
+
pages.append(
|
|
85
|
+
createPageInput(viewer),
|
|
86
|
+
createPageCount(viewer),
|
|
87
|
+
createZoomOut(viewer),
|
|
88
|
+
createZoomSelect(viewer),
|
|
89
|
+
createZoomIn(viewer)
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
viewer.element.prepend(tools) // above the document
|
|
93
|
+
viewer.element.append(pages) // below it
|
|
94
|
+
|
|
95
|
+
viewer.open(otherFile) // destroys the current document; the latest call wins
|
|
96
|
+
viewer.open(lockedFile, { password }) // this password applies to this load only
|
|
97
|
+
viewer.destroy()
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
It takes the [`mountViewer` options](#mounting-a-viewer) except `container` and `document`, plus `password`, `defaults`, `history`, `onReady` and `onError`. Set `notes` or `suggestions` to `false` to leave that part out. `viewer.status`, `viewer.document`, `viewer.engine`, `viewer.viewport` and `viewer.tools` are readables; the last two change when the engine rebuilds. A password you pass to `open` replaces the `createViewer` one for that load. The controls are `createToolButton`, `createUndo`, `createRedo`, `createZoomIn`, `createZoomOut`, `createZoomSelect`, `createPageInput`, `createPageCount`, `createPrevPage` and `createNextPage`. Each one disables itself while no document is ready and lives as long as its viewer. Buttons show an icon from [`@inkbar/icons`](https://github.com/inkbar/inkbar/tree/main/packages/icons). Replace their children with your own icon and the accessible name stays; `createIcon` draws any `@inkbar/icons` icon for your own buttons. `createToolbar` gives one Tab stop to all its buttons, with arrow keys between them, and `createToolbarSeparator` divides groups of controls in it. An input, select or textarea you add keeps its arrow keys and its own `tabindex`: it is a Tab stop unless you give it `tabindex="-1"`.
|
|
101
|
+
|
|
102
|
+
`createViewer` reads the theme's colours from the element you pass, so that element needs to be in the page when you call it.
|
|
103
|
+
|
|
104
|
+
### Mounting a viewer
|
|
105
|
+
|
|
106
|
+
`mountViewer` wires the viewport, page layers, tools and zoom and page keys onto a scroll container. You render the chrome around it.
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import { mountViewer } from '@inkbar/core/dom'
|
|
110
|
+
|
|
111
|
+
const engine = mountViewer({
|
|
112
|
+
container, // a focusable, scrollable element
|
|
113
|
+
document: doc,
|
|
114
|
+
zoom: 'fit-width',
|
|
115
|
+
onPageError: (page, error) => console.warn(page, error),
|
|
116
|
+
})
|
|
117
|
+
engine.tools.set('highlight')
|
|
118
|
+
engine.rebuild({ gap: 32 }) // construction-only options; zoom, page, tool and scroll position carry over
|
|
119
|
+
engine.destroy()
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Options:
|
|
123
|
+
|
|
124
|
+
- `container` and `document`, both required.
|
|
125
|
+
- `zoom` (a number, `'fit-width'` or `'fit-page'`; default `'fit-width'`), `page`, `mode`, `rotation` and `tool` set the starting view. Change them later through `engine.viewport` and `engine.tools`.
|
|
126
|
+
- `gap`, `buffer` and `customAnnotations` are fixed for a build. Change them with `engine.rebuild`.
|
|
127
|
+
- `onActivate(id)` fires when the user activates an annotation.
|
|
128
|
+
- `onBuild(viewport, tools)` fires after every build, the first one included, before `mountViewer` returns. Subscribe to the new viewport and tool controller here.
|
|
129
|
+
- `onPageMount(page, mount, host)` and `onPageUnmount(page)` fire as pages enter and leave the mounted range.
|
|
130
|
+
- `onPageError(page, error)` reports a page whose canvas or text layer failed, and `onPageError(page, null)` once every failed layer renders again. Unmounting a failed page emits no `null`, so clear any per-page error in `onPageUnmount`.
|
|
131
|
+
|
|
132
|
+
The viewport mounts pages synchronously, so a build's page callbacks fire before its `onBuild`. During a rebuild, `engine.viewport` and `engine.tools` still point at the destroyed objects while those callbacks run. Read the arguments instead, or wait for `onBuild`.
|
|
133
|
+
|
|
134
|
+
## Annotation changes
|
|
135
|
+
|
|
136
|
+
`doc.annotations.on('change', fn)`, and `onChange(next, change)` in controlled mode, describe each mutation:
|
|
137
|
+
|
|
138
|
+
- `{ kind: 'add', annotation }`
|
|
139
|
+
- `{ kind: 'update', id, before, after }`
|
|
140
|
+
- `{ kind: 'remove', annotation, removed }`. Removing a note also removes its replies, so `removed` lists everything that went: `annotation` first, then the replies. A host that syncs by change should delete every id in `removed`.
|
|
141
|
+
- `{ kind: 'replace', reason }` for an import, a history step or a controlled sync. Read the whole list.
|
|
142
|
+
|
|
143
|
+
`add` throws if `input.id` is already in the list. Locked annotations refuse changes to their geometry, `page` and `contents`.
|
|
144
|
+
|
|
145
|
+
`doc.history.batch(fn)` records everything `fn` changes as one undo step, so a host that adds many annotations at once can undo them together. Change events still fire once per annotation. `fn` must be synchronous, and nested calls join the outer batch.
|
|
146
|
+
|
|
147
|
+
## Tools and keyboard
|
|
148
|
+
|
|
149
|
+
`createToolController` from `@inkbar/core/dom` routes pointer and key events on the viewport to the active tool: `view`, `select`, `highlight`, `redact-area` or `note`. Every tool works from the keyboard as well as the pointer.
|
|
150
|
+
|
|
151
|
+
| Where focus is | Key | Effect |
|
|
152
|
+
| ---------------------------- | ----------------------------------- | --------------------------------------------------------------------------------------------------- |
|
|
153
|
+
| Viewport, `highlight` tool | `Enter` | Turns the text selection into a highlight |
|
|
154
|
+
| Viewport, `redact-area` tool | `Enter` | Turns the text selection into text redactions |
|
|
155
|
+
| Viewport, `note` tool | `Enter` | Places a note at the centre of the current page and focuses it |
|
|
156
|
+
| Viewport | `+` or `=`, `-` | Zoom in, zoom out |
|
|
157
|
+
| Viewport | `Home`, `End`, `PageUp`, `PageDown` | First, last, previous, next page |
|
|
158
|
+
| Annotation | `Enter` or `Space` | Selects and activates it |
|
|
159
|
+
| Annotation | `ArrowUp`, `ArrowDown` | Previous or next annotation on the page, in reading order |
|
|
160
|
+
| Annotation | `Shift` + arrow | Moves it 1 pt (`Shift` + `Alt` + arrow: 10 pt) |
|
|
161
|
+
| Annotation | `Ctrl` or `Cmd` + `Shift` + arrow | Resizes an area redaction, text box or shape by 1 pt. Right and Down grow it, Left and Up shrink it |
|
|
162
|
+
| Annotation | `Delete` or `Backspace` | Removes it |
|
|
163
|
+
| Annotation | `Escape` | Clears the selection |
|
|
164
|
+
|
|
165
|
+
The tool controller and the overlay handle the tool and annotation keys. The zoom and page keys (`+`, `=`, `-`, `Home`, `End`, `PageUp`, `PageDown`) come from `attachViewportKeys`, which the tool controller does not install. Attach it to the same container:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
import { attachViewportKeys } from '@inkbar/core/dom'
|
|
169
|
+
|
|
170
|
+
const detach = attachViewportKeys(container, {
|
|
171
|
+
viewport,
|
|
172
|
+
pageCount: () => doc.pages.length,
|
|
173
|
+
})
|
|
174
|
+
// later: detach()
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
On a focused annotation, `Enter` or `Space` selects it and calls `onActivate`; what activation opens is up to the host.
|
|
178
|
+
|
|
179
|
+
Locked annotations, such as those embedded in the file, ignore move, resize and delete. Without a text selection, `Enter` in the `highlight` and `redact-area` tools does nothing.
|
|
180
|
+
|
|
181
|
+
`redact-area` draws an area mark when you drag. To redact text instead, select the text with the `view` tool, switch to `redact-area`, then click inside the selection or press `Enter`. The tool keeps the press from starting a new text selection, so it cannot select text itself.
|
|
182
|
+
|
|
183
|
+
With the `select` tool, a selected area redaction, text box or shape shows four corner handles (`.ink-handle[data-handle="nw|ne|sw|se"]`). Dragging one resizes it; the store records a single update on release. Drag the annotation itself to move it.
|
|
184
|
+
|
|
185
|
+
## Serving PDF.js assets
|
|
186
|
+
|
|
187
|
+
PDF.js needs a worker script plus directories of CMaps, standard fonts, wasm decoders and ICC profiles. Inkbar loads them from URLs you pass as the `assets` option. The option has no default, because the right URLs depend on how you deploy your app.
|
|
188
|
+
|
|
189
|
+
### With Vite
|
|
190
|
+
|
|
191
|
+
Add the plugin:
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
// vite.config.ts
|
|
195
|
+
import { inkbar } from '@inkbar/core/vite'
|
|
196
|
+
import { defineConfig } from 'vite'
|
|
197
|
+
|
|
198
|
+
export default defineConfig({
|
|
199
|
+
plugins: [inkbar()],
|
|
200
|
+
})
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Then import the URLs from the virtual module:
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
import { assets } from 'virtual:inkbar-assets'
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
In development the plugin serves the files from `node_modules`. In a build it copies them into `<outDir>/inkbar/`. The URLs respect your `base`. With a relative `base` such as `./`, the plugin resolves the URLs at runtime from the location of your bundle, so the build works from any path. Pass `inkbar({ assetsDir: 'pdf' })` to use a different directory name.
|
|
210
|
+
|
|
211
|
+
For TypeScript, reference the module declaration once, for example in `env.d.ts`:
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
/// <reference types="@inkbar/core/vite/client" />
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Without Vite
|
|
218
|
+
|
|
219
|
+
Copy these from the installed `pdfjs-dist` package to a path your server exposes:
|
|
220
|
+
|
|
221
|
+
- `build/pdf.worker.min.mjs`
|
|
222
|
+
- `cmaps/`
|
|
223
|
+
- `standard_fonts/`
|
|
224
|
+
- `wasm/`
|
|
225
|
+
- `iccs/` (optional)
|
|
226
|
+
|
|
227
|
+
Pass their URLs. Directory URLs end in `/`:
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
const assets = {
|
|
231
|
+
worker: '/pdfjs/pdf.worker.min.mjs',
|
|
232
|
+
cMaps: '/pdfjs/cmaps/',
|
|
233
|
+
standardFonts: '/pdfjs/standard_fonts/',
|
|
234
|
+
wasm: '/pdfjs/wasm/',
|
|
235
|
+
icc: '/pdfjs/iccs/',
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Use the `pdfjs-dist` version that `@inkbar/core` depends on, so the worker matches the library.
|
|
240
|
+
|
|
241
|
+
## Licence
|
|
242
|
+
|
|
243
|
+
[MIT](https://github.com/inkbar/inkbar/blob/main/LICENSE). From the team behind [Redactr](https://redactr.io).
|