index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <template>
  2. <div class="go-canvas-setting">
  3. <n-form inline :label-width="45" size="small" label-placement="left">
  4. <n-form-item label="宽度">
  5. <!-- 尺寸选择 -->
  6. <n-input-number
  7. size="small"
  8. v-model:value="canvasConfig.width"
  9. :disabled="editCanvas.lockScale"
  10. :validator="validator"
  11. @update:value="changeSizeHandle"
  12. ></n-input-number>
  13. </n-form-item>
  14. <n-form-item label="高度">
  15. <n-input-number
  16. size="small"
  17. v-model:value="canvasConfig.height"
  18. :disabled="editCanvas.lockScale"
  19. :validator="validator"
  20. @update:value="changeSizeHandle"
  21. ></n-input-number>
  22. </n-form-item>
  23. </n-form>
  24. <n-card class="upload-box">
  25. <n-upload
  26. v-model:file-list="uploadFileListRef"
  27. :show-file-list="false"
  28. :customRequest="customRequest"
  29. :onBeforeUpload="beforeUploadHandle"
  30. >
  31. <n-upload-dragger>
  32. <img
  33. v-if="canvasConfig.backgroundImage"
  34. class="upload-show"
  35. :src="canvasConfig.backgroundImage"
  36. alt="背景"
  37. />
  38. <div class="upload-img" v-show="!canvasConfig.backgroundImage">
  39. <img src="@/assets/images/canvas/noImage.png" />
  40. <n-text class="upload-desc" depth="3">
  41. 背景图需小于 {{ backgroundImageSize }}M ,格式为 png/jpg/gif
  42. 的文件
  43. </n-text>
  44. </div>
  45. </n-upload-dragger>
  46. </n-upload>
  47. </n-card>
  48. <n-space vertical :size="12">
  49. <n-space>
  50. <n-text>背景色</n-text>
  51. <n-color-picker
  52. style="width: 326px;"
  53. :showPreview="true"
  54. :swatches="swatchesColors"
  55. v-model:value="canvasConfig.background"
  56. ></n-color-picker>
  57. </n-space>
  58. <n-space>
  59. <n-text>颜色应用</n-text>
  60. <n-switch
  61. size="small"
  62. v-model:value="canvasConfig.selectColor"
  63. :loading="switchSelectColorLoading"
  64. :round="false"
  65. :disabled="!canvasConfig.backgroundImage"
  66. :onUpdate="switchSelectColorHandle"
  67. ></n-switch>
  68. </n-space>
  69. <n-space>
  70. <n-text>背景控制</n-text>
  71. <n-button size="small" :disabled="!canvasConfig.backgroundImage" @click="clearImage">清除背景图</n-button>
  72. <n-button size="small" :disabled="!canvasConfig.background" @click="clearColor">清除颜色</n-button>
  73. </n-space>
  74. <n-space>
  75. <n-text>预览方式</n-text>
  76. <n-button-group>
  77. <n-button
  78. ghost
  79. v-for="item in previewTypeList"
  80. :key="item.key"
  81. :type="canvasConfig.previewScaleType === item.key ? 'primary' : 'tertiary'"
  82. size="small"
  83. @click="selectPreviewType(item.key)">
  84. <n-tooltip :show-arrow="false" trigger="hover">
  85. <template #trigger>
  86. <n-icon size="18">
  87. <component :is="item.icon"></component>
  88. </n-icon>
  89. </template>
  90. {{ item.desc }}
  91. </n-tooltip>
  92. </n-button>
  93. </n-button-group>
  94. </n-space>
  95. </n-space>
  96. <!-- 滤镜 -->
  97. <styles-setting :is-canvas="true" :chartStyles="canvasConfig"></styles-setting>
  98. <n-divider style="margin: 10px 0;"></n-divider>
  99. <!-- 主题选择和全局配置 -->
  100. <n-tabs class="tabs-box" size="small" type="segment">
  101. <n-tab-pane
  102. v-for="item in globalTabList"
  103. :key="item.key"
  104. :name="item.key"
  105. size="small"
  106. display-directive="show:lazy"
  107. >
  108. <template #tab>
  109. <n-space>
  110. <span>{{ item.title }}</span>
  111. <n-icon size="16" class="icon-position">
  112. <component :is="item.icon"></component>
  113. </n-icon>
  114. </n-space>
  115. </template>
  116. <component :is="item.render"></component>
  117. </n-tab-pane>
  118. </n-tabs>
  119. </div>
  120. </template>
  121. <script setup lang="ts">
  122. import { ref, nextTick } from 'vue'
  123. import { backgroundImageSize } from '@/settings/designSetting'
  124. import { FileTypeEnum } from '@/enums/fileTypeEnum'
  125. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  126. import { EditCanvasConfigEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
  127. import { StylesSetting } from '@/components/Pages/ChartItemSetting'
  128. import { UploadCustomRequestOptions } from 'naive-ui'
  129. import { fileToUrl, loadAsyncComponent } from '@/utils'
  130. import { PreviewScaleEnum } from '@/enums/styleEnum'
  131. import { icon } from '@/plugins'
  132. const { ColorPaletteIcon } = icon.ionicons5
  133. const { ScaleIcon, FitToScreenIcon, FitToHeightIcon, FitToWidthIcon } = icon.carbon
  134. const chartEditStore = useChartEditStore()
  135. const canvasConfig = chartEditStore.getEditCanvasConfig
  136. const editCanvas = chartEditStore.getEditCanvas
  137. const uploadFileListRef = ref()
  138. const switchSelectColorLoading = ref(false)
  139. const ChartThemeColor = loadAsyncComponent(() =>
  140. import('./components/ChartThemeColor/index.vue')
  141. )
  142. // 北京默认展示颜色列表
  143. const swatchesColors = [
  144. '#232324',
  145. '#2a2a2b',
  146. '#313132',
  147. '#373739',
  148. '#757575',
  149. '#e0e0e0',
  150. '#eeeeee',
  151. '#fafafa'
  152. ]
  153. const globalTabList = [
  154. {
  155. key: 'ChartTheme',
  156. title: '主题颜色',
  157. icon: ColorPaletteIcon,
  158. render: ChartThemeColor
  159. }
  160. ]
  161. const previewTypeList = [
  162. {
  163. key: PreviewScaleEnum.FIT,
  164. title: '自适应',
  165. icon: ScaleIcon,
  166. desc: '自适应比例展示,页面会有留白'
  167. },
  168. {
  169. key: PreviewScaleEnum.SCROLL_Y,
  170. title: 'Y轴滚动',
  171. icon: FitToWidthIcon,
  172. desc: 'X轴铺满,Y轴自适应滚动'
  173. },
  174. {
  175. key: PreviewScaleEnum.SCROLL_X,
  176. title: 'X轴滚动',
  177. icon: FitToHeightIcon,
  178. desc: 'Y轴铺满,X轴自适应滚动'
  179. },
  180. {
  181. key: PreviewScaleEnum.FULL,
  182. title: '铺满',
  183. icon: FitToScreenIcon,
  184. desc: '强行拉伸画面,填充所有视图'
  185. },
  186. ]
  187. // 画布尺寸规则
  188. const validator = (x: number) => x > 50
  189. // 修改尺寸
  190. const changeSizeHandle = () => {
  191. chartEditStore.computedScale()
  192. }
  193. // 上传图片前置处理
  194. //@ts-ignore
  195. const beforeUploadHandle = async ({ file }) => {
  196. uploadFileListRef.value = []
  197. const type = file.file.type
  198. const size = file.file.size
  199. if (size > 1024 * 1024 * backgroundImageSize) {
  200. window['$message'].warning(
  201. `图片超出 ${backgroundImageSize}M 限制,请重新上传!`
  202. )
  203. return false
  204. }
  205. if (type !== FileTypeEnum.PNG && type !== FileTypeEnum.JPEG && type !== FileTypeEnum.GIF) {
  206. window['$message'].warning('文件格式不符合,请重新上传!')
  207. return false
  208. }
  209. return true
  210. }
  211. // 清除背景
  212. const clearImage = () => {
  213. chartEditStore.setEditCanvasConfig(
  214. EditCanvasConfigEnum.BACKGROUND_IMAGE,
  215. undefined
  216. )
  217. chartEditStore.setEditCanvasConfig(
  218. EditCanvasConfigEnum.SELECT_COLOR,
  219. true
  220. )
  221. }
  222. // 清除颜色
  223. const clearColor = () => {
  224. chartEditStore.setEditCanvasConfig(
  225. EditCanvasConfigEnum.BACKGROUND,
  226. undefined
  227. )
  228. if (canvasConfig.backgroundImage) {
  229. chartEditStore.setEditCanvasConfig(
  230. EditCanvasConfigEnum.SELECT_COLOR,
  231. false
  232. )
  233. }
  234. }
  235. // 启用/关闭 颜色
  236. const switchSelectColorHandle = () => {
  237. switchSelectColorLoading.value = true
  238. setTimeout(() => {
  239. switchSelectColorLoading.value = false
  240. }, 1000)
  241. }
  242. // 自定义上传操作
  243. const customRequest = (options: UploadCustomRequestOptions) => {
  244. const { file } = options
  245. nextTick(() => {
  246. if (file.file) {
  247. const ImageUrl = fileToUrl(file.file)
  248. chartEditStore.setEditCanvasConfig(
  249. EditCanvasConfigEnum.BACKGROUND_IMAGE,
  250. ImageUrl
  251. )
  252. chartEditStore.setEditCanvasConfig(
  253. EditCanvasConfigEnum.SELECT_COLOR,
  254. false
  255. )
  256. } else {
  257. window['$message'].error('添加图片失败,请稍后重试!')
  258. }
  259. })
  260. }
  261. // 选择预览方式
  262. const selectPreviewType = (key: PreviewScaleEnum) => {
  263. chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.PREVIEW_SCALE_TYPE, key)
  264. }
  265. </script>
  266. <style lang="scss" scoped>
  267. $updloadWidth: 326px;
  268. $updloadHeight: 193px;
  269. @include go(canvas-setting) {
  270. padding-top: 20px;
  271. .upload-box {
  272. cursor: pointer;
  273. margin-bottom: 20px;
  274. @include deep() {
  275. .n-card__content {
  276. padding: 0;
  277. overflow: hidden;
  278. }
  279. .n-upload-dragger {
  280. padding: 5px;
  281. width: $updloadWidth;
  282. }
  283. }
  284. .upload-show {
  285. width: -webkit-fill-available;
  286. height: $updloadHeight;
  287. border-radius: 5px;
  288. }
  289. .upload-img {
  290. display: flex;
  291. flex-direction: column;
  292. align-items: center;
  293. img {
  294. height: 150px;
  295. }
  296. .upload-desc {
  297. padding: 10px 0;
  298. }
  299. }
  300. }
  301. .icon-position {
  302. padding-top: 2px;
  303. }
  304. .tabs-box {
  305. margin-top: 20px;
  306. }
  307. }
  308. </style>