Skip to content

Commit 2b379da

Browse files
feat: simple pagination
1 parent 40b0d1b commit 2b379da

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

src/Components/PDFPage.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export default {
2424
}
2525
},
2626
watch: {
27+
page(){
28+
this.render()
29+
},
2730
scale(newScale) {
2831
this.dynamicScale = newScale // Monitor changes in the scale attribute and update dynamic scaling
2932
this.render()

src/VuePdfEditor.vue

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@
7777
@click="savePDF">
7878
{{ saving ? 'saving' : 'keep' }}
7979
</button>
80+
81+
<div v-if="pages.length" class="flex items-center gap-4">
82+
<button :disabled="currentPage==0" @click="onPreviousPage">Previous</button>
83+
<input class="" v-model="currentPage"/>
84+
<button @click="onNextPage">Next</button>
85+
</div>
86+
8087
</div>
8188
<div v-if="addingDrawing">
8289
<div class="fixed z-10 top-0 left-0 right-0 border-b border-gray-300 bg-white shadow-lg"
@@ -107,27 +114,26 @@
107114

108115
<!-- PDF main body -->
109116
<div class="w-full" style="text-align: center;">
110-
<div v-for="(page,pIndex) in pages" :key="pIndex" style="display: inline-block;">
117+
<div style="display: inline-block;">
111118
<div class="p-5 items-center"
112119
style="text-align: center"
113-
@mousedown="selectPage(pIndex)"
114-
@touchstart="selectPage(pIndex)">
120+
@mousedown="selectPage(currentPage)"
121+
@touchstart="selectPage(currentPage)">
115122
<div style="display: inline-block;"
116-
class="relative shadow-lg"
117-
:class="[pIndex === selectedPageIndex ?'shadowOutline':'']">
118-
<PDFPage :ref="`page${pIndex}`"
123+
class="relative shadow-lg">
124+
<PDFPage :ref="`page${currentPage}`"
119125
:scale="scale"
120-
:data-key="pIndex"
121-
:page="page"
122-
@onMeasure="onMeasure($event, pIndex)" />
126+
:data-key="currentPage"
127+
:page="pages[currentPage]"
128+
@onMeasure="onMeasure($event, currentPage)" />
123129
<div class="absolute top-0 left-0 transform origin-top-left noTouchAction"
124-
:style="{transform: `scale(${pagesScale[pIndex]})`}">
125-
<div v-for="(object, oIndex) in allObjects[pIndex]" :key="oIndex">
130+
:style="{transform: `scale(${pagesScale[currentPage]})`}">
131+
<div v-for="(object, oIndex) in allObjects[currentPage]" :key="oIndex">
126132
<div>
127133
<div v-if="object.type === 'custom'">
128134
<slot name="custom"
129135
:object="object"
130-
:pagesScale="pagesScale[pIndex]"
136+
:pagesScale="pagesScale[currentPage]"
131137
@onUpdate="updateObject(object.id, $event)"
132138
@onDelete="deleteObject(object.id)" />
133139
</div>
@@ -141,7 +147,7 @@
141147
:height="object.height"
142148
:origin-width="object.originWidth"
143149
:origin-height="object.originHeight"
144-
:page-scale="pagesScale[pIndex]"
150+
:page-scale="pagesScale[currentPage]"
145151
@onUpdate="updateObject(object.id, $event)"
146152
@onDelete="deleteObject(object.id)" />
147153
</div>
@@ -157,7 +163,7 @@
157163
:line-height="object.lineHeight"
158164
:font-family="object.fontFamily"
159165
:current-page="object.currentPage"
160-
:page-scale="pagesScale[pIndex]"
166+
:page-scale="pagesScale[currentPage]"
161167
@onUpdate="updateObject(object.id, $event)"
162168
@onDelete="deleteObject(object.id)"
163169
@onSelectFont="selectFontFamily" />
@@ -169,7 +175,7 @@
169175
:width="object.width"
170176
:origin-width="object.originWidth"
171177
:origin-height="object.originHeight"
172-
:page-scale="pagesScale[pIndex]"
178+
:page-scale="pagesScale[currentPage]"
173179
@onUpdate="updateObject(object.id, $event)"
174180
@onDelete="deleteObject(object.id)" />
175181
</div>
@@ -324,6 +330,7 @@ export default {
324330
numPages: null,
325331
pdfDocument: null,
326332
pages: [],
333+
currentPage: 0,
327334
pagesScale: [],
328335
allObjects: [],
329336
currentFont: 'Courier',
@@ -397,20 +404,20 @@ export default {
397404
for (let i = 0; i < this.pages.length; i++) {
398405
this.selectedPageIndex = i
399406
for (let j = 0; j < this.initTextFields.length; j++) {
400-
const text = this.initTextFields[j]
401-
this.addTextField(text, 0, j * 60, this.selectedPageIndex)
407+
// const text = this.initTextFields[j]
408+
// this.addTextField(text, 0, j * 60, this.selectedPageIndex)
402409
}
403410
}
404411
this.selectedPageIndex = 0
405412
const checker = setInterval(() => {
406-
if (this.$refs.textItem.length === this.initTextFields.length * this.pages.length) {
407-
document.getElementById('pdfBody').dispatchEvent(new MouseEvent('mousedown', {
408-
bubbles: true,
409-
cancelable: true,
410-
view: window,
411-
}))
413+
// if (this.$refs.textItem.length === this.initTextFields.length * this.pages.length) {
414+
// document.getElementById('pdfBody').dispatchEvent(new MouseEvent('mousedown', {
415+
// bubbles: true,
416+
// cancelable: true,
417+
// view: window,
418+
// }))
412419
clearInterval(checker)
413-
}
420+
// }
414421
}, 100)
415422
416423
},
@@ -681,7 +688,19 @@ export default {
681688
this.saving = false
682689
}
683690
},
684-
},
691+
onPreviousPage(){
692+
const previousPage = this.currentPage-1
693+
const canSetPreviousPage = previousPage >= 0
694+
if(!canSetPreviousPage) return;
695+
this.currentPage = previousPage;
696+
},
697+
onNextPage(){
698+
const nextPage = this.currentPage+1
699+
const canSetNextPage = nextPage <= this.pages.length-1
700+
if(!canSetNextPage) return;
701+
this.currentPage = nextPage;
702+
},
703+
}
685704
}
686705
</script>
687706

0 commit comments

Comments
 (0)