|
@@ -32,8 +32,6 @@ export default class AddImage {
|
|
|
this.onMousedown = this.handleMouseDown.bind(this)
|
|
|
this.onMouseup = this.handleMouseUp.bind(this)
|
|
|
this.onMousemove = this.handleMouseMove.bind(this)
|
|
|
-
|
|
|
- this.init()
|
|
|
}
|
|
|
|
|
|
init () {
|
|
@@ -80,6 +78,58 @@ export default class AddImage {
|
|
|
this.container.removeEventListener('touchstart', this.onMousedown)
|
|
|
}
|
|
|
|
|
|
+ getRect (width, height, point) {
|
|
|
+ const pageWidth = this.viewport.viewBox[2]
|
|
|
+ const pageHeight = this.viewport.viewBox[3]
|
|
|
+ const fixedWidth = pageWidth / 2
|
|
|
+ const fixedHeight = pageHeight / 2
|
|
|
+
|
|
|
+ let scaledWidth = width
|
|
|
+ let scaledHeight = height
|
|
|
+
|
|
|
+ if (width > fixedWidth || height > fixedHeight) {
|
|
|
+ const imageRatio = width / height
|
|
|
+ const rectRatio = fixedWidth / fixedHeight
|
|
|
+
|
|
|
+ if (imageRatio > rectRatio) {
|
|
|
+ scaledWidth = fixedWidth
|
|
|
+ scaledHeight = fixedWidth / imageRatio
|
|
|
+ } else {
|
|
|
+ scaledWidth = fixedHeight * imageRatio
|
|
|
+ scaledHeight = fixedHeight
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const rect = {
|
|
|
+ left: point.x - scaledWidth / 2,
|
|
|
+ top: point.y - scaledHeight / 2,
|
|
|
+ right: point.x + scaledWidth / 2,
|
|
|
+ bottom: point.y + scaledHeight / 2,
|
|
|
+ }
|
|
|
+
|
|
|
+ if (rect.left < 7) {
|
|
|
+ const offset = 7 - rect.left
|
|
|
+ rect.left += offset
|
|
|
+ rect.right += offset
|
|
|
+ }
|
|
|
+ if (rect.top < 7) {
|
|
|
+ const offset = 7 - rect.top
|
|
|
+ rect.top += offset
|
|
|
+ rect.bottom += offset
|
|
|
+ }
|
|
|
+ if (rect.right > pageWidth) {
|
|
|
+ const offset = rect.right - pageWidth + 7
|
|
|
+ rect.right -= offset
|
|
|
+ rect.left -= offset
|
|
|
+ }
|
|
|
+ if (rect.bottom > pageHeight) {
|
|
|
+ const offset = rect.bottom - pageHeight + 7
|
|
|
+ rect.bottom -= offset
|
|
|
+ rect.top -= offset
|
|
|
+ }
|
|
|
+ return rect
|
|
|
+ }
|
|
|
+
|
|
|
handleMouseDown (event) {
|
|
|
if (this.contentContainer.selectedFrameIndex !== -1) return
|
|
|
if (!event.target.className && !event.target.className.includes('contentContainer')) return
|
|
@@ -88,6 +138,7 @@ export default class AddImage {
|
|
|
const { x, y } = getAbsoluteCoordinate(this.container, event)
|
|
|
this.initStartPoint = { x, y }
|
|
|
|
|
|
+ if (this.contentContainer.pageViewer.toolMode === 'annotation') return
|
|
|
document.addEventListener('mousemove', this.onMousemove)
|
|
|
document.addEventListener('mouseup', this.onMouseup)
|
|
|
|
|
@@ -137,16 +188,16 @@ export default class AddImage {
|
|
|
scaledHeight = area.height
|
|
|
}
|
|
|
|
|
|
- const centerPoint = {
|
|
|
+ const point = {
|
|
|
x: area.left + area.width / 2,
|
|
|
y: area.top + area.height / 2
|
|
|
}
|
|
|
|
|
|
const rect = {
|
|
|
- left: centerPoint.x - scaledWidth / 2,
|
|
|
- top: centerPoint.y - scaledHeight / 2,
|
|
|
- right: centerPoint.x + scaledWidth / 2,
|
|
|
- bottom: centerPoint.y + scaledHeight / 2,
|
|
|
+ left: point.x - scaledWidth / 2,
|
|
|
+ top: point.y - scaledHeight / 2,
|
|
|
+ right: point.x + scaledWidth / 2,
|
|
|
+ bottom: point.y + scaledHeight / 2,
|
|
|
}
|
|
|
|
|
|
this.contentContainer.addImageEditor({
|
|
@@ -158,59 +209,13 @@ export default class AddImage {
|
|
|
console.error(error)
|
|
|
})
|
|
|
} else { // 单击区域
|
|
|
- const centerPoint = getInitialPoint(initStartPoint, this.viewport, this.viewport.scale)
|
|
|
- const pageWidth = this.viewport.viewBox[2]
|
|
|
- const pageHeight = this.viewport.viewBox[3]
|
|
|
- const fixedWidth = pageWidth / 2
|
|
|
- const fixedHeight = pageHeight / 2
|
|
|
+ const point = getInitialPoint(initStartPoint, this.viewport, this.viewport.scale)
|
|
|
|
|
|
this.uploadFile()
|
|
|
.then((data) => {
|
|
|
const { imageBase64, width, height } = data
|
|
|
|
|
|
- let scaledWidth = width
|
|
|
- let scaledHeight = height
|
|
|
-
|
|
|
- if (width > fixedWidth || height > fixedHeight) {
|
|
|
- const imageRatio = width / height
|
|
|
- const rectRatio = fixedWidth / fixedHeight
|
|
|
-
|
|
|
- if (imageRatio > rectRatio) {
|
|
|
- scaledWidth = fixedWidth
|
|
|
- scaledHeight = fixedWidth / imageRatio
|
|
|
- } else {
|
|
|
- scaledWidth = fixedHeight * imageRatio
|
|
|
- scaledHeight = fixedHeight
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- const rect = {
|
|
|
- left: centerPoint.x - scaledWidth / 2,
|
|
|
- top: centerPoint.y - scaledHeight / 2,
|
|
|
- right: centerPoint.x + scaledWidth / 2,
|
|
|
- bottom: centerPoint.y + scaledHeight / 2,
|
|
|
- }
|
|
|
-
|
|
|
- if (rect.left < 7) {
|
|
|
- const offset = 7 - rect.left
|
|
|
- rect.left += offset
|
|
|
- rect.right += offset
|
|
|
- }
|
|
|
- if (rect.top < 7) {
|
|
|
- const offset = 7 - rect.top
|
|
|
- rect.top += offset
|
|
|
- rect.bottom += offset
|
|
|
- }
|
|
|
- if (rect.right > pageWidth) {
|
|
|
- const offset = rect.right - pageWidth + 7
|
|
|
- rect.right -= offset
|
|
|
- rect.left -= offset
|
|
|
- }
|
|
|
- if (rect.bottom > pageHeight) {
|
|
|
- const offset = rect.bottom - pageHeight + 7
|
|
|
- rect.bottom -= offset
|
|
|
- rect.top -= offset
|
|
|
- }
|
|
|
+ const rect = this.getRect(width, height, point)
|
|
|
|
|
|
this.contentContainer.addImageEditor({
|
|
|
rect,
|