1 | /*
|
---|
2 | * file: RemoveDOMAction.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 | // @DEPENDS: UndoMan
|
---|
20 | bootstrap.provides("actions.RemoveDOMAction");
|
---|
21 |
|
---|
22 | _registerAction("RemoveDOM",{
|
---|
23 |
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @class
|
---|
27 | * An undoable dom action. Removes a given range from the document.
|
---|
28 | *
|
---|
29 | * @author Brook Novak
|
---|
30 | *
|
---|
31 | * @param {Node} startNode The starting dom node of the fragments range.
|
---|
32 | *
|
---|
33 | * @param {Number} startIndex The inclusive start index in the start node.
|
---|
34 | * Ranges from 0 to the text length for text nodes.
|
---|
35 | * Where 0 indicates that the range begins at the first char, and text length
|
---|
36 | * indicates that the range begins directly after the text node, but not including it.
|
---|
37 | * <br>
|
---|
38 | * Ranges from 0 to 1 for elements.
|
---|
39 | * Where 0 indicates that the range includes the element and it's decendants,
|
---|
40 | * and 1 indicates that the range excludes the element and it's decendants.
|
---|
41 | *
|
---|
42 | *
|
---|
43 | * @param {Node} endNode The ending dom node of the fragments range.
|
---|
44 | *
|
---|
45 | * @param {Number} endIndex The inclusive end index in the end node.
|
---|
46 | * Ranges from 0 to the text length for text nodes.
|
---|
47 | * Where 0 indicates that the range ends just before the text node, but not including it,
|
---|
48 | * and text length indicates that the range ends at the last charactor in the text run.
|
---|
49 | * <br>
|
---|
50 | * Ranges from 0 to 1 for elements.
|
---|
51 | * Where 0 indicates that the range excludes the element and it's decendants,
|
---|
52 | * and 1 indicates that the range includes the element and it's decendants.
|
---|
53 | *
|
---|
54 | */
|
---|
55 | exec : function(startNode, startIndex, endNode, endIndex) {
|
---|
56 |
|
---|
57 | var fragmentRoot = _buildFragment(_getCommonAncestor(startNode, endNode, false),
|
---|
58 | startNode, startIndex, endNode, endIndex);
|
---|
59 |
|
---|
60 | // Convert all whitespaces to non-breaking spaces
|
---|
61 | _convertWSToNBSP(fragmentRoot.node);
|
---|
62 |
|
---|
63 | // Collapse the range
|
---|
64 | var fMigrantNode = fragmentRoot.collapse();
|
---|
65 |
|
---|
66 | // Normalize any NBSP entities
|
---|
67 | _normalizeNBSP(fragmentRoot.node);
|
---|
68 |
|
---|
69 | if (this.flags & de.UndoMan.ExecFlag.UPDATE_SELECTION) {
|
---|
70 |
|
---|
71 | var newCursorPos;
|
---|
72 |
|
---|
73 | // If there was a migrant, get the cursor descriptor from this
|
---|
74 | if (fMigrantNode)
|
---|
75 | newCursorPos = de.cursor.getNearestCursorDesc(fMigrantNode, 0, false, true);
|
---|
76 | else {
|
---|
77 | // Get the deepest node on the starting bounds that still resides in the document
|
---|
78 | var fragment = fragmentRoot.getStartFragment();
|
---|
79 | while (fragment.node != docBody && !_isAncestor(docBody, fragment.node)) {
|
---|
80 | fragment = fragment.parent;
|
---|
81 | }
|
---|
82 | var domNode = fragment.node;
|
---|
83 |
|
---|
84 | // Check to see if there are any nodes in the start-bounds place
|
---|
85 | if (fragment.children.length > 0 && domNode.childNodes.length > 0 &&
|
---|
86 | fragment.children[0].pos <= domNode.childNodes.length) {
|
---|
87 |
|
---|
88 | if (fragment.children[0].pos == 0) {
|
---|
89 | newCursorPos = de.cursor.getNearestCursorDesc(domNode.firstChild, 0, false, false);
|
---|
90 | } else {
|
---|
91 | var cNode = domNode.childNodes[fragment.children[0].pos - 1];
|
---|
92 | newCursorPos = de.cursor.getNearestCursorDesc(cNode, _nodeLength(cNode, 2) - 1, true, false);
|
---|
93 | }
|
---|
94 | } else newCursorPos = de.cursor.getNearestCursorDesc(fragment.node, _nodeLength(fragment.node, 2) - 1, true, true);
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (newCursorPos)
|
---|
98 | this.selAfter = {
|
---|
99 | startNode : newCursorPos.domNode,
|
---|
100 | startIndex : newCursorPos.relIndex + (newCursorPos.domNode.nodeType == Node.TEXT_NODE && newCursorPos.isRightOf ? 1 : 0)
|
---|
101 | };
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | });
|
---|
106 |
|
---|
107 |
|
---|
108 | _registerAction("RemoveNode",{
|
---|
109 | /**
|
---|
110 | * A simple undoable action. Removes a dom node from the document.
|
---|
111 | * Selection will always be cleared afterwards.
|
---|
112 | *
|
---|
113 | * @param {Node} node The node to remove
|
---|
114 | *
|
---|
115 | */
|
---|
116 | exec: function(node) {
|
---|
117 | _execOp(_Operation.REMOVE_NODE, node);
|
---|
118 | }
|
---|
119 | });
|
---|