SelectBackground.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <template>
  2. <el-dialog
  3. custom-class="background"
  4. width="608px"
  5. append-to-body
  6. :close-on-click-modal="false"
  7. :visible="visible"
  8. :before-close="handleClose"
  9. >
  10. <div class="select-background">
  11. <div class="select-background-top">
  12. <span class="tab">{{ title }}</span>
  13. <SelectUpload :is-show-resource="isResource" type="image" @uploadSuccess="uploadSuccess" />
  14. <el-select v-model="imageMode" style="width: 100px; margin-left: 4px" @change="changeImageMode">
  15. <el-option v-for="item in imageModeList" :key="item.value" :label="item.label" :value="item.value" />
  16. </el-select>
  17. </div>
  18. <div class="background-img" :style="computedBackgroundImg()">
  19. <div
  20. v-if="file_url"
  21. class="img-set"
  22. :style="{ top: imgTop, left: imgLeft, position: imageMode !== 'repeat' ? 'relative' : '' }"
  23. >
  24. <div class="dot top-left" @mousedown="dragStart($event, 'nwse-resize', 'top-left')"></div>
  25. <div class="horizontal-line" @mousedown="dragStart($event, 'ns-resize', 'top')"></div>
  26. <div class="dot top-right" @mousedown="dragStart($event, 'nesw-resize', 'top-right')"></div>
  27. <div class="vertical-line" @mousedown="dragStart($event, 'ew-resize', 'left')"></div>
  28. <img
  29. ref="img"
  30. :src="file_url"
  31. draggable="false"
  32. :alt="title"
  33. :style="{ width: imgWidth, height: imgHeight }"
  34. @mousedown="dragStart($event, 'move', 'move')"
  35. />
  36. <div class="vertical-line" @mousedown="dragStart($event, 'ew-resize', 'right')"></div>
  37. <div class="dot bottom-left" @mousedown="dragStart($event, 'nesw-resize', 'bottom-left')"></div>
  38. <div class="horizontal-line" @mousedown="dragStart($event, 'ns-resize', 'bottom')"></div>
  39. <div class="dot bottom-right" @mousedown="dragStart($event, 'nwse-resize', 'bottom-right')"></div>
  40. </div>
  41. </div>
  42. </div>
  43. <div slot="footer">
  44. <el-button @click="handleClose">取消</el-button>
  45. <el-button type="primary" @click="confirm">确定</el-button>
  46. </div>
  47. </el-dialog>
  48. </template>
  49. <script>
  50. import SelectUpload from './common/SelectUpload.vue';
  51. import { imageModeList } from '@/views/book/courseware/data/common';
  52. export default {
  53. name: 'SelectBackground',
  54. components: {
  55. SelectUpload,
  56. },
  57. props: {
  58. visible: {
  59. type: Boolean,
  60. required: true,
  61. },
  62. title: {
  63. type: String,
  64. default: '背景图',
  65. },
  66. url: {
  67. type: String,
  68. default: '',
  69. },
  70. position: {
  71. type: Object,
  72. default: () => ({ width: 0, height: 0, top: 0, left: 0 }),
  73. },
  74. // 是否使用资源
  75. isResource: {
  76. type: Boolean,
  77. default: true,
  78. },
  79. // 图片填充模式
  80. imageFillMode: {
  81. type: String,
  82. default: 'normal', // normal:正常,cover:覆盖
  83. },
  84. type: {
  85. type: String,
  86. default: 'page', // component:组件背景图,page:页面背景图
  87. },
  88. },
  89. data() {
  90. return {
  91. maxWidth: 576,
  92. maxHeight: 310,
  93. file_url: this.url,
  94. drag: {
  95. dragging: false,
  96. startX: 0,
  97. startY: 0,
  98. type: '',
  99. },
  100. imgData: this.position,
  101. imageModeList,
  102. imageMode: 'free',
  103. };
  104. },
  105. computed: {
  106. imgTop() {
  107. if (this.imageMode === 'repeat') return '-9px';
  108. return `${this.imgData.top - 9}px`;
  109. },
  110. imgLeft() {
  111. if (this.imageMode === 'repeat') return '';
  112. return `${this.imgData.left}px`;
  113. },
  114. imgWidth() {
  115. if (this.imageMode === 'repeat') return '';
  116. return `${this.imgData.width}px`;
  117. },
  118. imgHeight() {
  119. if (this.imageMode === 'repeat') return '';
  120. return `${this.imgData.height}px`;
  121. },
  122. },
  123. watch: {
  124. visible(val) {
  125. if (val) {
  126. this.$nextTick(() => {
  127. document.querySelector('.background-img').addEventListener('mousemove', this.mouseMove);
  128. });
  129. } else {
  130. document.querySelector('.background-img')?.removeEventListener('mousemove', this.mouseMove);
  131. }
  132. },
  133. url(val) {
  134. this.file_url = val;
  135. },
  136. position: {
  137. handler(val) {
  138. if (val) {
  139. this.imgData = {
  140. width: (val.width * this.maxWidth) / 100,
  141. height: (val.height * this.maxHeight) / 100,
  142. top: (val.top * this.maxHeight) / 100,
  143. left: (val.left * this.maxWidth) / 100,
  144. };
  145. this.imageMode = val.image_mode || 'free';
  146. }
  147. },
  148. deep: true,
  149. },
  150. },
  151. mounted() {
  152. document.body.addEventListener('mouseup', this.mouseUp);
  153. },
  154. beforeDestroy() {
  155. document.querySelector('.background-img')?.removeEventListener('mousemove', this.mouseMove);
  156. document.body.removeEventListener('mouseup', this.mouseUp);
  157. },
  158. methods: {
  159. handleClose() {
  160. this.$emit('update:visible', false);
  161. },
  162. confirm() {
  163. this.$emit('update:visible', false);
  164. const { width, height, top, left } = this.imgData;
  165. this.$emit('setBackgroundImage', this.file_url, {
  166. width: (width / this.maxWidth) * 100,
  167. height: (height / this.maxHeight) * 100,
  168. top: (top / this.maxHeight) * 100,
  169. left: (left / this.maxWidth) * 100,
  170. image_mode: this.imageMode,
  171. });
  172. },
  173. // 计算背景图片
  174. computedBackgroundImg() {
  175. if (this.imageMode === 'repeat') {
  176. return {
  177. display: 'flex',
  178. 'justify-content': 'center',
  179. 'align-items': 'center',
  180. };
  181. }
  182. },
  183. /**
  184. * 拖拽开始
  185. * @param {MouseEvent} event
  186. * @param {string} cursor
  187. * @param {string} type
  188. */
  189. dragStart(event, cursor, type) {
  190. const { clientX, clientY } = event;
  191. this.drag = {
  192. dragging: true,
  193. startX: clientX,
  194. startY: clientY,
  195. type,
  196. };
  197. document.querySelector('.el-dialog__wrapper').style.cursor = cursor;
  198. },
  199. /**
  200. * 鼠标移动
  201. * @param {MouseEvent} event
  202. */
  203. mouseMove(event) {
  204. if (!this.drag.dragging) return;
  205. if (this.imageMode === 'fill') return; // 填充模式下不允许拖拽改变大小和位置
  206. const { clientX, clientY } = event;
  207. const { startX, startY, type } = this.drag;
  208. const widthDiff = clientX - startX;
  209. const heightDiff = clientY - startY;
  210. if (type === 'top-left') {
  211. this.imgData.width = Math.min(this.maxWidth, Math.max(0, this.imgData.width - widthDiff));
  212. this.imgData.height = Math.min(this.maxHeight, Math.max(0, this.imgData.height - heightDiff));
  213. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top + heightDiff));
  214. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left + widthDiff));
  215. } else if (type === 'top-right') {
  216. this.imgData.width = Math.min(this.maxWidth, Math.max(this.imgData.width + widthDiff));
  217. this.imgData.height = Math.min(this.maxHeight, Math.max(0, this.imgData.height - heightDiff));
  218. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top + heightDiff));
  219. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left));
  220. } else if (type === 'bottom-left') {
  221. this.imgData.width = Math.min(this.maxWidth, Math.max(0, this.imgData.width - widthDiff));
  222. this.imgData.height = Math.min(this.maxHeight, Math.max(this.imgData.height + heightDiff));
  223. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top));
  224. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left + widthDiff));
  225. } else if (type === 'bottom-right') {
  226. this.imgData.width = Math.min(this.maxWidth, Math.max(this.imgData.width + widthDiff));
  227. this.imgData.height = Math.min(this.maxHeight, Math.max(this.imgData.height + heightDiff));
  228. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top));
  229. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left));
  230. }
  231. if (type === 'top') {
  232. this.imgData.height = Math.min(this.maxHeight, Math.max(0, this.imgData.height - heightDiff));
  233. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top + heightDiff));
  234. } else if (type === 'bottom') {
  235. this.imgData.height = Math.min(this.maxHeight, Math.max(this.imgData.height + heightDiff));
  236. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top));
  237. } else if (type === 'left') {
  238. this.imgData.width = Math.min(this.maxWidth, Math.max(this.imgData.width - widthDiff));
  239. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left + widthDiff));
  240. } else if (type === 'right') {
  241. this.imgData.width = Math.min(this.maxWidth, Math.max(this.imgData.width + widthDiff));
  242. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left));
  243. }
  244. if (type === 'move') {
  245. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top + heightDiff));
  246. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left + widthDiff));
  247. }
  248. this.drag.startX = clientX;
  249. this.drag.startY = clientY;
  250. },
  251. /**
  252. * 鼠标抬起
  253. */
  254. mouseUp() {
  255. this.drag.dragging = false;
  256. document.querySelector('.el-dialog__wrapper').style.cursor = 'auto';
  257. },
  258. /**
  259. * 上传成功
  260. * @param {array} fileList
  261. */
  262. uploadSuccess(fileList) {
  263. if (fileList.length > 0) {
  264. let file_url = fileList[0].file_url;
  265. const img = new Image();
  266. img.src = file_url;
  267. img.onload = () => {
  268. const { width, height } = img;
  269. if (this.imageFillMode === 'cover') {
  270. this.coverComputed(width, height);
  271. } else if (this.imageFillMode === 'normal') {
  272. this.normalComputed(width, height);
  273. }
  274. this.file_url = fileList[0].file_url_open;
  275. };
  276. }
  277. },
  278. /**
  279. * 正常填充
  280. * @param {number} width 图片宽度
  281. * @param {number} height 图片高度
  282. */
  283. normalComputed(width, height) {
  284. if (width > this.maxWidth || height > this.maxHeight) {
  285. const wScale = width / this.maxWidth;
  286. const hScale = height / this.maxHeight;
  287. const scale = wScale > hScale ? this.maxWidth / 2 / width : this.maxHeight / 2 / height;
  288. this.imgData = {
  289. width: width * scale,
  290. height: height * scale,
  291. top: 0,
  292. left: 0,
  293. };
  294. } else {
  295. this.imgData = {
  296. width,
  297. height,
  298. top: 0,
  299. left: 0,
  300. };
  301. }
  302. },
  303. /**
  304. * 覆盖填充
  305. * @param {number} width 图片宽度
  306. * @param {number} height 图片高度
  307. */
  308. coverComputed(width, height) {
  309. this.imgData = {
  310. width: width * (this.maxWidth / width),
  311. height: height * (this.maxHeight / height),
  312. top: 0,
  313. left: 0,
  314. };
  315. },
  316. // 填充模式改变时,重新计算图片位置和大小
  317. changeImageMode() {
  318. if (this.imageMode === 'fill') {
  319. this.coverComputed(this.maxWidth, this.maxHeight);
  320. }
  321. if (this.imageMode === 'repeat') {
  322. // 如果图片小于最大高度,最大宽度,设置时,按照比例居中显示在显示框中。
  323. }
  324. },
  325. },
  326. };
  327. </script>
  328. <style lang="scss" scoped>
  329. .select-background {
  330. &-top {
  331. display: flex;
  332. align-items: center;
  333. margin-bottom: 16px;
  334. .tab {
  335. width: 80px;
  336. font-size: 14px;
  337. color: $font-light-color;
  338. }
  339. }
  340. .background-img {
  341. height: 310px;
  342. border: 1px dashed rgba(0, 0, 0, 8%);
  343. .img-set {
  344. display: inline-grid;
  345. grid-template:
  346. ' . . . ' 2px
  347. ' . img . ' auto
  348. ' . . . ' 2px
  349. / 2px auto 2px;
  350. img {
  351. object-fit: cover;
  352. }
  353. .horizontal-line,
  354. .vertical-line {
  355. background-color: $main-color;
  356. }
  357. .horizontal-line {
  358. width: 100%;
  359. height: 2px;
  360. cursor: ns-resize;
  361. }
  362. .vertical-line {
  363. width: 2px;
  364. height: 100%;
  365. cursor: ew-resize;
  366. }
  367. .dot {
  368. z-index: 1;
  369. width: 6px;
  370. height: 6px;
  371. background-color: $main-color;
  372. &.top-left {
  373. top: -2px;
  374. left: -2px;
  375. }
  376. &.top-right {
  377. top: -2px;
  378. right: 2px;
  379. }
  380. &.bottom-left {
  381. bottom: 2px;
  382. left: -2px;
  383. }
  384. &.bottom-right {
  385. right: 2px;
  386. bottom: 2px;
  387. }
  388. &.top-left,
  389. &.bottom-right {
  390. position: relative;
  391. cursor: nwse-resize;
  392. }
  393. &.top-right,
  394. &.bottom-left {
  395. position: relative;
  396. cursor: nesw-resize;
  397. }
  398. }
  399. }
  400. }
  401. }
  402. </style>
  403. <style lang="scss">
  404. .el-dialog.background {
  405. .el-dialog__header {
  406. display: none;
  407. }
  408. .el-dialog__body {
  409. padding: 8px 16px;
  410. }
  411. }
  412. </style>