From 05f41bac2fabf3f79bbc8dbbaaeb7ffaf9db84c8 Mon Sep 17 00:00:00 2001 From: lli Date: Fri, 13 Dec 2013 11:41:38 +1100 Subject: [PATCH] Added hyperlink support for text elements --- CMakeLists.txt | 33 +++--- webodf/lib/gui/HyperlinkManager.js | 173 ++++++++++++++++++++++++++++ webodf/lib/gui/SessionController.js | 12 +- webodf/lib/manifest.json | 25 ++++ webodf/lib/ops/OpApplyHyperlink.js | 131 +++++++++++++++++++++ webodf/lib/ops/OpRemoveHyperlink.js | 87 ++++++++++++++ webodf/lib/ops/OperationFactory.js | 6 +- webodf/tests/ops/operationtests.xml | 64 +++++++++- 8 files changed, 512 insertions(+), 19 deletions(-) create mode 100644 webodf/lib/gui/HyperlinkManager.js create mode 100644 webodf/lib/ops/OpApplyHyperlink.js create mode 100644 webodf/lib/ops/OpRemoveHyperlink.js diff --git a/CMakeLists.txt b/CMakeLists.txt index 94549f03..d81d5461 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -309,19 +309,6 @@ set(UNTYPEDLIBJSFILES lib/ops/OdtCursor.js lib/ops/OdtDocument.js lib/ops/Operation.js - lib/xmldom/RelaxNG.js - lib/xmldom/RelaxNG2.js - lib/gui/Caret.js - lib/gui/EventManager.js - lib/gui/ShadowCursor.js - lib/gui/UndoManager.js - lib/gui/UndoStateRules.js - lib/ops/EditInfo.js - lib/ops/OpAddAnnotation.js - lib/ops/OpAddCursor.js - lib/ops/OpAddMember.js - lib/ops/OpAddStyle.js - lib/ops/OpApplyDirectStyling.js lib/ops/OpInsertImage.js lib/ops/OpInsertTable.js lib/ops/OpInsertText.js @@ -329,6 +316,7 @@ set(UNTYPEDLIBJSFILES lib/ops/OpRemoveAnnotation.js lib/ops/OpRemoveBlob.js lib/ops/OpRemoveCursor.js + lib/ops/OpRemoveHyperlink.js lib/ops/OpRemoveMember.js lib/ops/OpRemoveStyle.js lib/ops/OpRemoveText.js @@ -338,20 +326,35 @@ set(UNTYPEDLIBJSFILES lib/ops/OpUpdateMember.js lib/ops/OpUpdateMetadata.js lib/ops/OpUpdateParagraphStyle.js + lib/xmldom/RelaxNG.js + lib/xmldom/RelaxNG2.js + lib/gui/Caret.js + lib/gui/EventManager.js + lib/gui/PlainTextPasteboard.js + lib/gui/ShadowCursor.js + lib/gui/UndoManager.js + lib/gui/UndoStateRules.js + lib/ops/EditInfo.js + lib/ops/OpAddAnnotation.js + lib/ops/OpAddCursor.js + lib/ops/OpAddMember.js + lib/ops/OpAddStyle.js + lib/ops/OpApplyDirectStyling.js + lib/ops/OpApplyHyperlink.js lib/ops/OperationFactory.js lib/ops/OperationRouter.js lib/ops/OperationTransformMatrix.js - lib/ops/OperationTransformer.js lib/ops/TrivialOperationRouter.js lib/gui/EditInfoMarker.js - lib/gui/PlainTextPasteboard.js lib/gui/SelectionView.js lib/gui/SelectionViewManager.js lib/gui/TrivialUndoManager.js + lib/ops/OperationTransformer.js lib/ops/Session.js lib/gui/AnnotationController.js lib/gui/DirectParagraphStyler.js lib/gui/DirectTextStyler.js + lib/gui/HyperlinkManager.js lib/gui/ImageManager.js lib/gui/TextManipulator.js lib/gui/SessionController.js diff --git a/webodf/lib/gui/HyperlinkManager.js b/webodf/lib/gui/HyperlinkManager.js new file mode 100644 index 00000000..173f80df --- /dev/null +++ b/webodf/lib/gui/HyperlinkManager.js @@ -0,0 +1,173 @@ +/** + * Copyright (C) 2013 KO GmbH + * + * @licstart + * The JavaScript code in this page is free software: you can redistribute it + * and/or modify it under the terms of the GNU Affero General Public License + * (GNU AGPL) as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. The code is distributed + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details. + * + * As additional permission under GNU AGPL version 3 section 7, you + * may distribute non-source (e.g., minimized or compacted) forms of + * that code without the copy of the GNU GPL normally required by + * section 4, provided you include this license notice and a URL + * through which recipients can access the Corresponding Source. + * + * As a special exception to the AGPL, any HTML file which merely makes function + * calls to this code, and for that purpose includes it by reference shall be + * deemed a separate work for copyright law purposes. In addition, the copyright + * holders of this code give you permission to combine this code with free + * software libraries that are released under the GNU LGPL. You may copy and + * distribute such a system following the terms of the GNU AGPL for this code + * and the LGPL for the libraries. If you modify this code, you may extend this + * exception to your version of the code, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your + * version. + * + * This license applies to this entire compilation. + * @licend + * @source: http://www.webodf.org/ + * @source: http://gitorious.org/webodf/webodf/ + */ + +/*global runtime, core, gui, Node, ops, odf */ + +runtime.loadClass("odf.OdfUtils"); + +/** + * @constructor + * @param {!ops.Session} session + * @param {!string} inputMemberId + */ +gui.HyperlinkManager = function HyperlinkManager(session, inputMemberId) { + "use strict"; + + var odfUtils = new odf.OdfUtils(), + odtDocument = session.getOdtDocument(); + + /** + * Convert the current selection into a hyperlink + * @param {!string} hyperlink Hyperlink to insert + * @param {!string=} insertionText Optional text to insert as the text content for the hyperlink. + * Note, the insertion text will not replace the existing selection content. + */ + function addHyperlink(hyperlink, insertionText) { + var selection = odtDocument.getCursorSelection(inputMemberId), + op = new ops.OpApplyHyperlink(), + operations = []; + + if (selection.length === 0 || insertionText) { + insertionText = insertionText || hyperlink; + op = new ops.OpInsertText(); + op.init({ + memberid: inputMemberId, + position: selection.position, + text: insertionText + }); + selection.length = insertionText.length; + operations.push(op); + } + + op = new ops.OpApplyHyperlink(); + op.init({ + memberid: inputMemberId, + position: selection.position, + length: selection.length, + hyperlink: hyperlink + }); + operations.push(op); + session.enqueue(operations); + } + this.addHyperlink = addHyperlink; + + /** + * Remove all hyperlinks within the current selection. If a range of text is selected, + * this will only unlink the selection. If the current selection is collapsed within a + * link, that entire link will be removed. + */ + function removeHyperlinks() { + var iterator = gui.SelectionMover.createPositionIterator(odtDocument.getRootNode()), + selectedRange = odtDocument.getCursor(inputMemberId).getSelectedRange(), + links = odfUtils.getHyperlinkElements(selectedRange), + removeEntireLink = selectedRange.collapsed && links.length === 1, + domRange = odtDocument.getDOM().createRange(), + operations = [], + cursorRange, firstLink, lastLink, offset, op; + + if (links.length === 0) { + return; + } + + // Remove any links that overlap with the current selection + links.forEach(function (link) { + domRange.selectNodeContents(link); + cursorRange = odtDocument.convertDomToCursorRange({ + anchorNode: /**@type{!Node}*/(domRange.startContainer), + anchorOffset: domRange.startOffset, + focusNode: /**@type{!Node}*/(domRange.endContainer), + focusOffset: domRange.endOffset + }); + op = new ops.OpRemoveHyperlink(); + op.init({ + memberid: inputMemberId, + position: cursorRange.position, + length: cursorRange.length + }); + operations.push(op); + }); + + if (!removeEntireLink) { + // Re-add any leading or trailing links that were only partially selected + firstLink = /**@type{!Element}*/(links[0]); + if (selectedRange.comparePoint(firstLink, 0) === -1) { + domRange.setStart(firstLink, 0); + domRange.setEnd(selectedRange.startContainer, selectedRange.startOffset); + cursorRange = odtDocument.convertDomToCursorRange({ + anchorNode: /**@type{!Node}*/(domRange.startContainer), + anchorOffset: domRange.startOffset, + focusNode: /**@type{!Node}*/(domRange.endContainer), + focusOffset: domRange.endOffset + }); + if (cursorRange.length > 0) { + op = new ops.OpApplyHyperlink(); + op.init({ + memberid: inputMemberId, + position: cursorRange.position, + length: cursorRange.length, + hyperlink: odfUtils.getHyperlinkTarget(firstLink) + }); + operations.push(op); + } + } + lastLink = /**@type{!Element}*/(links[links.length - 1]); + iterator.moveToEndOfNode(lastLink); + offset = iterator.unfilteredDomOffset(); + if (selectedRange.comparePoint(lastLink, offset) === 1) { + domRange.setStart(selectedRange.endContainer, selectedRange.endOffset); + domRange.setEnd(lastLink, offset); + cursorRange = odtDocument.convertDomToCursorRange({ + anchorNode: /**@type{!Node}*/(domRange.startContainer), + anchorOffset: domRange.startOffset, + focusNode: /**@type{!Node}*/(domRange.endContainer), + focusOffset: domRange.endOffset + }); + if (cursorRange.length > 0) { + op = new ops.OpApplyHyperlink(); + op.init({ + memberid: inputMemberId, + position: cursorRange.position, + length: cursorRange.length, + hyperlink: odfUtils.getHyperlinkTarget(lastLink) + }); + operations.push(op); + } + } + } + + session.enqueue(operations); + domRange.detach(); + } + this.removeHyperlinks = removeHyperlinks; +}; diff --git a/webodf/lib/gui/SessionController.js b/webodf/lib/gui/SessionController.js index af5f3f9a..573cbf71 100644 --- a/webodf/lib/gui/SessionController.js +++ b/webodf/lib/gui/SessionController.js @@ -50,6 +50,7 @@ runtime.loadClass("gui.Clipboard"); runtime.loadClass("gui.DirectTextStyler"); runtime.loadClass("gui.DirectParagraphStyler"); runtime.loadClass("gui.KeyboardHandler"); +runtime.loadClass("gui.HyperlinkManager"); runtime.loadClass("gui.ImageManager"); runtime.loadClass("gui.ImageSelector"); runtime.loadClass("gui.TextManipulator"); @@ -107,7 +108,8 @@ gui.SessionController = (function () { drawShadowCursorTask, redrawRegionSelectionTask, pasteHandler = new gui.PlainTextPasteboard(odtDocument, inputMemberId), - clickCount = 0; + clickCount = 0, + hyperlinkManager = new gui.HyperlinkManager(session, inputMemberId); runtime.assert(window !== null, "Expected to be run in an environment which has a global window, like a browser."); @@ -1126,7 +1128,6 @@ gui.SessionController = (function () { return undoManager; }; - /** * @returns {?gui.AnnotationController} */ @@ -1148,6 +1149,13 @@ gui.SessionController = (function () { return directParagraphStyler; }; + /** + * @returns {!gui.HyperlinkManager} + */ + this.getHyperlinkManager = function () { + return hyperlinkManager; + }; + /** * @returns {!gui.ImageManager} */ diff --git a/webodf/lib/manifest.json b/webodf/lib/manifest.json index c1ed9519..8c510e01 100644 --- a/webodf/lib/manifest.json +++ b/webodf/lib/manifest.json @@ -111,6 +111,15 @@ "gui/EventManager.js": [ "ops/OdtDocument.js" ], + "gui/HyperlinkManager.js": [ + "gui/SelectionMover.js", + "odf/OdfUtils.js", + "ops/OpApplyHyperlink.js", + "ops/OpInsertText.js", + "ops/OpMoveCursor.js", + "ops/OpRemoveHyperlink.js", + "ops/Session.js" + ], "gui/ImageManager.js": [ "odf/Namespaces.js", "odf/ObjectNameGenerator.js", @@ -164,6 +173,7 @@ "gui/DirectParagraphStyler.js", "gui/DirectTextStyler.js", "gui/EventManager.js", + "gui/HyperlinkManager.js", "gui/ImageManager.js", "gui/ImageSelector.js", "gui/KeyboardHandler.js", @@ -362,6 +372,13 @@ "ops/OdtDocument.js", "ops/Operation.js" ], + "ops/OpApplyHyperlink.js": [ + "core/DomUtils.js", + "odf/Namespaces.js", + "odf/OdfUtils.js", + "ops/OdtDocument.js", + "ops/Operation.js" + ], "ops/OpInsertImage.js": [ "odf/Namespaces.js", "ops/OdtDocument.js", @@ -392,6 +409,12 @@ "ops/OpRemoveCursor.js": [ "ops/Operation.js" ], + "ops/OpRemoveHyperlink.js": [ + "core/DomUtils.js", + "odf/OdfUtils.js", + "ops/OdtDocument.js", + "ops/Operation.js" + ], "ops/OpRemoveMember.js": [ "ops/Member.js", "ops/OdtDocument.js", @@ -444,6 +467,7 @@ "ops/OpAddMember.js", "ops/OpAddStyle.js", "ops/OpApplyDirectStyling.js", + "ops/OpApplyHyperlink.js", "ops/OpInsertImage.js", "ops/OpInsertTable.js", "ops/OpInsertText.js", @@ -451,6 +475,7 @@ "ops/OpRemoveAnnotation.js", "ops/OpRemoveBlob.js", "ops/OpRemoveCursor.js", + "ops/OpRemoveHyperlink.js", "ops/OpRemoveMember.js", "ops/OpRemoveStyle.js", "ops/OpRemoveText.js", diff --git a/webodf/lib/ops/OpApplyHyperlink.js b/webodf/lib/ops/OpApplyHyperlink.js new file mode 100644 index 00000000..0f1a4a0f --- /dev/null +++ b/webodf/lib/ops/OpApplyHyperlink.js @@ -0,0 +1,131 @@ +/** + * @license + * Copyright (C) 2012-2013 KO GmbH + * + * @licstart + * The JavaScript code in this page is free software: you can redistribute it + * and/or modify it under the terms of the GNU Affero General Public License + * (GNU AGPL) as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. The code is distributed + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details. + * + * As additional permission under GNU AGPL version 3 section 7, you + * may distribute non-source (e.g., minimized or compacted) forms of + * that code without the copy of the GNU GPL normally required by + * section 4, provided you include this license notice and a URL + * through which recipients can access the Corresponding Source. + * + * As a special exception to the AGPL, any HTML file which merely makes function + * calls to this code, and for that purpose includes it by reference shall be + * deemed a separate work for copyright law purposes. In addition, the copyright + * holders of this code give you permission to combine this code with free + * software libraries that are released under the GNU LGPL. You may copy and + * distribute such a system following the terms of the GNU AGPL for this code + * and the LGPL for the libraries. If you modify this code, you may extend this + * exception to your version of the code, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your + * version. + * + * This license applies to this entire compilation. + * @licend + * @source: http://www.webodf.org/ + * @source: http://gitorious.org/webodf/webodf/ + */ + +/*global ops, odf, core, runtime, Node */ +runtime.loadClass("core.DomUtils"); +runtime.loadClass("odf.OdfUtils"); +/** + * @constructor + * @implements ops.Operation + */ +ops.OpApplyHyperlink = function OpApplyHyperlink() { + "use strict"; + + var memberid, timestamp, position, length, hyperlink, + domUtils = new core.DomUtils(), + odfUtils = new odf.OdfUtils(); + + this.init = function (data) { + memberid = data.memberid; + timestamp = data.timestamp; + position = data.position; + length = data.length; + hyperlink = data.hyperlink; + }; + + this.isEdit = true; + + function createHyperlink(document, hyperlink) { + var node = document.createElementNS(odf.Namespaces.textns, 'text:a'); + node.setAttributeNS(odf.Namespaces.xlinkns, 'xlink:type', 'simple'); + node.setAttributeNS(odf.Namespaces.xlinkns, 'xlink:href', hyperlink); + return node; + } + + function isPartOfLink(node) { + while (node) { + if (odfUtils.isHyperlink(node)) { + return true; + } + node = node.parentNode; + } + return false; + } + + // TODO: support adding image link + this.execute = function (odtDocument) { + var ownerDocument = odtDocument.getDOM(), + range = odtDocument.convertCursorToDomRange(position, length), + boundaryNodes = domUtils.splitBoundaries(range), + modifiedParagraphs = [], + textNodes = odfUtils.getTextNodes(range, false); + + if (textNodes.length === 0) { + return false; + } + + textNodes.forEach(function (node) { + var linkNode, + paragraph = odfUtils.getParagraphElement(node); + runtime.assert(isPartOfLink(node) === false, "The given range should not contain any link."); + // TODO there could be a small efficiency gain by merging adjacent text nodes into a single hyperlink parent. + // This is probably only a corner case however and there are not likely to be major repercussions for not doing it + linkNode = createHyperlink(ownerDocument, hyperlink); + node.parentNode.insertBefore(linkNode, node); + linkNode.appendChild(node); + odtDocument.getOdfCanvas().registerLink(linkNode); + if (modifiedParagraphs.indexOf(paragraph) === -1) { + modifiedParagraphs.push(paragraph); + } + }); + + + boundaryNodes.forEach(domUtils.normalizeTextNodes); + range.detach(); + + odtDocument.getOdfCanvas().refreshSize(); + odtDocument.getOdfCanvas().rerenderAnnotations(); + modifiedParagraphs.forEach(function (paragraph) { + odtDocument.emit(ops.OdtDocument.signalParagraphChanged, { + paragraphElement: paragraph, + memberId: memberid, + timeStamp: timestamp + }); + }); + + return true; + }; + + this.spec = function () { + return { + optype: "ApplyHyperlink", + memberid: memberid, + timestamp: timestamp, + position: position, + length: length, + hyperlink: hyperlink + }; + }; +}; diff --git a/webodf/lib/ops/OpRemoveHyperlink.js b/webodf/lib/ops/OpRemoveHyperlink.js new file mode 100644 index 00000000..fd48d6c7 --- /dev/null +++ b/webodf/lib/ops/OpRemoveHyperlink.js @@ -0,0 +1,87 @@ +/** + * @license + * Copyright (C) 2012-2013 KO GmbH + * + * @licstart + * The JavaScript code in this page is free software: you can redistribute it + * and/or modify it under the terms of the GNU Affero General Public License + * (GNU AGPL) as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. The code is distributed + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details. + * + * As additional permission under GNU AGPL version 3 section 7, you + * may distribute non-source (e.g., minimized or compacted) forms of + * that code without the copy of the GNU GPL normally required by + * section 4, provided you include this license notice and a URL + * through which recipients can access the Corresponding Source. + * + * As a special exception to the AGPL, any HTML file which merely makes function + * calls to this code, and for that purpose includes it by reference shall be + * deemed a separate work for copyright law purposes. In addition, the copyright + * holders of this code give you permission to combine this code with free + * software libraries that are released under the GNU LGPL. You may copy and + * distribute such a system following the terms of the GNU AGPL for this code + * and the LGPL for the libraries. If you modify this code, you may extend this + * exception to your version of the code, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your + * version. + * + * This license applies to this entire compilation. + * @licend + * @source: http://www.webodf.org/ + * @source: http://gitorious.org/webodf/webodf/ + */ + +/*global ops, odf, core, runtime */ +runtime.loadClass("core.DomUtils"); +runtime.loadClass("odf.OdfUtils"); +/** + * @constructor + * @implements ops.Operation + */ +ops.OpRemoveHyperlink = function OpRemoveHyperlink() { + "use strict"; + + var memberid, timestamp, position, length, + domUtils = new core.DomUtils(), + odfUtils = new odf.OdfUtils(); + + this.init = function (data) { + memberid = data.memberid; + timestamp = data.timestamp; + position = data.position; + length = data.length; + }; + + this.isEdit = true; + + this.execute = function (odtDocument) { + var range = odtDocument.convertCursorToDomRange(position, length), + links = odfUtils.getHyperlinkElements(range), + node; + + runtime.assert(links.length === 1, "The given range should only contain a single link."); + node = domUtils.mergeIntoParent(links[0]); + range.detach(); + + odtDocument.getOdfCanvas().refreshSize(); + odtDocument.emit(ops.OdtDocument.signalParagraphChanged, { + paragraphElement: odfUtils.getParagraphElement(node), + memberId: memberid, + timeStamp: timestamp + }); + odtDocument.getOdfCanvas().rerenderAnnotations(); + return true; + }; + + this.spec = function () { + return { + optype: "RemoveHyperlink", + memberid: memberid, + timestamp: timestamp, + position: position, + length: length + }; + }; +}; diff --git a/webodf/lib/ops/OperationFactory.js b/webodf/lib/ops/OperationFactory.js index 65657bc8..70d238b4 100644 --- a/webodf/lib/ops/OperationFactory.js +++ b/webodf/lib/ops/OperationFactory.js @@ -63,6 +63,8 @@ runtime.loadClass("ops.OpRemoveStyle"); runtime.loadClass("ops.OpAddAnnotation"); runtime.loadClass("ops.OpRemoveAnnotation"); runtime.loadClass("ops.OpUpdateMetadata"); +runtime.loadClass("ops.OpApplyHyperlink"); +runtime.loadClass("ops.OpRemoveHyperlink"); /** * @constructor @@ -128,7 +130,9 @@ ops.OperationFactory = function OperationFactory() { RemoveCursor : constructor(ops.OpRemoveCursor), AddAnnotation : constructor(ops.OpAddAnnotation), RemoveAnnotation : constructor(ops.OpRemoveAnnotation), - UpdateMetadata: constructor(ops.OpUpdateMetadata) + UpdateMetadata: constructor(ops.OpUpdateMetadata), + ApplyHyperlink: constructor(ops.OpApplyHyperlink), + RemoveHyperlink: constructor(ops.OpRemoveHyperlink) }; } diff --git a/webodf/tests/ops/operationtests.xml b/webodf/tests/ops/operationtests.xml index 2cc8cc67..52dc5ca7 100644 --- a/webodf/tests/ops/operationtests.xml +++ b/webodf/tests/ops/operationtests.xml @@ -568,6 +568,20 @@ efgh + + Hello + + + + + + + HelloWorld + + + + Hell + AB CD @@ -1396,5 +1410,53 @@ 1Alice2013-08-05T12:34:07.061Z - + + Hello + + + + Hello + + + HelloWorld + + + + HelloWorld + + + helloworld + + + + helloworld + + + worldhello + + + + worldhello + + + HelloWorld + + + + HelloWorld + + + helloworld + + + + helloworld + + + helloworld + + + + helloworld +