2012-11-02 21:04:01 +01:00
|
|
|
/**
|
2013-04-06 05:21:18 +02:00
|
|
|
* @license
|
|
|
|
|
* Copyright (C) 2012-2013 KO GmbH <copyright@kogmbh.com>
|
|
|
|
|
*
|
2012-11-02 21:04:01 +01:00
|
|
|
* @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.
|
|
|
|
|
*
|
2013-10-21 17:55:16 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
2012-11-02 21:04:01 +01:00
|
|
|
* 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/
|
2013-10-23 18:20:30 +02:00
|
|
|
* @source: https://github.com/kogmbh/WebODF/
|
2012-11-02 21:04:01 +01:00
|
|
|
*/
|
|
|
|
|
|
2013-10-08 16:01:37 +02:00
|
|
|
/*global ops, runtime, odf, core, Node*/
|
2013-07-10 23:17:52 +10:00
|
|
|
|
2013-07-23 11:32:20 +10:00
|
|
|
runtime.loadClass("odf.Namespaces");
|
2013-07-10 23:17:52 +10:00
|
|
|
runtime.loadClass("odf.OdfUtils");
|
|
|
|
|
runtime.loadClass("core.DomUtils");
|
2012-11-02 21:04:01 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @constructor
|
2012-11-02 22:24:03 +01:00
|
|
|
* @implements ops.Operation
|
2012-11-02 21:04:01 +01:00
|
|
|
*/
|
2013-04-06 04:39:39 +02:00
|
|
|
ops.OpRemoveText = function OpRemoveText() {
|
2012-11-02 21:04:01 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
2013-07-02 23:11:21 +02:00
|
|
|
var memberid, timestamp,
|
|
|
|
|
/**@type {number}*/
|
|
|
|
|
position,
|
|
|
|
|
/**@type {number}*/
|
|
|
|
|
length,
|
2013-07-23 11:32:20 +10:00
|
|
|
odfUtils,
|
2013-07-10 23:17:52 +10:00
|
|
|
domUtils,
|
2013-09-19 16:19:23 +10:00
|
|
|
editinfons = 'urn:webodf:names:editinfo',
|
|
|
|
|
/**@type {!Object.<!string, !boolean>}*/
|
|
|
|
|
odfNodeNamespaceMap = {};
|
2012-11-02 21:04:01 +01:00
|
|
|
|
2012-11-10 10:56:38 +01:00
|
|
|
this.init = function (data) {
|
2013-07-03 11:39:51 +10:00
|
|
|
runtime.assert(data.length >= 0, "OpRemoveText only supports positive lengths");
|
2012-11-02 21:04:01 +01:00
|
|
|
memberid = data.memberid;
|
2013-03-13 16:35:35 +05:30
|
|
|
timestamp = data.timestamp;
|
2013-07-02 23:11:21 +02:00
|
|
|
position = parseInt(data.position, 10);
|
|
|
|
|
length = parseInt(data.length, 10);
|
2013-06-06 17:08:48 +10:00
|
|
|
odfUtils = new odf.OdfUtils();
|
2013-07-10 23:17:52 +10:00
|
|
|
domUtils = new core.DomUtils();
|
2013-09-19 16:19:23 +10:00
|
|
|
|
|
|
|
|
// only add odf element namespaces here.
|
|
|
|
|
// Namespaces solely used for attributes are excluded. eg. fo, xlink & xml
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.dbns] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.dcns] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.dr3dns] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.drawns] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.chartns] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.formns] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.numberns] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.officens] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.presentationns] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.stylens] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.svgns] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.tablens] = true;
|
|
|
|
|
odfNodeNamespaceMap[odf.Namespaces.textns] = true;
|
2012-11-02 21:04:01 +01:00
|
|
|
};
|
|
|
|
|
|
2013-06-17 22:24:17 +10:00
|
|
|
/**
|
2013-07-23 11:32:20 +10:00
|
|
|
* Defines a set of rules for how elements can be collapsed based on whether they contain ODT content (e.g.,
|
|
|
|
|
* text or character elements).
|
|
|
|
|
* @param {!Node} rootNode Root text element of the odtDocument
|
|
|
|
|
* @constructor
|
2013-06-17 22:24:17 +10:00
|
|
|
*/
|
2013-07-23 11:32:20 +10:00
|
|
|
function CollapsingRules(rootNode) {
|
|
|
|
|
/**
|
2013-09-19 16:19:23 +10:00
|
|
|
* Returns true if the given node is an odf node
|
|
|
|
|
* @param {!Node} node
|
|
|
|
|
* @returns {!boolean}
|
|
|
|
|
*/
|
|
|
|
|
function isOdfNode(node) {
|
|
|
|
|
return odfNodeNamespaceMap.hasOwnProperty(node.namespaceURI);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-09 15:03:50 +11:00
|
|
|
/**
|
|
|
|
|
* Returns true if a given node is odf node or a text node that has a odf parent.
|
|
|
|
|
* @param {!Node} node
|
|
|
|
|
* @returns {!boolean}
|
|
|
|
|
*/
|
|
|
|
|
function shouldRemove(node) {
|
2013-10-16 18:38:17 +11:00
|
|
|
return isOdfNode(node)
|
|
|
|
|
|| (node.localName === "br" && odfUtils.isLineBreak(node.parentNode))
|
|
|
|
|
|| (node.nodeType === Node.TEXT_NODE && isOdfNode(/** @type {!Node}*/(node.parentNode)));
|
2013-10-09 15:03:50 +11:00
|
|
|
}
|
|
|
|
|
|
2013-09-19 16:19:23 +10:00
|
|
|
/**
|
|
|
|
|
* Returns true if the supplied node contains no text or ODF elements
|
2013-10-01 15:18:39 +02:00
|
|
|
* @param {!Node} node
|
|
|
|
|
* @returns {!boolean}
|
2013-07-23 11:32:20 +10:00
|
|
|
*/
|
|
|
|
|
function isEmpty(node) {
|
|
|
|
|
var childNode;
|
|
|
|
|
if (odfUtils.isCharacterElement(node)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (node.nodeType === Node.TEXT_NODE) {
|
|
|
|
|
return node.textContent.length === 0;
|
|
|
|
|
}
|
2013-06-17 22:24:17 +10:00
|
|
|
childNode = node.firstChild;
|
|
|
|
|
while (childNode) {
|
2013-09-19 16:19:23 +10:00
|
|
|
if (isOdfNode(childNode) || !isEmpty(childNode)) {
|
2013-06-17 22:24:17 +10:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
childNode = childNode.nextSibling;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-07-23 11:32:20 +10:00
|
|
|
this.isEmpty = isEmpty;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if the supplied node should be automatically collapsed (i.e., removed) if it contains no
|
|
|
|
|
* text or ODF character elements. The only element that should always be kept is a paragraph element.
|
|
|
|
|
* Paragraph elements can only be deleted through merging
|
2013-10-01 15:18:39 +02:00
|
|
|
* @param {!Node} node
|
|
|
|
|
* @returns {!boolean}
|
2013-07-23 11:32:20 +10:00
|
|
|
*/
|
|
|
|
|
function isCollapsibleContainer(node) {
|
|
|
|
|
return !odfUtils.isParagraph(node) && node !== rootNode && isEmpty(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Merge all child nodes into the node's parent and remove the node entirely
|
2013-10-01 15:18:39 +02:00
|
|
|
* @param {!Node} targetNode Node to merge into parent
|
2013-07-23 11:32:20 +10:00
|
|
|
* @return {!Node} Final parent node collapsing ended at
|
|
|
|
|
*/
|
2013-09-19 16:19:23 +10:00
|
|
|
function mergeChildrenIntoParent(targetNode) {
|
|
|
|
|
var parent;
|
|
|
|
|
if (targetNode.nodeType === Node.TEXT_NODE) {
|
|
|
|
|
parent = targetNode.parentNode;
|
|
|
|
|
parent.removeChild(targetNode);
|
|
|
|
|
} else {
|
|
|
|
|
// removes all odf nodes
|
2013-10-09 15:03:50 +11:00
|
|
|
parent = domUtils.removeUnwantedNodes(targetNode, shouldRemove);
|
2013-09-19 16:19:23 +10:00
|
|
|
}
|
2013-07-23 11:32:20 +10:00
|
|
|
if (isCollapsibleContainer(parent)) {
|
|
|
|
|
return mergeChildrenIntoParent(parent);
|
|
|
|
|
}
|
|
|
|
|
return parent;
|
|
|
|
|
}
|
|
|
|
|
this.mergeChildrenIntoParent = mergeChildrenIntoParent;
|
2013-06-17 22:24:17 +10:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 13:35:53 +00:00
|
|
|
/**
|
2013-07-23 11:32:20 +10:00
|
|
|
* Merges the 'second' paragraph into the 'first' paragraph.
|
|
|
|
|
* If the first paragraph is empty, it will be replaced by the second
|
|
|
|
|
* paragraph instead (all non-odt elements will be migrated however).
|
|
|
|
|
* @param {!Node} first Paragraph to merge content into
|
|
|
|
|
* @param {!Node} second Paragraph to merge content from
|
2013-10-01 15:18:39 +02:00
|
|
|
* @param {!CollapsingRules} collapseRules
|
2013-07-23 11:32:20 +10:00
|
|
|
* @return {!Node} Destination paragraph
|
2013-05-16 13:35:53 +00:00
|
|
|
*/
|
2013-07-23 11:32:20 +10:00
|
|
|
function mergeParagraphs(first, second, collapseRules) {
|
|
|
|
|
var child,
|
2013-10-01 15:20:09 +02:00
|
|
|
mergeForward = false,
|
2013-07-23 11:32:20 +10:00
|
|
|
destination = first,
|
|
|
|
|
source = second,
|
|
|
|
|
secondParent,
|
2013-09-30 18:47:37 +10:00
|
|
|
insertionPoint = null;
|
2013-05-16 16:51:53 +00:00
|
|
|
|
2013-07-23 11:32:20 +10:00
|
|
|
if (collapseRules.isEmpty(first)) {
|
|
|
|
|
mergeForward = true;
|
|
|
|
|
if (second.parentNode !== first.parentNode) {
|
|
|
|
|
// We're just about to move the second paragraph in to the right position for the merge.
|
|
|
|
|
// Therefore, we need to remember if the second paragraph is from a different parent in order to clean
|
|
|
|
|
// it up afterwards
|
|
|
|
|
secondParent = second.parentNode;
|
|
|
|
|
first.parentNode.insertBefore(second, first.nextSibling);
|
2013-05-16 13:35:53 +00:00
|
|
|
}
|
2013-07-23 11:32:20 +10:00
|
|
|
source = first;
|
|
|
|
|
destination = second;
|
|
|
|
|
insertionPoint = destination.getElementsByTagNameNS(editinfons, 'editinfo')[0] || destination.firstChild;
|
2013-05-16 13:35:53 +00:00
|
|
|
}
|
2013-05-16 16:51:53 +00:00
|
|
|
|
2013-07-23 11:32:20 +10:00
|
|
|
while (source.hasChildNodes()) {
|
|
|
|
|
child = mergeForward ? source.lastChild : source.firstChild;
|
|
|
|
|
source.removeChild(child);
|
|
|
|
|
if (child.localName !== 'editinfo') {
|
|
|
|
|
destination.insertBefore(child, insertionPoint);
|
|
|
|
|
}
|
2013-06-06 17:08:48 +10:00
|
|
|
}
|
2013-07-23 11:32:20 +10:00
|
|
|
|
|
|
|
|
if (secondParent && collapseRules.isEmpty(secondParent)) {
|
|
|
|
|
// Make sure the second paragraph's original parent is checked to see if it can be cleaned up too
|
|
|
|
|
collapseRules.mergeChildrenIntoParent(secondParent);
|
|
|
|
|
}
|
|
|
|
|
// All children have been migrated, now consume up the source parent chain
|
|
|
|
|
collapseRules.mergeChildrenIntoParent(source);
|
|
|
|
|
return destination;
|
2013-05-16 13:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-06 04:39:39 +02:00
|
|
|
this.execute = function (odtDocument) {
|
2013-07-23 11:32:20 +10:00
|
|
|
var paragraphElement,
|
|
|
|
|
destinationParagraph,
|
|
|
|
|
range,
|
|
|
|
|
textNodes,
|
|
|
|
|
paragraphs,
|
2013-10-10 17:30:23 +11:00
|
|
|
cursor = odtDocument.getCursor(memberid),
|
2013-07-23 11:32:20 +10:00
|
|
|
collapseRules = new CollapsingRules(odtDocument.getRootNode());
|
2013-04-06 02:43:32 +02:00
|
|
|
|
2013-07-23 11:32:20 +10:00
|
|
|
odtDocument.upgradeWhitespacesAtPosition(position);
|
|
|
|
|
odtDocument.upgradeWhitespacesAtPosition(position + length);
|
2013-05-24 10:47:24 +02:00
|
|
|
|
2013-11-12 22:03:30 +11:00
|
|
|
range = odtDocument.convertCursorToDomRange(position, length);
|
|
|
|
|
domUtils.splitBoundaries(range);
|
2013-07-23 11:32:20 +10:00
|
|
|
paragraphElement = odtDocument.getParagraphElement(range.startContainer);
|
2013-10-29 23:53:41 +11:00
|
|
|
textNodes = odfUtils.getTextElements(range, false, true);
|
2013-10-09 14:29:24 +11:00
|
|
|
paragraphs = odfUtils.getParagraphElements(range);
|
2013-07-23 11:32:20 +10:00
|
|
|
range.detach();
|
2013-04-22 18:07:07 +05:30
|
|
|
|
2013-07-23 11:32:20 +10:00
|
|
|
// Each character element is fully contained within the range, so will be completely removed
|
|
|
|
|
textNodes.forEach(function (element) {
|
|
|
|
|
collapseRules.mergeChildrenIntoParent(element);
|
|
|
|
|
});
|
2013-05-16 13:35:53 +00:00
|
|
|
|
2013-07-23 11:32:20 +10:00
|
|
|
destinationParagraph = paragraphs.reduce(function (destination, paragraph) {
|
|
|
|
|
return mergeParagraphs(destination, paragraph, collapseRules);
|
|
|
|
|
});
|
2013-05-16 13:35:53 +00:00
|
|
|
|
2013-11-07 12:07:38 +11:00
|
|
|
odtDocument.emit(ops.OdtDocument.signalStepsRemoved, {position: position, length: length});
|
2013-09-17 17:44:16 +10:00
|
|
|
odtDocument.downgradeWhitespacesAtPosition(position);
|
2013-07-24 18:10:51 +10:00
|
|
|
odtDocument.fixCursorPositions();
|
2013-05-16 13:35:53 +00:00
|
|
|
odtDocument.getOdfCanvas().refreshSize();
|
2013-09-25 23:31:52 +02:00
|
|
|
// TODO: signal also the deleted paragraphs, so e.g. SessionView can clean up the EditInfo
|
2013-05-16 13:35:53 +00:00
|
|
|
odtDocument.emit(ops.OdtDocument.signalParagraphChanged, {
|
2013-07-23 11:32:20 +10:00
|
|
|
paragraphElement: destinationParagraph || paragraphElement,
|
2013-05-16 13:35:53 +00:00
|
|
|
memberId: memberid,
|
|
|
|
|
timeStamp: timestamp
|
|
|
|
|
});
|
2013-10-10 17:30:23 +11:00
|
|
|
|
|
|
|
|
if (cursor) {
|
|
|
|
|
cursor.resetSelectionType();
|
|
|
|
|
odtDocument.emit(ops.OdtDocument.signalCursorMoved, cursor);
|
|
|
|
|
}
|
2013-05-16 13:35:53 +00:00
|
|
|
|
2013-07-31 16:59:24 +02:00
|
|
|
odtDocument.getOdfCanvas().rerenderAnnotations();
|
2013-05-16 13:35:53 +00:00
|
|
|
return true;
|
2012-11-02 21:04:01 +01:00
|
|
|
};
|
|
|
|
|
|
2012-11-10 10:56:38 +01:00
|
|
|
this.spec = function () {
|
2012-11-02 21:04:01 +01:00
|
|
|
return {
|
|
|
|
|
optype: "RemoveText",
|
|
|
|
|
memberid: memberid,
|
2013-03-13 16:35:35 +05:30
|
|
|
timestamp: timestamp,
|
2012-11-02 21:04:01 +01:00
|
|
|
position: position,
|
2013-08-15 21:00:37 +02:00
|
|
|
length: length
|
2012-11-02 21:04:01 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|