类型
参考和示例中会出现 ExportedImage、ToolId 等类型。本页展示每个类型包含的内容,方便你为自己的代码添加类型。每个类型都链接到讲解它的页面。
从你安装的包中导入类型。@paperpic/vue 和 @paperpic/react 导出了 @paperpic/core 的所有类型,因此无需安装 @paperpic/core:
import type { ExportedImage, PaperPicState, ToolId } from '@paperpic/core'在 Nuxt 应用中,请从 @paperpic/nuxt 导入,它导出了 @paperpic/vue 的所有类型。
选项
PaperPicOptions
interface PaperPicOptions {
alt?: string
aspectRatio?: number
download?: boolean
editable?: boolean
filename?: string
icons?: Partial<PaperPicIcons>
labels?: Partial<PaperPicLabels>
maxOutputSize?: number
output?: PaperPicOutput
softness?: number
src?: PaperPicSource
toolbar?: boolean
tools?: readonly ToolId[]
wheelZoom?: boolean
}构造函数和 setOptions() 接收的内容。在 Vue 和 React 中,它们就是组件的 props。选项逐一讲解。
PaperPicSource
type PaperPicSource = string | Blobsrc 中的图片:URL,或 File、Blob。PaperPicDownload 的 source 也是这个类型。
PaperPicOutput
interface PaperPicOutput {
quality?: number
type?: string
}编辑后图片的格式,用于 output。
ToolId
type ToolId = 'adjust' | 'crop' | 'finish' | 'flip' | 'perspective' | 'redact' | 'rotate'编辑栏中的一个工具,用于 tools、openTool() 和状态的 activeTool。tools 列出了每个工具对应的按钮。
PaperPicLabels
interface PaperPicLabels {
adjust: string
adjustmentLevel: (name: string, value: number) => string
adjustUnavailable: string
applied: string
apply: string
applyError: string
brightness: string
cancel: string
cancelTool: string
confirm: string
contrast: string
cornerBottomLeft: string
cornerBottomRight: string
cornerKeys: string
cornerMoved: (corner: string, x: number, y: number) => string
cornerSelected: (corner: string) => string
cornerTopLeft: string
cornerTopRight: string
crop: string
cropHint: string
discard: string
discardQuestion: string
download: string
edit: string
editBar: string
editing: string
finish: string
finishGray: string
finishLevel: (percent: number) => string
finishMono: string
finishOriginal: string
finishSensitivity: string
fitHeight: string
fitWidth: string
flip: string
inUse: string
keepEditing: string
loadError: string
loading: string
paper: string
paperA4: string
paperAuto: string
paperCard: string
paperLetter: string
perspective: string
perspectiveFound: string
perspectiveHint: string
previewError: string
processing: string
redact: string
redactAngle: (degrees: number) => string
redactArea: string
redactBlack: string
redactHint: string
redactTurn: string
redactTurnKeys: string
redactWhite: string
redo: string
rotateLeft: string
rotateRight: string
toolbar: string
undo: string
viewer: string
zoomIn: string
zoomLevel: (percent: number) => string
zoomOut: string
}界面中的每段文本,用于 labels。包含数值的文本(如缩放级别)是函数。标签给出了每段文本的英文原文。
PaperPicIcons
interface PaperPicIcons {
adjust: string
apply: string
brightness: string
cancel: string
contrast: string
crop: string
download: string
edit: string
finish: string
fitHeight: string
fitWidth: string
flip: string
perspective: string
redact: string
redo: string
rotateDocument: string
rotateLeft: string
rotateRight: string
undo: string
zoomIn: string
zoomOut: string
}界面中的每个图标,以单个元素的标记表示,用于 icons,那里也列出了每个图标所在的按钮。
状态
PaperPicState
interface PaperPicState {
activeTool: ToolId | null
adjustments: { brightness: number, contrast: number }
busy: boolean
canApply: boolean
canConfirm: boolean
canRedo: boolean
canUndo: boolean
finish: { mode: FinishMode, sensitivity: number }
fit: Fit | null
hint: string | null
image: Size | null
mode: 'view' | 'edit'
paper: { size: PaperSize }
redaction: { color: RedactColor }
rotation: Rotation
status: 'empty' | 'loading' | 'ready' | 'error'
zoom: number
}getState() 返回、change 事件携带的内容。状态讲解了每个字段。
FinishMode
type FinishMode = 'original' | 'gray' | 'mono'清理的效果:原始色调、灰度,或 'mono' 的黑白。setFinish() 接收它,状态的 finish 保存它。
PaperSize
type PaperSize = 'auto' | 'a4' | 'letter' | 'card'为矫正的结果提供宽高比的纸张。setPaper() 接收它,状态的 paper 保存它。
RedactColor
type RedactColor = 'black' | 'white'遮盖使用的颜色。setRedaction() 接收它,状态的 redaction 保存它。
Fit
type Fit = 'image' | 'width' | 'height'图片如何适应查看器:完整显示、按宽度或按高度。fit() 接收它,状态的 fit 保存它。
Rotation
type Rotation = 0 | 90 | 180 | 270显示的图片顺时针旋转的角度,位于状态的 rotation 中。
Size
interface Size {
height: number
width: number
}以像素为单位的宽度和高度,例如状态的 image,或 load 事件携带的内容。
事件
PaperPicEvents
interface PaperPicEvents {
apply: ExportedImage
change: PaperPicState
download: PaperPicDownload
error: { error: Error, phase: 'load' | 'preview' | 'apply' }
load: Size
}ExportedImage
interface ExportedImage {
blob: Blob
height: number
type: string
width: number
}apply 事件携带的编辑后图片。type 是 blob 的格式,由 output 选择。
PaperPicDownload
interface PaperPicDownload {
filename: string
preventDefault: () => void
source: PaperPicSource
}download 事件携带的内容。调用 preventDefault() 可以用其他方式保存文件。
方法
ViewPoint
interface ViewPoint {
x: number
y: number
}从查看器左上角起算的点,以 CSS 像素为单位,用于 zoomTo()。
Vue 和 React
@paperpic/vue 和 @paperpic/react 还导出了各自组件的类型,@paperpic/nuxt 导出的类型与 @paperpic/vue 相同。Vue 和 React 介绍了用法。
PaperPicInstance
type PaperPicInstance = PaperPic组件提供的查看器:在 Vue 中是 instance,在 React 中是 ref 接收的值。它就是 @paperpic/core 的 PaperPic 类,拥有全部方法。
PaperPicProps
interface PaperPicProps extends PaperPicOptions {
className?: string
onApply?: (result: PaperPicEvents['apply']) => void
onChange?: (state: PaperPicEvents['change']) => void
onDownload?: (download: PaperPicEvents['download']) => void
onError?: (failure: PaperPicEvents['error']) => void
onLoad?: (size: PaperPicEvents['load']) => void
ref?: Ref<PaperPicInstance>
style?: CSSProperties
}React 中组件的 props:除了各个选项,还有其元素的类名和样式、每个事件的回调以及 ref。在 Vue 中,props 的字段与 PaperPicOptions 相同,事件则是组件事件。