zhengyiming
17 小时以前 745f1e2ee7072731611391b89c5c0020783828bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!--
 * @Author: 秦少卫
 * @Date: 2023-08-05 17:47:35
 * @LastEditors: 秦少卫
 * @LastEditTime: 2024-07-06 16:55:25
 * @Description: 字体样式
-->
 
<template>
  <div>
    <!-- 搜索组件 -->
    <searchType
      ref="selectTypeRef"
      :typeListApi="getFontStyleTypes"
      @change="searchChange"
    ></searchType>
 
    <!-- 分类列表 -->
    <typeList
      v-show="!filters.font_style_type.$contains && !filters.name.$contains"
      :typeApi="getFontStyleTypes"
      :typeListApi="getFontStyleListByType"
      typeKey="font_style_type"
      :formatData="formatData"
      @selectType="selectType"
      @click="addItem"
      @dragend="dragItem"
    ></typeList>
 
    <!-- 搜索分页列表 -->
    <pageList
      v-if="filters.font_style_type.$contains || filters.name.$contains"
      DOMId="fontMaterialList"
      :pageListApi="getFontStyles"
      :filters="filters"
      :formatData="formatData"
      @click="addItem"
      @dragend="dragItem"
    ></pageList>
  </div>
</template>
 
<script setup name="ImportSvg">
import searchType from '@/components/common/searchType';
import typeList from '@/components/common/typeList.vue';
import pageList from '@/components/common/pageList.vue';
import useSelect from '@/hooks/select';
import useCalculate from '@/hooks/useCalculate';
import { getMaterialInfoUrl, getMaterialPreviewUrl } from '@/hooks/usePageList';
import { getFontStyleTypes, getFontStyleListByType, getFontStyles } from '@/api/material';
import { fabric } from 'fabric';
import { v4 as uuid } from 'uuid';
import { Spin } from 'view-ui-plus';
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
 
const { canvasEditor } = useSelect();
 
const { isOutsideCanvas } = useCalculate();
 
// 按照类型渲染
const dragItem = async ({ e, info: item }) => {
  if (isOutsideCanvas(e.clientX, e.clientY)) return;
  Spin.show({
    render: (h) => h('div', t('alert.loading_data')),
  });
  await canvasEditor.downFontByJSON(JSON.stringify(item.json));
  const el = JSON.parse(JSON.stringify(item.json));
  const elType = capitalizeFirstLetter(el.type);
  new fabric[elType].fromObject(el, (fabricEl) => {
    canvasEditor.addBaseType(fabricEl, { event: e });
    Spin.hide();
  });
};
 
const addItem = async ({ info: item }) => {
  Spin.show({
    render: (h) => h('div', t('alert.loading_data')),
  });
  await canvasEditor.downFontByJSON(JSON.stringify(item.json));
  const el = JSON.parse(JSON.stringify(item.json));
  el.id = uuid();
  const elType = capitalizeFirstLetter(el.type);
  new fabric[elType].fromObject(el, (fabricEl) => {
    canvasEditor.addBaseType(fabricEl);
    Spin.hide();
  });
};
 
function capitalizeFirstLetter(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}
 
const selectTypeRef = ref();
 
const filters = reactive({
  font_style_type: {
    $contains: '',
  },
  name: {
    $contains: '',
  },
});
 
// 分页格式化
const formatData = (data) => {
  return data.map((item) => {
    return {
      id: item.id,
      name: item.attributes.name,
      desc: item.attributes.desc,
      json: item.attributes.json,
      src: getMaterialInfoUrl(item.attributes.img),
      previewSrc: getMaterialPreviewUrl(item.attributes.img),
    };
  });
};
 
// 搜索改变
const searchChange = async ({ searchKeyWord, typeValue }) => {
  filters.name.$contains = '';
  filters.font_style_type.$contains = '';
  await nextTick();
  filters.name.$contains = searchKeyWord;
  filters.font_style_type.$contains = typeValue;
};
 
// 分类列表选择
const selectType = async (type) => {
  filters.font_style_type.$contains = type;
  selectTypeRef.value.setType(type);
  await nextTick();
};
</script>
 
<style scoped lang="less"></style>