@paperpic/vue 0.0.0-stage → 0.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.
@@ -0,0 +1,160 @@
1
+ ---
2
+ title: Options
3
+ description: Every option of PaperPic, with its type, its default and what it changes, for the constructor, setOptions() and the props of the Vue and React components.
4
+ ---
5
+
6
+ # Options
7
+
8
+ Options set what the viewer shows and what the user can do with it. Pass them when you create the viewer, or change them later with [`setOptions()`](./methods#setoptions). The Vue and React components take them as props with the same names.
9
+
10
+ ```ts
11
+ const pic = new PaperPic(container, { src: '/receipt.jpg', alt: 'A receipt' })
12
+
13
+ pic.setOptions({ editable: false })
14
+ ```
15
+
16
+ `setOptions()` changes only the options you pass, and `undefined` brings an option back to its default. It never emits `change`.
17
+
18
+ ## Image
19
+
20
+ ### `src`
21
+
22
+ - **Type:** `string | Blob`
23
+ - **Default:** none
24
+
25
+ The image to show: a URL from the same origin, or a `File` or `Blob` attached on the client.
26
+
27
+ Passing the same value again does not reload the image, so the result of an edit stays on display. Call [`reload()`](./methods#reload) to bring back the original. A new value while editing discards the edits.
28
+
29
+ ### `alt`
30
+
31
+ - **Type:** `string`
32
+ - **Default:** `''`
33
+
34
+ Describes the image for screen readers. Without it, screen readers skip the image, as they skip an `<img>` with an empty `alt`.
35
+
36
+ ## Interface
37
+
38
+ ### `editable`
39
+
40
+ - **Type:** `boolean`
41
+ - **Default:** `true`
42
+
43
+ Whether the user can edit the image. With `false`, it is a read-only viewer, and its toolbar adds buttons to rotate the image. That rotation only changes the display. Changing this option while editing discards the edits.
44
+
45
+ ### `toolbar`
46
+
47
+ - **Type:** `boolean`
48
+ - **Default:** `true`
49
+
50
+ Whether to show the built-in toolbar and edit bar. Without them, build your own controls with the [methods](./methods).
51
+
52
+ ### `tools`
53
+
54
+ - **Type:** `readonly ToolId[]`
55
+ - **Default:** every tool
56
+
57
+ The tools the edit bar offers. They keep the order of this table, whatever the order of the array.
58
+
59
+ | Id | Button |
60
+ | --------------- | ----------------------------------- |
61
+ | `'crop'` | Crop |
62
+ | `'perspective'` | Straighten |
63
+ | `'rotate'` | Rotate left |
64
+ | `'flip'` | Flip |
65
+ | `'adjust'` | Adjust, for brightness and contrast |
66
+ | `'threshold'` | Clean up |
67
+
68
+ ### `wheelZoom`
69
+
70
+ - **Type:** `boolean`
71
+ - **Default:** `true`
72
+
73
+ Whether the wheel zooms the image. Turn it off when the viewer sits in a long page, so the wheel scrolls the page.
74
+
75
+ ### `download`
76
+
77
+ - **Type:** `boolean`
78
+ - **Default:** `false`
79
+
80
+ Whether the toolbar has a button that saves the image on display: the result of the last Apply, or else the source. The button is hidden while editing. The [`download`](./events#download) event lets you save the file another way.
81
+
82
+ ### `labels`
83
+
84
+ - **Type:** `Partial<PaperPicLabels>`
85
+ - **Default:** the English texts
86
+
87
+ Replaces any of the texts that the component shows or announces. [Labels](./labels) lists every key.
88
+
89
+ ### `icons`
90
+
91
+ - **Type:** `Partial<PaperPicIcons>`
92
+ - **Default:** the built-in icons
93
+
94
+ Replaces any of the icons with your own markup of one element, such as an `<svg>`. The markup goes into the page as HTML, so pass only markup you trust. An empty string leaves the buttons of that icon without one. [Styling](../guide/styling#icons) shows an example.
95
+
96
+ Each key names an icon, and some icons appear on two buttons:
97
+
98
+ | Key | Button |
99
+ | ---------------- | -------------------------------------------------- |
100
+ | `adjust` | Adjust |
101
+ | `apply` | Apply, and Done in every tool |
102
+ | `brightness` | Brightness, in the panel of Adjust |
103
+ | `cancel` | Cancel, and Cancel this tool |
104
+ | `contrast` | Contrast, in the panel of Adjust |
105
+ | `crop` | Crop |
106
+ | `download` | Download |
107
+ | `edit` | Edit |
108
+ | `fitHeight` | Fit to height |
109
+ | `fitWidth` | Fit to width |
110
+ | `flip` | Flip |
111
+ | `perspective` | Straighten |
112
+ | `redo` | Redo |
113
+ | `rotateDocument` | Rotate left, in the edit bar |
114
+ | `rotateLeft` | Rotate left, in the toolbar of a read-only viewer |
115
+ | `rotateRight` | Rotate right, in the toolbar of a read-only viewer |
116
+ | `threshold` | Clean up |
117
+ | `undo` | Undo |
118
+ | `zoomIn` | Zoom in |
119
+ | `zoomOut` | Zoom out |
120
+
121
+ ## Result
122
+
123
+ ### `output`
124
+
125
+ - **Type:** `{ type?: string, quality?: number }`
126
+ - **Default:** the format of the source
127
+
128
+ The format of the edited image. `type` is `'image/jpeg'`, `'image/png'` or `'image/webp'`, and `quality` goes from 0 to 1 for JPEG and WebP, with `0.92` by default.
129
+
130
+ Without a `type`, a cleaned up image is PNG, because JPEG would damage its sharp edges, and any other image keeps the format of the source. A format that the browser cannot encode gives PNG.
131
+
132
+ ### `maxOutputSize`
133
+
134
+ - **Type:** `number`
135
+ - **Default:** the full resolution
136
+
137
+ The longest side of the edited image, in pixels. A larger result is scaled down.
138
+
139
+ ### `filename`
140
+
141
+ - **Type:** `string`
142
+ - **Default:** the name of the source
143
+
144
+ The name of the saved file. Its extension follows the format of the image, so a cleaned up `receipt.jpg` saves as `receipt.png`. When the source has no name, such as a `Blob`, the file is called `image`.
145
+
146
+ ## Tools
147
+
148
+ ### `aspectRatio`
149
+
150
+ - **Type:** `number`
151
+ - **Default:** estimated
152
+
153
+ The real proportions of the document, as width divided by height, such as `210 / 297` for an A4 sheet. Straighten gives its result these proportions. Without them, it estimates them from the photo.
154
+
155
+ ### `softness`
156
+
157
+ - **Type:** `number`, from 0 to 1
158
+ - **Default:** `0.25`
159
+
160
+ How much gray Clean up keeps at the edges of the letters, which makes them easier to read. Use `0` for strict black and white, which gives smaller files but breaks thin strokes.
@@ -0,0 +1,84 @@
1
+ ---
2
+ title: React
3
+ description: How the PaperPic component for React maps the options, events and methods of the core to props, callback props and a ref.
4
+ ---
5
+
6
+ # React
7
+
8
+ `@paperpic/react` wraps the viewer in a component for React 19 or newer. The options are props, the events are callback props, and the methods are on the viewer that `ref` receives.
9
+
10
+ ```tsx
11
+ import type { ExportedImage } from '@paperpic/react'
12
+ import { PaperPic } from '@paperpic/react'
13
+ import '@paperpic/react/style.css'
14
+
15
+ function upload({ blob }: ExportedImage) {
16
+ // Send the edited image to your server
17
+ }
18
+
19
+ export function Receipt() {
20
+ return (
21
+ <div style={{ height: 480 }}>
22
+ <PaperPic src="/receipt.jpg" alt="A receipt" onApply={upload} />
23
+ </div>
24
+ )
25
+ }
26
+ ```
27
+
28
+ The component fills its parent element, so give the parent a height.
29
+
30
+ ## Props
31
+
32
+ Every [option](./options) is a prop with the same name, such as `editable={false}` or `aspectRatio={210 / 297}`. Two more props style the element that holds the viewer:
33
+
34
+ | Prop | Type | Description |
35
+ | ----------- | --------------- | --------------------------------- |
36
+ | `className` | `string` | Classes on top of `paperpic-host` |
37
+ | `style` | `CSSProperties` | Inline styles |
38
+
39
+ Objects and arrays, such as `labels` or `tools`, reach the viewer again whenever they are new. Define them outside the component, or with `useMemo`.
40
+
41
+ ## Events
42
+
43
+ | Prop | Payload |
44
+ | ------------ | -------------------------------------- |
45
+ | `onApply` | `{ blob, width, height, type }` |
46
+ | `onChange` | [`PaperPicState`](./state) |
47
+ | `onDownload` | `{ source, filename, preventDefault }` |
48
+ | `onError` | `{ error }` |
49
+ | `onLoad` | `{ width, height }` |
50
+
51
+ [Events](./events) describes each one. A new function does not recreate the viewer, so inline functions are fine. A change of props never calls `onChange`.
52
+
53
+ ## Methods
54
+
55
+ `ref` receives the viewer, of type `PaperPicInstance`, with every [method](./methods). It is `null` until the component mounts:
56
+
57
+ ```tsx
58
+ import type { PaperPicInstance } from '@paperpic/react'
59
+ import { PaperPic } from '@paperpic/react'
60
+ import { useRef } from 'react'
61
+
62
+ export function Viewer({ src }: { src: string }) {
63
+ const picRef = useRef<PaperPicInstance>(null)
64
+
65
+ return (
66
+ <>
67
+ <button type="button" onClick={() => picRef.current?.zoomIn()}>Zoom in</button>
68
+ <div style={{ height: 480 }}>
69
+ <PaperPic ref={picRef} src={src} toolbar={false} />
70
+ </div>
71
+ </>
72
+ )
73
+ }
74
+ ```
75
+
76
+ Passing the same `src` again does not reload the image, so it keeps the result of an edit. To bring back the original, call `picRef.current.reload()`.
77
+
78
+ ## Types
79
+
80
+ `@paperpic/react` exports the types you need, so you do not need to install `@paperpic/core`: `PaperPicProps`, `PaperPicInstance`, `PaperPicState`, `PaperPicEvents`, `PaperPicIcons`, `PaperPicLabels`, `PaperPicOutput`, `PaperPicDownload`, `ExportedImage` and `ToolId`.
81
+
82
+ ## Server-side rendering
83
+
84
+ The component is marked with `'use client'`. With server-side rendering, such as in Next.js, it renders an empty element on the server and creates the viewer in the browser.
@@ -0,0 +1,30 @@
1
+ ---
2
+ title: State
3
+ description: What getState() returns and the change event carries, such as the status of the image, the mode, the zoom, the edits and whether there is something to apply.
4
+ ---
5
+
6
+ # State
7
+
8
+ The state describes what the viewer shows at a given moment. [`getState()`](./methods#getstate) returns it, and the [`change`](./events#change) event carries it. Use it to keep controls of your own in step, such as an Apply button:
9
+
10
+ ```ts
11
+ pic.on('change', ({ canApply, busy }) => {
12
+ applyButton.disabled = !canApply || busy
13
+ })
14
+ ```
15
+
16
+ | Field | Type | Description |
17
+ | ------------- | -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
18
+ | `activeTool` | `ToolId \| null` | The open tool, such as `'crop'` or `'adjust'`, or `null` |
19
+ | `adjustments` | `{ brightness, contrast }` | The brightness and the contrast, each from -1 to 1. Clean up leaves them out. While Adjust is open, the values it shows |
20
+ | `busy` | `boolean` | Whether Apply is running |
21
+ | `canApply` | `boolean` | Whether there are changes to apply. Rotations and flips that cancel out are no change |
22
+ | `canRedo` | `boolean` | Whether there is an edit to bring back |
23
+ | `canUndo` | `boolean` | Whether there is an edit to undo |
24
+ | `fit` | `'image' \| 'width' \| 'height' \| null` | The fit in effect, or `null` once the user has zoomed by hand |
25
+ | `image` | `{ width, height } \| null` | The size of the image on display in pixels, with the edits so far, or `null` without an image |
26
+ | `mode` | `'view' \| 'edit'` | Whether the user is viewing or editing the image |
27
+ | `rotation` | `0 \| 90 \| 180 \| 270` | How far the image on display is turned, in degrees clockwise |
28
+ | `status` | `'empty' \| 'loading' \| 'ready' \| 'error'` | Whether there is an image, and whether it loaded |
29
+ | `threshold` | `{ enabled, sensitivity }` | Whether Clean up is on, and its sensitivity from 0 to 1. While Clean up is open, what it shows |
30
+ | `zoom` | `number` | Screen pixels per pixel of the image |
@@ -0,0 +1,78 @@
1
+ ---
2
+ title: Vue
3
+ description: How the PaperPic component for Vue maps the options, events and methods of the core to props, component events and a template ref.
4
+ ---
5
+
6
+ # Vue
7
+
8
+ `@paperpic/vue` wraps the viewer in a component for Vue 3.5 or newer. The options are props, the events are component events, and the methods are on the `instance` that the component exposes.
9
+
10
+ ```vue
11
+ <script setup lang="ts">
12
+ import type { ExportedImage } from '@paperpic/vue'
13
+ import { PaperPic } from '@paperpic/vue'
14
+ import '@paperpic/vue/style.css'
15
+
16
+ function upload({ blob }: ExportedImage) {
17
+ // Send the edited image to your server
18
+ }
19
+ </script>
20
+
21
+ <template>
22
+ <div style="height: 480px">
23
+ <PaperPic src="/receipt.jpg" alt="A receipt" @apply="upload" />
24
+ </div>
25
+ </template>
26
+ ```
27
+
28
+ The component fills its parent element, so give the parent a height.
29
+
30
+ ## Props
31
+
32
+ Every [option](./options) is a prop with the same name, such as `:editable="false"` or `:aspect-ratio="210 / 297"`. A change of a prop reaches the viewer through `setOptions()`.
33
+
34
+ Objects and arrays that the template builds, such as `:labels="{ apply: t('apply') }"`, are new on every render of the parent, and the viewer receives them again each time. Build them with `computed` instead.
35
+
36
+ ## Events
37
+
38
+ | Event | Payload |
39
+ | ---------- | -------------------------------------- |
40
+ | `apply` | `{ blob, width, height, type }` |
41
+ | `change` | [`PaperPicState`](./state) |
42
+ | `download` | `{ source, filename, preventDefault }` |
43
+ | `error` | `{ error }` |
44
+ | `load` | `{ width, height }` |
45
+
46
+ [Events](./events) describes each one. A change of a prop never emits `change`.
47
+
48
+ ## Methods
49
+
50
+ The component exposes the viewer as `instance`, with every [method](./methods). It is `null` until the component mounts:
51
+
52
+ ```vue
53
+ <script setup lang="ts">
54
+ import { PaperPic } from '@paperpic/vue'
55
+ import { useTemplateRef } from 'vue'
56
+
57
+ const pic = useTemplateRef('pic')
58
+ </script>
59
+
60
+ <template>
61
+ <button type="button" @click="pic?.instance?.zoomIn()">
62
+ Zoom in
63
+ </button>
64
+ <div style="height: 480px">
65
+ <PaperPic ref="pic" src="/receipt.jpg" :toolbar="false" />
66
+ </div>
67
+ </template>
68
+ ```
69
+
70
+ Passing the same `src` again does not reload the image, so it keeps the result of an edit. To bring back the original, call `instance.reload()`.
71
+
72
+ ## Types
73
+
74
+ `@paperpic/vue` exports the types you need, so you do not need to install `@paperpic/core`: `PaperPicProps`, `PaperPicState`, `PaperPicEvents`, `PaperPicIcons`, `PaperPicLabels`, `PaperPicOutput`, `PaperPicDownload`, `ExportedImage` and `ToolId`.
75
+
76
+ ## Server-side rendering
77
+
78
+ With server-side rendering, such as in Nuxt, the component renders an empty element on the server and creates the viewer in the browser.
@@ -0,0 +1,84 @@
1
+ import { ExportedImage, PaperPic, PaperPicDownload, PaperPicEvents, PaperPicIcons, PaperPicIcons as PaperPicIcons$1, PaperPicLabels, PaperPicLabels as PaperPicLabels$1, PaperPicOutput, PaperPicOutput as PaperPicOutput$1, PaperPicState, ToolId, ToolId as ToolId$1 } from "@paperpic/core";
2
+ /**
3
+ * The props of `PaperPic`, which fills its parent element. Give the parent a height, and import the stylesheet once,
4
+ * from `@paperpic/vue/style.css`.
5
+ *
6
+ * @example
7
+ * <script setup lang="ts">
8
+ * import { PaperPic } from '@paperpic/vue'
9
+ * import '@paperpic/vue/style.css'
10
+ * </script>
11
+ *
12
+ * <template>
13
+ * <div style="height: 480px">
14
+ * <PaperPic src="/receipt.jpg" alt="A receipt" @apply="({ blob }) => upload(blob)" />
15
+ * </div>
16
+ * </template>
17
+ */
18
+ interface PaperPicProps {
19
+ /** Describes the image for screen readers. Without it, they skip the image, as they skip an `<img>` with an empty `alt`. */
20
+ alt?: string;
21
+ /** The real proportions of the document, as width divided by height, such as `210 / 297` for A4. Without it, straightening estimates them. */
22
+ aspectRatio?: number;
23
+ /** Whether the toolbar has a button that saves the image on display, which is the result of the last Apply or else the source. */
24
+ download?: boolean;
25
+ /** Whether the image can be edited. With `false` it is a read-only viewer, whose toolbar can also rotate the image. */
26
+ editable?: boolean;
27
+ /** The name of the saved file. By default, the name of the source. The extension follows the format of the image. */
28
+ filename?: string;
29
+ /**
30
+ * Replaces any of the icons with the markup of one element, such as an `<svg>`. The markup is inserted as HTML, so
31
+ * pass only markup you trust. An empty string leaves the buttons of that icon without one.
32
+ */
33
+ icons?: Partial<PaperPicIcons$1>;
34
+ /** Replaces any of the texts, which are in English by default. */
35
+ labels?: Partial<PaperPicLabels$1>;
36
+ /** The longest side of the edited image, in pixels. By default, it keeps the full resolution. */
37
+ maxOutputSize?: number;
38
+ /** The format of the edited image. By default, the format of the source. */
39
+ output?: PaperPicOutput$1;
40
+ /**
41
+ * From 0 to 1. How much gray cleaning up a document keeps, which makes letters easier to read. By default, 0.25.
42
+ * Use 0 for strict black and white, which gives smaller files but breaks thin strokes.
43
+ */
44
+ softness?: number;
45
+ /** The image to show: a URL from the same origin, or a file attached on the client. */
46
+ src?: string | Blob;
47
+ /** Whether to show the built-in toolbar and edit bar. Without them, build your own with the methods of `instance`. */
48
+ toolbar?: boolean;
49
+ /** Which tools the edit bar offers. By default, all of them. */
50
+ tools?: readonly ToolId$1[];
51
+ /** Whether the wheel zooms. Turn it off when the viewer sits in a long page. */
52
+ wheelZoom?: boolean;
53
+ }
54
+ declare const __VLS_export: import("vue").DefineComponent<PaperPicProps, {
55
+ instance: import("vue").ShallowRef<PaperPic | null, PaperPic | null>;
56
+ }, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
57
+ download: (payload: import("@paperpic/core").PaperPicDownload) => any;
58
+ apply: (payload: import("@paperpic/core").ExportedImage) => any;
59
+ change: (payload: import("@paperpic/core").PaperPicState) => any;
60
+ error: (payload: {
61
+ error: Error;
62
+ }) => any;
63
+ load: (payload: {
64
+ height: number;
65
+ width: number;
66
+ }) => any;
67
+ }, string, import("vue").PublicProps, Readonly<PaperPicProps> & Readonly<{
68
+ onDownload?: ((payload: import("@paperpic/core").PaperPicDownload) => any) | undefined;
69
+ onApply?: ((payload: import("@paperpic/core").ExportedImage) => any) | undefined;
70
+ onChange?: ((payload: import("@paperpic/core").PaperPicState) => any) | undefined;
71
+ onError?: ((payload: {
72
+ error: Error;
73
+ }) => any) | undefined;
74
+ onLoad?: ((payload: {
75
+ height: number;
76
+ width: number;
77
+ }) => any) | undefined;
78
+ }>, {
79
+ editable: boolean;
80
+ toolbar: boolean;
81
+ wheelZoom: boolean;
82
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
83
+ declare const _default: typeof __VLS_export;
84
+ export { type ExportedImage, _default as PaperPic, type PaperPicDownload, type PaperPicEvents, type PaperPicIcons, type PaperPicLabels, type PaperPicOutput, type PaperPicProps, type PaperPicState, type ToolId };
package/dist/index.js ADDED
@@ -0,0 +1,68 @@
1
+ import { createElementBlock, defineComponent, onBeforeUnmount, onMounted, openBlock, shallowRef, useTemplateRef, watch } from "vue";
2
+ import { PaperPic } from "@paperpic/core";
3
+ // src/components/PaperPic.vue
4
+ const _sfc_main = /*@__PURE__*/ defineComponent({
5
+ __name: "PaperPic",
6
+ props: {
7
+ alt: {},
8
+ aspectRatio: {},
9
+ download: { type: Boolean },
10
+ editable: {
11
+ type: Boolean,
12
+ default: true
13
+ },
14
+ filename: {},
15
+ icons: {},
16
+ labels: {},
17
+ maxOutputSize: {},
18
+ output: {},
19
+ softness: {},
20
+ src: {},
21
+ toolbar: {
22
+ type: Boolean,
23
+ default: true
24
+ },
25
+ tools: {},
26
+ wheelZoom: {
27
+ type: Boolean,
28
+ default: true
29
+ }
30
+ },
31
+ emits: [
32
+ "apply",
33
+ "change",
34
+ "download",
35
+ "error",
36
+ "load"
37
+ ],
38
+ setup(__props, { expose: __expose, emit: __emit }) {
39
+ const props = __props;
40
+ const emit = __emit;
41
+ const host = useTemplateRef("host");
42
+ /** The viewer, for a custom interface. It is `null` until the component mounts. */
43
+ const instance = shallowRef(null);
44
+ onMounted(() => {
45
+ const pic = new PaperPic(host.value, { ...props });
46
+ pic.on("apply", (payload) => emit("apply", payload));
47
+ pic.on("change", (payload) => emit("change", payload));
48
+ pic.on("download", (payload) => emit("download", payload));
49
+ pic.on("error", (payload) => emit("error", payload));
50
+ pic.on("load", (payload) => emit("load", payload));
51
+ instance.value = pic;
52
+ });
53
+ watch(() => ({ ...props }), (options) => instance.value?.setOptions(options));
54
+ onBeforeUnmount(() => {
55
+ instance.value?.destroy();
56
+ instance.value = null;
57
+ });
58
+ __expose({ instance });
59
+ return (_ctx, _cache) => {
60
+ return openBlock(), createElementBlock("div", {
61
+ ref_key: "host",
62
+ ref: host,
63
+ class: "paperpic-host"
64
+ }, null, 512);
65
+ };
66
+ }
67
+ });
68
+ export { _sfc_main as PaperPic };