<template>
|
<div class="box attr-item-box" v-if="isOne && isMatchType">
|
<!-- <h3>字体属性</h3> -->
|
<el-divider content-position="left"><h4>字体属性</h4></el-divider>
|
<div>
|
<!-- <Divider plain orientation="left">{{ $t('attributes.font') }}</Divider> -->
|
<div class="flex-view">
|
<div class="flex-item">
|
<!-- <div class="left font-selector">
|
<Select v-model="baseAttr.fontFamily" @on-change="changeFontFamily">
|
<Option v-for="item in fontsList" :value="item.name" :key="`font-${item.name}`">
|
<div class="font-item" :style="`background-image:url('${item.img}');`">
|
{{ !item.img ? item : '' }}
|
<span style="display: none">{{ item.name }}</span>
|
</div>
|
</Option>
|
</Select>
|
</div> -->
|
<div class="right2">
|
<el-input-number
|
v-model="baseAttr.fontSize"
|
@change="(value) => changeCommon('fontSize', value)"
|
:min="1"
|
controls-position="right"
|
>
|
<template #prefix>字号</template>
|
</el-input-number>
|
</div>
|
</div>
|
</div>
|
|
<div class="flex-view">
|
<div class="flex-item">
|
<el-radio-group
|
class="button-group"
|
v-model="baseAttr.textAlign"
|
@change="(value) => changeCommon('textAlign', value)"
|
>
|
<el-radio-button v-for="(item, i) in textAlignList" :value="item" :key="item">
|
<span v-html="textAlignListSvg[i]"></span>
|
</el-radio-button>
|
</el-radio-group>
|
</div>
|
</div>
|
|
<div class="flex-view">
|
<div class="flex-item">
|
<el-radio-group class="button-group">
|
<el-button @click="changeFontWeight('fontWeight', baseAttr.fontWeight)">
|
<fontWeight
|
:fill="baseAttr.fontWeight === 'bold' ? '#305ef4' : '#666'"
|
width="14"
|
height="14"
|
></fontWeight>
|
</el-button>
|
<el-button @click="changeFontStyle('fontStyle', baseAttr.fontStyle)">
|
<fontStyle
|
:fill="baseAttr.fontStyle === 'italic' ? '#305ef4' : '#666'"
|
width="14"
|
height="14"
|
></fontStyle>
|
</el-button>
|
<el-button @click="changeLineThrough('linethrough', baseAttr.linethrough)">
|
<linethrough
|
:fill="baseAttr.linethrough ? '#305ef4' : '#666'"
|
width="14"
|
height="14"
|
></linethrough>
|
</el-button>
|
<el-button @click="changeUnderline('underline', baseAttr.underline)">
|
<underline
|
:fill="baseAttr.underline ? '#305ef4' : '#666'"
|
width="14"
|
height="14"
|
></underline>
|
</el-button>
|
</el-radio-group>
|
</div>
|
</div>
|
|
<el-row class="flex-view">
|
<el-col :span="12">
|
<el-input-number
|
v-model="baseAttr.lineHeight"
|
@change="(value) => changeCommon('lineHeight', value)"
|
:step="0.1"
|
controls-position="right"
|
style="width: 100%"
|
>
|
<template #prefix>行高</template>
|
</el-input-number>
|
</el-col>
|
<el-col :span="12">
|
<el-input-number
|
v-model="baseAttr.charSpacing"
|
@change="(value) => changeCommon('charSpacing', value)"
|
controls-position="right"
|
style="margin-left: 10px; width: 100%"
|
>
|
<template #prefix>间距</template>
|
</el-input-number>
|
</el-col>
|
</el-row>
|
|
<!-- <div class="flex-view">
|
<div class="flex-item">
|
<span class="label">{{ $t('background') }}</span>
|
<div class="content">
|
<ColorPicker
|
v-model="baseAttr.textBackgroundColor"
|
@on-change="(value) => changeCommon('textBackgroundColor', value)"
|
alpha
|
/>
|
</div>
|
</div>
|
</div> -->
|
</div>
|
<!-- <Divider plain></Divider> -->
|
</div>
|
</template>
|
|
<script setup name="AttrBute">
|
import useSelect from '@/fabric-editor/hooks/select';
|
// import InputNumber from '@/components/inputNumber';
|
import fontWeight from '@/fabric-editor/assets/icon/attribute/fontWeight.svg?component';
|
import fontStyle from '@/fabric-editor/assets/icon/attribute/fontStyle.svg?component';
|
import linethrough from '@/fabric-editor/assets/icon/attribute/linethrough.svg?component';
|
import underline from '@/fabric-editor/assets/icon/attribute/underline.svg?component';
|
|
import textAlignLeft from '@/fabric-editor/assets/icon/attribute/textAlignLeft.svg?raw';
|
import textAlignRight from '@/fabric-editor/assets/icon/attribute/textAlignRight.svg?raw';
|
import textAlignCenter from '@/fabric-editor/assets/icon/attribute/textAlignCenter.svg?raw';
|
import textAlignJustitfy from '@/fabric-editor/assets/icon/attribute/textAlignJustitfy.svg?raw';
|
import { ElLoading } from 'element-plus';
|
import { TemplateParamObjectName } from '@/fabric-editor/customObject';
|
|
const update = getCurrentInstance();
|
|
// 文字元素
|
const textType = ['i-text', 'textbox', 'text', TemplateParamObjectName];
|
const { canvasEditor, isMatchType, isOne } = useSelect(textType);
|
|
// 属性值
|
const baseAttr = reactive({
|
fontSize: 0,
|
fontFamily: '',
|
lineHeight: 0,
|
charSpacing: 0,
|
fontWeight: '',
|
textBackgroundColor: '#fff',
|
textAlign: '',
|
fontStyle: '',
|
underline: false,
|
linethrough: false,
|
overline: false,
|
});
|
|
const fontsList = ref([]);
|
canvasEditor.getFontList().then((list) => {
|
fontsList.value = list;
|
});
|
|
// 字体对齐方式
|
const textAlignList = ['left', 'center', 'right', 'justify'];
|
// 对齐图标
|
const textAlignListSvg = [textAlignLeft, textAlignCenter, textAlignRight, textAlignJustitfy];
|
|
// 属性获取
|
const getObjectAttr = (e) => {
|
const activeObject = canvasEditor.canvas.getActiveObject();
|
// 不是当前obj,跳过
|
if (e && e.target && e.target !== activeObject) return;
|
if (activeObject && isMatchType) {
|
baseAttr.fontSize = activeObject.get('fontSize');
|
baseAttr.fontFamily = activeObject.get('fontFamily');
|
baseAttr.lineHeight = activeObject.get('lineHeight');
|
baseAttr.textAlign = activeObject.get('textAlign');
|
baseAttr.underline = activeObject.get('underline');
|
baseAttr.linethrough = activeObject.get('linethrough');
|
baseAttr.charSpacing = activeObject.get('charSpacing');
|
baseAttr.overline = activeObject.get('overline');
|
baseAttr.fontStyle = activeObject.get('fontStyle');
|
baseAttr.textBackgroundColor = activeObject.get('textBackgroundColor');
|
baseAttr.fontWeight = activeObject.get('fontWeight');
|
}
|
};
|
|
// 通用属性改变
|
const changeCommon = (key, value) => {
|
const activeObject = canvasEditor.canvas.getActiveObjects()[0];
|
if (activeObject) {
|
activeObject && activeObject.set(key, value);
|
canvasEditor.canvas.renderAll();
|
}
|
};
|
|
const selectCancel = () => {
|
update?.proxy?.$forceUpdate();
|
};
|
|
//@ts-ignore
|
let loadingInstance;
|
|
const changeFontFamily = async (fontName) => {
|
if (!fontName) return;
|
loadingInstance = ElLoading.service({
|
fullscreen: true,
|
lock: false,
|
background: 'transparent',
|
});
|
canvasEditor.loadFont(fontName).finally(() => {
|
if (loadingInstance) {
|
loadingInstance.close();
|
}
|
});
|
};
|
const changeFontWeight = (key, value) => {
|
const nValue = value === 'normal' ? 'bold' : 'normal';
|
baseAttr.fontWeight = nValue;
|
const activeObject = canvasEditor.canvas.getActiveObjects()[0];
|
activeObject && activeObject.set(key, nValue);
|
canvasEditor.canvas.renderAll();
|
};
|
|
// 斜体
|
const changeFontStyle = (key, value) => {
|
const nValue = value === 'normal' ? 'italic' : 'normal';
|
baseAttr.fontStyle = nValue;
|
const activeObject = canvasEditor.canvas.getActiveObjects()[0];
|
activeObject && activeObject.set(key, nValue);
|
canvasEditor.canvas.renderAll();
|
};
|
|
// 中划
|
const changeLineThrough = (key, value) => {
|
const nValue = value === false;
|
baseAttr.linethrough = nValue;
|
const activeObject = canvasEditor.canvas.getActiveObjects()[0];
|
activeObject && activeObject.set(key, nValue);
|
canvasEditor.canvas.renderAll();
|
};
|
|
// 下划
|
const changeUnderline = (key, value) => {
|
const nValue = value === false;
|
baseAttr.underline = nValue;
|
const activeObject = canvasEditor.canvas.getActiveObjects()[0];
|
activeObject && activeObject.set(key, nValue);
|
canvasEditor.canvas.renderAll();
|
};
|
|
onMounted(() => {
|
// 获取字体数据
|
getObjectAttr();
|
canvasEditor.on('selectCancel', selectCancel);
|
canvasEditor.on('selectOne', getObjectAttr);
|
canvasEditor.canvas.on('object:modified', getObjectAttr);
|
});
|
|
onBeforeUnmount(() => {
|
canvasEditor.off('selectCancel', selectCancel);
|
canvasEditor.off('selectOne', getObjectAttr);
|
canvasEditor.canvas.off('object:modified', getObjectAttr);
|
});
|
</script>
|
|
<style scoped lang="scss">
|
:deep(.ivu-input-number) {
|
display: block;
|
width: 100%;
|
}
|
|
:deep(.ivu-color-picker) {
|
display: block;
|
}
|
|
.ivu-row {
|
margin-bottom: 8px;
|
|
.ivu-col {
|
position: inherit;
|
|
&__box {
|
display: flex;
|
align-items: center;
|
border-radius: 4px;
|
background: #f8f8f8;
|
gap: 8px;
|
}
|
}
|
|
.label {
|
padding-left: 8px;
|
}
|
|
.content {
|
flex: 1;
|
|
:deep(.--input),
|
:deep(.ivu-select-selection) {
|
border: none !important;
|
background-color: transparent;
|
box-shadow: none !important;
|
}
|
}
|
}
|
|
.font-selector {
|
:deep(.ivu-select-item) {
|
padding: 1px 4px;
|
}
|
|
.font-item {
|
width: 280px;
|
height: 40px;
|
background-repeat: no-repeat;
|
background-size: auto 28px;
|
}
|
}
|
|
.flex-view {
|
display: inline-flex;
|
justify-content: space-between;
|
margin-bottom: 5px;
|
padding: 5px;
|
width: 100%;
|
border-radius: 5px;
|
background: #f6f7f9;
|
}
|
|
.flex-item {
|
display: inline-flex;
|
flex: 1;
|
|
.label {
|
display: inline-block;
|
width: 32px;
|
height: 32px;
|
font-size: 14px;
|
line-height: 32px;
|
// color: #333333;
|
}
|
|
.content {
|
flex: 1;
|
// width: 60px;
|
}
|
|
.slider-box {
|
margin-left: 10px;
|
width: calc(100% - 50px);
|
}
|
|
.left {
|
flex: 1;
|
}
|
|
.right {
|
flex: 1;
|
margin-left: 10px;
|
|
:deep(.ivu-input-number) {
|
display: block;
|
width: 100%;
|
}
|
}
|
|
:deep(.ivu-slider-wrap) {
|
margin: 13px 0;
|
}
|
|
:deep(.ivu-radio-group-button, ) {
|
display: flex;
|
flex: 1;
|
width: 100%;
|
|
& .ivu-radio-wrapper {
|
// width: 48px;
|
flex: 1;
|
line-height: 40px;
|
text-align: center;
|
|
svg {
|
vertical-align: baseline;
|
}
|
}
|
}
|
|
:deep() {
|
.el-radio-group {
|
width: 100%;
|
|
.el-radio-button {
|
flex: 1;
|
min-width: 0;
|
|
.el-radio-button__inner {
|
width: 100%;
|
}
|
}
|
}
|
}
|
|
:deep(.ivu-btn-group) {
|
display: flex;
|
flex: 1;
|
|
.ivu-btn {
|
flex: 1;
|
}
|
}
|
|
:deep(.ivu-btn-group-large) {
|
& > .ivu-btn {
|
font-size: 24px;
|
flex: 1;
|
}
|
}
|
|
:deep(.ivu-radio-group-button) {
|
&.ivu-radio-group-large .ivu-radio-wrapper {
|
font-size: 24px;
|
}
|
}
|
}
|
</style>
|