From a0340f141917ff382b7a5af73c0edc7b96509163 Mon Sep 17 00:00:00 2001
From: yflory <yann.flory@xwiki.com>
Date: Thu, 23 Feb 2017 14:27:18 +0100
Subject: [PATCH] Open link in a new tab in /pad

---
 www/pad/links.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
 www/pad/main.js  |  4 +++-
 2 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 www/pad/links.js

diff --git a/www/pad/links.js b/www/pad/links.js
new file mode 100644
index 000000000..ed369c506
--- /dev/null
+++ b/www/pad/links.js
@@ -0,0 +1,58 @@
+define(function () {
+    // Adds a context menu entry to open the selected link in a new tab.
+    // See https://github.com/xwiki-contrib/application-ckeditor/commit/755d193497bf23ed874d874b4ae92fbee887fc10
+    return {
+        addSupportForOpeningLinksInNewTab : function (Ckeditor) {
+            return function(event) {
+                var editor = event.editor;
+                if (!Ckeditor.plugins.link) {
+                    return;
+                }
+                editor.addCommand( 'openLink', {
+                    exec: function(editor) {
+                        var anchor = getActiveLink(editor);
+                        if (anchor) {
+                            var href = anchor.getAttribute('href');
+                            if (href) {
+                                window.open(href);
+                            }
+                        }
+                    }
+                });
+                if (typeof editor.addMenuItem === 'function') {
+                    editor.addMenuItem('openLink', {
+                        label: 'Open Link in New Tab',
+                        command: 'openLink',
+                        group: 'link',
+                        order: -1
+                    });
+                }
+                if (editor.contextMenu) {
+                    editor.contextMenu.addListener(function(startElement, selection, path) {
+                        if (startElement) {
+                            var anchor = getActiveLink(editor);
+                            if (anchor && anchor.getAttribute('href')) {
+                                return {openLink: Ckeditor.TRISTATE_OFF};
+                            }
+                        }
+                    });
+                    editor.contextMenu._.panelDefinition.css.push('.cke_button__openLink_icon {' +
+                        Ckeditor.skin.getIconStyle('link') + '}');
+                }
+                // Returns the DOM element of the active (currently focused) link. It has also support for linked image widgets.
+                // @return {CKEDITOR.dom.element}
+                var getActiveLink = function(editor) {
+                    var anchor = Ckeditor.plugins.link.getSelectedLink(editor),
+                        // We need to do some special checking against widgets availability.
+                        activeWidget = editor.widgets && editor.widgets.focused;
+                    // If default way of getting links didn't return anything useful..
+                    if (!anchor && activeWidget && activeWidget.name == 'image' && activeWidget.parts.link) {
+                        // Since CKEditor 4.4.0 image widgets may be linked.
+                        anchor = activeWidget.parts.link;
+                    }
+                    return anchor;
+                };
+            };
+        }
+    };
+});
\ No newline at end of file
diff --git a/www/pad/main.js b/www/pad/main.js
index 7b3fbf4b8..f105e7582 100644
--- a/www/pad/main.js
+++ b/www/pad/main.js
@@ -12,12 +12,13 @@ define([
     '/common/cryptpad-common.js',
     '/common/visible.js',
     '/common/notify.js',
+    '/pad/links.js',
     '/bower_components/file-saver/FileSaver.min.js',
     '/bower_components/diff-dom/diffDOM.js',
     '/bower_components/jquery/dist/jquery.min.js',
 ], function (Crypto, realtimeInput, Hyperjson,
     Toolbar, Cursor, JsonOT, TypingTest, JSONSortify, TextPatcher, Cryptpad,
-    Visible, Notify) {
+    Visible, Notify, Links) {
     var $ = window.jQuery;
     var saveAs = window.saveAs;
     var Messages = Cryptpad.Messages;
@@ -102,6 +103,7 @@ define([
             customConfig: '/customize/ckeditor-config.js',
         });
 
+        editor.on('instanceReady', Links.addSupportForOpeningLinksInNewTab(Ckeditor));
         editor.on('instanceReady', function (Ckeditor) {
             var $bar = $('#pad-iframe')[0].contentWindow.$('#cke_1_toolbox');
             var parsedHash = Cryptpad.parsePadUrl(window.location.href);