Difference between revisions of "User:Argent/common.js"

From The Coppermind
Jump to navigation Jump to search
m
m
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
  +
// Main page: https://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_customization
// 2019-03-03 autofill artist pages
 
  +
// - Library: https://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_customization/Library
;mw.hook('wikipage.editform').add(function () {
 
  +
// - Icons: https://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_icons
   
  +
var customizeToolbar = function() {
const page_name = mw.config.get('wgPageName')
 
  +
// add a <br/> button that clears right-aligned items
if (!page_name.startsWith('Coppermind:Artists/')) { return; }
 
  +
$('#wpTextbox1').wikiEditor('addToToolbar', {
  +
section: 'main',
  +
group: 'insert',
  +
tools: {
  +
"category": {
  +
label: 'br',
  +
type: 'button',
  +
icon: '//upload.wikimedia.org/wikipedia/commons/1/13/Button_enter.png',
  +
action: {
  +
type: 'encapsulate',
  +
options: {
  +
pre: "<br style=\"clear:right;\"/>"
  +
}
  +
}
 
}
 
}
  +
}); // <br> button
   
  +
// add a <gallery> button for easy artist page initialization
const edit = document.querySelector('textarea')
 
  +
$('#wpTextbox1').wikiEditor('addToToolbar', {
// show pending progress
 
  +
section: 'main',
mw.notify('scanning for new files linking to '+ page_name).then(function(note) {
 
  +
group: 'insert',
// perform API query
 
  +
tools: {
;(new mw.Api()).get({
 
action: "query",
+
"category": {
  +
label: 'artist-gallery',
format: "json",
 
  +
type: 'button',
prop: "linkshere",
 
  +
icon: '//upload.wikimedia.org/wikipedia/commons/d/d8/Vector_toolbar_insert_image_button.png',
titles: page_name,
 
  +
action: {
lhnamespace: "6", // File: namespace only
 
  +
type: 'encapsulate',
}).then(function(resp) {
 
  +
options: {
const pid = Object.keys(resp.query.pages)[0]
 
  +
pre: "<gallery mode=\"packed\" heights=250px caption=\"Gallery\">",
const new_files = []
 
  +
post: "</gallery>"
// hide progress notification
 
  +
}
note.close()
 
  +
}
// determine which new files link here
 
  +
}
if (!resp.query.pages[pid].linkshere) { mw.notify('no files link to '+ page_name); return; }
 
  +
}
const pages = resp.query.pages[pid].linkshere.map(function(page) { return page.title.split(':')[1] })
 
  +
}); // <gallery> button
for (var i in pages) {
 
  +
const page = pages[i]
 
  +
}; // customizeToolbar
if (edit.value.includes(page)) { continue; }
 
  +
new_files.push(pages[i] + ' | ')
 
  +
/*
}
 
  +
* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar...
// don't change the page if there's nothing to change
 
  +
* Don't touch below this line!
if (new_files.length == 0) { mw.notify('no new files link to '+ page_name); return; }
 
  +
*/
mw.notify(new_files.length +' new files link to '+ page_name);
 
if (edit.value.includes('</gallery>')) {
+
if (['edit', 'submit'].indexOf(mw.config.get('wgAction')) !== -1) {
  +
mw.loader.using('user.options').then(function() {
// add the lines to the end of the current gallery
 
  +
// This can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
edit.value = edit.value.replace('</gallery>', new_files.join("\n") +"\n</gallery>")
 
  +
if (mw.user.options.get('usebetatoolbar') == 1) {
} else {
 
  +
$.when(
// create a gallery at the bottom of the page
 
  +
mw.loader.using('ext.wikiEditor'), $.ready
edit.value += "\n\n<gallery mode="packed" heights=150px>\n"+ new_files.join("\n") +"\n</gallery>\n"
 
  +
).then(customizeToolbar);
}
 
  +
}
}) // api.get.then
 
  +
});
}) // note.close
 
});
+
}

Latest revision as of 22:32, 22 September 2021

// Main page: https://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_customization
// - Library: https://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_customization/Library
// - Icons:   https://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_icons

var customizeToolbar = function() {
  // add a <br/> button that clears right-aligned items
    $('#wpTextbox1').wikiEditor('addToToolbar', {
    section: 'main',
    group: 'insert',
    tools: {
      "category": {
        label: 'br',
        type: 'button',
        icon: '//upload.wikimedia.org/wikipedia/commons/1/13/Button_enter.png',
        action: {
          type: 'encapsulate',
          options: {
            pre:  "<br style=\"clear:right;\"/>"
          }
        }
      }
    }
  }); // <br> button

  // add a <gallery> button for easy artist page initialization
    $('#wpTextbox1').wikiEditor('addToToolbar', {
    section: 'main',
    group: 'insert',
    tools: {
      "category": {
        label: 'artist-gallery',
        type: 'button',
        icon: '//upload.wikimedia.org/wikipedia/commons/d/d8/Vector_toolbar_insert_image_button.png',
        action: {
          type: 'encapsulate',
          options: {
            pre:  "<gallery mode=\"packed\" heights=250px caption=\"Gallery\">",
            post: "</gallery>"
          }
        }
      }
    }
  }); // <gallery> button

}; // customizeToolbar 

/* 
 * Check if view is in edit mode and that the required modules are available. Then, customize the toolbar...
 * Don't touch below this line!
 */
if (['edit', 'submit'].indexOf(mw.config.get('wgAction')) !== -1) {
  mw.loader.using('user.options').then(function() {
    // This can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
    if (mw.user.options.get('usebetatoolbar') == 1) {
      $.when(
        mw.loader.using('ext.wikiEditor'), $.ready
      ).then(customizeToolbar);
    }
  });
}