« MediaWiki:Common.js » : différence entre les versions
Page de l’interface de MediaWiki
Autres actions
Annulation des modifications 3831 de Hiob (discussion) Balise : Annulation |
Aucun résumé des modifications |
||
| Ligne 149 : | Ligne 149 : | ||
}()); | }()); | ||
/* === Modèle:Règle — copie d'ancre === */ | |||
mw.hook('wikipage.content').add(function ($content) { | |||
// Crée le toast s'il n'existe pas encore | |||
var $toast = $('#regle-toast'); | |||
if ($toast.length === 0) { | |||
$toast = $('<div>') | |||
.attr('id', 'regle-toast') | |||
.addClass('regle-toast') | |||
.text('Lien copié !'); | |||
$('body').append($toast); | |||
} | |||
var toastTimer; | |||
function showToast() { | |||
clearTimeout(toastTimer); | |||
$toast.addClass('regle-toast--visible'); | |||
toastTimer = setTimeout(function () { | |||
$toast.removeClass('regle-toast--visible'); | |||
}, 2000); | |||
} | |||
$content.find('a.regle-lien').off('click.regle').on('click.regle', function (e) { | |||
e.preventDefault(); | |||
var ancre = $(this).data('ancre'); | |||
var url = window.location.origin | |||
+ window.location.pathname | |||
+ '#' + ancre; | |||
// Défilement vers l'ancre | |||
var $cible = $('#' + ancre); | |||
if ($cible.length) { | |||
$cible[0].scrollIntoView({ behavior: 'smooth', block: 'start' }); | |||
} | |||
// Copie dans le presse-papier | |||
if (navigator.clipboard) { | |||
navigator.clipboard.writeText(url).then(showToast); | |||
} else { | |||
// Fallback pour navigateurs anciens | |||
var $tmp = $('<textarea>').val(url).appendTo('body'); | |||
$tmp[0].select(); | |||
document.execCommand('copy'); | |||
$tmp.remove(); | |||
showToast(); | |||
} | |||
// Met à jour l'URL sans recharger | |||
history.replaceState(null, '', '#' + ancre); | |||
}); | |||
}); | |||
/* === fin Modèle:Règle === */ | |||