1 | /*
|
---|
2 | * file: BlockQuoteAction.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.BlockQuoteAction");
|
---|
21 |
|
---|
22 | _registerAction("Blockquote", {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * An undoable blockquote action. Encapsulates a range with a block quote. If there is any
|
---|
26 | * block quotes within the range, or if the range is within a block quote, then the
|
---|
27 | * block quote will be removed instead.
|
---|
28 | *
|
---|
29 | * @author Brook Novak
|
---|
30 | *
|
---|
31 | * @param {Node} startNode (Optional) The starting dom node of the range to align.
|
---|
32 | * If not provided then the current selection will be used.
|
---|
33 | * If provieded must also provide endNode
|
---|
34 | *
|
---|
35 | * @param {Node} endNode (Optional) The ending dom node of the range to align. Can be the same as start node
|
---|
36 | * If not provided then the current selection will be used.
|
---|
37 | *
|
---|
38 | */
|
---|
39 | exec : function(startNode, endNode) {
|
---|
40 |
|
---|
41 | // Auto-set range if not provided.
|
---|
42 | if (!startNode) {
|
---|
43 |
|
---|
44 | if (!this.selBefore)
|
---|
45 | return; // Nothing to select
|
---|
46 |
|
---|
47 | if (this.selBefore.endNode) {
|
---|
48 | startNode = this.selBeforeOrdered.startNode;
|
---|
49 | endNode = this.selBeforeOrdered.endNode;
|
---|
50 | } else
|
---|
51 | startNode = endNode = this.selBefore.startNode;
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | debug.assert(endNode, "Supplied start node but not the end node");
|
---|
56 |
|
---|
57 |
|
---|
58 | // Is there a block quote in the given range?
|
---|
59 | var bq = null, ca = _getCommonAncestor(startNode, endNode, true);
|
---|
60 |
|
---|
61 | if (isBlockQuote(ca))
|
---|
62 | bq = ca;
|
---|
63 | else {
|
---|
64 |
|
---|
65 | _visitAllNodes(ca, startNode, true, function(domNode){
|
---|
66 |
|
---|
67 | if (isBlockQuote(domNode))
|
---|
68 | bq = domNode;
|
---|
69 |
|
---|
70 | return bq == null && domNode != endNode;
|
---|
71 |
|
---|
72 | });
|
---|
73 | }
|
---|
74 |
|
---|
75 | // Is the range inside a block quote?
|
---|
76 | if (!bq) {
|
---|
77 | bq = _findAncestor(ca, docBody, isBlockQuote, true)
|
---|
78 | ||
|
---|
79 | _findAncestor(startNode, ca, isBlockQuote, true); // Initial traversal will have missed these nodes
|
---|
80 |
|
---|
81 | // Check that looking outside range was ok (not venturing outside editable area)
|
---|
82 | if (!de.doc.isNodeEditable(bq))
|
---|
83 | bq = null;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (bq) {
|
---|
87 |
|
---|
88 | // Move block quote children outside of block quote
|
---|
89 | while(bq.firstChild) {
|
---|
90 | var migrant = bq.firstChild;
|
---|
91 | _execOp(_Operation.REMOVE_NODE, migrant);
|
---|
92 | _execOp(_Operation.INSERT_NODE, migrant, bq.parentNode, _indexInParent(bq));
|
---|
93 | }
|
---|
94 |
|
---|
95 | // Remove the block quote
|
---|
96 | _execOp(_Operation.REMOVE_NODE, bq);
|
---|
97 |
|
---|
98 | } else { // Encapsulate range with a block quote
|
---|
99 |
|
---|
100 | // Normalize containers in range and get list of all the containers
|
---|
101 | var containers = _getNormalizedContainerRange(startNode, endNode);
|
---|
102 |
|
---|
103 | var newbq = $createElement("blockquote");
|
---|
104 |
|
---|
105 | if (containers.length > 0 && _isValidRelationship(newbq , containers[0].parentNode)) {
|
---|
106 |
|
---|
107 | // Add the new block quote to the document
|
---|
108 | _execOp(_Operation.INSERT_NODE, newbq, containers[0].parentNode, _indexInParent(containers[0]));
|
---|
109 |
|
---|
110 | // Migrate containers into block quote
|
---|
111 | for (var i in containers) {
|
---|
112 | var con = containers[i];
|
---|
113 | _execOp(_Operation.REMOVE_NODE, con);
|
---|
114 | _execOp(_Operation.INSERT_NODE, con, newbq);
|
---|
115 | }
|
---|
116 |
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | this.selAfter = this.selBefore;
|
---|
121 |
|
---|
122 | function isBlockQuote(domNode) {
|
---|
123 | return _nodeName(domNode) == "blockquote";
|
---|
124 | }
|
---|
125 | }
|
---|
126 | });
|
---|
127 |
|
---|