1 /**
  2  * Copyright (C) 2012-2013 KO GmbH <copyright@kogmbh.com>
  3  *
  4  * @licstart
  5  * This file is part of WebODF.
  6  *
  7  * WebODF is free software: you can redistribute it and/or modify it
  8  * under the terms of the GNU Affero General Public License (GNU AGPL)
  9  * as published by the Free Software Foundation, either version 3 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * WebODF is distributed in the hope that it will be useful, but
 13  * WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  * GNU Affero General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Affero General Public License
 18  * along with WebODF.  If not, see <http://www.gnu.org/licenses/>.
 19  * @licend
 20  *
 21  * @source: http://www.webodf.org/
 22  * @source: https://github.com/kogmbh/WebODF/
 23  */
 24 
 25 /*global ops, runtime, odf, core*/
 26 
 27 /**
 28  * @constructor
 29  * @implements ops.Operation
 30  */
 31 ops.OpRemoveText = function OpRemoveText() {
 32     "use strict";
 33 
 34     var memberid, timestamp,
 35         /**@type {number}*/
 36         position,
 37         /**@type {number}*/
 38         length,
 39         odfUtils = odf.OdfUtils,
 40         domUtils = core.DomUtils;
 41 
 42     /**
 43      * @param {!ops.OpRemoveText.InitSpec} data
 44      */
 45     this.init = function (data) {
 46         runtime.assert(data.length >= 0, "OpRemoveText only supports positive lengths");
 47         memberid = data.memberid;
 48         timestamp = data.timestamp;
 49         position = parseInt(data.position, 10);
 50         length = parseInt(data.length, 10);
 51     };
 52 
 53     this.isEdit = true;
 54     this.group = undefined;
 55 
 56     /**
 57      * @param {!ops.Document} document
 58      */
 59     this.execute = function (document) {
 60         var odtDocument = /**@type{ops.OdtDocument}*/(document),
 61             range,
 62             textNodes,
 63             paragraph,
 64             cursor = odtDocument.getCursor(memberid),
 65             collapseRules = new odf.CollapsingRules(odtDocument.getRootNode());
 66 
 67         odtDocument.upgradeWhitespacesAtPosition(position);
 68         odtDocument.upgradeWhitespacesAtPosition(position + length);
 69 
 70         range = odtDocument.convertCursorToDomRange(position, length);
 71         domUtils.splitBoundaries(range);
 72         textNodes = odfUtils.getTextElements(range, false, true);
 73         paragraph = /**@type{!Element}*/(odfUtils.getParagraphElement(range.startContainer, range.startOffset));
 74         runtime.assert(paragraph !== undefined, "Attempting to remove text outside a paragraph element");
 75         range.detach();
 76 
 77         // Each character element is fully contained within the range, so will be completely removed
 78         textNodes.forEach(function (element) {
 79             if (element.parentNode) {
 80                 // In order to guarantee OT-ability, this rule needs to be enforced, otherwise it's impossible to tell
 81                 // which paragraph boundaries might be impacted by the remove op.
 82                 runtime.assert(domUtils.containsNode(paragraph, element),
 83                     "RemoveText only supports removing elements within the same paragraph");
 84 
 85                 collapseRules.mergeChildrenIntoParent(element);
 86             } else {
 87                 // If this is an empty text node, it might have already been removed from it's container.
 88                 // Although WebODF specifically avoids empty text nodes at all times, incorrect 3rd party
 89                 // DOM manipulation (or undiscovered WebODF bugs) may leave these behind.
 90                 runtime.log("WARN: text element has already been removed from it's container");
 91             }
 92         });
 93 
 94         odtDocument.emit(ops.OdtDocument.signalStepsRemoved, {position: position});
 95         odtDocument.downgradeWhitespacesAtPosition(position);
 96         odtDocument.fixCursorPositions();
 97         odtDocument.getOdfCanvas().refreshSize();
 98         odtDocument.emit(ops.OdtDocument.signalParagraphChanged, {
 99             paragraphElement: paragraph,
100             memberId: memberid,
101             timeStamp: timestamp
102         });
103 
104         if (cursor) {
105             cursor.resetSelectionType();
106             odtDocument.emit(ops.Document.signalCursorMoved, cursor);
107         }
108 
109         odtDocument.getOdfCanvas().rerenderAnnotations();
110         return true;
111     };
112 
113     /**
114      * @return {!ops.OpRemoveText.Spec}
115      */
116     this.spec = function () {
117         return {
118             optype: "RemoveText",
119             memberid: memberid,
120             timestamp: timestamp,
121             position: position,
122             length: length
123         };
124     };
125 };
126 /**@typedef{{
127     optype:string,
128     memberid:string,
129     timestamp:number,
130     position:number,
131     length:number
132 }}*/
133 ops.OpRemoveText.Spec;
134 /**@typedef{{
135     memberid:string,
136     timestamp:(number|undefined),
137     position:number,
138     length:number
139 }}*/
140 ops.OpRemoveText.InitSpec;
141