User:Dixtosa/expandable.js
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
// Description: can make basic lists expandable without reloading a page
// can be tested here [[User:Dixtosa/Georgian protologisms]]
// Problems/Todo: see below "BAD_CODE"
// can enjoy mw.api.parse and +- character offset
let expandable = {};
expandable.changeWikitext = function(wikitext, number, text)
{
let replaceCounter = 0;
let prefix = "* [[";
let suffix = "]]";
let newlines = "\n";
wikitext = wikitext.replace(/{{expandable(.*?)}}/g,
function(match, capture) {
if (replaceCounter++ == number) {
capture = capture.split("|");
if (capture.length == 2) {
[prefix, suffix] = [capture[1], ""];
}
else if (capture.length == 3) {
[prefix, suffix] = [capture[1], capture[2]];
}
else if (capture.length > 3) {
[prefix, suffix] = [capture[1], capture[2]];
let offsetY = capture[3].match(/y=(\d+)/);
if (offsetY !== null) {
newlines += "\n".repeat(parseInt(offsetY[1]));
}
}
[prefix, suffix] = $.map([prefix, suffix], decodeURIComponent);
return match + newlines + prefix + text + suffix;
}
else return match;
}
);
return wikitext;
};
function insertStuff(number)
{
let $expandable = $("#expandabledotjs" + number);
let text = $expandable.val();
new Editor().addEdit({
edit: wikitext => expandable.changeWikitext(wikitext, number, text),
redo: () => { $expandable.next().after("<ul><li>" + text + "</li></ul>"); },
undo: () => { $expandable.next().next().remove(); },
summary: "[[User:Dixtosa/expandable.js|expanded]]; "
});
}
let eachCounter = -1;
$(".expandabledotjs").replaceWith(() => {
eachCounter++;
let textInput = $("<input type = 'text'/>").attr("id", "expandabledotjs" + eachCounter); //BAD_CODE
let submit = $(`<input type='submit' value='+' onclick='javascript:insertStuff(${eachCounter}); return false;'>`);
return $("<form></form>").css("display", "inline").append(textInput).append(submit);
});
$(".expandabledotjs_affix").css("display", "inline").each(function() {
$(this).text(decodeURIComponent($(this).text()));
});