editor: automatically convert pasted urls to attachments to a proper links with ...
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 12 May 2014 11:20:50 +0000 (13:20 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 12 May 2014 11:20:50 +0000 (13:20 +0200)
Changing p tags to divs in dialog templates is necessary for a jQuery
to create a proper collection object (compare $('<p><div></div></p>')
to $('<div><div></div></div>') in jQuery 1.9.1).

src/editor/modules/data/document.js
src/editor/plugins/core/core.js
src/editor/plugins/core/links/linkElement.js
src/editor/views/dialog/dialog.js
src/editor/views/dialog/templates/checkbox.html
src/editor/views/dialog/templates/input.html
src/editor/views/dialog/templates/select.html
src/editor/views/dialog/templates/textarea.html

index db2b7a8..23aa000 100644 (file)
@@ -45,6 +45,14 @@ _.extend(Document.prototype, {
             link = cfg.documentAttachmentUrl(link.substr(7));
         }
         return link;
             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;
     }
 });
 
     }
 });
 
index ade5d93..935b6d4 100644 (file)
@@ -266,7 +266,11 @@ var createLinkFromSelection = function(callback, params) {
             executeButtonText: gettext('Apply'),
             cancelButtonText: gettext('Cancel'),
             fields: [
             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;
             ]
         }),
         action = this;
index 8a9edd7..9e4a935 100644 (file)
@@ -44,7 +44,11 @@ _.extend(linkElement, {
             executeButtonText: gettext('Apply'),
             cancelButtonText: gettext('Cancel'),
             fields: [
             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();
             ]
         });
         e.preventDefault();
index bad349d..dff0b6f 100644 (file)
@@ -2,7 +2,8 @@ define(function(require) {
 
     'use strict';
 
 
     'use strict';
 
-    var _ = require('libs/underscore'),
+    var $ = require('libs/jquery'),
+        _ = require('libs/underscore'),
         Backbone = require('libs/backbone'),
         dialogTemplate = require('libs/text!./dialog.html'),
         fieldTemplates = {};
         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));
                 }
                 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) {
             });
 
             if(this.options.text) {
index 3c14092..a6ad457 100644 (file)
@@ -1,5 +1,5 @@
-<p>
+<div>
     <div style="float: left; width:100px;"><%= label %>:</div>
     <input type="checkbox" name="<%= name %>"/> 
     <span class="description"><%= description %></span>
     <div style="float: left; width:100px;"><%= label %>:</div>
     <input type="checkbox" name="<%= name %>"/> 
     <span class="description"><%= description %></span>
-</p>
\ No newline at end of file
+</div>
\ No newline at end of file
index 1600d87..704972a 100644 (file)
@@ -1,4 +1,4 @@
-<p>
+<div>
     <div style="float: left; width:100px;">
     <%= label %>:
     </div>
     <div style="float: left; width:100px;">
     <%= label %>:
     </div>
@@ -7,4 +7,4 @@
         <span class="description"><%= description %></span>
     </div>
     
         <span class="description"><%= description %></span>
     </div>
     
-</p>
\ No newline at end of file
+</div>
\ No newline at end of file
index 4b5a1de..1e527a6 100644 (file)
@@ -1,4 +1,4 @@
-<p>
+<div>
     <div style="float: left; width:100px;"><%= label %>:</div>
     <select name="<%= name %>">
     <% options.forEach(function(option) { %>
     <div style="float: left; width:100px;"><%= label %>:</div>
     <select name="<%= name %>">
     <% options.forEach(function(option) { %>
@@ -6,4 +6,4 @@
     <% }); %>
     </select>
     <span class="description"><%= description %></span>
     <% }); %>
     </select>
     <span class="description"><%= description %></span>
-</p>
\ No newline at end of file
+</div>
\ No newline at end of file
index 839e7f4..bae15b3 100644 (file)
@@ -1,5 +1,5 @@
-<p>
+<div>
     <label><%= label %></label>
     <textarea name="<%= name %>" rows="5"><%= initialValue %></textarea>
     <span class="description"><%= description %></span>
     <label><%= label %></label>
     <textarea name="<%= name %>" rows="5"><%= initialValue %></textarea>
     <span class="description"><%= description %></span>
-</p>
+</div>