@microscope-js/core 0.1.4 → 0.1.5
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 +66 -8
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,33 +1,77 @@
|
|
|
1
1
|
# @microscope-js/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@microscope-js/core)
|
|
4
|
+
[](https://bundlephobia.com/package/@microscope-js/core)
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://docs.npmjs.com/generating-provenance-statements)
|
|
7
|
+
|
|
8
|
+
> **The framework-agnostic heart of [microscope-js](https://github.com/shubham8550/microscope-js).** One `Renderer` interface, one `Registry`, one `mount()`. Everything else is a plugin.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pnpm add @microscope-js/core @microscope-js/renderer-pdf
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
You almost never use `core` alone — pair it with one or more `@microscope-js/renderer-*` packages.
|
|
17
|
+
|
|
18
|
+
## Use
|
|
4
19
|
|
|
5
20
|
```ts
|
|
6
21
|
import { createRegistry, mount } from '@microscope-js/core';
|
|
7
22
|
import { pdfRenderer } from '@microscope-js/renderer-pdf';
|
|
23
|
+
import { imageRenderer } from '@microscope-js/renderer-image';
|
|
24
|
+
|
|
25
|
+
const registry = createRegistry([pdfRenderer, imageRenderer]);
|
|
8
26
|
|
|
9
|
-
const registry = createRegistry([pdfRenderer]);
|
|
10
27
|
const handle = await mount({
|
|
11
|
-
source:
|
|
28
|
+
source: fileInput.files[0], // File | Blob | ArrayBuffer | Uint8Array | URL | string
|
|
12
29
|
container: document.getElementById('viewer')!,
|
|
13
30
|
registry,
|
|
14
31
|
});
|
|
15
|
-
|
|
32
|
+
|
|
33
|
+
// later, when navigating away
|
|
16
34
|
handle.destroy();
|
|
17
35
|
```
|
|
18
36
|
|
|
19
|
-
|
|
37
|
+
`mount()` normalizes the source, sniffs the MIME type if needed, picks the highest-priority renderer that claims it, clears the container, and renders.
|
|
38
|
+
|
|
39
|
+
## API
|
|
40
|
+
|
|
41
|
+
### `createRegistry(renderers)`
|
|
42
|
+
|
|
43
|
+
Build a `Registry` from a list of `Renderer`s. Later entries win by default; pass `{ renderer, priority }` to override.
|
|
44
|
+
|
|
45
|
+
### `composeRegistries(...registries)`
|
|
46
|
+
|
|
47
|
+
Merge multiple registries — useful when an app layers custom renderers on top of defaults without losing tree-shakability.
|
|
48
|
+
|
|
49
|
+
### `mount(opts): Promise<RenderHandle>`
|
|
50
|
+
|
|
51
|
+
| Option | Type | Description |
|
|
52
|
+
| ------------- | --------------------------------- | ----------- |
|
|
53
|
+
| `source` | `Source` | What to render — `File`, `Blob`, `ArrayBuffer`, `Uint8Array`, `URL`, or `string` URL |
|
|
54
|
+
| `container` | `HTMLElement` | DOM node to mount into. Cleared before render. |
|
|
55
|
+
| `registry` | `Registry` | Where to look up the renderer |
|
|
56
|
+
| `options?` | `Record<string, unknown>` | Per-render options, forwarded to the matched renderer |
|
|
57
|
+
| `signal?` | `AbortSignal` | Cancels in-flight rendering |
|
|
58
|
+
| `rendererId?` | `string` | Force-pick a renderer by id (skips matching) |
|
|
59
|
+
| `t?` | `(key, fallback) => string` | Translation hook for user-facing strings |
|
|
60
|
+
|
|
61
|
+
Returns a `RenderHandle` with a required `destroy()` and optional renderer-specific `capabilities` (e.g. `pageCount` / `scrollToPage` from PDF, `sheetNames` / `showSheet` from XLSX).
|
|
62
|
+
|
|
63
|
+
## Define your own renderer
|
|
20
64
|
|
|
21
65
|
```ts
|
|
22
66
|
import type { Renderer } from '@microscope-js/core';
|
|
23
67
|
|
|
24
68
|
export const myRenderer: Renderer = {
|
|
25
69
|
id: 'myformat',
|
|
26
|
-
name: 'My
|
|
70
|
+
name: 'My format',
|
|
27
71
|
mimes: ['application/x-myformat'],
|
|
28
72
|
extensions: ['myf'],
|
|
29
73
|
async render({ source, container, signal }) {
|
|
30
|
-
//
|
|
74
|
+
// …draw `source.blob` into `container`, honor `signal` for cancellation
|
|
31
75
|
return {
|
|
32
76
|
destroy() { /* clean up */ },
|
|
33
77
|
};
|
|
@@ -35,4 +79,18 @@ export const myRenderer: Renderer = {
|
|
|
35
79
|
};
|
|
36
80
|
```
|
|
37
81
|
|
|
38
|
-
Renderers are
|
|
82
|
+
Renderers are plain values — no class instantiation, no global state. The registry handles matching by MIME + extension, with an optional `canRender()` override for byte-sniffing.
|
|
83
|
+
|
|
84
|
+
## Re-exports
|
|
85
|
+
|
|
86
|
+
For convenience the core barrel also exports `MicroscopeError`, `Source`, `NormalizedSource`, `normalizeSource`, `sniffMime`, and `extOf` from `@microscope-js/utils` — so simple consumers don't need a second install.
|
|
87
|
+
|
|
88
|
+
## See also
|
|
89
|
+
|
|
90
|
+
- [`@microscope-js/react`](https://www.npmjs.com/package/@microscope-js/react) — `<Viewer />` and `useViewer()` for React / Next.js
|
|
91
|
+
- [`@microscope-js/utils`](https://www.npmjs.com/package/@microscope-js/utils) — shared low-level helpers
|
|
92
|
+
- [Repository](https://github.com/shubham8550/microscope-js) · [Live demo](https://shubham8550.github.io/microscope-js) · [API docs](https://shubham8550.github.io/microscope-js/docs)
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
[MIT](https://github.com/shubham8550/microscope-js/blob/main/LICENSE)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microscope-js/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Framework-agnostic core for microscope-js: Renderer interface + registry + mount() entry point",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"README.md"
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@microscope-js/utils": "0.1.
|
|
23
|
+
"@microscope-js/utils": "0.1.5"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"tsup": "^8.3.5",
|