Index: trunk/extensions/ParserPlayground/ParserPlayground.i18n.php |
— | — | @@ -13,4 +13,7 @@ |
14 | 14 | */ |
15 | 15 | $messages['en'] = array( |
16 | 16 | 'parserplayground-desc' => 'Parser and editor experiments', |
| 17 | + |
| 18 | + 'vis-edit-source-ok' => 'Ok', |
| 19 | + 'vis-edit-source-cancel' => 'Cancel', |
17 | 20 | ); |
Index: trunk/extensions/ParserPlayground/ParserPlayground.php |
— | — | @@ -74,6 +74,10 @@ |
75 | 75 | 'jquery.nodetree.css', |
76 | 76 | 'ext.parserPlayground.css', |
77 | 77 | ), |
| 78 | + 'messages' => array( |
| 79 | + 'vis-edit-source-ok', |
| 80 | + 'vis-edit-source-cancel', |
| 81 | + ), |
78 | 82 | 'dependencies' => array( |
79 | 83 | 'ext.wikiEditor' |
80 | 84 | ), |
Index: trunk/extensions/ParserPlayground/modules/ext.parserPlayground.js |
— | — | @@ -155,7 +155,7 @@ |
156 | 156 | |
157 | 157 | window.setTimeout(function() { |
158 | 158 | var context = editor.data('wikiEditor-context'); |
159 | | - context.parserPlayground = { |
| 159 | + var pp = context.parserPlayground = { |
160 | 160 | parser: new FakeParser(), |
161 | 161 | tree: undefined, |
162 | 162 | useInspector: false, |
— | — | @@ -176,28 +176,31 @@ |
177 | 177 | context.$parserInspector = $inspector; |
178 | 178 | |
179 | 179 | var src = $('#wpTextbox1').val(); |
180 | | - var $dest = $target.find('div'); |
181 | 180 | |
182 | | - var parser = context.parserPlayground.parser; |
183 | | - var treeMap = context.parserPlayground.treeMap = new HashMap(), |
184 | | - renderMap = new HashMap(); |
| 181 | + var parser = pp.parser; |
185 | 182 | parser.parseToTree(src, function(tree, err) { |
186 | | - context.parserPlayground.tree = tree; |
187 | | - if (context.parserPlayground.useInspector) { |
188 | | - $inspector.nodeTree( tree, function( node, el ) { |
189 | | - treeMap.put( node, el ); |
190 | | - }); |
191 | | - } |
192 | | - parser.treeToHtml(tree, function(node, err) { |
193 | | - $dest.append(node); |
194 | | - context.parserPlayground.fn.setupEditor($target); |
195 | | - setupInspector($target, $inspector, renderMap, treeMap); |
196 | | - }, renderMap); |
| 183 | + pp.tree = tree; |
| 184 | + pp.fn.displayTree(); |
197 | 185 | }); |
198 | 186 | |
199 | 187 | context.$textarea.closest('form').submit( context.parserPlayground.fn.onSubmit ); |
200 | 188 | |
201 | 189 | }, |
| 190 | + displayTree: function() { |
| 191 | + pp.treeMap = new HashMap(); |
| 192 | + pp.renderMap = new HashMap(); |
| 193 | + if (pp.useInspector) { |
| 194 | + context.$parserInspector.nodeTree( pp.tree, function( node, el ) { |
| 195 | + pp.treeMap.put( node, el ); |
| 196 | + }); |
| 197 | + } |
| 198 | + pp.parser.treeToHtml(pp.tree, function(node, err) { |
| 199 | + var $dest = context.$parserContainer.find('div'); |
| 200 | + $dest.empty().append(node); |
| 201 | + context.parserPlayground.fn.setupEditor(context.$parserContainer); |
| 202 | + setupInspector(context.$parserContainer, context.$parserInspector, pp.renderMap, pp.treeMap); |
| 203 | + }, pp.renderMap); |
| 204 | + }, |
202 | 205 | hide: function() { |
203 | 206 | $('#pegparser-source').hide(); // it'll reshow; others won't need it |
204 | 207 | context.$iframe = undefined; |
— | — | @@ -257,7 +260,12 @@ |
258 | 261 | // Ok, not 100% kosher right now but... :D |
259 | 262 | var parser = context.parserPlayground.parser; |
260 | 263 | parser.treeToSource(node, function(src, err) { |
261 | | - alert( src ); |
| 264 | + //alert( src ); |
| 265 | + pp.sel = { |
| 266 | + node: node, |
| 267 | + src: src |
| 268 | + }; |
| 269 | + context.$textarea.wikiEditor('openDialog', 'vis-edit-source'); |
262 | 270 | }); |
263 | 271 | event.preventDefault(); |
264 | 272 | return false; |
— | — | @@ -271,6 +279,81 @@ |
272 | 280 | } |
273 | 281 | } |
274 | 282 | } |
| 283 | + editor.wikiEditor( 'addDialog', { |
| 284 | + 'vis-edit-source': { |
| 285 | + title: 'Edit source fragment', |
| 286 | + id: 'vis-edit-source-dialog', |
| 287 | + html: '\ |
| 288 | + <fieldset>\ |
| 289 | + <div class="wikieditor-toolbar-field-wrapper">\ |
| 290 | + <textarea id="vis-edit-source-text"></textarea>\ |
| 291 | + </div>\ |
| 292 | + </fieldset>', |
| 293 | + init: function() { |
| 294 | + // |
| 295 | + }, |
| 296 | + dialog: { |
| 297 | + width: 500, |
| 298 | + dialogClass: 'wikiEditor-toolbar-dialog', |
| 299 | + buttons: { |
| 300 | + 'vis-edit-source-ok': function() { |
| 301 | + var origNode = pp.sel.node, |
| 302 | + $textarea = $('#vis-edit-source-text'), |
| 303 | + $dlg = $(this); |
| 304 | + |
| 305 | + pp.parser.parseToTree($textarea.val(), function(tree, err) { |
| 306 | + // Silly and freaky hack :D |
| 307 | + // Crap... no good way to replace or find parent here. Bad temp dom. ;) |
| 308 | + var replaceNode = function(searchFor, replaceWithNodes, haystack) { |
| 309 | + // Look in 'data' arrays for subnodes. |
| 310 | + if ('content' in haystack) { |
| 311 | + var content = haystack.content, len = content.length; |
| 312 | + for (var i = 0; i < len; i++) { |
| 313 | + if (content[i] === searchFor) { |
| 314 | + //Array.splice.apply(content, [i, 1].concat(replaceWithNodes)); |
| 315 | + var before = content.slice(0, i), |
| 316 | + after = content.slice(i + 1); |
| 317 | + content = before.concat(replaceWithNodes).concat(after); |
| 318 | + haystack.content = content; |
| 319 | + return true; |
| 320 | + } else { |
| 321 | + if (replaceNode(searchFor, replaceWithNodes, content[i])) { |
| 322 | + return true; |
| 323 | + } |
| 324 | + } |
| 325 | + } |
| 326 | + } |
| 327 | + return false; |
| 328 | + }; |
| 329 | + // @fixme avoid bad nesting |
| 330 | + var newNodes = tree.content; |
| 331 | + if (origNode.type != 'para' && newNodes.length == 1 && newNodes[0].type == 'para') { |
| 332 | + // To avoid funky nesting; find the good stuff! |
| 333 | + // Ideally, we would pass proper parse context in so wouldn't need to do this. |
| 334 | + newNodes = newNodes[0].content; |
| 335 | + } |
| 336 | + if (replaceNode(origNode, newNodes, pp.tree)) { |
| 337 | + pp.sel = null; |
| 338 | + $textarea.empty(); |
| 339 | + $dlg.dialog( 'close' ); |
| 340 | + |
| 341 | + pp.fn.displayTree(); // todo: nicer update :D |
| 342 | + } else { |
| 343 | + alert('Could not find original node to replace!'); |
| 344 | + } |
| 345 | + }); |
| 346 | + }, |
| 347 | + 'vis-edit-source-cancel': function() { |
| 348 | + pp.sel = null; |
| 349 | + $(this).dialog( 'close' ); |
| 350 | + } |
| 351 | + }, |
| 352 | + open: function() { |
| 353 | + $('#vis-edit-source-text').val(pp.sel.src); |
| 354 | + } |
| 355 | + } |
| 356 | + } |
| 357 | + }); |
275 | 358 | editor.wikiEditor( 'addToToolbar', { |
276 | 359 | 'sections': { |
277 | 360 | 'richedit': { |