Jump to content

User:Quarl/auto summary.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// [[User:Quarl/auto_summary.js]] - automatically fill in edit summary based on diff; short cuts

// depends: wikipage.js, wikiedit.js, util.js, diff.js, shortcuts.js
// recommends: smartsubmit.js

// originally based on http://wiki.riteme.site/wiki/Wikipedia:WikiProject_User_scripts/Scripts/Force_edit_summary

// 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

// <pre><nowiki>

var autosummary = new Object();

// see also: [[Wikipedia: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.prompt = 'Enter edit summary.  ' + autosummary.shortcuts.msg();

// whether to query even when filled via auto diff
autosummary.query = true;

// whether to default edit summary to diff (not needed if
// autosummary.auto_diff is enabled)
autosummary.diff = true;

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

// whether to automatically select text after the /* section */ and « diff »
autosummary.auto_select = true;

autosummary._load = function()
{
    if (!WikiPage.editingP) return;
    if (WikiPage.newSectionP) return;

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

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

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

autosummary._autoDiffSetup = function() {
    hookEventObj(document.editform.wpTextbox1, 'blur', autosummary._updateAutoDiff);
    autosummary._autoDiffTimerUpdate();
}

autosummary._autoDiffTimerUpdate = function() {
    autosummary._updateAutoDiff();
    setTimeout(autosummary._autoDiffTimerUpdate, 1000*autosummary.auto_diff);
}

autosummary._textFilter = function(s) {
    // ignore signature when diffing
    return s.replace(/~~~~/g,'').replace(/<span class=[\'\"]user-sig.*?<\/span>/g, '');
}

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

autosummary.diffTextbox = function() {
    var editor = wikiPage.getEditor();
    return autosummary._diffStrings(editor.wpTextbox1_orig, editor.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) {
        s = "«" + s + "» ";
    }

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

    editor.updateForm();
}

autosummary._preSubmitEvent = function(event)
{
    if (autosummary.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 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.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
        editor.wpSummary = autosummary.shortcuts.substWords(editor.wpSummary);
        return true;
    } else if (autosummary.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.query) {
        var r = prompt(autosummary.prompt, editor.wpSummary);
        if(r == null) { return false; } // cancel
        editor.wpSummary = autosummary.shortcuts.substWords(r);
    }

    return true;
}

$(autosummary._load);

// </nowiki></pre>