2012-11-01 15:07:18 +01:00
|
|
|
/**
|
|
|
|
|
* @license
|
2013-04-06 05:21:18 +02:00
|
|
|
* Copyright (C) 2012-2013 KO GmbH <copyright@kogmbh.com>
|
2012-11-01 15:07:18 +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-01 15:07:18 +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-01 15:07:18 +01:00
|
|
|
*/
|
2013-04-06 05:21:18 +02:00
|
|
|
|
2012-11-01 15:07:18 +01:00
|
|
|
/*global ops*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @constructor
|
2012-11-02 22:24:03 +01:00
|
|
|
* @implements ops.Operation
|
2012-11-01 15:07:18 +01:00
|
|
|
*/
|
2013-04-06 04:39:39 +02:00
|
|
|
ops.OpInsertText = function OpInsertText() {
|
2012-11-01 15:07:18 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
2013-09-17 15:35:54 +10:00
|
|
|
var space = " ",
|
|
|
|
|
tab = "\t",
|
|
|
|
|
memberid, timestamp, position, text;
|
2012-11-01 15:07:18 +01:00
|
|
|
|
2012-11-10 10:56:38 +01:00
|
|
|
this.init = function (data) {
|
2012-11-01 15:07:18 +01:00
|
|
|
memberid = data.memberid;
|
2013-03-13 16:35:35 +05:30
|
|
|
timestamp = data.timestamp;
|
2012-11-01 15:07:18 +01:00
|
|
|
position = data.position;
|
|
|
|
|
text = data.text;
|
|
|
|
|
};
|
|
|
|
|
|
2013-07-24 17:56:39 +02:00
|
|
|
/**
|
|
|
|
|
* This is a workaround for a bug where webkit forgets to relayout
|
2013-04-06 02:43:32 +02:00
|
|
|
* the text when a new character is inserted at the beginning of a line in
|
|
|
|
|
* a Text Node.
|
2013-07-24 17:56:39 +02:00
|
|
|
* @param {!Node} textNode
|
|
|
|
|
* @return {undefined}
|
2013-04-06 02:43:32 +02:00
|
|
|
*/
|
2013-09-17 15:04:22 +10:00
|
|
|
function triggerLayoutInWebkit(textNode) {
|
2013-04-06 02:43:32 +02:00
|
|
|
var parent = textNode.parentNode,
|
2013-09-17 15:04:22 +10:00
|
|
|
next = textNode.nextSibling;
|
2013-05-28 15:33:00 +10:00
|
|
|
|
2013-04-06 02:43:32 +02:00
|
|
|
parent.removeChild(textNode);
|
|
|
|
|
parent.insertBefore(textNode, next);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 15:35:54 +10:00
|
|
|
/**
|
|
|
|
|
* Returns true if the particular character in the text string is a space character that is immediately
|
2013-11-04 14:02:16 +11:00
|
|
|
* preceded by another space character (or is the first or last space in the text block).
|
2013-09-17 15:35:54 +10:00
|
|
|
* Logic is based on http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#element-text_s
|
|
|
|
|
* @param {!string} text
|
|
|
|
|
* @param {!number} index
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
|
|
|
|
function requiresSpaceElement(text, index) {
|
2013-11-04 14:02:16 +11:00
|
|
|
return text[index] === space && (index === 0 || index === text.length - 1 || text[index - 1] === space);
|
2013-09-17 15:35:54 +10:00
|
|
|
}
|
|
|
|
|
|
2013-04-06 04:39:39 +02:00
|
|
|
this.execute = function (odtDocument) {
|
2013-05-03 10:27:06 +00:00
|
|
|
var domPosition,
|
2013-05-06 11:54:50 +00:00
|
|
|
previousNode,
|
2013-09-18 17:41:41 +02:00
|
|
|
parentElement,
|
2013-09-30 18:47:37 +10:00
|
|
|
nextNode = null,
|
2013-08-13 00:22:22 +02:00
|
|
|
ownerDocument = odtDocument.getDOM(),
|
2013-05-03 10:27:06 +00:00
|
|
|
paragraphElement,
|
|
|
|
|
textns = "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
|
2013-09-18 17:41:41 +02:00
|
|
|
toInsertIndex = 0,
|
2013-07-24 15:55:13 +10:00
|
|
|
spaceTag,
|
2013-09-18 17:41:41 +02:00
|
|
|
spaceElement,
|
2013-05-03 10:27:06 +00:00
|
|
|
i;
|
2013-04-06 02:43:32 +02:00
|
|
|
|
2013-09-18 17:41:41 +02:00
|
|
|
function insertTextNode(toInsertText) {
|
|
|
|
|
parentElement.insertBefore(ownerDocument.createTextNode(toInsertText), nextNode);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 17:44:16 +10:00
|
|
|
odtDocument.upgradeWhitespacesAtPosition(position);
|
2013-11-13 16:52:58 +11:00
|
|
|
domPosition = odtDocument.getTextNodeAtStep(position, memberid);
|
2013-09-18 17:41:41 +02:00
|
|
|
|
2013-04-06 02:43:32 +02:00
|
|
|
if (domPosition) {
|
2013-05-06 11:54:50 +00:00
|
|
|
previousNode = domPosition.textNode;
|
2013-09-18 17:41:41 +02:00
|
|
|
nextNode = previousNode.nextSibling;
|
|
|
|
|
parentElement = previousNode.parentNode;
|
2013-05-06 11:54:50 +00:00
|
|
|
paragraphElement = odtDocument.getParagraphElement(previousNode);
|
2013-05-03 10:27:06 +00:00
|
|
|
|
2013-09-18 17:41:41 +02:00
|
|
|
// first do the insertion with any contained tabs or spaces
|
2013-07-24 17:56:39 +02:00
|
|
|
for (i = 0; i < text.length; i += 1) {
|
2013-09-17 15:35:54 +10:00
|
|
|
if (requiresSpaceElement(text, i) || text[i] === tab) {
|
2013-09-18 17:41:41 +02:00
|
|
|
// no nodes inserted yet?
|
|
|
|
|
if (toInsertIndex === 0) {
|
|
|
|
|
// if inserting in the middle the given text node needs to be split up
|
|
|
|
|
// if previousNode becomes empty, it will be cleaned up on finishing
|
|
|
|
|
if (domPosition.offset !== previousNode.length) {
|
|
|
|
|
nextNode = previousNode.splitText(domPosition.offset);
|
|
|
|
|
}
|
|
|
|
|
// normal text to insert before this space?
|
|
|
|
|
if (0 < i) {
|
|
|
|
|
previousNode.appendData(text.substring(0, i));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// normal text to insert before this space?
|
|
|
|
|
if (toInsertIndex < i) {
|
|
|
|
|
insertTextNode(text.substring(toInsertIndex, i));
|
2013-07-24 15:55:13 +10:00
|
|
|
}
|
|
|
|
|
}
|
2013-09-18 17:41:41 +02:00
|
|
|
toInsertIndex = i + 1;
|
2013-07-24 15:55:13 +10:00
|
|
|
|
2013-09-18 17:41:41 +02:00
|
|
|
// insert space element
|
2013-07-24 15:55:13 +10:00
|
|
|
spaceTag = text[i] === space ? "text:s" : "text:tab";
|
2013-09-18 17:41:41 +02:00
|
|
|
spaceElement = ownerDocument.createElementNS(textns, spaceTag);
|
|
|
|
|
spaceElement.appendChild(ownerDocument.createTextNode(text[i]));
|
|
|
|
|
parentElement.insertBefore(spaceElement, nextNode);
|
2013-07-24 15:55:13 +10:00
|
|
|
}
|
2013-05-07 14:11:01 +00:00
|
|
|
}
|
2013-09-18 17:41:41 +02:00
|
|
|
|
|
|
|
|
// then insert rest
|
|
|
|
|
// text can be completely inserted, no spaces/tabs?
|
|
|
|
|
if (toInsertIndex === 0) {
|
|
|
|
|
previousNode.insertData(domPosition.offset, text);
|
|
|
|
|
} else if (toInsertIndex < text.length) {
|
|
|
|
|
insertTextNode(text.substring(toInsertIndex));
|
2013-05-03 10:27:06 +00:00
|
|
|
}
|
2013-04-06 02:43:32 +02:00
|
|
|
|
|
|
|
|
// FIXME A workaround.
|
2013-09-17 15:04:22 +10:00
|
|
|
triggerLayoutInWebkit(previousNode);
|
2013-04-06 02:43:32 +02:00
|
|
|
|
2013-09-18 17:41:41 +02:00
|
|
|
// Clean up the possibly created empty text node
|
2013-05-07 14:11:01 +00:00
|
|
|
if (previousNode.length === 0) {
|
2013-05-06 11:54:50 +00:00
|
|
|
previousNode.parentNode.removeChild(previousNode);
|
2013-05-03 13:49:18 +00:00
|
|
|
}
|
2013-09-17 17:44:16 +10:00
|
|
|
|
2013-11-07 12:07:38 +11:00
|
|
|
odtDocument.emit(ops.OdtDocument.signalStepsInserted, {position: position, length: text.length});
|
2013-09-17 17:44:16 +10:00
|
|
|
if (position > 0) {
|
|
|
|
|
// Necessary to match upgradeWhitespaces behaviour which searches the preceding positions as well
|
2013-09-19 23:51:16 +10:00
|
|
|
if (position > 1) {
|
|
|
|
|
odtDocument.downgradeWhitespacesAtPosition(position - 2);
|
|
|
|
|
}
|
2013-11-04 14:02:16 +11:00
|
|
|
odtDocument.downgradeWhitespacesAtPosition(position - 1);
|
2013-09-17 17:44:16 +10:00
|
|
|
}
|
2013-11-04 14:02:16 +11:00
|
|
|
// Try and downgrade first position in the added text
|
2013-09-17 17:44:16 +10:00
|
|
|
odtDocument.downgradeWhitespacesAtPosition(position);
|
2013-11-04 14:02:16 +11:00
|
|
|
// Try and downgrade last position in the added text
|
|
|
|
|
odtDocument.downgradeWhitespacesAtPosition(position + text.length - 1);
|
|
|
|
|
// Try and downgrade the next position just *after* the added text
|
2013-09-17 17:44:16 +10:00
|
|
|
odtDocument.downgradeWhitespacesAtPosition(position + text.length);
|
|
|
|
|
|
2013-04-23 18:01:06 +02:00
|
|
|
|
|
|
|
|
odtDocument.getOdfCanvas().refreshSize();
|
2013-04-06 04:21:18 +02:00
|
|
|
odtDocument.emit(ops.OdtDocument.signalParagraphChanged, {
|
2013-05-03 10:27:06 +00:00
|
|
|
paragraphElement: paragraphElement,
|
2013-04-06 02:43:32 +02:00
|
|
|
memberId: memberid,
|
|
|
|
|
timeStamp: timestamp
|
|
|
|
|
});
|
2013-07-31 14:31:22 +02:00
|
|
|
|
2013-07-31 16:59:24 +02:00
|
|
|
odtDocument.getOdfCanvas().rerenderAnnotations();
|
2013-04-06 05:08:22 +02:00
|
|
|
return true;
|
2013-04-06 02:43:32 +02:00
|
|
|
}
|
2013-04-06 05:08:22 +02:00
|
|
|
return false;
|
2012-11-01 15:07:18 +01:00
|
|
|
};
|
|
|
|
|
|
2012-11-10 10:56:38 +01:00
|
|
|
this.spec = function () {
|
2012-11-02 01:39:23 +01:00
|
|
|
return {
|
|
|
|
|
optype: "InsertText",
|
|
|
|
|
memberid: memberid,
|
2013-03-13 16:35:35 +05:30
|
|
|
timestamp: timestamp,
|
2012-11-02 01:39:23 +01:00
|
|
|
position: position,
|
|
|
|
|
text: text
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2012-11-01 15:07:18 +01:00
|
|
|
};
|