1 | /*
|
---|
2 | * file: TextAlignAction.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.TextAlignAction");
|
---|
22 |
|
---|
23 | (function() {
|
---|
24 |
|
---|
25 | var excludeAlignBlocks = $createLookupMap("dt,dd,caption,colgroup,col,thead,tfoot,tbody,legend,optgroup,option,area,frame");
|
---|
26 |
|
---|
27 | _registerAction("TextAlign", {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * An undoable alignment action. Sets the text alignment for all block levels in given range.
|
---|
31 | * Creates new containers on the fly if needed.
|
---|
32 | *
|
---|
33 | * @author Brook Novak
|
---|
34 | *
|
---|
35 | * @param {String} alignment The CSS text-alignment value. Either left, right, center or justify.
|
---|
36 | *
|
---|
37 | * @param {Node} startNode (Optional) The starting dom node of the range to align.
|
---|
38 | * If not provided then the current selection will be used.
|
---|
39 | * If provieded must also provide endNode
|
---|
40 | *
|
---|
41 | * @param {Node} endNode (Optional) The ending dom node of the range to align. Can be the same as start node
|
---|
42 | * If not provided then the current selection will be used.
|
---|
43 | *
|
---|
44 | */
|
---|
45 | exec : function(alignment, startNode, endNode) {
|
---|
46 |
|
---|
47 | // Auto-set range if not provided.
|
---|
48 | if (!startNode) {
|
---|
49 |
|
---|
50 | if (!this.selBefore)
|
---|
51 | return; // Nothing to select
|
---|
52 |
|
---|
53 | if (this.selBefore.endNode) {
|
---|
54 | startNode = this.selBeforeOrdered.startNode;
|
---|
55 | endNode = this.selBeforeOrdered.endNode;
|
---|
56 | } else
|
---|
57 | startNode = endNode = this.selBefore.startNode;
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | debug.assert(endNode, "Supplied start node but not the end node");
|
---|
62 |
|
---|
63 | var containers;
|
---|
64 |
|
---|
65 | // First check for special case: If the ranges first block level common ancestor
|
---|
66 | // is a list item, then set alignment for the list item rather than normalizing within a list item.
|
---|
67 | var ca = _getCommonAncestor(startNode, endNode);
|
---|
68 | for (var level = 0; level < 2; level++) {
|
---|
69 | while (ca != docBody && !_isBlockLevel(ca)) {
|
---|
70 | ca = ca.parentNode;
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (ca == docBody)
|
---|
74 | break;
|
---|
75 |
|
---|
76 | if (_nodeName(ca) == "li") {
|
---|
77 | containers = [ca];
|
---|
78 | break;
|
---|
79 | }
|
---|
80 | ca = ca.parentNode;
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (!containers)
|
---|
84 | // Normalize containers in range and get list of all the containers
|
---|
85 | containers = _getNormalizedContainerRange(startNode, endNode);
|
---|
86 |
|
---|
87 | for (var i in containers) {
|
---|
88 | // NOTES: CSS 2+ spec allows text-align style to be applied to all block level elements.
|
---|
89 | _visitAllNodes(containers[i], containers[i], true, function(domNode) {
|
---|
90 | if (_isBlockLevel(domNode) && !excludeAlignBlocks[_nodeName(domNode)]) {
|
---|
91 | _execOp(_Operation.SET_CSS_STYLE, domNode, "textAlign", alignment);
|
---|
92 | }
|
---|
93 | });
|
---|
94 | }
|
---|
95 |
|
---|
96 | this.selAfter = this.selBefore;
|
---|
97 | }
|
---|
98 | });
|
---|
99 |
|
---|
100 | })();
|
---|