editor-modal-media.vue 22 KB

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