From: Aleksander Ɓukasz Date: Mon, 12 May 2014 11:20:50 +0000 (+0200) Subject: editor: automatically convert pasted urls to attachments to a proper links with ... X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/796338e669626012da93ebea5ec7afa482a70ed7?hp=b9558104eca9970ffba7f8e461e4878e70214114 editor: automatically convert pasted urls to attachments to a proper links with "file://" prefix Changing p tags to divs in dialog templates is necessary for a jQuery to create a proper collection object (compare $('

') to $('
') in jQuery 1.9.1). --- diff --git a/src/editor/modules/data/document.js b/src/editor/modules/data/document.js index db2b7a8..23aa000 100644 --- a/src/editor/modules/data/document.js +++ b/src/editor/modules/data/document.js @@ -45,6 +45,14 @@ _.extend(Document.prototype, { link = cfg.documentAttachmentUrl(link.substr(7)); } return link; + }, + getLinkForUrl: function(url) { + /* globals window */ + var baseUrl = function(url) {return url.split('/').slice(0,-1).join('/');}; + if(baseUrl(url) === baseUrl(window.location.origin + this.getUrlForLink('file://test'))) { + return 'file://' + _.last(url.split('/')); + } + return url; } }); diff --git a/src/editor/plugins/core/core.js b/src/editor/plugins/core/core.js index ade5d93..935b6d4 100644 --- a/src/editor/plugins/core/core.js +++ b/src/editor/plugins/core/core.js @@ -266,7 +266,11 @@ var createLinkFromSelection = function(callback, params) { executeButtonText: gettext('Apply'), cancelButtonText: gettext('Cancel'), fields: [ - {label: gettext('Link'), name: 'href', type: 'input'} + {label: gettext('Link'), name: 'href', type: 'input', + prePasteHandler: function(text) { + return params.fragment.document.getLinkForUrl(text); + }.bind(this) + } ] }), action = this; diff --git a/src/editor/plugins/core/links/linkElement.js b/src/editor/plugins/core/links/linkElement.js index 8a9edd7..9e4a935 100644 --- a/src/editor/plugins/core/links/linkElement.js +++ b/src/editor/plugins/core/links/linkElement.js @@ -44,7 +44,11 @@ _.extend(linkElement, { executeButtonText: gettext('Apply'), cancelButtonText: gettext('Cancel'), fields: [ - {label: gettext('Link'), name: 'href', type: 'input', initialValue: el.wlxmlNode.getAttr('href')} + {label: gettext('Link'), name: 'href', type: 'input', initialValue: el.wlxmlNode.getAttr('href'), + prePasteHandler: function(text) { + return this.wlxmlNode.document.getLinkForUrl(text); + }.bind(this) + } ] }); e.preventDefault(); diff --git a/src/editor/views/dialog/dialog.js b/src/editor/views/dialog/dialog.js index bad349d..dff0b6f 100644 --- a/src/editor/views/dialog/dialog.js +++ b/src/editor/views/dialog/dialog.js @@ -2,7 +2,8 @@ define(function(require) { 'use strict'; - var _ = require('libs/underscore'), + var $ = require('libs/jquery'), + _ = require('libs/underscore'), Backbone = require('libs/backbone'), dialogTemplate = require('libs/text!./dialog.html'), fieldTemplates = {}; @@ -36,9 +37,21 @@ define(function(require) { if(!template) { throw new Error('Field type {type} not recognized.'.replace('{type}', field.type)); } - body.append( - _.template(template)(_.extend({description: '', initialValue: ''}, field)) - ); + var widget = $(_.template(template)(_.extend({description: '', initialValue: ''}, field))); + + body.append(widget); + + if(_.isFunction(field.prePasteHandler) && field.type === 'input') { // TODO: extract this out to widget specific impl. + widget.find('input').on('paste', function(e) { + var clipboardData = e.originalEvent.clipboardData; + if(!clipboardData || !clipboardData.getData) { + return; + } + e.preventDefault(); + var text = clipboardData.getData('text/plain').replace(/\r?\n|\r/g, ' '); + $(e.target).val(field.prePasteHandler(text)); + }); + } }); if(this.options.text) { diff --git a/src/editor/views/dialog/templates/checkbox.html b/src/editor/views/dialog/templates/checkbox.html index 3c14092..a6ad457 100644 --- a/src/editor/views/dialog/templates/checkbox.html +++ b/src/editor/views/dialog/templates/checkbox.html @@ -1,5 +1,5 @@ -

+

<%= label %>:
<%= description %> -

\ No newline at end of file +
\ No newline at end of file diff --git a/src/editor/views/dialog/templates/input.html b/src/editor/views/dialog/templates/input.html index 1600d87..704972a 100644 --- a/src/editor/views/dialog/templates/input.html +++ b/src/editor/views/dialog/templates/input.html @@ -1,4 +1,4 @@ -

+

<%= label %>:
@@ -7,4 +7,4 @@ <%= description %>
-

\ No newline at end of file + \ No newline at end of file diff --git a/src/editor/views/dialog/templates/select.html b/src/editor/views/dialog/templates/select.html index 4b5a1de..1e527a6 100644 --- a/src/editor/views/dialog/templates/select.html +++ b/src/editor/views/dialog/templates/select.html @@ -1,4 +1,4 @@ -

+

<%= label %>:
<%= description %> -

\ No newline at end of file +
\ No newline at end of file diff --git a/src/editor/views/dialog/templates/textarea.html b/src/editor/views/dialog/templates/textarea.html index 839e7f4..bae15b3 100644 --- a/src/editor/views/dialog/templates/textarea.html +++ b/src/editor/views/dialog/templates/textarea.html @@ -1,5 +1,5 @@ -

+

<%= description %> -

+