1 | /*
|
---|
2 | * file: FormatEnvironment.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 | bootstrap.provides("FormatEnvironment");
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Stores all format environment variables. These are used by the undoable "Format" action.
|
---|
24 | */
|
---|
25 | var _formatEnvironment = {};
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Sets a specific format variable in the format environment. Can use to create or override-existing
|
---|
29 | * formatting profiles which are used by the "Format" action.
|
---|
30 | *
|
---|
31 | * @param {String} name The name of the format type. For example "bold" (case insensitive)
|
---|
32 | * @param {Function} evalFunc The evaluation function. TODO: DOCUMENT
|
---|
33 | * @param {Function} wrapperFunc The wrapper function. Must return an inline element. TODO DOCUMENT
|
---|
34 | */
|
---|
35 | function _setFormatEnvVar(name, evalFunc, wrapperFunc) {
|
---|
36 | name = name.toLowerCase();
|
---|
37 | _formatEnvironment[name + "Eval"] = evalFunc;
|
---|
38 | _formatEnvironment[name + "Wrapper"] = wrapperFunc;
|
---|
39 | };
|
---|
40 |
|
---|
41 | // Create the default set of formatting variables
|
---|
42 | (function() {
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Creates a span with a CSS style
|
---|
46 | * @param {String} css The CSS style name
|
---|
47 | * @param {String} val The style value to set
|
---|
48 | * @return {Node} The created wrapper
|
---|
49 | */
|
---|
50 | function createCSSSpan(css,val) {
|
---|
51 | var wrapper = $createElement("span");
|
---|
52 | _setStyle(wrapper, css,val);
|
---|
53 | return wrapper;
|
---|
54 | }
|
---|
55 |
|
---|
56 | (function(envVars) {
|
---|
57 | for (var i in envVars) {
|
---|
58 | _setFormatEnvVar(i, envVars[i][0], envVars[i][1]);
|
---|
59 | }
|
---|
60 |
|
---|
61 | })({
|
---|
62 |
|
---|
63 | bold: [
|
---|
64 | /* Evaluation function */
|
---|
65 | function(ele) {
|
---|
66 |
|
---|
67 | var matches = [];
|
---|
68 |
|
---|
69 | if (_nodeName(ele) == "b" || _nodeName(ele) == "strong")
|
---|
70 | matches.push({type: 1});
|
---|
71 |
|
---|
72 | var fw = ele.style.fontWeight;
|
---|
73 | if (fw) {
|
---|
74 | var isBold = fw == "bold";
|
---|
75 | if (!isBold) {
|
---|
76 | fw = parseInt(fw);
|
---|
77 | isBold = (!isNaN(fw) && fw >= 700)
|
---|
78 | }
|
---|
79 | if (isBold)
|
---|
80 | matches.push({type: 3, match:"fontWeight"});
|
---|
81 | }
|
---|
82 |
|
---|
83 | return matches.length > 0 ? {
|
---|
84 | strip: matches,
|
---|
85 | inline: $createElement("strong"), // Extracted inline equivalent
|
---|
86 | value:true
|
---|
87 | } : null;
|
---|
88 |
|
---|
89 | },
|
---|
90 |
|
---|
91 | /* Wrapper */
|
---|
92 | function() {return $createElement("strong");}
|
---|
93 | ],
|
---|
94 |
|
---|
95 | italics: [
|
---|
96 | /* Evaluation function */
|
---|
97 | function(ele) {
|
---|
98 |
|
---|
99 | var matches = [];
|
---|
100 |
|
---|
101 | if (_nodeName(ele) == "i" || _nodeName(ele) == "em")
|
---|
102 | matches.push({type: 1});
|
---|
103 |
|
---|
104 | if (ele.style.fontStyle == "italic")
|
---|
105 | matches.push({type: 3, match:"fontStyle"});
|
---|
106 |
|
---|
107 | return matches.length > 0 ? {
|
---|
108 | strip: matches,
|
---|
109 | inline: $createElement("em"), // Extracted inline equivalent
|
---|
110 | value:true
|
---|
111 | } : null;
|
---|
112 | },
|
---|
113 |
|
---|
114 | /* Wrapper */
|
---|
115 | function() {return $createElement("em");}
|
---|
116 |
|
---|
117 | ],
|
---|
118 |
|
---|
119 | underline: [
|
---|
120 | /* Evaluation function */
|
---|
121 | function(ele) {
|
---|
122 |
|
---|
123 | var matches = [];
|
---|
124 |
|
---|
125 | if (_nodeName(ele) == "u")
|
---|
126 | matches.push({type: 1});
|
---|
127 |
|
---|
128 | if (ele.style.textDecoration == "underline")
|
---|
129 | matches.push({type: 3, match:"textDecoration"});
|
---|
130 |
|
---|
131 | return matches.length > 0 ? {
|
---|
132 | strip: matches,
|
---|
133 | inline: createCSSSpan("textDecoration", "underline"), // Extracted inline equivalent
|
---|
134 | value:true
|
---|
135 | } : null;
|
---|
136 | },
|
---|
137 |
|
---|
138 | /* Wrapper */
|
---|
139 | function() {
|
---|
140 | return createCSSSpan("textDecoration", "underline");
|
---|
141 | }
|
---|
142 |
|
---|
143 | ],
|
---|
144 |
|
---|
145 | strike: [
|
---|
146 | /* Evaluation function */
|
---|
147 | function(ele) {
|
---|
148 |
|
---|
149 | var matches = [];
|
---|
150 |
|
---|
151 | if (_nodeName(ele) == "strike")
|
---|
152 | matches.push({type: 1});
|
---|
153 |
|
---|
154 | if (ele.style.textDecoration == "line-through")
|
---|
155 | matches.push({type: 3, match:"textDecoration"});
|
---|
156 |
|
---|
157 | return matches.length > 0 ? {
|
---|
158 | strip: matches,
|
---|
159 | inline: createCSSSpan("textDecoration", "line-through"), // Extracted inline equivalent
|
---|
160 | value:true
|
---|
161 | } : null;
|
---|
162 | },
|
---|
163 |
|
---|
164 | /* Wrapper */
|
---|
165 | function() {
|
---|
166 | return createCSSSpan("textDecoration", "line-through");
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | ],
|
---|
171 |
|
---|
172 | color: [
|
---|
173 |
|
---|
174 | /* Evaluation function */
|
---|
175 | function(ele) {
|
---|
176 |
|
---|
177 | if (ele.style.color && ele.style.color.length > 0)
|
---|
178 | return {
|
---|
179 | strip:[{type: 3, match:"color"}],
|
---|
180 | inline: createCSSSpan("color", ele.style.color),
|
---|
181 | value : ele.style.color
|
---|
182 | };
|
---|
183 |
|
---|
184 | },
|
---|
185 |
|
---|
186 | /* Wrapper */
|
---|
187 | function(value) {
|
---|
188 | return createCSSSpan("color", value);
|
---|
189 | }
|
---|
190 |
|
---|
191 | ],
|
---|
192 |
|
---|
193 | backcolor: [
|
---|
194 |
|
---|
195 | /* Evaluation function */
|
---|
196 | function(ele) {
|
---|
197 |
|
---|
198 | if (ele.style.backgroundColor && ele.style.backgroundColor.length > 0)
|
---|
199 | return {
|
---|
200 | strip:[{type: 3, match:"backgroundColor"}],
|
---|
201 | inline: createCSSSpan("backgroundColor", ele.style.backgroundColor),
|
---|
202 | value : ele.style.backgroundColor
|
---|
203 | };
|
---|
204 |
|
---|
205 | },
|
---|
206 |
|
---|
207 | /* Wrapper */
|
---|
208 | function(value) {
|
---|
209 | return createCSSSpan("backgroundColor", value);
|
---|
210 | }
|
---|
211 |
|
---|
212 | ],
|
---|
213 |
|
---|
214 | fontsize: [
|
---|
215 | /* Evaluation function */
|
---|
216 | function(ele) {
|
---|
217 |
|
---|
218 | if (_nodeName(ele) == "small" || _nodeName(ele) == "big") {
|
---|
219 | var val = _nodeName(ele) == "small" ? "smaller" : "larger";
|
---|
220 | return {
|
---|
221 | strip:[{
|
---|
222 | type: 1
|
---|
223 | }],
|
---|
224 | inline: createCSSSpan("fontSize", val),
|
---|
225 | value : val
|
---|
226 | };
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (ele.style.fontSize && ele.style.fontSize.length > 0)
|
---|
230 | return {
|
---|
231 | strip:[{type: 3, match:"fontSize"}],
|
---|
232 | inline: createCSSSpan("fontSize", ele.style.fontSize),
|
---|
233 | value: ele.style.fontSize
|
---|
234 | };
|
---|
235 | },
|
---|
236 |
|
---|
237 | /* Wrapper */
|
---|
238 | function(value) {
|
---|
239 | return createCSSSpan("fontSize", value);
|
---|
240 | }
|
---|
241 | ],
|
---|
242 |
|
---|
243 | fontfamily: [
|
---|
244 | /* Evaluation function */
|
---|
245 | function(ele) {
|
---|
246 | if (ele.style.fontFamily && ele.style.fontFamily.length > 0)
|
---|
247 | return {
|
---|
248 | strip:[{type: 3, match:"fontFamily"}],
|
---|
249 | inline: createCSSSpan("fontFamily", ele.style.fontFamily),
|
---|
250 | value: ele.style.fontFamily
|
---|
251 | };
|
---|
252 | },
|
---|
253 |
|
---|
254 | /* Wrapper */
|
---|
255 | function(value) {
|
---|
256 | return createCSSSpan("fontFamily", value);
|
---|
257 | }
|
---|
258 | ],
|
---|
259 |
|
---|
260 | link: [
|
---|
261 |
|
---|
262 | /* Evaluation function */
|
---|
263 | function(ele) {
|
---|
264 | if (_nodeName(ele) == "a")
|
---|
265 | return {
|
---|
266 | strip:[{type: 1}],
|
---|
267 | inline: ele.cloneNode(false),
|
---|
268 | value: {
|
---|
269 | url: ele.href,
|
---|
270 | title: ele.title
|
---|
271 | }
|
---|
272 | };
|
---|
273 | },
|
---|
274 |
|
---|
275 | /* Wrapper */
|
---|
276 | function(value) {
|
---|
277 | var wrapper = $createElement("a");
|
---|
278 | wrapper.href = value.url;
|
---|
279 | wrapper.title = value.title;
|
---|
280 | return wrapper;
|
---|
281 | }
|
---|
282 | ]
|
---|
283 | });
|
---|
284 |
|
---|
285 | })();
|
---|
286 |
|
---|