editor-modal-media.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <template lang='pug'>
  2. v-card.editor-modal-media.animated.fadeInLeft(flat, tile)
  3. v-container.pa-3(grid-list-lg, fluid)
  4. v-layout(row, wrap)
  5. v-flex(xs12, xl9)
  6. v-card.radius-7.animated.fadeInLeft.wait-p1s(light)
  7. v-card-text
  8. .d-flex
  9. v-toolbar.radius-7(color='teal lighten-5', dense, flat, height='44')
  10. .body-2.teal--text Assets
  11. v-btn.ml-3.my-0.radius-7(outline, large, color='teal', icon, @click='refresh')
  12. v-icon cached
  13. v-dialog(v-model='newFolderDialog', max-width='550')
  14. v-btn.my-0.mr-0.radius-7(outline, large, color='teal', :icon='$vuetify.breakpoint.xsOnly', slot='activator')
  15. v-icon(:left='$vuetify.breakpoint.mdAndUp') add
  16. span.hidden-sm-and-down New Folder
  17. v-card.wiki-form
  18. .dialog-header.is-short New Folder
  19. v-card-text
  20. v-text-field.md2(
  21. outline
  22. background-color='grey lighten-3'
  23. prepend-icon='folder'
  24. v-model='newFolderName'
  25. label='Folder Name'
  26. counter='255'
  27. @keyup.enter='createFolder'
  28. @keyup.esc='newFolderDialog = false'
  29. ref='folderNameIpt'
  30. )
  31. .caption.grey--text.text--darken-1.pl-5 Must follow the asset folder #[a(href='https://docs-beta.requarks.io/guide/assets#naming-restrictions', target='_blank') naming rules].
  32. v-card-chin
  33. v-spacer
  34. v-btn(flat, @click='newFolderDialog = false') Cancel
  35. v-btn(color='primary', @click='createFolder', :disabled='!isFolderNameValid', :loading='newFolderLoading') Create
  36. template(v-if='folders.length > 0 || currentFolderId > 0')
  37. .pt-2
  38. v-btn.is-icon.mx-1(color='grey darken-2', outline, :dark='currentFolderId > 0', @click='upFolder()', :disabled='currentFolderId === 0')
  39. v-icon keyboard_arrow_up
  40. v-btn.btn-normalcase.mx-1(v-for='folder of folders', :key='folder.id', depressed, color='grey darken-2', dark, @click='downFolder(folder)')
  41. v-icon(left) folder
  42. span {{ folder.name }}
  43. v-divider.mt-2
  44. v-data-table(
  45. :items='assets'
  46. :headers='headers'
  47. :pagination.sync='pagination'
  48. :rows-per-page-items='[15]'
  49. :loading='loading'
  50. must-sort,
  51. hide-actions
  52. )
  53. template(slot='items', slot-scope='props')
  54. tr.is-clickable(
  55. @click.left='currentFileId = props.item.id'
  56. @click.right.prevent=''
  57. :class='currentFileId === props.item.id ? `teal lighten-5` : ``'
  58. )
  59. td.text-xs-right(v-if='$vuetify.breakpoint.smAndUp') {{ props.item.id }}
  60. td
  61. .body-2(:class='currentFileId === props.item.id ? `teal--text` : ``') {{ props.item.filename }}
  62. .caption.grey--text {{ props.item.description }}
  63. td.text-xs-center(v-if='$vuetify.breakpoint.lgAndUp')
  64. v-chip.ma-0(small, :color='$vuetify.dark ? `grey darken-4` : `grey lighten-4`')
  65. .caption {{props.item.ext.toUpperCase().substring(1)}}
  66. td(v-if='$vuetify.breakpoint.mdAndUp') {{ props.item.fileSize | prettyBytes }}
  67. td(v-if='$vuetify.breakpoint.mdAndUp') {{ props.item.createdAt | moment('from') }}
  68. td(v-if='$vuetify.breakpoint.smAndUp')
  69. v-menu(offset-x)
  70. v-btn.ma-0(icon, slot='activator')
  71. v-icon(color='grey darken-2') more_horiz
  72. v-list.py-0(style='border-top: 5px solid #444;')
  73. v-list-tile(@click='')
  74. v-list-tile-avatar
  75. v-icon(color='teal') short_text
  76. v-list-tile-content Properties
  77. v-divider
  78. template(v-if='props.item.kind === `IMAGE`')
  79. v-list-tile(@click='')
  80. v-list-tile-avatar
  81. v-icon(color='green') image_search
  82. v-list-tile-content Preview
  83. v-divider
  84. v-list-tile(@click='')
  85. v-list-tile-avatar
  86. v-icon(color='indigo') crop_rotate
  87. v-list-tile-content Edit
  88. v-divider
  89. v-list-tile(@click='')
  90. v-list-tile-avatar
  91. v-icon(color='purple') offline_bolt
  92. v-list-tile-content Optimize
  93. v-divider
  94. v-list-tile(@click='')
  95. v-list-tile-avatar
  96. v-icon(color='orange') keyboard
  97. v-list-tile-content Rename
  98. v-divider
  99. v-list-tile(@click='')
  100. v-list-tile-avatar
  101. v-icon(color='blue') forward
  102. v-list-tile-content Move
  103. v-divider
  104. v-list-tile(@click='')
  105. v-list-tile-avatar
  106. v-icon(color='red') delete
  107. v-list-tile-content Delete
  108. template(slot='no-data')
  109. v-alert.mt-3.radius-7(icon='folder_open', :value='true', outline, color='teal') This asset folder is empty.
  110. .text-xs-center.py-2(v-if='this.pageTotal > 1')
  111. v-pagination(v-model='pagination.page', :length='pageTotal')
  112. .d-flex.mt-3
  113. v-toolbar.radius-7(flat, color='grey lighten-4', dense, height='44')
  114. template(v-if='folderTree.length > 0')
  115. .body-2
  116. span.mr-1 /
  117. template(v-for='folder of folderTree')
  118. span(:key='folder.id') {{folder.name}}
  119. span.mx-1 /
  120. .body-2(v-else) / #[em root]
  121. template(v-if='$vuetify.breakpoint.smAndUp')
  122. v-spacer
  123. .body-1.grey--text.text--darken-1 {{assets.length}} files
  124. v-btn.ml-3.mr-0.my-0.radius-7(color='teal', large, @click='insert', :disabled='!currentFileId', :dark='currentFileId !== null')
  125. v-icon(left) save_alt
  126. span Insert
  127. v-flex(xs12, xl3)
  128. v-card.radius-7.animated.fadeInRight.wait-p3s(light)
  129. v-card-text
  130. .d-flex
  131. v-toolbar.radius-7(color='teal lighten-5', dense, flat, height='44')
  132. v-icon.mr-3(color='teal') cloud_upload
  133. .body-2.teal--text Upload Assets
  134. v-btn.my-0.ml-3.mr-0.radius-7(outline, large, color='teal', @click='browse', v-if='$vuetify.breakpoint.mdAndUp')
  135. v-icon(left) touch_app
  136. span Browse
  137. file-pond.mt-3(
  138. name='mediaUpload'
  139. ref='pond'
  140. label-idle='Browse or Drop files here...'
  141. allow-multiple='true'
  142. :files='files'
  143. max-files='10'
  144. server='/u'
  145. :instant-upload='false'
  146. :allow-revert='false'
  147. @processfile='onFileProcessed'
  148. )
  149. v-divider
  150. v-card-actions.pa-3
  151. .caption.grey--text.text-darken-2 Max 10 files, 5 MB each
  152. v-spacer
  153. v-btn(color='teal', dark, @click='upload') Upload
  154. v-card.mt-3.radius-7.animated.fadeInRight.wait-p4s(light)
  155. v-card-text.pb-0
  156. v-toolbar.radius-7(color='teal lighten-5', dense, flat)
  157. v-icon.mr-3(color='teal') cloud_download
  158. .body-2.teal--text Fetch Remote Image
  159. v-spacer
  160. v-chip(label, color='white', small).teal--text coming soon
  161. v-text-field.mt-3(
  162. v-model='remoteImageUrl'
  163. outline
  164. single-line
  165. background-color='grey lighten-2'
  166. placeholder='https://example.com/image.jpg'
  167. )
  168. v-divider
  169. v-card-actions.pa-3
  170. .caption.grey--text.text-darken-2 Max 5 MB
  171. v-spacer
  172. v-btn(color='teal', disabled) Fetch
  173. v-card.mt-3.radius-7.animated.fadeInRight.wait-p4s(light)
  174. v-card-text.pb-0
  175. v-toolbar.radius-7(color='teal lighten-5', dense, flat)
  176. v-icon.mr-3(color='teal') format_align_left
  177. .body-2.teal--text Image Alignment
  178. v-select.mt-3(
  179. v-model='imageAlignment'
  180. :items='imageAlignments'
  181. outline
  182. single-line
  183. background-color='grey lighten-2'
  184. placeholder='None'
  185. )
  186. </template>
  187. <script>
  188. import _ from 'lodash'
  189. import { sync } from 'vuex-pathify'
  190. import vueFilePond from 'vue-filepond'
  191. import 'filepond/dist/filepond.min.css'
  192. import listAssetQuery from 'gql/editor/editor-media-query-list.gql'
  193. import listFolderAssetQuery from 'gql/editor/editor-media-query-folder-list.gql'
  194. import createAssetFolderMutation from 'gql/editor/editor-media-mutation-folder-create.gql'
  195. const FilePond = vueFilePond()
  196. const localeSegmentRegex = /^[A-Z]{2}(-[A-Z]{2})?$/i
  197. const disallowedFolderChars = /[A-Z()=.!@#$%?&*+`~<>,;:\\/[\]¬{| ]/
  198. export default {
  199. components: {
  200. FilePond
  201. },
  202. props: {
  203. value: {
  204. type: Boolean,
  205. default: false
  206. }
  207. },
  208. data() {
  209. return {
  210. folders: [],
  211. folderTree: [],
  212. files: [],
  213. assets: [],
  214. pagination: {},
  215. remoteImageUrl: '',
  216. imageAlignments: [
  217. { text: 'None', value: '' },
  218. { text: 'Left', value: 'left' },
  219. { text: 'Centered', value: 'center' },
  220. { text: 'Right', value: 'right' },
  221. { text: 'Absolute Top Right', value: 'abstopright' }
  222. ],
  223. imageAlignment: '',
  224. currentFolderId: 0,
  225. currentFileId: null,
  226. previousFolderId: 0,
  227. loading: false,
  228. newFolderDialog: false,
  229. newFolderName: '',
  230. newFolderLoading: false
  231. }
  232. },
  233. computed: {
  234. isShown: {
  235. get() { return this.value },
  236. set(val) { this.$emit('input', val) }
  237. },
  238. activeModal: sync('editor/activeModal'),
  239. pageTotal () {
  240. if (this.pagination.rowsPerPage == null || this.pagination.totalItems == null) {
  241. return 0
  242. }
  243. return Math.ceil(this.assets.length / this.pagination.rowsPerPage)
  244. },
  245. headers() {
  246. return _.compact([
  247. this.$vuetify.breakpoint.smAndUp && { text: 'ID', value: 'id', width: 50, align: 'right' },
  248. { text: 'Filename', value: 'filename' },
  249. this.$vuetify.breakpoint.lgAndUp && { text: 'Type', value: 'ext', width: 50 },
  250. this.$vuetify.breakpoint.mdAndUp && { text: 'File Size', value: 'fileSize', width: 110 },
  251. this.$vuetify.breakpoint.mdAndUp && { text: 'Added', value: 'createdAt', width: 175 },
  252. this.$vuetify.breakpoint.smAndUp && { text: 'Actions', value: '', width: 40, sortable: false, align:'right' }
  253. ])
  254. },
  255. isFolderNameValid() {
  256. return this.newFolderName.length > 1 && !localeSegmentRegex.test(this.newFolderName) && !disallowedFolderChars.test(this.newFolderName)
  257. }
  258. },
  259. watch: {
  260. newFolderDialog(newValue, oldValue) {
  261. if (newValue) {
  262. this.$nextTick(() => {
  263. this.$refs.folderNameIpt.focus()
  264. })
  265. }
  266. }
  267. },
  268. filters: {
  269. prettyBytes(num) {
  270. if (typeof num !== 'number' || isNaN(num)) {
  271. throw new TypeError('Expected a number')
  272. }
  273. let exponent
  274. let unit
  275. let neg = num < 0
  276. let units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
  277. if (neg) {
  278. num = -num
  279. }
  280. if (num < 1) {
  281. return (neg ? '-' : '') + num + ' B'
  282. }
  283. exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1)
  284. num = (num / Math.pow(1000, exponent)).toFixed(2) * 1
  285. unit = units[exponent]
  286. return (neg ? '-' : '') + num + ' ' + unit
  287. }
  288. },
  289. methods: {
  290. async refresh() {
  291. await this.$apollo.queries.assets.refetch()
  292. this.$store.commit('showNotification', {
  293. message: 'List of assets refreshed successfully.',
  294. style: 'success',
  295. icon: 'check'
  296. })
  297. },
  298. insert () {
  299. const asset = _.find(this.assets, ['id', this.currentFileId])
  300. const assetPath = this.folderTree.map(f => f.slug).join('/')
  301. this.$root.$emit('editorInsert', {
  302. kind: asset.kind,
  303. path: this.currentFolderId > 0 ? `/${assetPath}/${asset.filename}` : `/${asset.filename}`,
  304. text: asset.filename,
  305. align: this.imageAlignment
  306. })
  307. this.activeModal = ''
  308. },
  309. browse () {
  310. this.$refs.pond.browse()
  311. },
  312. async upload () {
  313. const files = this.$refs.pond.getFiles()
  314. if (files.length < 1) {
  315. return this.$store.commit('showNotification', {
  316. message: 'You must choose a file to upload first!',
  317. style: 'warning',
  318. icon: 'warning'
  319. })
  320. }
  321. for (let file of files) {
  322. file.setMetadata({
  323. folderId: this.currentFolderId
  324. })
  325. }
  326. await this.$refs.pond.processFiles()
  327. },
  328. async onFileProcessed (err, file) {
  329. if (err) {
  330. return this.$store.commit('showNotification', {
  331. message: 'File upload failed.',
  332. style: 'error',
  333. icon: 'error'
  334. })
  335. }
  336. _.delay(() => {
  337. this.$refs.pond.removeFile(file.id)
  338. }, 5000)
  339. await this.$apollo.queries.assets.refetch()
  340. },
  341. downFolder(folder) {
  342. this.folderTree.push(folder)
  343. this.currentFolderId = folder.id
  344. this.currentFileId = null
  345. },
  346. upFolder() {
  347. this.folderTree.pop()
  348. const parentFolder = _.last(this.folderTree)
  349. this.currentFolderId = parentFolder ? parentFolder.id : 0
  350. this.currentFileId = null
  351. },
  352. async createFolder() {
  353. this.$store.commit(`loadingStart`, 'editor-media-createfolder')
  354. this.newFolderLoading = true
  355. try {
  356. const resp = await this.$apollo.mutate({
  357. mutation: createAssetFolderMutation,
  358. variables: {
  359. parentFolderId: this.currentFolderId,
  360. slug: this.newFolderName
  361. }
  362. })
  363. if (_.get(resp, 'data.assets.createFolder.responseResult.succeeded', false)) {
  364. await this.$apollo.queries.folders.refetch()
  365. this.$store.commit('showNotification', {
  366. message: 'Asset folder created successfully.',
  367. style: 'success',
  368. icon: 'check'
  369. })
  370. this.newFolderDialog = false
  371. this.newFolderName = ''
  372. } else {
  373. this.$store.commit('pushGraphError', new Error(_.get(resp, 'data.assets.createFolder.responseResult.message')))
  374. }
  375. } catch (err) {
  376. this.$store.commit('pushGraphError', err)
  377. }
  378. this.newFolderLoading = false
  379. this.$store.commit(`loadingStop`, 'editor-media-createfolder')
  380. }
  381. },
  382. apollo: {
  383. folders: {
  384. query: listFolderAssetQuery,
  385. variables() {
  386. return {
  387. parentFolderId: this.currentFolderId
  388. }
  389. },
  390. fetchPolicy: 'network-only',
  391. update: (data) => data.assets.folders,
  392. watchLoading (isLoading) {
  393. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'editor-media-folders-list-refresh')
  394. }
  395. },
  396. assets: {
  397. query: listAssetQuery,
  398. variables() {
  399. return {
  400. folderId: this.currentFolderId,
  401. kind: 'ALL'
  402. }
  403. },
  404. throttle: 1000,
  405. fetchPolicy: 'network-only',
  406. update: (data) => data.assets.list,
  407. watchLoading (isLoading) {
  408. this.loading = isLoading
  409. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'editor-media-list-refresh')
  410. }
  411. }
  412. }
  413. }
  414. </script>
  415. <style lang='scss'>
  416. .editor-modal-media {
  417. position: fixed;
  418. top: 112px;
  419. left: 64px;
  420. z-index: 10;
  421. width: calc(100vw - 64px - 17px);
  422. height: calc(100vh - 112px - 24px);
  423. background-color: rgba(darken(mc('grey', '900'), 3%), .9) !important;
  424. overflow: auto;
  425. @include until($tablet) {
  426. left: 40px;
  427. width: calc(100vw - 40px);
  428. height: calc(100vh - 112px - 24px);
  429. }
  430. .filepond--root {
  431. margin-bottom: 0;
  432. }
  433. .filepond--drop-label {
  434. cursor: pointer;
  435. > label {
  436. cursor: pointer;
  437. }
  438. }
  439. .v-btn--icon {
  440. padding: 0 20px;
  441. }
  442. }
  443. </style>