User:Euphoria/common.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
fix
fix
Line 1: Line 1:

//<nowiki>
//<nowiki>
mw.loader.using('mediawiki.util', function () {
$(() => {
if (mw.config.get('wgPageName').startsWith('User:Euphoria/TestVfD/')) {
const VfDResolver = {
config: {
// Add "Close as delete"
mw.util.addPortletLink(
vfdPrefix: 'User:Euphoria/TestVfD/' // or 'Wikiquote:Votes_for_deletion/'
},
'p-cactions',
'#',
'Close as delete',
'ca-close-delete',
'Close this VfD as delete'
).addEventListener('click', function (e) {
e.preventDefault();
insertClosure('delete');
});


actionOptions: [
// Add "Close as keep"
mw.util.addPortletLink(
{ data: 'delete', label: 'Close as delete' },
{ data: 'keep', label: 'Close as keep' },
'p-cactions',
{ data: 'no consensus', label: 'Close as no consensus' }
'#',
],
'Close as keep',
'ca-close-keep',
'Close this VfD as keep'
).addEventListener('click', function (e) {
e.preventDefault();
insertClosure('keep');
});


init() {
// Add "Close as no consensus"
mw.util.addPortletLink(
const currentPage = mw.config.get('wgPageName');
'p-cactions',
if (!currentPage.startsWith(this.config.vfdPrefix)) return;
'#',
'Close as no consensus',
'ca-close-nc',
'Close this VfD as no consensus'
).addEventListener('click', function (e) {
e.preventDefault();
insertClosure('no consensus');
});


function insertClosure(action) {
mw.loader.using(['oojs-ui', 'mediawiki.util', 'mediawiki.api'], this.run.bind(this));
var topText = '{{subst:vt|' + action + '. --~~~~}}\n\n';
},
var bottomText = '\n\n{{subst:vb}}';


run() {
// Access the edit box
this.addStyle();
var textbox = document.getElementById('wpTextbox1');
this.addButtons();
if (!textbox) {
alert('Edit box not found!');
},
return;
}


// Insert templates at top and bottom
addStyle() {
textbox.value = topText + textbox.value + bottomText;
const darkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
const bg = darkMode ? '#202122' : '#f5f5f5';
const border = darkMode ? '#343434' : '#ccc';
const text = darkMode ? '#eaecf0' : '#000';
const previewBg = darkMode ? '#101418' : 'white';


mw.util.addCSS(`
// Prefill summary
document.getElementById('wpSummary').value = 'Closed as ' + action;
.vfd-closure-panel {
background-color: ${bg};
border: 1px solid ${border};
margin: 10px 0;
padding: 10px;
border-radius: 5px;
color: ${text};
position: relative;
}
.vfd-closure-preview {
background-color: ${previewBg};
color: ${text};
border: 1px solid ${border};
padding: 10px;
margin: 10px 0;
border-radius: 3px;
}
.vfd-closure-copy-icons {
display: flex;
gap: 6px;
position: absolute;
right: 8px;
bottom: 5px;
z-index: 10;
}
.vfd-closure-copy-icon {
width: 16px;
height: 16px;
cursor: pointer;
opacity: 0.7;
transition: opacity 0.1s;
background: none;
border: none;
padding: 0;
}
.vfd-closure-copy-icon:hover { opacity: 1; }
.vfd-closure-copy-icon-copied { opacity: 1; box-shadow: 0 0 2px 2px #a2e37c; }
.vfd-closure-copy-tooltip {
position: absolute;
right: 24px;
bottom: 24px;
padding: 1px 6px;
border-radius: 2px;
font-size: 11px;
white-space: nowrap;
box-shadow: 0 1px 3px #0003;
}
`);
},

addButtons() {
this.actionOptions.forEach(opt => {
mw.util.addPortletLink('p-cactions', '#', opt.label, `ca-close-${opt.data}`, `Close this VfD as ${opt.data}`)
.addEventListener('click', e => {
e.preventDefault();
this.openPanel(opt.data);
});
});
},

openPanel(action) {
const textbox = document.getElementById('wpTextbox1');
if (!textbox) return alert('Edit box not found!');

const panelId = 'vfd-closure-panel';
$('#' + panelId).remove();

const $panel = $('<div>').attr('id', panelId).addClass('vfd-closure-panel');
$panel.append($('<h4>').text(`VfD Closure: ${action}`));

const commentInput = new OO.ui.MultilineTextInputWidget({
placeholder: 'Optional comment',
autosize: true
});
$panel.append(new OO.ui.FieldLayout(commentInput, { label: 'Comment:', align: 'left' }).$element);

const markResolvedCheckbox = new OO.ui.CheckboxInputWidget({ selected: true });
$panel.append(new OO.ui.FieldLayout(markResolvedCheckbox, { label: 'Add {{subst:vb}} template', align: 'inline' }).$element);

const $previewArea = $('<div>').addClass('vfd-closure-preview').hide();
$panel.append($previewArea);

const submitBtn = new OO.ui.ButtonWidget({ label: 'Insert', flags: ['progressive'] });
const previewBtn = new OO.ui.ButtonWidget({ label: 'Preview' });
const cancelBtn = new OO.ui.ButtonWidget({ label: 'Cancel', flags: ['destructive'] });

const $btnArea = $('<div>').css('margin-top', '10px').append(previewBtn.$element).append(' ').append(submitBtn.$element).append(' ').append(cancelBtn.$element);
$panel.append($btnArea);

$(textbox).before($panel);

previewBtn.on('click', () => {
const wikitext = this.generateWikitext(action, commentInput.getValue(), markResolvedCheckbox.isSelected());
$previewArea.show().html('<p>Loading preview...</p>');

const api = new mw.Api();
api.get({
action: 'parse',
text: wikitext,
prop: 'text',
formatversion: 2,
pst: true,
disableeditsection: true,
sectionpreview: true
}).then(resp => {
$previewArea.html(resp.parse && resp.parse.text ? resp.parse.text : '<p>Error loading preview.</p>');
}).catch(() => { $previewArea.html('<p>Error loading preview.</p>'); });
});

submitBtn.on('click', () => {
const wikitext = this.generateWikitext(action, commentInput.getValue(), markResolvedCheckbox.isSelected());
textbox.value = wikitext;

const summaryBox = document.getElementById('wpSummary');
if (summaryBox) summaryBox.value = `Closed as ${action}`;

$panel.remove();
});

cancelBtn.on('click', () => { $panel.remove(); });
},

generateWikitext(action, comment, markResolved) {
const timestamp = '~~~~';
let topText = `{{subst:vt|${action}. --${timestamp}${comment ? ' ' + comment : ''}}}\n\n`;
let bottomText = markResolved ? '\n\n{{subst:vb}}' : '';
return topText + bottomText;
}
}
};
}

VfDResolver.init();
});
});
//</nowiki>
//</nowiki>

