« MediaWiki:Common.js » : différence entre les versions
Page de l’interface de MediaWiki
Autres actions
Aucun résumé des modifications |
Module:Règle |
||
| Ligne 153 : | Ligne 153 : | ||
/* === Modèle:Règle — copie d'ancre === */ | /* === Modèle:Règle — copie d'ancre === */ | ||
mw.hook('wikipage.content').add(function ($content) { | mw.hook('wikipage.content').add(function ($content) { | ||
/ | |||
/* Crée le toast une seule fois */ | |||
var $toast = $('#regle-toast'); | var $toast = $('#regle-toast'); | ||
if ($toast.length === 0) { | if ($toast.length === 0) { | ||
| Ligne 159 : | Ligne 160 : | ||
.attr('id', 'regle-toast') | .attr('id', 'regle-toast') | ||
.addClass('regle-toast') | .addClass('regle-toast') | ||
. | .appendTo('body'); | ||
} | } | ||
var toastTimer; | var toastTimer; | ||
function showToast() { | function showToast(message) { | ||
clearTimeout(toastTimer); | clearTimeout(toastTimer); | ||
$toast.addClass('regle-toast--visible'); | $toast.text(message).addClass('regle-toast--visible'); | ||
toastTimer = setTimeout(function () { | toastTimer = setTimeout(function () { | ||
$toast.removeClass('regle-toast--visible'); | $toast.removeClass('regle-toast--visible'); | ||
| Ligne 173 : | Ligne 173 : | ||
} | } | ||
$content.find('a | /* Cible tous les liens internes dont le href commence par #r- */ | ||
$content.find('a[href^="#r-"]').off('click.regle').on('click.regle', function (e) { | |||
e.preventDefault(); | e.preventDefault(); | ||
/ | var ancre = $(this).attr('href').replace('#', ''); | ||
var | var url = window.location.origin | ||
if ( | + window.location.pathname | ||
+ '#' + ancre; | |||
/* Défilement vers l'ancre */ | |||
var cible = document.getElementById(ancre); | |||
if (cible) { | |||
cible.scrollIntoView({ behavior: 'smooth', block: 'start' }); | |||
} | } | ||
// Copie dans le presse-papier | /* Mise à jour de l'URL */ | ||
if (navigator.clipboard) { | history.replaceState(null, '', '#' + ancre); | ||
navigator.clipboard.writeText(url).then(showToast); | |||
/* Copie dans le presse-papier */ | |||
if (navigator.clipboard && window.isSecureContext) { | |||
navigator.clipboard.writeText(url) | |||
.then(function () { showToast('🔗 Lien copié !'); }) | |||
.catch(function () { showToast('❌ Copie impossible'); }); | |||
} else { | } else { | ||
/ | /* Fallback navigateurs anciens */ | ||
var $tmp = $('<textarea>').val(url).appendTo('body'); | var $tmp = $('<textarea>') | ||
.val(url) | |||
.css({ position: 'fixed', opacity: 0 }) | |||
.appendTo('body'); | |||
$tmp[0].select(); | $tmp[0].select(); | ||
document.execCommand('copy'); | try { | ||
document.execCommand('copy'); | |||
showToast('🔗 Lien copié !'); | |||
} catch (err) { | |||
showToast('❌ Copie impossible'); | |||
} | |||
$tmp.remove(); | $tmp.remove(); | ||
} | } | ||
}); | |||
}); | }); | ||
/* === fin Modèle:Règle === */ | /* === fin Modèle:Règle === */ | ||