Dialogue Test/ScriptTools.js

//

```
mw.hook('dev.modal').add(function(modal) {
 var mwConfig = mw.config.get([
 'wgPageName',
 'wgAction'
 ]);
 if (mwConfig.wgPageName.includes('/Script') && ['edit', 'submit'].indexOf(mwConfig.wgAction) > -1) {
 var editor = document.querySelector('.CodeMirror-code');
 var wikiEditor = document.querySelector('.wikiEditor-ui-text');
 var modalContent = 'Enter the name(s), seperated by commas, and spoken dialogue below.<br>' +
 '<input placeholder="Name(s)" type="text" name="dnames" onkeydown="mw.hook(\'dialogue.next\').fire(event);"><br>' +
 '<input placeholder="Dialogue" type="text" name="dtext" onkeydown="mw.hook(\'dialogue.next\').fire(event);">';
 var dModal = new modal.Modal({
 size: 'medium',
 id: 'insert-dialogue',
 title: 'Insert Dialogue',
 content: modalContent,
 buttons: [
 {
 type: 'button',
 text: 'Add',
 sprite: 'edit',
 event: 'add'
 }
 ],
 events: {
 add: add
 }
 });
 function add() {
 var dNames = document.querySelector('input[name="dnames"]');
 var dText = document.querySelector('input[name="dtext');
 if (!dText.value) {
 return;
 }
 if (!dNames.value) {
 addDialogue('EVENT', dText.value);
 dNames.value = '';
 dText.value = '';
 dNames.focus();
 return;
 }
 var namesArr = dNames.value.split(/,|\sand\s/).map(function(i) {
 return i.trim().charAt(0).toUpperCase() + i.trim().substring(1, i.length);
 });
 addDialogue(namesArr, dText.value);
 dNames.value = '';
 dText.value = '';
 dNames.focus();
 }
 function addEntry() {
 var event = new KeyboardEvent('keydown', {
 code: 'Enter',
 key: 'Enter',
 charCode: 13,
 keyCode: 13,
 view: window,
 bubbles: true
 });
 editor.dispatchEvent(event);
 }
 function commasAnd(array) {
 var str = '';
 if (array.length === 1) {
 return array[0];
 }
 if (array.length === 2) {
 return array.join(' and ');
 }
 for (var i = 0; i < array.length; i++) {
 var e = array[i];
 if (array.indexOf(e) === array.length - 1) {
 str = str + 'and ' + e;
 }
 else {
 str = str + e + ', ';
 }
 }
 return str;
 }
 function addDialogue(names, text) {
 var str = '';
 if (names === 'EVENT') {
 str = "''" + text + "''";
 if (editor.children.length > 1) {
 console.log('Adding entry.');
 addEntry();
 }
 }
 else {
 names = names.map(function(i) {
 return "'''" + i + "'''";
 });
 str = commasAnd(names) + ': ' + text;
 if (editor.children.length > 1) {
 console.log('Adding entry.');
 addEntry();
 }
 }
 for (var i = 0; i < str.length; i++) {
 var event = new KeyboardEvent('keypress', {
 code: str[i],
 key: str[i],
 charCode: str[i].charCodeAt(0),
 keyCode: str[i].charCodeAt(0),
 view: window,
 bubbles: true
 });
 editor.lastChild.lastChild.focus();
 editor.dispatchEvent(event);
 }
 addEntry();
 }
 mw.hook('dialogue.next').add(function(e) {
 if (e.keyCode === 13) {
 if (e.target.getAttribute('name') === 'dnames') {
 document.querySelector('input[name="dtext').focus();
 }
 if (e.target.getAttribute('name') === 'dtext') {
 add();
 }
 }
 });
 dModal.create().then(function() {
 function showPrompt(e) {
 var oldP = document.querySelector('.add-dialogue-prompt');
 if (oldP) {
 oldP.remove();
 }
 var p = document.createElement('div');
 p.classList.add('add-dialogue-prompt');
 p.style.position = 'absolute';
 p.style.top = e.pageY + 'px';
 p.style.left = e.pageX + 'px';
 p.style.padding = '10px';
 p.style.color = 'white';
 p.style.backgroundColor = 'blue';
 p.style.zIndex = 10;
 p.style.cursor = 'pointer';
 p.innerText = 'Add dialogue';
 document.body.append(p);
 p.onclick = function(\_e) {
 p.remove();
 dModal.show();
 };
 }
 wikiEditor.addEventListener('contextmenu', function(e) {
 var p = document.querySelector('.add-dialogue-prompt');
 if (!p) {
 e.preventDefault();
 showPrompt(e);
 } else {
 p.remove();
 }
 });
 document.body.addEventListener('click', function(e) {
 var p = document.querySelector('.add-dialogue-prompt');
 if (p && e.target !== p) {
 p.remove();
 }
 });
 });
 }
});

importArticle({
 type: 'script',
 article: 'u:dev:MediaWiki:Modal.js'
});
//

```