r90689 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90688‎ | r90689 | r90690 >
Date:01:12, 24 June 2011
Author:brion
Status:deferred
Tags:
Comment:
ParserPlayground: primitive dialog to edit portions of rendered document as source (round tripped from tree to source and back to tree, then updating view).
Does allow you to make and save changes! :D But of course it's.... not very good right now and can add extra lines or probably explode utterly. Should work with FakeParser and PegParser (not with MediaWikiParser) for now
Modified paths:
  • /trunk/extensions/ParserPlayground/ParserPlayground.i18n.php (modified) (history)
  • /trunk/extensions/ParserPlayground/ParserPlayground.php (modified) (history)
  • /trunk/extensions/ParserPlayground/modules/ext.parserPlayground.js (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserPlayground/ParserPlayground.i18n.php
@@ -13,4 +13,7 @@
1414 */
1515 $messages['en'] = array(
1616 'parserplayground-desc' => 'Parser and editor experiments',
 17+
 18+ 'vis-edit-source-ok' => 'Ok',
 19+ 'vis-edit-source-cancel' => 'Cancel',
1720 );
Index: trunk/extensions/ParserPlayground/ParserPlayground.php
@@ -74,6 +74,10 @@
7575 'jquery.nodetree.css',
7676 'ext.parserPlayground.css',
7777 ),
 78+ 'messages' => array(
 79+ 'vis-edit-source-ok',
 80+ 'vis-edit-source-cancel',
 81+ ),
7882 'dependencies' => array(
7983 'ext.wikiEditor'
8084 ),
Index: trunk/extensions/ParserPlayground/modules/ext.parserPlayground.js
@@ -155,7 +155,7 @@
156156
157157 window.setTimeout(function() {
158158 var context = editor.data('wikiEditor-context');
159 - context.parserPlayground = {
 159+ var pp = context.parserPlayground = {
160160 parser: new FakeParser(),
161161 tree: undefined,
162162 useInspector: false,
@@ -176,28 +176,31 @@
177177 context.$parserInspector = $inspector;
178178
179179 var src = $('#wpTextbox1').val();
180 - var $dest = $target.find('div');
181180
182 - var parser = context.parserPlayground.parser;
183 - var treeMap = context.parserPlayground.treeMap = new HashMap(),
184 - renderMap = new HashMap();
 181+ var parser = pp.parser;
185182 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();
197185 });
198186
199187 context.$textarea.closest('form').submit( context.parserPlayground.fn.onSubmit );
200188
201189 },
 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+ },
202205 hide: function() {
203206 $('#pegparser-source').hide(); // it'll reshow; others won't need it
204207 context.$iframe = undefined;
@@ -257,7 +260,12 @@
258261 // Ok, not 100% kosher right now but... :D
259262 var parser = context.parserPlayground.parser;
260263 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');
262270 });
263271 event.preventDefault();
264272 return false;
@@ -271,6 +279,81 @@
272280 }
273281 }
274282 }
 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+ });
275358 editor.wikiEditor( 'addToToolbar', {
276359 'sections': {
277360 'richedit': {

Status & tagging log