code: some optimization

This commit is contained in:
ShawnPhang 2024-03-01 23:02:30 +08:00
parent a1c9d30857
commit 00a9fb62c4
6 changed files with 117 additions and 110 deletions

View File

@ -3,7 +3,7 @@
* @Date: 2021-08-27 15:16:07 * @Date: 2021-08-27 15:16:07
* @Description: 素材列表主要用于文字组合列表 * @Description: 素材列表主要用于文字组合列表
* @LastEditors: ShawnPhang <https://m.palxp.cn> * @LastEditors: ShawnPhang <https://m.palxp.cn>
* @LastEditTime: 2024-01-11 17:59:57 * @LastEditTime: 2024-02-29 16:54:28
--> -->
<template> <template>
<div class="wrap"> <div class="wrap">
@ -241,14 +241,14 @@ export default defineComponent({
background: #f8fafc; background: #f8fafc;
} }
&__img { &__img {
cursor: pointer; cursor: grab;
width: 142px; width: 142px;
height: 142px; height: 142px;
padding: 4px; padding: 4px;
border-radius: 4px; border-radius: 4px;
} }
&__img-thumb { &__img-thumb {
cursor: pointer; cursor: grab;
width: 90px; width: 90px;
height: 90px; height: 90px;
background: #f8fafc; background: #f8fafc;

View File

@ -3,7 +3,7 @@
* @Date: 2021-08-27 15:16:07 * @Date: 2021-08-27 15:16:07
* @Description: 素材列表 * @Description: 素材列表
* @LastEditors: ShawnPhang <https://m.palxp.cn> * @LastEditors: ShawnPhang <https://m.palxp.cn>
* @LastEditTime: 2023-11-24 11:14:28 * @LastEditTime: 2024-02-29 16:49:59
--> -->
<template> <template>
<div class="wrap"> <div class="wrap">
@ -217,14 +217,14 @@ export default defineComponent({
background: #f8fafc; background: #f8fafc;
} }
&__img { &__img {
cursor: pointer; cursor: grab;
width: 142px; width: 142px;
height: 142px; height: 142px;
padding: 4px; padding: 4px;
border-radius: 4px; border-radius: 4px;
} }
&__img-thumb { &__img-thumb {
cursor: pointer; cursor: grab;
width: 90px; width: 90px;
height: 90px; height: 90px;
background: #f8fafc; background: #f8fafc;

View File

@ -70,7 +70,7 @@ export default {
{ {
text: '+ 添加文字', text: '+ 添加文字',
fontSize: 60, fontSize: 60,
fontWeight: 'bold', fontWeight: 'normal',
}, },
// { // {
// text: '+ ', // text: '+ ',
@ -97,8 +97,7 @@ export default {
</script> </script>
<style lang="less" scoped> <style lang="less" scoped>
// Color variables (appears count calculates by raw css) // @color0: #3b74f1;
@color0: #3b74f1; // Appears 2 times
#text-list-wrap { #text-list-wrap {
display: flex; display: flex;

View File

@ -3,7 +3,7 @@
* @Date: 2022-02-23 15:48:52 * @Date: 2022-02-23 15:48:52
* @Description: 图片列表组件 Bookshelf Layout * @Description: 图片列表组件 Bookshelf Layout
* @LastEditors: ShawnPhang <https://m.palxp.cn> * @LastEditors: ShawnPhang <https://m.palxp.cn>
* @LastEditTime: 2023-12-11 11:12:04 * @LastEditTime: 2024-02-29 16:52:37
--> -->
<template> <template>
<ul ref="listRef" class="img-list-wrap" :style="{ paddingBottom: isShort ? '15px' : '200px' }" @scroll="scrollEvent($event)"> <ul ref="listRef" class="img-list-wrap" :style="{ paddingBottom: isShort ? '15px' : '200px' }" @scroll="scrollEvent($event)">
@ -207,7 +207,7 @@ export default defineComponent({
&__img { &__img {
// background: #f1f2f4; // background: #f1f2f4;
display: inline-block; display: inline-block;
cursor: pointer; cursor: grab;
// margin: 0 6px 2px 0; // margin: 0 6px 2px 0;
margin-bottom: 3px; margin-bottom: 3px;
border-radius: 2px; border-radius: 2px;

View File

@ -53,11 +53,13 @@
// //
const NAME = 'w-text' const NAME = 'w-text'
import { mapGetters, mapActions } from 'vuex' import { defineComponent, reactive, toRefs, computed, onUpdated, watch, onMounted, ref } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { fontWithDraw } from '@/utils/widgets/loadFontRule' import { fontWithDraw } from '@/utils/widgets/loadFontRule'
import getGradientOrImg from './getGradientOrImg.ts' import getGradientOrImg from './getGradientOrImg.ts'
export default { export default defineComponent({
name: NAME, name: NAME,
setting: { setting: {
name: '文本', name: '文本',
@ -97,116 +99,122 @@ export default {
}, },
}, },
props: ['params', 'parent'], props: ['params', 'parent'],
data() { setup(props) {
return { const store = useStore()
const route = useRoute()
const state = reactive({
loading: false, loading: false,
editable: false, editable: false,
loadFontDone: false, loadFontDone: false,
} })
}, const widget = ref(null)
computed: { const editWrap = ref(null)
...mapGetters(['dActiveElement']),
isDraw() { const dActiveElement = computed(() => store.getters.dActiveElement)
return this.$route.name === 'Draw' && fontWithDraw const isDraw = computed(() => route.name === 'Draw' && fontWithDraw)
},
}, onUpdated(() => {
watch: { updateRecord()
params: { })
async handler(nval) {
this.updateText() onMounted(() => {
if (this.loading) { updateRecord()
props.params.transform && (widget.value.style.transform = props.params.transform)
props.params.rotate && (widget.value.style.transform += `translate(0px, 0px) rotate(${props.params.rotate}) scale(1, 1)`)
// store.commit('updateRect')
})
watch(
() => props.params,
async (nval) => {
updateText()
if (state.loading) {
return return
} }
let font = nval.fontClass let font = nval.fontClass
const isDone = font.value === this.loadFontDone const isDone = font.value === state.loadFontDone
if (font.url && !isDone) { if (font.url && !isDone) {
if (font.id && this.isDraw) { if (font.id && isDraw.value) {
// url // url
// demobug // demobug
this.loading = false state.loading = false
return return
} }
this.loading = !this.isDraw state.loading = !isDraw.value
const loadFont = new window.FontFace(font.value, `url(${font.url})`) const loadFont = new window.FontFace(font.value, `url(${font.url})`)
await loadFont.load() await loadFont.load()
document.fonts.add(loadFont) document.fonts.add(loadFont)
this.loadFontDone = font.value state.loadFontDone = font.value
this.loading = false state.loading = false
} else { } else {
this.loading = false state.loading = false
} }
}, },
immediate: true, { immediate: true, deep: true },
deep: true, )
},
editable(value) { watch(
this.updateWidgetData({ () => state.editable,
uuid: this.params.uuid, (value) => {
key: 'editable', store.dispatch('updateWidgetData', {
value, uuid: props.params.uuid,
pushHistory: false, key: 'editable',
}) value,
}, pushHistory: false,
}, })
updated() { },
this.updateRecord() )
},
async mounted() { function updateRecord() {
this.updateRecord() if (dActiveElement.value.uuid === props.params.uuid) {
// await this.$nextTick() let record = dActiveElement.value.record
this.params.transform && (this.$refs.widget.style.transform = this.params.transform) record.width = widget.value.offsetWidth
this.params.rotate && (this.$refs.widget.style.transform += `translate(0px, 0px) rotate(${this.params.rotate}) scale(1, 1)`) record.height = widget.value.offsetHeight
// this.$store.commit('updateRect') record.minWidth = props.params.fontSize
}, record.minHeight = props.params.fontSize * props.params.lineHeight
methods: { writingText()
...mapActions(['updateWidgetData', 'pushHistory']),
getGradientOrImg,
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
record.minWidth = this.params.fontSize
record.minHeight = this.params.fontSize * this.params.lineHeight
this.writingText()
} }
}, }
updateText(e) {
const value = e ? e.target.innerHTML : this.params.text.replace(/\n/g, '<br/>') function updateText(e) {
// const value = (e ? e.target.innerText : this.params.text).replace(/<br\/>/g, '\r\n').replace(/&nbsp;/g, ' ') const value = e ? e.target.innerHTML : props.params.text.replace(/\n/g, '<br/>')
if (value !== this.params.text) { // const value = (e ? e.target.innerText : props.params.text).replace(/<br\/>/g, '\r\n').replace(/&nbsp;/g, ' ')
this.updateWidgetData({ if (value !== props.params.text) {
uuid: this.params.uuid, store.dispatch('updateWidgetData', {
uuid: props.params.uuid,
key: 'text', key: 'text',
value, value,
pushHistory: false, pushHistory: false,
}) })
} }
}, }
writingText(e) {
// this.updateText(e) function writingText(e) {
// updateText(e)
// TODO: // TODO:
const el = this.$refs.editWrap || this.$refs.widget const el = editWrap.value || widget.value
this.updateWidgetData({ store.dispatch('updateWidgetData', {
uuid: this.params.uuid, uuid: props.params.uuid,
key: 'height', key: 'height',
value: el.offsetHeight, value: el.offsetHeight,
pushHistory: false, pushHistory: false,
}) })
this.$store.commit('updateRect') store.commit('updateRect')
}, }
writeDone(e) {
this.editable = false function writeDone(e) {
state.editable = false
setTimeout(() => { setTimeout(() => {
this.pushHistory('文字修改') store.dispatch('pushHistory', '文字修改')
}, 100) }, 100)
this.updateText(e) updateText(e)
}, }
dblclickText(e) {
// this.$store.commit('setShowMoveable', false) function dblclickText(e) {
this.editable = true // store.commit('setShowMoveable', false)
const el = this.$refs.editWrap || this.$refs.widget state.editable = true
const el = editWrap.value || widget.value
setTimeout(() => { setTimeout(() => {
el.focus() el.focus()
if (document.selection) { if (document.selection) {
@ -220,9 +228,21 @@ export default {
window.getSelection().addRange(range) window.getSelection().addRange(range)
} }
}, 100) }, 100)
}, }
return {
...toRefs(state),
getGradientOrImg,
updateRecord,
writingText,
updateText,
writeDone,
dblclickText,
widget,
editWrap,
}
}, },
} })
</script> </script>
<style lang="less" scoped> <style lang="less" scoped>
@ -247,13 +267,4 @@ export default {
width: 100%; width: 100%;
height: 100%; height: 100%;
} }
// @font-face {
// font-family: 'FONT-AZPPT';
// font-display: swap;
// src: url('./AZPPT.ttf') format('truetype');
// }
// .FONT-AZPPT {
// font-family: 'FONT-AZPPT';
// }
</style> </style>

View File

@ -214,20 +214,17 @@ export default {
for (let i = 0; i < this.styleIconList1.length; ++i) { for (let i = 0; i < this.styleIconList1.length; ++i) {
let key = this.styleIconList1[i].key let key = this.styleIconList1[i].key
this.styleIconList1[i].select = false this.styleIconList1[i].select = false
const [unchecked, checked] = this.styleIconList1[i].value
switch (key) { switch (key) {
case 'fontWeight': case 'fontWeight':
case 'fontStyle':
if (this.innerElement[key] !== 'normal') {
this.styleIconList1[i].select = true
}
break
case 'textDecoration': case 'textDecoration':
if (this.innerElement[key] !== this.styleIconList1[i].value[0] && this.innerElement[key] == this.styleIconList1[i].value[1]) { case 'fontStyle':
if (this.innerElement[key] !== unchecked && this.innerElement[key] == checked) {
this.styleIconList1[i].select = !this.styleIconList1[i].select this.styleIconList1[i].select = !this.styleIconList1[i].select
} }
break break
case 'writingMode': case 'writingMode':
if (this.innerElement[key] !== this.styleIconList1[i].value[0]) { if (this.innerElement[key] !== unchecked) {
this.styleIconList1[i].select = true this.styleIconList1[i].select = true
} }
break break