var Phylly = {
BLOCKS: [
{name: 'intertitre', tag: 'h4', klass: 'intertitle'},
+ {name: 'illustration', special: 'img', tag: 'DIV', subtag: true, klass: 'figure'},
{name: 'code', tag: 'PRE', klass: 'code'},
{name: 'note', tag: 'DIV', subtag: true, klass: 'note'},
],
$(style_popup).hide();
}
});
+ var $image_upload = $('<input type="file" nam="image" id="image-upload">');
+ $image_upload.on('change', upload_image);
+ $image_upload.appendTo(document.body);
+
+ $(document).on('click', 'div.figure', function() {
+ window.active_figure = this;
+ $('#image-upload').trigger('click');
+ return true;
+ });
},
off: function() {
+ $('#image-upload').remove();
$(document).off('selectionchange');
},
}
window.Phylly = Phylly;
+ function upload_image() {
+ 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) {
+ var img = document.createElement('IMG');
+ img.src = data.url;
+ $(window.active_figure).empty().append(img);
+ });
+ }
+ }
+
function get_contenteditable_subnode(node) {
if (node === null) return null;
if (node.parentNode.contentEditable === 'true') return node;
function block_style() {
var sel = window.getSelection();
var current_anchor = sel.anchorNode;
+ if (this.action_block.special == 'img') {
+ action = 'insertHTML';
+ param = '<div class="figure"><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_popup();
+
+ return;
+ }
if (this.classList.contains('on')) { // toggle off
if (this.action_block.subtag) {
// unwrap
url(r'^wiki/newpage/$', wiki_views.new_page),
url(r'^wiki/ajax/newpage/$', wiki_views.ajax_new_page),
+ url(r'^wiki/ajax/image/$', wiki_views.ajax_image),
url(r'^wiki/', combo.public.views.page),
url(r'^ajax/cell/(?P<page_pk>\d+)/(?P<cell_reference>[\w_]+-\d+)/$',
combo.public.views.ajax_page_cell, name='combo-public-ajax-page-cell'),
from django.contrib.auth.decorators import login_required
+from django.core.files.storage import default_storage
from django.db.models import Q
from django.http import JsonResponse
from django.utils.text import slugify
'url': page.get_online_url(),
'request_id': request.POST['request_id'],
})
+
+
+@csrf_exempt
+def ajax_image(request, *args, **kwargs):
+ img = request.FILES['image']
+ # TODO: resize if necessary, and store both original and resized images
+ saved_path = default_storage.save('wiki/images/%s' % img.name, img)
+ return JsonResponse({'url': '/media/' + saved_path})