mirror of
https://github.com/palxiao/poster-design.git
synced 2025-07-15 16:02:19 +08:00
feat: convert wQrcode component to composition API
This commit is contained in:
parent
b8ed7da1bd
commit
de248b0e11
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { onMounted, ref, watch, nextTick } from 'vue'
|
import { onMounted, ref, watch, nextTick } from 'vue'
|
||||||
import QRCodeStyling, {DotType, Options } from 'qr-code-styling'
|
import QRCodeStyling, { Options } from 'qr-code-styling'
|
||||||
import { debounce } from 'throttle-debounce'
|
import { debounce } from 'throttle-debounce'
|
||||||
import { generateOption } from './method'
|
import { generateOption } from './method'
|
||||||
|
|
||||||
@ -20,10 +20,7 @@ export type TQrcodeProps = {
|
|||||||
height?: number
|
height?: number
|
||||||
image?: string
|
image?: string
|
||||||
value?: string
|
value?: string
|
||||||
dotsOptions: {
|
dotsOptions: Options['dotsOptions']
|
||||||
color: string,
|
|
||||||
type: DotType,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = withDefaults(defineProps<TQrcodeProps>(), {
|
const props = withDefaults(defineProps<TQrcodeProps>(), {
|
||||||
|
@ -28,9 +28,10 @@
|
|||||||
import { ref, onMounted } from 'vue'
|
import { ref, onMounted } from 'vue'
|
||||||
import { useStore } from 'vuex'
|
import { useStore } from 'vuex'
|
||||||
import { useRoute } from 'vue-router'
|
import { useRoute } from 'vue-router'
|
||||||
import wQrcode from '../../widgets/wQrcode/wQrcode.vue'
|
// import wQrcode from '../../widgets/wQrcode/wQrcode.vue'
|
||||||
import imageCutout from '@/components/business/image-cutout'
|
import imageCutout from '@/components/business/image-cutout'
|
||||||
import { useSetupMapGetters } from '@/common/hooks/mapGetters'
|
import { useSetupMapGetters } from '@/common/hooks/mapGetters'
|
||||||
|
import { wQrcodeSetting } from '../../widgets/wQrcode/wQrcodeSetting'
|
||||||
|
|
||||||
const store = useStore()
|
const store = useStore()
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
@ -59,7 +60,7 @@ onMounted(() => {
|
|||||||
|
|
||||||
function addQrcode() {
|
function addQrcode() {
|
||||||
store.commit('setShowMoveable', false) // 清理掉上一次的选择
|
store.commit('setShowMoveable', false) // 清理掉上一次的选择
|
||||||
let setting = JSON.parse(JSON.stringify(wQrcode.setting))
|
let setting = JSON.parse(JSON.stringify(wQrcodeSetting))
|
||||||
const { width: pW, height: pH } = dPage.value
|
const { width: pW, height: pH } = dPage.value
|
||||||
setting.left = pW / 2 - setting.width / 2
|
setting.left = pW / 2 - setting.width / 2
|
||||||
setting.top = pH / 2 - setting.height / 2
|
setting.top = pH / 2 - setting.height / 2
|
||||||
|
@ -7,8 +7,8 @@
|
|||||||
-->
|
-->
|
||||||
<template>
|
<template>
|
||||||
<div
|
<div
|
||||||
:id="params.uuid"
|
:id="`${params.uuid}`"
|
||||||
ref="widget"
|
ref="widgetRef"
|
||||||
:class="['w-qrcode', { 'layer-lock': params.lock }]"
|
:class="['w-qrcode', { 'layer-lock': params.lock }]"
|
||||||
:style="{
|
:style="{
|
||||||
position: 'absolute',
|
position: 'absolute',
|
||||||
@ -19,109 +19,101 @@
|
|||||||
opacity: params.opacity,
|
opacity: params.opacity,
|
||||||
}"
|
}"
|
||||||
>
|
>
|
||||||
<QRCode ref="qrcode" v-bind="qrCodeOptions" :width="width" :height="width" class="target" :image="params.url" :value="params.value" />
|
<QRCode
|
||||||
|
ref="qrcode"
|
||||||
|
v-bind="state.qrCodeOptions"
|
||||||
|
:width="width"
|
||||||
|
:height="width"
|
||||||
|
class="target"
|
||||||
|
:image="params.url"
|
||||||
|
:value="params.value"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script setup lang="ts">
|
||||||
// 图片组件
|
// 图片组件
|
||||||
const NAME = 'w-qrcode'
|
// const NAME = 'w-qrcode'
|
||||||
|
|
||||||
import { mapGetters, mapActions } from 'vuex'
|
import { mapGetters, mapActions, useStore } from 'vuex'
|
||||||
import QRCode from '@/components/business/qrcode'
|
import QRCode from '@/components/business/qrcode'
|
||||||
|
import { TWQrcodeSetting } from './wQrcodeSetting';
|
||||||
|
import { computed, nextTick, onMounted, onUpdated, reactive, ref, watch } from 'vue';
|
||||||
|
import { useSetupMapGetters } from '@/common/hooks/mapGetters';
|
||||||
|
import { Options } from 'qr-code-styling';
|
||||||
|
|
||||||
export default {
|
type TProps = {
|
||||||
name: NAME,
|
params: TWQrcodeSetting & {
|
||||||
components: { QRCode },
|
rotate?: number
|
||||||
setting: {
|
lock?: boolean
|
||||||
name: '二维码',
|
}
|
||||||
type: NAME,
|
parent: {
|
||||||
uuid: -1,
|
top: number
|
||||||
width: 300,
|
left: number
|
||||||
height: 300,
|
}
|
||||||
left: 0,
|
}
|
||||||
top: 0,
|
|
||||||
zoom: 1,
|
type TState = {
|
||||||
transform: '',
|
qrCodeOptions: Options
|
||||||
radius: 0,
|
}
|
||||||
opacity: 1,
|
|
||||||
parent: '-1',
|
const store = useStore()
|
||||||
url: '',
|
const props = defineProps<TProps>()
|
||||||
dotType: 'classy',
|
const state = reactive<TState>({
|
||||||
dotColorType: 'single',
|
qrCodeOptions: {}
|
||||||
dotRotation: 270,
|
})
|
||||||
dotColor: '#35495E',
|
const { dActiveElement, dZoom } = useSetupMapGetters(['dActiveElement', 'dZoom'])
|
||||||
dotColor2: '#35495E',
|
const width = computed(() => Number(props.params.width))
|
||||||
value: 'https://xp.palxp.cn',
|
const widgetRef = ref<HTMLElement | null>(null)
|
||||||
setting: [],
|
|
||||||
record: {
|
watch(
|
||||||
width: 0,
|
() => props.params,
|
||||||
height: 0,
|
() => {
|
||||||
minWidth: 10,
|
changeValues()
|
||||||
minHeight: 10,
|
|
||||||
dir: 'all',
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
props: ['params', 'parent'],
|
{ immediate: true, deep: true, }
|
||||||
data() {
|
)
|
||||||
return {
|
|
||||||
qrCodeOptions: {},
|
onUpdated(() => {
|
||||||
}
|
updateRecord()
|
||||||
},
|
store.commit('updateRect')
|
||||||
computed: {
|
})
|
||||||
...mapGetters(['dActiveElement', 'dZoom']),
|
|
||||||
width() {
|
onMounted(async () => {
|
||||||
return Number(this.params.width)
|
updateRecord()
|
||||||
},
|
await nextTick()
|
||||||
},
|
if (widgetRef.value){
|
||||||
watch: {
|
props.params.rotate && (widgetRef.value.style.transform += `rotate(${props.params.rotate})`)
|
||||||
params: {
|
}
|
||||||
async handler(nval) {
|
})
|
||||||
this.changeValues()
|
// ...mapActions(['updateWidgetData']),
|
||||||
|
function updateRecord() {
|
||||||
|
if (dActiveElement.value.uuid === props.params.uuid) {
|
||||||
|
let record = dActiveElement.value.record
|
||||||
|
if (!widgetRef.value) return
|
||||||
|
record.width = widgetRef.value.offsetWidth
|
||||||
|
record.height = widgetRef.value.offsetHeight
|
||||||
|
}
|
||||||
|
// this.updateZoom()
|
||||||
|
}
|
||||||
|
|
||||||
|
function changeValues() {
|
||||||
|
state.qrCodeOptions = {
|
||||||
|
qrOptions: { typeNumber: 0, mode: 'Byte', errorCorrectionLevel: 'H' },
|
||||||
|
// dotsOptions: { color: '#999999' },
|
||||||
|
dotsOptions: {
|
||||||
|
type: props.params.dotType,
|
||||||
|
color: props.params.dotColor,
|
||||||
|
gradient: {
|
||||||
|
type: 'linear',
|
||||||
|
rotation: props.params.dotRotation,
|
||||||
|
colorStops: [
|
||||||
|
{ offset: 0, color: props.params.dotColor },
|
||||||
|
{ offset: 1, color: props.params.dotColorType === 'single' ? props.params.dotColor : props.params.dotColor2 },
|
||||||
|
],
|
||||||
},
|
},
|
||||||
immediate: true,
|
|
||||||
deep: true,
|
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
updated() {
|
|
||||||
this.updateRecord()
|
|
||||||
this.$store.commit('updateRect')
|
|
||||||
},
|
|
||||||
|
|
||||||
async mounted() {
|
|
||||||
this.updateRecord()
|
|
||||||
await this.$nextTick()
|
|
||||||
this.params.rotate && (this.$refs.widget.style.transform += `rotate(${this.params.rotate})`)
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
...mapActions(['updateWidgetData']),
|
|
||||||
updateRecord() {
|
|
||||||
if (this.dActiveElement.uuid === this.params.uuid) {
|
|
||||||
let record = this.dActiveElement.record
|
|
||||||
record.width = this.$refs.widget.offsetWidth
|
|
||||||
record.height = this.$refs.widget.offsetHeight
|
|
||||||
}
|
|
||||||
// this.updateZoom()
|
|
||||||
},
|
|
||||||
changeValues() {
|
|
||||||
this.qrCodeOptions = {
|
|
||||||
qrOptions: { typeNumber: 0, mode: 'Byte', errorCorrectionLevel: 'H' },
|
|
||||||
// dotsOptions: { color: '#999999' },
|
|
||||||
dotsOptions: {
|
|
||||||
type: this.params.dotType,
|
|
||||||
color: this.params.dotColor,
|
|
||||||
gradient: {
|
|
||||||
type: 'linear',
|
|
||||||
rotation: this.params.dotRotation,
|
|
||||||
colorStops: [
|
|
||||||
{ offset: 0, color: this.params.dotColor },
|
|
||||||
{ offset: 1, color: this.params.dotColorType === 'single' ? this.params.dotColor : this.params.dotColor2 },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
61
src/components/modules/widgets/wQrcode/wQrcodeSetting.ts
Normal file
61
src/components/modules/widgets/wQrcode/wQrcodeSetting.ts
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
import { DotType } from "qr-code-styling"
|
||||||
|
|
||||||
|
export type TWQrcodeSetting = {
|
||||||
|
name: string
|
||||||
|
type: string
|
||||||
|
uuid: string | number
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
left: number
|
||||||
|
top: number
|
||||||
|
zoom: number
|
||||||
|
transform: string
|
||||||
|
radius: number
|
||||||
|
opacity: number
|
||||||
|
parent: string
|
||||||
|
url: string
|
||||||
|
dotType: DotType
|
||||||
|
dotColorType: string
|
||||||
|
dotRotation: number
|
||||||
|
dotColor: string
|
||||||
|
dotColor2: string
|
||||||
|
value: string
|
||||||
|
setting: Record<string, any>[]
|
||||||
|
record: {
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
minWidth: number
|
||||||
|
minHeight: number
|
||||||
|
dir: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const wQrcodeSetting: TWQrcodeSetting = {
|
||||||
|
name: '二维码',
|
||||||
|
type: 'w-qrcode',
|
||||||
|
uuid: -1,
|
||||||
|
width: 300,
|
||||||
|
height: 300,
|
||||||
|
left: 0,
|
||||||
|
top: 0,
|
||||||
|
zoom: 1,
|
||||||
|
transform: '',
|
||||||
|
radius: 0,
|
||||||
|
opacity: 1,
|
||||||
|
parent: '-1',
|
||||||
|
url: '',
|
||||||
|
dotType: 'classy',
|
||||||
|
dotColorType: 'single',
|
||||||
|
dotRotation: 270,
|
||||||
|
dotColor: '#35495E',
|
||||||
|
dotColor2: '#35495E',
|
||||||
|
value: 'https://xp.palxp.cn',
|
||||||
|
setting: [],
|
||||||
|
record: {
|
||||||
|
width: 0,
|
||||||
|
height: 0,
|
||||||
|
minWidth: 10,
|
||||||
|
minHeight: 10,
|
||||||
|
dir: 'all',
|
||||||
|
},
|
||||||
|
}
|
@ -1,3 +1,6 @@
|
|||||||
|
<!--
|
||||||
|
TODO: 重构
|
||||||
|
-->
|
||||||
<template>
|
<template>
|
||||||
<div
|
<div
|
||||||
:id="params.uuid"
|
:id="params.uuid"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user