{name: 'intertitre', tag: 'H4', klass: 'intertitle'},
{name: 'liste', special: 'list', tag: 'UL', klass: 'list'},
{name: 'illustration', special: 'img', tag: 'DIV', subtag: true, klass: 'figure'},
+ {name: 'document', special: 'doc', tag: 'DIV', subtag: true, klass: 'document'},
{name: 'code', tag: 'PRE', klass: 'code'},
{name: 'note', tag: 'DIV', subtag: true, klass: 'note'},
],
$(inline_style_toolbar).hide();
}
});
- var $image_upload = $('<input type="file" nam="image" id="image-upload">');
+ var $image_upload = $('<input type="file" nam="image" id="image-upload" accept="image/*">');
$image_upload.on('change', upload_image);
$image_upload.appendTo(document.body);
+ var $document_upload = $('<input type="file" nam="document" id="document-upload">');
+ $document_upload.on('change', upload_document);
+ $document_upload.appendTo(document.body);
+
document.execCommand('defaultParagraphSeparator', false, 'p');
$(document).on('click', 'div.figure span.empty', function() {
window.active_figure = this.parentNode;
$('#image-upload').trigger('click');
return true;
});
+ $(document).on('click', 'div.document span.empty', function() {
+ window.active_document = this.parentNode;
+ $('#document-upload').trigger('click');
+ return true;
+ });
},
off: function() {
$('#image-upload').remove();
+ $('#document-upload').remove();
if (block_style_toolbar) { block_style_toolbar.hide(); }
if (inline_style_toolbar) { inline_style_toolbar.hide(); }
$(document).off('selectionchange');
if ($(this).prop('files').length > 0) {
var file = $(this).prop('files')[0];
var params = new FormData();
- params.append('image', file);
- $.post({url: '/wiki/ajax/image/', processData: false, data: params, contentType: false}).success(function(data) {
+ params.append('upload', file);
+ $.post({url: '/wiki/ajax/upload/', processData: false, data: params, contentType: false}).success(function(data) {
var img = document.createElement('IMG');
img.src = data.url;
if (data.orig_url) {
}
}
+ function upload_document() {
+ if ($(this).prop('files').length > 0) {
+ var file = $(this).prop('files')[0];
+ var params = new FormData();
+ params.append('upload', file);
+ $.post({url: '/wiki/ajax/upload/', processData: false, data: params, contentType: false}).success(function(data) {
+ var doc_link = document.createElement('A');
+ doc_link.className = 'button';
+ doc_link.textContent = 'Télécharger ' + data.filename;
+ doc_link.href = data.url;
+ $(window.active_document).empty().append(doc_link);
+ });
+ }
+ }
+
function get_contenteditable_subnode(node) {
if (node === null) return null;
if (node.contentEditable === 'true') return node; // but we shouldn't arrive at root
sel.removeAllRanges();
sel.addRange(range);
update_block_style_toolbar();
-
+ return;
+ }
+ if (this.action_block.special == 'doc') {
+ action = 'insertHTML';
+ param = '<div class="document"><span class="empty"></span></div><p id="new-p"></p>';
+ document.execCommand(action, false, param);
+ current_anchor = $('#new-p')[0];
+ $(current_anchor).attr('id', null);
+ var range = document.createRange();
+ range.setStart(current_anchor, 0);
+ sel.removeAllRanges();
+ sel.addRange(range);
+ update_block_style_toolbar();
return;
}
if (this.action_block.special == 'list') {
+import os
+
from django.contrib.auth.decorators import login_required
from django.core.files.storage import default_storage
from django.db.models import Q
@csrf_exempt
-def ajax_image(request, *args, **kwargs):
- img = request.FILES['image']
- saved_path = default_storage.save('wiki/images/%s' % img.name, img)
+def ajax_upload(request, *args, **kwargs):
+ upload = request.FILES['upload']
+ upload_path = 'uploads'
+ if os.path.splitext(upload.name.lower())[-1] in ('.jpg', '.jpeg', '.png', '.gif', '.svg'):
+ upload_path = 'images'
+ saved_path = default_storage.save('wiki/%s/%s' % (upload_path, upload.name), upload)
url = '/media/' + saved_path
- response = {'url': url}
- if default_storage.size(saved_path) > 500_000 and not img.name.endswith('.svg'):
- response['orig_url'] = url
- try:
- response['url'] = get_thumbnail(saved_path, '1000', upscale=False).url
- except OSError:
- pass
+ response = {'url': url, 'filename': upload.name}
+ if upload_path == 'images':
+ if default_storage.size(saved_path) > 500_000 and not upload.name.endswith('.svg'):
+ response['orig_url'] = url
+ try:
+ response['url'] = get_thumbnail(saved_path, '1000', upscale=False).url
+ except OSError:
+ pass
return JsonResponse(response)