Utilisatrice:Darmo117/common.js/substitute.js

Note : après avoir enregistré vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : Maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ou Ctrl-R (⌘-R sur un Mac) ;
  • Google Chrome : Appuyez sur Ctrl-Maj-R (⌘-Shift-R sur un Mac) ;
  • Internet Explorer : Maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ;
  • Opera : Allez dans Menu → Settings (Opera → Préférences sur un Mac) et ensuite à Confidentialité & sécurité → Effacer les données d'exploration → Images et fichiers en cache.
/********************************************************************
 * (en)
 * On-the-fly character substitution when in edit mode.
 * Enables typing characters that are missing from the French AZERTY
 * keyboard.
 ********************************************************************
 * (fr)
 * Remplacement à la volée de certains caractères lors de l’édition.
 * Pensé pour les caractères absents du clavier AZERTY français pour
 * écrire le français.
 ********************************************************************/
// <nowiki>
$(function () {
  if (window.wikt) {
    /** @type {Object<string, string>} */
    const TAGS = {
      "\u00a0": "&nbsp;",
    };

    /** @type {string|null} */
    let previousText = null;
    let codeMirrorActive = false;

    wikt.page.onDOMChanges(mutationsList => {
      mutationsList.forEach(mutation => {
        if (mutation.type === "childList") {
          if (!codeMirrorActive && wikt.edit.isCodeMirrorEnabled()) {
            // noinspection JSCheckFunctionSignatures
            wikt.edit.getCodeMirror().on("keyup", () => parse(wikt.edit.getEditBox()));
            codeMirrorActive = true;
          }
        }
      });
    }, document.body, {subtree: true, childList: true});

    /**
     * Parses the text of the given text input.
     * @param $input {jQuery} The input element.
     */
    function parse($input) {
      let text = wikt.edit.getText($input);

      if (text !== previousText) {
        const cursorPos = wikt.edit.getCursorLocation($input);
        let newPos = -1;
        let start, end;

        if (!wikt.edit.getSelectedText($input)) {
          for (const [tag, repl] of Object.entries(TAGS)) {
            start = cursorPos - tag.length;
            end = cursorPos;
            if (text.substring(start, end) === tag) {
              text = text.substring(0, start) + repl + text.substring(end);
              newPos = start + repl.length;
              break;
            }
          }

          if (newPos >= 0) {
            // noinspection JSCheckFunctionSignatures
            wikt.edit.setText(text, $input);
            wikt.edit.setCursorLocation(newPos, $input);
          }
        }

        previousText = text;
      }
    }
  }
});
// </nowiki>