mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2025-11-15 09:46:06 +01:00
Added drag-drop for media in Content editor
This commit is contained in:
@@ -2,6 +2,7 @@ import $ from 'jquery';
|
||||
import Dropzone from 'dropzone';
|
||||
import request from '../../utils/request';
|
||||
import { config, translations } from 'grav-config';
|
||||
import { Instance as Editor } from '../../forms/fields/editor';
|
||||
|
||||
Dropzone.autoDiscover = false;
|
||||
Dropzone.options.gravPageDropzone = {};
|
||||
@@ -68,6 +69,7 @@ export default class PageMedia {
|
||||
this.dropzone.on('error', this.onDropzoneError.bind(this));
|
||||
|
||||
this.fetchMedia();
|
||||
this.attachDragDrop();
|
||||
}
|
||||
|
||||
fetchMedia() {
|
||||
@@ -176,6 +178,46 @@ export default class PageMedia {
|
||||
modal.find('.error-content').html(msg);
|
||||
$.remodal.lookup[modal.data('remodal')].open();
|
||||
}
|
||||
|
||||
attachDragDrop() {
|
||||
this.container.delegate('[data-dz-insert]', 'click', (e) => {
|
||||
let target = $(e.currentTarget).parent('.dz-preview').find('.dz-filename');
|
||||
let editor = Editor.editors.filter((index, editor) => $(editor).attr('name') === 'content');
|
||||
|
||||
if (editor.length) {
|
||||
editor = editor.data('codemirror');
|
||||
editor.focus();
|
||||
|
||||
let filename = encodeURI(target.text());
|
||||
let shortcode = UriToMarkdown(filename);
|
||||
editor.doc.replaceSelection(shortcode);
|
||||
}
|
||||
});
|
||||
|
||||
this.container.delegate('.dz-preview', 'dragstart', (e) => {
|
||||
let target = $(e.currentTarget);
|
||||
let uri = encodeURI(target.find('.dz-filename').text());
|
||||
let shortcode = UriToMarkdown(uri);
|
||||
this.dropzone.disable();
|
||||
target.addClass('hide-backface');
|
||||
e.originalEvent.dataTransfer.effectAllowed = 'copy';
|
||||
e.originalEvent.dataTransfer.setData('text', shortcode);
|
||||
});
|
||||
|
||||
this.container.delegate('.dz-preview', 'dragend', (e) => {
|
||||
let target = $(e.currentTarget);
|
||||
this.dropzone.enable();
|
||||
target.removeClass('hide-backface');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function UriToMarkdown(uri) {
|
||||
uri = uri.replace(/@3x|@2x|@1x/, '');
|
||||
uri = uri.replace(/\(/g, '%28');
|
||||
uri = uri.replace(/\)/g, '%29');
|
||||
|
||||
return uri.match(/\.(jpe?g|png|gif|svg)$/i) ? `` : `[${decodeURI(uri)}](${uri})`;
|
||||
}
|
||||
|
||||
export let Instance = new PageMedia();
|
||||
|
||||
2
themes/grav/css-compiled/template.css
vendored
2
themes/grav/css-compiled/template.css
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
27
themes/grav/js/admin.min.js
vendored
27
themes/grav/js/admin.min.js
vendored
File diff suppressed because one or more lines are too long
40
themes/grav/js/vendor.min.js
vendored
40
themes/grav/js/vendor.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -146,6 +146,12 @@ $preview-height: 100px;
|
||||
&:hover .dz-insert {
|
||||
right: inherit;
|
||||
}
|
||||
|
||||
&.hide-backface:hover {
|
||||
.dz-remove, .dz-insert {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
&.dz-processing {
|
||||
.dz-details {
|
||||
|
||||
Reference in New Issue
Block a user