wikijs-fork/client/js/components/editor-image.js

407 lines
13 KiB
JavaScript
Raw Normal View History

'use strict'
2017-02-09 01:52:37 +00:00
import $ from 'jquery'
import Vue from 'vue'
import _ from 'lodash'
import 'jquery-contextmenu'
import 'jquery-simple-upload'
2017-02-09 01:52:37 +00:00
module.exports = (alerts, mde, mdeModalOpenState, socket) => {
let vueImage = new Vue({
el: '#modal-editor-image',
data: {
isLoading: false,
isLoadingText: '',
newFolderName: '',
newFolderShow: false,
newFolderError: false,
fetchFromUrlURL: '',
fetchFromUrlShow: false,
folders: [],
currentFolder: '',
currentImage: '',
currentAlign: 'left',
images: [],
uploadSucceeded: false,
postUploadChecks: 0,
renameImageShow: false,
renameImageId: '',
renameImageFilename: '',
deleteImageShow: false,
deleteImageId: '',
deleteImageFilename: ''
2017-02-09 01:52:37 +00:00
},
methods: {
open: () => {
mdeModalOpenState = true
$('#modal-editor-image').addClass('is-active')
vueImage.refreshFolders()
},
cancel: (ev) => {
mdeModalOpenState = false
$('#modal-editor-image').removeClass('is-active')
},
// -------------------------------------------
// INSERT IMAGE
// -------------------------------------------
selectImage: (imageId) => {
vueImage.currentImage = imageId
},
insertImage: (ev) => {
if (mde.codemirror.doc.somethingSelected()) {
mde.codemirror.execCommand('singleSelection')
}
2016-10-17 03:24:19 +00:00
let selImage = _.find(vueImage.images, ['_id', vueImage.currentImage])
selImage.normalizedPath = (selImage.folder === 'f:') ? selImage.filename : selImage.folder.slice(2) + '/' + selImage.filename
selImage.titleGuess = _.startCase(selImage.basename)
let imageText = '![' + selImage.titleGuess + '](/uploads/' + selImage.normalizedPath + ' "' + selImage.titleGuess + '")'
switch (vueImage.currentAlign) {
case 'center':
imageText += '{.align-center}'
break
case 'right':
imageText += '{.align-right}'
break
case 'logo':
imageText += '{.pagelogo}'
break
}
2017-02-09 01:52:37 +00:00
mde.codemirror.doc.replaceSelection(imageText)
vueImage.cancel()
},
// -------------------------------------------
// NEW FOLDER
// -------------------------------------------
newFolder: (ev) => {
vueImage.newFolderName = ''
vueImage.newFolderError = false
vueImage.newFolderShow = true
_.delay(() => { $('#txt-editor-image-newfoldername').focus() }, 400)
},
newFolderDiscard: (ev) => {
vueImage.newFolderShow = false
},
newFolderCreate: (ev) => {
let regFolderName = new RegExp('^[a-z0-9][a-z0-9-]*[a-z0-9]$')
vueImage.newFolderName = _.kebabCase(_.trim(vueImage.newFolderName))
if (_.isEmpty(vueImage.newFolderName) || !regFolderName.test(vueImage.newFolderName)) {
vueImage.newFolderError = true
return
}
2017-02-09 01:52:37 +00:00
vueImage.newFolderDiscard()
vueImage.isLoadingText = 'Creating new folder...'
vueImage.isLoading = true
2017-02-09 01:52:37 +00:00
Vue.nextTick(() => {
socket.emit('uploadsCreateFolder', { foldername: vueImage.newFolderName }, (data) => {
vueImage.folders = data
vueImage.currentFolder = vueImage.newFolderName
vueImage.images = []
vueImage.isLoading = false
})
2017-02-09 01:52:37 +00:00
})
},
// -------------------------------------------
// FETCH FROM URL
// -------------------------------------------
fetchFromUrl: (ev) => {
vueImage.fetchFromUrlURL = ''
vueImage.fetchFromUrlShow = true
_.delay(() => { $('#txt-editor-image-fetchurl').focus() }, 400)
},
fetchFromUrlDiscard: (ev) => {
vueImage.fetchFromUrlShow = false
},
fetchFromUrlGo: (ev) => {
vueImage.fetchFromUrlDiscard()
vueImage.isLoadingText = 'Fetching image...'
vueImage.isLoading = true
2016-10-17 03:24:19 +00:00
Vue.nextTick(() => {
socket.emit('uploadsFetchFileFromURL', { folder: vueImage.currentFolder, fetchUrl: vueImage.fetchFromUrlURL }, (data) => {
if (data.ok) {
vueImage.waitChangeComplete(vueImage.images.length, true)
} else {
vueImage.isLoading = false
alerts.pushError('Upload error', data.msg)
}
})
})
},
// -------------------------------------------
// RENAME IMAGE
// -------------------------------------------
renameImage: () => {
let c = _.find(vueImage.images, [ '_id', vueImage.renameImageId ])
vueImage.renameImageFilename = c.basename || ''
vueImage.renameImageShow = true
_.delay(() => {
$('#txt-editor-image-rename').focus()
_.defer(() => { $('#txt-editor-image-rename').select() })
}, 400)
},
renameImageDiscard: () => {
vueImage.renameImageShow = false
},
renameImageGo: () => {
vueImage.renameImageDiscard()
vueImage.isLoadingText = 'Renaming image...'
vueImage.isLoading = true
2016-10-17 03:24:19 +00:00
Vue.nextTick(() => {
socket.emit('uploadsRenameFile', { uid: vueImage.renameImageId, folder: vueImage.currentFolder, filename: vueImage.renameImageFilename }, (data) => {
if (data.ok) {
vueImage.waitChangeComplete(vueImage.images.length, false)
} else {
vueImage.isLoading = false
alerts.pushError('Rename error', data.msg)
}
})
2017-02-09 01:52:37 +00:00
})
},
// -------------------------------------------
// MOVE IMAGE
// -------------------------------------------
moveImage: (uid, fld) => {
vueImage.isLoadingText = 'Moving image...'
vueImage.isLoading = true
Vue.nextTick(() => {
socket.emit('uploadsMoveFile', { uid, folder: fld }, (data) => {
if (data.ok) {
vueImage.loadImages()
} else {
vueImage.isLoading = false
alerts.pushError('Rename error', data.msg)
}
})
2017-02-09 01:52:37 +00:00
})
},
2016-10-17 03:24:19 +00:00
// -------------------------------------------
// DELETE IMAGE
// -------------------------------------------
2016-10-17 03:24:19 +00:00
deleteImageWarn: (show) => {
if (show) {
let c = _.find(vueImage.images, [ '_id', vueImage.deleteImageId ])
vueImage.deleteImageFilename = c.filename || 'this image'
}
vueImage.deleteImageShow = show
},
deleteImageGo: () => {
vueImage.deleteImageWarn(false)
vueImage.isLoadingText = 'Deleting image...'
vueImage.isLoading = true
Vue.nextTick(() => {
socket.emit('uploadsDeleteFile', { uid: vueImage.deleteImageId }, (data) => {
2017-02-09 01:52:37 +00:00
vueImage.loadImages()
})
2017-02-09 01:52:37 +00:00
})
},
2016-10-17 03:24:19 +00:00
// -------------------------------------------
// LOAD FROM REMOTE
// -------------------------------------------
2016-10-17 03:24:19 +00:00
selectFolder: (fldName) => {
vueImage.currentFolder = fldName
vueImage.loadImages()
},
refreshFolders: () => {
vueImage.isLoadingText = 'Fetching folders list...'
vueImage.isLoading = true
vueImage.currentFolder = ''
vueImage.currentImage = ''
Vue.nextTick(() => {
socket.emit('uploadsGetFolders', { }, (data) => {
vueImage.folders = data
vueImage.loadImages()
})
2017-02-09 01:52:37 +00:00
})
},
2016-10-17 03:24:19 +00:00
loadImages: (silent) => {
if (!silent) {
vueImage.isLoadingText = 'Fetching images...'
vueImage.isLoading = true
}
return new Promise((resolve, reject) => {
Vue.nextTick(() => {
socket.emit('uploadsGetImages', { folder: vueImage.currentFolder }, (data) => {
vueImage.images = data
if (!silent) {
vueImage.isLoading = false
}
vueImage.attachContextMenus()
resolve(true)
})
})
})
},
2016-10-17 03:24:19 +00:00
waitChangeComplete: (oldAmount, expectChange) => {
expectChange = (_.isBoolean(expectChange)) ? expectChange : true
2017-02-09 01:52:37 +00:00
vueImage.postUploadChecks++
vueImage.isLoadingText = 'Processing...'
2017-02-09 01:52:37 +00:00
Vue.nextTick(() => {
vueImage.loadImages(true).then(() => {
if ((vueImage.images.length !== oldAmount) === expectChange) {
vueImage.postUploadChecks = 0
2017-02-09 01:52:37 +00:00
vueImage.isLoading = false
} else if (vueImage.postUploadChecks > 5) {
vueImage.postUploadChecks = 0
vueImage.isLoading = false
alerts.pushError('Unable to fetch updated listing', 'Try again later')
} else {
_.delay(() => {
vueImage.waitChangeComplete(oldAmount, expectChange)
}, 1500)
2017-02-09 01:52:37 +00:00
}
})
})
},
2017-02-09 01:52:37 +00:00
// -------------------------------------------
// IMAGE CONTEXT MENU
// -------------------------------------------
2017-02-09 01:52:37 +00:00
attachContextMenus: () => {
let moveFolders = _.map(vueImage.folders, (f) => {
return {
name: (f !== '') ? f : '/ (root)',
icon: 'fa-folder',
callback: (key, opt) => {
let moveImageId = _.toString($(opt.$trigger).data('uid'))
let moveImageDestFolder = _.nth(vueImage.folders, key)
vueImage.moveImage(moveImageId, moveImageDestFolder)
}
2017-02-09 01:52:37 +00:00
}
})
$.contextMenu('destroy', '.editor-modal-image-choices > figure')
$.contextMenu({
selector: '.editor-modal-image-choices > figure',
appendTo: '.editor-modal-image-choices',
position: (opt, x, y) => {
$(opt.$trigger).addClass('is-contextopen')
let trigPos = $(opt.$trigger).position()
let trigDim = { w: $(opt.$trigger).width() / 2, h: $(opt.$trigger).height() / 2 }
opt.$menu.css({ top: trigPos.top + trigDim.h, left: trigPos.left + trigDim.w })
2017-02-09 01:52:37 +00:00
},
events: {
hide: (opt) => {
$(opt.$trigger).removeClass('is-contextopen')
}
2017-02-09 01:52:37 +00:00
},
items: {
rename: {
name: 'Rename',
icon: 'fa-edit',
callback: (key, opt) => {
vueImage.renameImageId = _.toString(opt.$trigger[0].dataset.uid)
vueImage.renameImage()
}
},
move: {
name: 'Move to...',
icon: 'fa-folder-open-o',
items: moveFolders
},
delete: {
name: 'Delete',
icon: 'fa-trash',
callback: (key, opt) => {
vueImage.deleteImageId = _.toString(opt.$trigger[0].dataset.uid)
vueImage.deleteImageWarn(true)
}
2017-02-09 01:52:37 +00:00
}
}
})
}
}
})
2017-02-09 01:52:37 +00:00
$('#btn-editor-image-upload input').on('change', (ev) => {
let curImageAmount = vueImage.images.length
2017-02-09 01:52:37 +00:00
$(ev.currentTarget).simpleUpload('/uploads/img', {
2017-02-09 01:52:37 +00:00
name: 'imgfile',
data: {
folder: vueImage.currentFolder
},
limit: 20,
expect: 'json',
allowedExts: ['jpg', 'jpeg', 'gif', 'png', 'webp'],
allowedTypes: ['image/png', 'image/jpeg', 'image/gif', 'image/webp'],
maxFileSize: 3145728, // max 3 MB
2017-02-09 01:52:37 +00:00
init: (totalUploads) => {
vueImage.uploadSucceeded = false
vueImage.isLoadingText = 'Preparing to upload...'
vueImage.isLoading = true
},
progress: (progress) => {
vueImage.isLoadingText = 'Uploading...' + Math.round(progress) + '%'
},
success: (data) => {
if (data.ok) {
let failedUpls = _.filter(data.results, ['ok', false])
if (failedUpls.length) {
_.forEach(failedUpls, (u) => {
alerts.pushError('Upload error', u.msg)
2017-02-09 01:52:37 +00:00
})
if (failedUpls.length < data.results.length) {
alerts.push({
title: 'Some uploads succeeded',
message: 'Files that are not mentionned in the errors above were uploaded successfully.'
})
vueImage.uploadSucceeded = true
}
} else {
2017-02-09 01:52:37 +00:00
vueImage.uploadSucceeded = true
}
} else {
alerts.pushError('Upload error', data.msg)
2017-02-09 01:52:37 +00:00
}
},
2017-02-09 01:52:37 +00:00
error: (error) => {
alerts.pushError(error.message, this.upload.file.name)
},
2017-02-09 01:52:37 +00:00
finish: () => {
if (vueImage.uploadSucceeded) {
vueImage.waitChangeComplete(curImageAmount, true)
} else {
vueImage.isLoading = false
}
2017-02-09 01:52:37 +00:00
}
})
2017-02-09 01:52:37 +00:00
})
return vueImage
}