User:Poccil/autosummary.js

// $Id: autosummary.js 1257 2006-02-25 08:05:33Z quarl $

// autosummary.js - automatically fill in edit summary based on diff; short cuts

// quarl 2006-01-26 added shortcuts // quarl 2006-01-30 auto diff

// TODO: add a checkbox for "auto" next to summary, to disable auto diff

var $autosummary = new Module('autosummary.js'); $autosummary.depend('wikipage.js', 'wikiedit.js', 'util.js', 'diff.js', 'shortcuts.js'); // recommends: smartsubmit.js

// see also: Edit_summary_legend $autosummary.shortcuts = Shortcuts({ // 'ed' : 'editing', // 'edit'  'cped,cpediting,cpyed,copyed,copyedit' : 'copy-editing',  'mn ' : 'minor',  'mnf ' : 'minor fixes',  'fmt' : 'formatting',  'mfmt ' : 'minor formatting',  'rv' : 'reverting',  'rvv' : 'reverting vandalism',  'gr' : 'grammar',  'sp' : 'spelling',  'rd ' : 'redirect',  'cmt' : 'commenting',  'cla' : 'clarifying',  'xl,xlink' : 'external link',  'sa' : 'see also',  'cap' : 'capitalization',  'catz' : 'categorizing',  'cl,cu' : 'cleaning up',  'newart,creat' : 'creating new article',  'dab,disamb,disam,disambig' : 'disambiguating',  'rddab' : 'replacing redirect with disambiguation page',  //'st' : 'see Talk page',  // 'style' : 'style',  'punc,punct,pnct' : 'punctuation',  'wfy,wkfy' : 'wikifying' });

$autosummary.options = { prompt : 'Enter edit summary.',

// whether to query even when filled via auto diff query: true,

// whether to default edit summary to diff (not needed if   // $autosummary.auto_diff is enabled) diff: true,

// whether to automatically prepend "(auto diff)" to edit summary while // editing. Use number of seconds for update interval (in sec), or false // to disable. auto_diff: 1.5,

// whether to automatically select text after the /* section */ and Â« diff // Â» auto_select: true,

// whether shortcut expansion is enabled shortcuts_enabled: false };

$autosummary.load = function {   if (!wikiDoc.editingP) return; if (wikiDoc.newSectionP) return;

if ($autosummary.options.auto_diff) { $autosummary._autoDiffSetup; }

if ($autosummary.options.auto_select) { $util.hookEventObj(document.editform.wpSummary, 'focus', $autosummary._focusSummaryEvent); }

$util.hookEventObj(document.editform.wpSave, 'click', $autosummary._preSubmitEvent); }

$autosummary._autoDiffSetup = function { $util.hookEventObj(document.editform.wpTextbox1, 'blur', $autosummary._updateAutoDiff); $autosummary.intervalHandle = setInterval($autosummary._updateAutoDiff,                                             1000*$autosummary.options.auto_diff); }

$autosummary._textFilter = function(s) { // ignore signature when diffing return s //   return s.replace(new RegExp(""+"","g"),).replace(//g, ); }

$autosummary._diffStrings = function(s1,s2) { return $diff.diffSummary($autosummary._textFilter(s1), $autosummary._textFilter(s2)); }

$autosummary.diffTextbox = function { var editor = wikiPage.getEditor; //   alert(editor.wpTextbox1_orig) //   alert(editor.wpTextbox1.value) return $autosummary._diffStrings(editor.wpTextbox1_orig, document.editform.wpTextbox1); }

// update the edit summary with diff $autosummary._updateAutoDiff = function { var editor = wikiPage.getEditor; editor.updateThis;

if (editor.wpTextbox1_prev == editor.wpTextbox1) { // no change since last update return; }   editor.wpTextbox1_prev = editor.wpTextbox1;

var s = $autosummary.diffTextbox; if (s) { if (wikiDoc.editingNewP) { // add "Creating page" since edit summary survives page moves s = s.replace(/^new /,                         $util.pprintf("Creating $1 ", wikiPage.page)); }

s = "Â«" + s + "Â» "; }

if (editor.wpSummary.match(/\Â«/)) { editor.wpSummary = editor.wpSummary.replace(/Â«.*?Â» */, s); } else if (s && !$autosummary._pruneSection(editor.wpSummary)) { editor.wpSummary = $util.trimSpaces(editor.wpSummary); if (editor.wpSummary) editor.wpSummary += ' '; editor.wpSummary += s;   }

editor.updateForm; }

$autosummary._preSubmitEvent = function(event) {   if ($autosummary.options.auto_diff) $autosummary._updateAutoDiff; var editor = wikiPage.getEditor; editor.updateThis; var r = $autosummary._edit(editor); editor.updateForm;

if (!r) { event.preventDefault; event.stopPropagation; } }

// auto focus $autosummary._focusSummaryEvent = function(event) { var sumField = document.editform.wpSummary; if (sumField.value.match(/^(?:\/\*.*?\*\/)?\s*(?:Â«(?:.*)Â»)? ?/)) {       var n = RegExp.lastMatch.length; var m = sumField.value.length; // apparently you can't setSelectionRange in an onFocus handler, but // you can set a timer to do it 0 seconds from now. setTimeout(function { sumField.setSelectionRange(n, m) }, 0); } }

$autosummary._pruneSection = function(s) { return $util.trimSpaces(s.replace(/^\/\\*.*?\\*\//,'')); }

$autosummary._edit = function(editor) {   if (editor.wpTextbox1_orig == editor.wpTextbox1) { // no change return true; }

var auto = false;

if (!editor.wpSummary.match(/REDIRECT/i) &&       editor.wpTextbox1.match(/^#REDIRECT/i)) {       // it's a redirect. Annotate with REDIRECT. if ($autosummary.options.auto_diff) { // don't need auto diff // editor.wpSummary = editor.wpSummary.replace(/^ã€ˆ.*?ã€‰ */, ''); }       editor.wpSummary += editor.wpTextbox1; auto = true; } else if ($autosummary._pruneSection(editor.wpSummary)) { // non-negligible summary exists; continue with submission if ($autosummary.options.shortcuts_enabled) { editor.wpSummary = $autosummary.shortcuts.substWords(editor.wpSummary); }       return true; } else if ($autosummary.options.diff) { // if we get here then we're not using auto diff, or user manually // removed it       var s = $autosummary.diffTextbox; if (s) { editor.wpSummary += s;           auto = true; }   }

if (!auto || $autosummary.options.query) { var pr = $autosummary.options.prompt; if ($autosummary.options.shortcuts_enabled) { pr += ' ' + $autosummary.shortcuts.msg; }

var r = window.prompt(pr, editor.wpSummary); if(r == null) { return false; } // cancel if ($autosummary.options.shortcuts_enabled) { editor.wpSummary = $autosummary.shortcuts.substWords(r); }   }

return true; }

$util.addOnloadHook($autosummary.load);