MediaWiki:Gadget-scribunto-console-core.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
1 /**<nowiki>
2 * Gadget that loads a couple of snippets for the console on module pages
3 * Authors:
4 ** LapOnTheMoon
5 ** Gaz Lloyd
6 *
7 *
8 * The scribunto console is run by ext.scribunto.edit - https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/master/modules/ext.scribunto.edit.js
9 */
10 const FRAME_TEST = [
11 "local frame = {}",
12 "function frame.getParent()",
13 " local args = {",
14 " -- Args go here as ['name'] = value,",
15 " }",
16 " return { args = args }",
17 "end",
18 "mw.log( p.main(frame) )"
19 ],
20 FRAME_TEST_TEXT = FRAME_TEST.join('\n');
21 var $clearBtn, load_attempts = 0, $inp;
22
23 function addButtons() {
24 var $addTextBtn = $('<input class="console-control console-addFrameText" type="button" value="Frame text" title="Load code for a basic frame">');
25 $addTextBtn.on('click', function() {
26 $inp.val(FRAME_TEST_TEXT).attr('rows', FRAME_TEST.length+1);
27 });
28
29 var $prevHistory = $('<input class="console-control console-prevHist" type="button" value="History ˄" title="Load the previous input in your console history, if any">');
30 var $nextHistory = $('<input class="console-control console-nextHist" type="button" value="History ˅" title="Load the next in your console history, if any">');
31 // send the appropriate keydown events to the input element
32 // (ctrl+up / ctrl+down scrolls history)
33 $prevHistory.on('click', function(){
34 $inp.trigger({
35 type: 'keydown',
36 keyCode: 38, //UP arrow
37 ctrlKey: true
38 });
39 });
40 $nextHistory.on('click', function(){
41 $inp.trigger({
42 type: 'keydown',
43 keyCode: 40, //DOWN arrow
44 ctrlKey: true
45 });
46 });
47
48 var $allowEnter = $('<input class="console-control console-allowEnter" type="checkbox" name="console-allowEnter" id="console-allowEnter">'),
49 $allowEnterWrapper = $('<span title="Use the Run button or Ctrl+Enter to execute your code, when checked">');
50 $allowEnterWrapper.append($allowEnter, '<label for="console-allowEnter">Allow normal enter key</label>');
51
52 // don't actually need to have an event attached to this, we're just gonna check is(:checked)
53 // BUT we are adding an event to save state to localStorage
54 $allowEnter.prop('checked', window.localStorage.getItem('rsw-scribunto-console-allowEnter') === "true"); // do this before attaching event, just to be sure we don't fire it
55 $allowEnter.on('change', function(){
56 window.localStorage.setItem('rsw-scribunto-console-allowEnter', $allowEnter.is(':checked'));
57 });
58
59 // attach this to the parent element
60 // capture=true => this runs before the innermost element's event
61 document.getElementById('mw-scribunto-input').parentElement.addEventListener('keydown', function(e){
62 if (e.target.id !== 'mw-scribunto-input') return;
63 if (! (e.keyCode === 13)) return; // enter
64 if (e.ctrlKey || e.shiftKey) return;
65 if ($allowEnter.is(':checked')) {
66 e.stopImmediatePropagation();
67 $inp.attr('rows', $inp.val().split(/\r?\n/).length+1);
68 return;
69 }
70 }, true);
71
72 // enter runs the code
73 // ctrl+enter runs it too, and we're explicitly excluding ctrl+enter from allowEnter
74 var $run = $('<input class="console-control console-runButton" type="button" value="Run" title="Run the current input">');
75 $run.on('click', function(){
76 $inp.trigger({
77 type: 'keydown',
78 keyCode: 13,
79 ctrlKey: true
80 });
81 });
82 $clearBtn.parent().prepend($run).append($addTextBtn, $prevHistory, $nextHistory, $allowEnterWrapper);
83 }
84
85 function init() {
86 $clearBtn = $('input[type="button"][value="Clear"]');
87 $inp = $('#mw-scribunto-input');
88 if ($('#mw-scribunto-output').length && $clearBtn.length && $inp.length) {
89 $clearBtn.addClass('console-control console-clearButton').attr('title', 'Clear the console (no confirmation!)');
90 addButtons();
91 } else {
92 load_attempts++;
93 if (load_attempts > 100) return;
94 setTimeout(init, 50);
95 }
96 }
97 init();
98 //</nowiki>