index.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  3. import { withDirectives as _withDirectives, vShow as _vShow, resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";
  4. import LoadingOutlined from "@ant-design/icons-vue/es/icons/LoadingOutlined";
  5. import PaperClipOutlined from "@ant-design/icons-vue/es/icons/PaperClipOutlined";
  6. import PictureTwoTone from "@ant-design/icons-vue/es/icons/PictureTwoTone";
  7. import FileTwoTone from "@ant-design/icons-vue/es/icons/FileTwoTone";
  8. import { uploadListProps } from '../interface';
  9. import { previewImage, isImageUrl } from '../utils';
  10. import Button from '../../button';
  11. import ListItem from './ListItem';
  12. import { computed, defineComponent, getCurrentInstance, onMounted, ref, watchEffect } from 'vue';
  13. import { filterEmpty, initDefaultProps, isValidElement } from '../../_util/props-util';
  14. import useConfigInject from '../../_util/hooks/useConfigInject';
  15. import { getTransitionGroupProps, TransitionGroup } from '../../_util/transition';
  16. import collapseMotion from '../../_util/collapseMotion';
  17. var HackSlot = function HackSlot(_, _ref) {
  18. var _slots$default;
  19. var slots = _ref.slots;
  20. return filterEmpty((_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots))[0];
  21. };
  22. export default defineComponent({
  23. compatConfig: {
  24. MODE: 3
  25. },
  26. name: 'AUploadList',
  27. props: initDefaultProps(uploadListProps(), {
  28. listType: 'text',
  29. progress: {
  30. strokeWidth: 2,
  31. showInfo: false
  32. },
  33. showRemoveIcon: true,
  34. showDownloadIcon: false,
  35. showPreviewIcon: true,
  36. previewFile: previewImage,
  37. isImageUrl: isImageUrl,
  38. items: [],
  39. appendActionVisible: true
  40. }),
  41. setup: function setup(props, _ref2) {
  42. var slots = _ref2.slots,
  43. expose = _ref2.expose;
  44. var motionAppear = ref(false);
  45. var instance = getCurrentInstance();
  46. onMounted(function () {
  47. motionAppear.value == true;
  48. });
  49. watchEffect(function () {
  50. if (props.listType !== 'picture' && props.listType !== 'picture-card') {
  51. return;
  52. }
  53. (props.items || []).forEach(function (file) {
  54. if (typeof document === 'undefined' || typeof window === 'undefined' || !window.FileReader || !window.File || !(file.originFileObj instanceof File || file.originFileObj instanceof Blob) || file.thumbUrl !== undefined) {
  55. return;
  56. }
  57. file.thumbUrl = '';
  58. if (props.previewFile) {
  59. props.previewFile(file.originFileObj).then(function (previewDataUrl) {
  60. // Need append '' to avoid dead loop
  61. file.thumbUrl = previewDataUrl || '';
  62. instance.update();
  63. });
  64. }
  65. });
  66. });
  67. // ============================= Events =============================
  68. var onInternalPreview = function onInternalPreview(file, e) {
  69. if (!props.onPreview) {
  70. return;
  71. }
  72. e === null || e === void 0 ? void 0 : e.preventDefault();
  73. return props.onPreview(file);
  74. };
  75. var onInternalDownload = function onInternalDownload(file) {
  76. if (typeof props.onDownload === 'function') {
  77. props.onDownload(file);
  78. } else if (file.url) {
  79. window.open(file.url);
  80. }
  81. };
  82. var onInternalClose = function onInternalClose(file) {
  83. var _props$onRemove;
  84. (_props$onRemove = props.onRemove) === null || _props$onRemove === void 0 ? void 0 : _props$onRemove.call(props, file);
  85. };
  86. var internalIconRender = function internalIconRender(_ref3) {
  87. var file = _ref3.file;
  88. var iconRender = props.iconRender || slots.iconRender;
  89. if (iconRender) {
  90. return iconRender({
  91. file: file,
  92. listType: props.listType
  93. });
  94. }
  95. var isLoading = file.status === 'uploading';
  96. var fileIcon = props.isImageUrl && props.isImageUrl(file) ? _createVNode(PictureTwoTone, null, null) : _createVNode(FileTwoTone, null, null);
  97. var icon = isLoading ? _createVNode(LoadingOutlined, null, null) : _createVNode(PaperClipOutlined, null, null);
  98. if (props.listType === 'picture') {
  99. icon = isLoading ? _createVNode(LoadingOutlined, null, null) : fileIcon;
  100. } else if (props.listType === 'picture-card') {
  101. icon = isLoading ? props.locale.uploading : fileIcon;
  102. }
  103. return icon;
  104. };
  105. var actionIconRender = function actionIconRender(opt) {
  106. var customIcon = opt.customIcon,
  107. callback = opt.callback,
  108. prefixCls = opt.prefixCls,
  109. title = opt.title;
  110. var btnProps = {
  111. type: 'text',
  112. size: 'small',
  113. title: title,
  114. onClick: function onClick() {
  115. callback();
  116. },
  117. class: "".concat(prefixCls, "-list-item-card-actions-btn")
  118. };
  119. if (isValidElement(customIcon)) {
  120. return _createVNode(Button, btnProps, {
  121. icon: function icon() {
  122. return customIcon;
  123. }
  124. });
  125. }
  126. return _createVNode(Button, btnProps, {
  127. default: function _default() {
  128. return [_createVNode("span", null, [customIcon])];
  129. }
  130. });
  131. };
  132. expose({
  133. handlePreview: onInternalPreview,
  134. handleDownload: onInternalDownload
  135. });
  136. var _useConfigInject = useConfigInject('upload', props),
  137. prefixCls = _useConfigInject.prefixCls,
  138. direction = _useConfigInject.direction;
  139. var listClassNames = computed(function () {
  140. var _ref4;
  141. return _ref4 = {}, _defineProperty(_ref4, "".concat(prefixCls.value, "-list"), true), _defineProperty(_ref4, "".concat(prefixCls.value, "-list-").concat(props.listType), true), _defineProperty(_ref4, "".concat(prefixCls.value, "-list-rtl"), direction.value === 'rtl'), _ref4;
  142. });
  143. var transitionGroupProps = computed(function () {
  144. return _objectSpread(_objectSpread(_objectSpread({}, collapseMotion("".concat(prefixCls.value, "-").concat(props.listType === 'picture-card' ? 'animate-inline' : 'animate'))), getTransitionGroupProps("".concat(prefixCls.value, "-").concat(props.listType === 'picture-card' ? 'animate-inline' : 'animate'))), {}, {
  145. class: listClassNames.value,
  146. appear: motionAppear.value
  147. });
  148. });
  149. return function () {
  150. var listType = props.listType,
  151. locale = props.locale,
  152. isImgUrl = props.isImageUrl,
  153. _props$items = props.items,
  154. items = _props$items === void 0 ? [] : _props$items,
  155. showPreviewIcon = props.showPreviewIcon,
  156. showRemoveIcon = props.showRemoveIcon,
  157. showDownloadIcon = props.showDownloadIcon,
  158. removeIcon = props.removeIcon,
  159. previewIcon = props.previewIcon,
  160. downloadIcon = props.downloadIcon,
  161. progress = props.progress,
  162. appendAction = props.appendAction,
  163. itemRender = props.itemRender,
  164. appendActionVisible = props.appendActionVisible;
  165. var appendActionDom = appendAction === null || appendAction === void 0 ? void 0 : appendAction();
  166. return _createVNode(TransitionGroup, _objectSpread(_objectSpread({}, transitionGroupProps.value), {}, {
  167. "tag": "div"
  168. }), {
  169. default: function _default() {
  170. return [items.map(function (file) {
  171. var key = file.uid;
  172. return _createVNode(ListItem, {
  173. "key": key,
  174. "locale": locale,
  175. "prefixCls": prefixCls.value,
  176. "file": file,
  177. "items": items,
  178. "progress": progress,
  179. "listType": listType,
  180. "isImgUrl": isImgUrl,
  181. "showPreviewIcon": showPreviewIcon,
  182. "showRemoveIcon": showRemoveIcon,
  183. "showDownloadIcon": showDownloadIcon,
  184. "onPreview": onInternalPreview,
  185. "onDownload": onInternalDownload,
  186. "onClose": onInternalClose,
  187. "removeIcon": removeIcon,
  188. "previewIcon": previewIcon,
  189. "downloadIcon": downloadIcon,
  190. "itemRender": itemRender
  191. }, _objectSpread(_objectSpread({}, slots), {}, {
  192. iconRender: internalIconRender,
  193. actionIconRender: actionIconRender
  194. }));
  195. }), appendAction ? _withDirectives(_createVNode(HackSlot, {
  196. "key": "__ant_upload_appendAction"
  197. }, {
  198. default: function _default() {
  199. return appendActionDom;
  200. }
  201. }), [[_vShow, !!appendActionVisible]]) : null];
  202. }
  203. });
  204. };
  205. }
  206. });