自定义工具栏
当查看器的控件需要与界面的其余部分一致时,用 toolbar: false 隐藏内置控件,并从你自己的按钮调用方法。这个示例提供缩放、“矫正”、“撤销”和“应用”,并且只在按钮能起作用时才启用它。
change 事件在每次操作之后携带状态,包括由你的按钮发起的操作。load 和 error 报告图片到达或应用修改失败的时刻。
html
<div id="controls">
<button type="button" data-action="zoomIn">放大</button>
<button type="button" data-action="zoomOut">缩小</button>
<button type="button" data-action="edit">编辑</button>
<button type="button" data-action="straighten">矫正</button>
<button type="button" data-action="confirmTool">完成</button>
<button type="button" data-action="cancelTool">取消此工具</button>
<button type="button" data-action="undo">撤销</button>
<button type="button" data-action="cancel">取消</button>
<button type="button" data-action="apply">应用</button>
</div>
<p id="hint"></p>
<div id="viewer" style="height: 480px"></div>ts
import type { PaperPicState } from '@paperpic/core'
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'
const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
src: '/receipt.jpg',
alt: '一张收据',
toolbar: false,
})
const actions: Record<string, () => void> = {
zoomIn: () => pic.zoomIn(),
zoomOut: () => pic.zoomOut(),
edit: () => pic.edit(),
straighten: () => pic.openTool('perspective'),
confirmTool: () => pic.confirmTool(),
cancelTool: () => pic.cancelTool(),
undo: () => pic.undo(),
cancel: () => pic.cancel(),
apply: () => pic.apply(),
}
const buttons = document.querySelectorAll<HTMLButtonElement>('[data-action]')
for (const button of buttons) {
button.addEventListener('click', () => actions[button.dataset.action!]!())
}
function update({ status, mode, activeTool, hint, canUndo, canApply, canConfirm, busy }: PaperPicState) {
const editing = mode === 'edit'
const visible: Record<string, boolean> = {
zoomIn: true,
zoomOut: true,
edit: !editing,
straighten: editing && !activeTool,
confirmTool: Boolean(activeTool),
cancelTool: Boolean(activeTool),
undo: editing && !activeTool,
cancel: editing && !activeTool,
apply: editing && !activeTool,
}
for (const button of buttons) {
const action = button.dataset.action!
button.hidden = !visible[action]
}
document.querySelector<HTMLButtonElement>('[data-action="edit"]')!.disabled = status !== 'ready'
document.querySelector<HTMLButtonElement>('[data-action="undo"]')!.disabled = !canUndo || busy
document.querySelector<HTMLButtonElement>('[data-action="confirmTool"]')!.disabled = !canConfirm
document.querySelector<HTMLButtonElement>('[data-action="apply"]')!.disabled = !canApply || busy
document.querySelector('#hint')!.textContent = hint ?? ''
}
pic.on('change', update)
pic.on('load', () => update(pic.getState()))
pic.on('error', () => update(pic.getState()))
update(pic.getState())自定义滑块
为清理做一个自定义滑块时,先打开该工具。之后每次移动都调用 setFinish(),它会改变清理显示的内容,而 confirmTool() 会把最后的值保存为一次编辑。这里的“完成”按钮还会应用结果,“取消”按钮会退出编辑模式,因此这些控件就是全部界面。
html
<button id="clean-up" type="button">清理</button>
<div id="ruler" hidden>
<label>
灵敏度
<input id="sensitivity" type="range" min="0" max="1" step="0.05" />
</label>
<button id="done" type="button">完成</button>
<button id="cancel" type="button">取消</button>
</div>
<div id="viewer" style="height: 480px"></div>ts
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'
const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
src: '/receipt.jpg',
alt: '一张收据',
toolbar: false,
})
const slider = document.querySelector<HTMLInputElement>('#sensitivity')!
document.querySelector('#clean-up')!.addEventListener('click', () => {
pic.edit()
pic.openTool('finish')
})
slider.addEventListener('input', () => pic.setFinish({ sensitivity: slider.valueAsNumber }))
document.querySelector('#done')!.addEventListener('click', () => {
pic.confirmTool()
pic.apply()
})
document.querySelector('#cancel')!.addEventListener('click', () => {
pic.cancelTool()
pic.cancel()
})
pic.on('change', ({ activeTool, finish }) => {
const open = activeTool === 'finish'
document.querySelector<HTMLElement>('#ruler')!.hidden = !open
document.querySelector<HTMLElement>('#clean-up')!.hidden = open
slider.value = String(finish.sensitivity)
})说明
- 设置
toolbar: false时内置编辑栏也会隐藏,因此“裁剪”和“矫正”会显示手柄,但没有“完成”按钮。你的界面用confirmTool()确认它们,与Enter相同。要不保留修改就关闭它们,请像Esc一样调用cancelTool()。 - 打开“裁剪”或“矫正”时,状态中的
hint会像内置编辑栏一样说明要做什么,例如“Drag the corners”。使用toolbar: false时没有其他地方显示它,所以请显示它。新的提示(例如按下角之后,或“矫正”找到文档时)会随change一起到来。 - 状态中的
canConfirm说明confirmTool()能否保留工具所显示的内容。“矫正”会等到找到文档或用户移动某个角,因此在那之前请禁用你的“完成”按钮。 - 你自己的滑块可以直接调用
setFinish()和setAdjustments(),每次调用都会添加一项修改。若要让整次拖动只留下一项修改,请先用openTool()打开工具:这样同样的方法会改变工具显示的内容,confirmTool()再把它保留下来。 cancel()在放弃修改前不会询问。如果有修改,请先询问用户,例如在canApply为 true 时。- 焦点在查看器上时,键盘快捷键依然有效。参见无障碍。