[25160] | 1 | /*
|
---|
| 2 | * file: RemoveTextAction.js
|
---|
| 3 | *
|
---|
| 4 | * @BEGINLICENSE
|
---|
| 5 | * Copyright 2010 Brook Novak (email : [email protected])
|
---|
| 6 | * This program is free software; you can redistribute it and/or modify
|
---|
| 7 | * it under the terms of the GNU General Public License as published by
|
---|
| 8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 9 | * (at your option) any later version.
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | * You should have received a copy of the GNU General Public License
|
---|
| 15 | * along with this program; if not, write to the Free Software
|
---|
| 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 17 | * @ENDLICENSE
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | // @DEPENDS: UndoMan
|
---|
| 21 | bootstrap.provides("actions.RemoveTextAction");
|
---|
| 22 |
|
---|
| 23 | _registerAction("RemoveText", {
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Removes text within a single text node.
|
---|
| 27 | *
|
---|
| 28 | * @param {Node} textNode The text node to remove text from
|
---|
| 29 | *
|
---|
| 30 | * @param {Number} index The index at which to begin removing text from.
|
---|
| 31 | * Ranges from 0 - textlength - 1
|
---|
| 32 | *
|
---|
| 33 | * @param {Number} length The amount of charactors to remove start from the given index. Must be at least 1
|
---|
| 34 | */
|
---|
| 35 | exec : function(textNode, index, length) {
|
---|
| 36 |
|
---|
| 37 | debug.assert(index >= 0);
|
---|
| 38 | debug.assert(length > 0);
|
---|
| 39 | debug.assert(textNode.nodeType == Node.TEXT_NODE);
|
---|
| 40 | debug.assert((index + length) <= textNode.nodeValue.length);
|
---|
| 41 |
|
---|
| 42 | // Avoid removing text from placeholders
|
---|
| 43 | if (de.doc.isESPlaceHolder(textNode, false) || de.doc.isMNPlaceHolder(textNode, false)) {
|
---|
| 44 | // Keep current cursor position
|
---|
| 45 | if (this.flags & de.UndoMan.ExecFlag.UPDATE_SELECTION) {
|
---|
| 46 | var cDesc = de.cursor.getCurrentCursorDesc();
|
---|
| 47 | if (cDesc)
|
---|
| 48 | this.selAfter = {startNode : cDesc.domNode, startIndex : cDesc.relIndex + (cDesc.domNode.nodeType == Node.TEXT_NODE && cDesc.isRightOf ? 1 : 0)};
|
---|
| 49 | }
|
---|
| 50 | return;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | // Check for surrounding whitespace
|
---|
| 55 | var convertLeftCA, convertRightCA;
|
---|
| 56 | if (index == 0) {
|
---|
| 57 |
|
---|
| 58 | // Look for preceeding whitespace
|
---|
| 59 | _visitAllNodes(docBody, textNode, false, function(domNode) {
|
---|
| 60 |
|
---|
| 61 | if (domNode == textNode) return;
|
---|
| 62 |
|
---|
| 63 | var ca = _getCommonAncestor(textNode, domNode);
|
---|
| 64 |
|
---|
| 65 | if (_findAncestor(domNode, ca, _isBlockLevel, true))
|
---|
| 66 | return false;
|
---|
| 67 |
|
---|
| 68 | if (domNode.nodeType == Node.TEXT_NODE && _nodeLength(domNode) > 0) {
|
---|
| 69 | if (_isAllWhiteSpace(domNode.nodeValue.charAt(_nodeLength(domNode)-1))) {
|
---|
| 70 | convertLeftCA = ca;
|
---|
| 71 | return false;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | });
|
---|
| 75 |
|
---|
| 76 | } else {
|
---|
| 77 | if(_isAllWhiteSpace(textNode.nodeValue.charAt(index-1)))
|
---|
| 78 | convertLeftCA = textNode;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | if ((index + length) == _nodeLength(textNode)) {
|
---|
| 82 |
|
---|
| 83 | // Look for proceeding whitespace
|
---|
| 84 | _visitAllNodes(docBody, textNode, true, function(domNode) {
|
---|
| 85 |
|
---|
| 86 | if (domNode == textNode) return;
|
---|
| 87 |
|
---|
| 88 | var ca = _getCommonAncestor(textNode, domNode);
|
---|
| 89 |
|
---|
| 90 | if (_isBlockLevel(domNode) || _findAncestor(textNode, ca, _isBlockLevel, true))
|
---|
| 91 | return false;
|
---|
| 92 |
|
---|
| 93 | if (_nodeLength(domNode, 0) > 0) {
|
---|
| 94 | if (_isAllWhiteSpace(domNode.nodeValue.charAt(0))) {
|
---|
| 95 | convertRightCA = ca;
|
---|
| 96 | return false;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | });
|
---|
| 100 | } else {
|
---|
| 101 | if (_isAllWhiteSpace(textNode.nodeValue.charAt(index + length)))
|
---|
| 102 | convertRightCA = textNode;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | var convertTarget = (convertLeftCA && convertRightCA && convertLeftCA != convertRightCA) ?
|
---|
| 106 | _getCommonAncestor(convertLeftCA, convertRightCA, true) : convertLeftCA || convertRightCA;
|
---|
| 107 |
|
---|
| 108 | if (convertTarget)
|
---|
| 109 | _convertWSToNBSP(convertTarget);
|
---|
| 110 |
|
---|
| 111 | // Remove the text
|
---|
| 112 | _execOp(_Operation.REMOVE_TEXT, textNode, index, length);
|
---|
| 113 |
|
---|
| 114 | // Normalize converted white space
|
---|
| 115 | if (convertTarget)
|
---|
| 116 | _normalizeNBSP(convertTarget);
|
---|
| 117 |
|
---|
| 118 | // See if need to add a placeholder
|
---|
| 119 | var ph, phParent = de.doc.getEditSectionContainer(textNode);
|
---|
| 120 |
|
---|
| 121 | if (phParent && _doesNeedESPlaceholder(phParent))
|
---|
| 122 | ph = de.doc.createESPlaceholder(phParent);
|
---|
| 123 | else {
|
---|
| 124 | phParent = _findAncestor(textNode, docBody, _isBlockLevel, true) || docBody;
|
---|
| 125 | if (_doesNeedMNPlaceholder(phParent))
|
---|
| 126 | ph = de.doc.createMNPlaceholder();
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | // Add the needed placeholder
|
---|
| 130 | if (ph)
|
---|
| 131 | _execOp(_Operation.INSERT_NODE, ph, phParent);
|
---|
| 132 |
|
---|
| 133 | // Update the selection if requested
|
---|
| 134 | if (this.flags & de.UndoMan.ExecFlag.UPDATE_SELECTION) {
|
---|
| 135 | var cDesc = ph ? de.cursor.createCursorDesc(ph, 0, false) :
|
---|
| 136 | de.cursor.getNearestCursorDesc(textNode, index == 0 ? 0 : index-1, index > 0, false);
|
---|
| 137 | if (cDesc)
|
---|
| 138 | this.selAfter = {startNode : cDesc.domNode, startIndex : cDesc.relIndex + (cDesc.domNode.nodeType == Node.TEXT_NODE && cDesc.isRightOf ? 1 : 0)};
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | // If the text node is left without text then get rid of it
|
---|
| 142 | if (_nodeLength(textNode) == 0)
|
---|
| 143 | _execOp(_Operation.REMOVE_NODE, textNode);
|
---|
| 144 |
|
---|
| 145 | }
|
---|
| 146 | });
|
---|
| 147 |
|
---|
| 148 |
|
---|