Revision as of 08:03, 25 September 2025

//<nowiki>
mw.loader.using('mediawiki.util', function () {
    if (mw.config.get('wgPageName').startsWith('User:Euphoria/TestVfD/')) {
        // Add "Close as delete"
        mw.util.addPortletLink(
            'p-cactions',
            '#',
            'Close as delete',
            'ca-close-delete',
            'Close this VfD as delete'
        ).addEventListener('click', function (e) {
            e.preventDefault();
            insertClosure('delete');
        });

        // Add "Close as keep"
        mw.util.addPortletLink(
            'p-cactions',
            '#',
            'Close as keep',
            'ca-close-keep',
            'Close this VfD as keep'
        ).addEventListener('click', function (e) {
            e.preventDefault();
            insertClosure('keep');
        });

        // Add "Close as no consensus"
        mw.util.addPortletLink(
            'p-cactions',
            '#',
            'Close as no consensus',
            'ca-close-nc',
            'Close this VfD as no consensus'
        ).addEventListener('click', function (e) {
            e.preventDefault();
            insertClosure('no consensus');
        });

        function insertClosure(action) {
            var topText = '{{subst:vt|' + action + '. --~~~~}}\n\n';
            var bottomText = '\n\n{{subst:vb}}';

            // Access the edit box
            var textbox = document.getElementById('wpTextbox1');
            if (!textbox) {
                alert('Edit box not found!');
                return;
            }

            // Insert templates at top and bottom
            textbox.value = topText + textbox.value + bottomText;

            // Prefill summary
            document.getElementById('wpSummary').value = 'Closed as ' + action;
        }
    }
});
//</nowiki>