Skip to content

Commit 4f498a7

Browse files
refactor: pagination component
1 parent 2b379da commit 4f498a7

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

src/Components/Pagination.vue

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<template>
2+
<div class="inline-flex -space-x-px text-sm">
3+
<button :disabled="!canPreviousPage" class="flex items-center px-3 h-8 ms-0 leading-tight bg-blue-500 border border-e-0 rounded-s-lg hover:bg-blue-700 text-white font-bold disabled:dark:bg-blue-300" @click="onPreviousPage" :class="{'cursor-not-allowed': !canPreviousPage}">Previous</button>
4+
<p class="flex items-center px-3 h-8 leading-tight text-white bg-blue-500 border border-gray-300 font-bold">{{ page }} Of {{ pages }}</p>
5+
<button :disabled="!canNextPage" class="flex items-center px-3 h-8 ms-0 leading-tight bg-blue-500 border border-e-0 rounded-r-lg hover:bg-blue-700 text-white font-bold disabled:dark:bg-blue-300" @click="onNextPage" :class="{'cursor-not-allowed': !canNextPage}">Next</button>
6+
</div>
7+
</template>
8+
9+
<script>
10+
export default {
11+
name: 'PaginationComponent',
12+
data(){
13+
return {
14+
currentPage: 0,
15+
}
16+
},
17+
computed: {
18+
page(){
19+
return this.currentPage + 1
20+
},
21+
canNextPage(){
22+
return this.currentPage + 1 <= this.pages - 1
23+
},
24+
canPreviousPage(){
25+
return this.currentPage > 0
26+
}
27+
},
28+
updated(){
29+
this.$emit('onPageChange',{
30+
page: this.currentPage
31+
})
32+
},
33+
props:{
34+
pages: {
35+
type: Number,
36+
default: 0,
37+
required: true,
38+
},
39+
},
40+
methods: {
41+
onPreviousPage(){
42+
this.currentPage--
43+
},
44+
onNextPage(){
45+
this.currentPage++
46+
},
47+
}
48+
}
49+
</script>

src/VuePdfEditor.vue

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@
7979
</button>
8080

8181
<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>
82+
<Pagination :pages="pages.length" @onPageChange="onPageChange" />
8583
</div>
8684

8785
</div>
@@ -200,6 +198,7 @@
200198
import { fetchFont } from './utils/prepareAssets.js'
201199
202200
import PDFPage from './Components/PDFPage.vue'
201+
import Pagination from './Components/Pagination.vue'
203202
import ImageItem from './Components/Image.vue'
204203
import TextItem from './Components/TextItem.vue'
205204
import Drawing from './Components/Drawing.vue'
@@ -223,6 +222,7 @@ export default {
223222
TextIcon,
224223
GestureIcon,
225224
PencilIcon,
225+
Pagination,
226226
},
227227
props: {
228228
msg: String,
@@ -688,18 +688,9 @@ export default {
688688
this.saving = false
689689
}
690690
},
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-
},
691+
onPageChange({ page }){
692+
this.currentPage = page
693+
}
703694
}
704695
}
705696
</script>

0 commit comments

Comments
 (0)