User:Lucas Werkmeister/hyphenation-point.js

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.
/*
 * This user script replaces any | characters with ‧ U+2027 HYPHENATION POINT when editing hyphenation snaks.
 * • (U+2022 BULLET) and · (U+00B7 MIDDLE DOT) are also replaced.
 * Code partially based on [[User:TMg/currentDate.js]] and [[User:1Veertje/identifierInput.js]].
 *
 * To use this script, add the following to [[Special:MyPage/common.js|your common.js]]:
 * 
 *     mw.loader.load( '//www.wikidata.org/w/index.php?title=User:Lucas_Werkmeister/hyphenation-point.js&action=raw&ctype=text/javascript' );
 */
( function ( $ ) {
  $( '.wikibase-statementgrouplistview' ).on(
    'valueviewafterstartediting',
    function ( event ) {
      var $valueview = $( event.target ),
          valueview = $valueview.data( 'valueview' ),
          expert = valueview.expert(),
          $input = expert.$input,
          $snakview = $valueview.closest( '.wikibase-snakview' ),
          snakview = $snakview.data( 'snakview' ),
          property = snakview.value().property;

      if ( property !== 'P5279' ) {
        return;
      }
      
      function replace() {
        const value = $input.val();
        const replaced = value.replace( /[|•·]/g, '‧' );
        if ( value !== replaced ) {
          $input.val( replaced );
          expert._viewNotifier.notify( 'change' );
        }
      }

      $input.on( 'input', replace );
      replace();
    }
  );
} )( jQuery );