fix(shaarli/edit) restore native tag auto complete

This commit is contained in:
2025-06-04 23:59:49 +00:00
parent 722094f4a9
commit 5414a36b2d

View File

@ -148,6 +148,9 @@
document.addEventListener('DOMContentLoaded', function() {
var input = document.getElementById("lf_tags{$batchId}");
var suggestionsContainer = document.getElementById("tag-suggestions{$batchId}");
// liste complète des tags depuis data-list
var allTags = input.getAttribute('data-list').split(/,\s*/);
var mapping = [
{ keys: ['accessibility','accessibilité','web development','web design','html','css','websites'], tags: ['web'] },
{ keys: ['accounting','comptabilité'], tags: ['accounting'] },
@ -290,14 +293,21 @@
{ keys: ['youtube.com','invidious','peertube','watch?v'], tags: ['video'] },
{ keys: ['zoemp','zoemp.be'], tags: ['zoemp'] }
];
var matchCounts = {};
var tagify = new Tagify(input, {
delimiters: ",| ",
pattern: /^.{1,50}$/,
trim: true,
keepInvalidTags: false,
whitelist: [],
dropdown: { enabled: 0 },
whitelist: [], // sera mis à jour dynamiquement
dropdown: {
enabled: 1, // activation du dropdown à la frappe
fuzzySearch: true,
position: 'all',
maxItems: 50,
closeOnSelect: false
},
maxTags: 50,
duplicates: false,
originalInputValueFormat: function(valuesArr) {
@ -312,6 +322,8 @@
var existing = tagify.value.map(function(item){ return item.value; });
matchCounts = {};
var suggested = {};
// Calcule le nombre doccurrences de chaque tag via les règles
mapping.forEach(function(entry) {
var matchesKey = entry.keys.filter(function(k){ return desc.includes(k.toLowerCase()); }).length;
entry.tags.forEach(function(t) {
@ -319,6 +331,7 @@
suggested[t] = (suggested[t] || 0) + (matchesKey > 0 ? 1 : 0);
});
});
var highPriority = [], medPriority = [], lowPriority = [];
Object.keys(suggested).forEach(function(t) {
if (existing.indexOf(t) !== -1) return;
@ -326,9 +339,21 @@
else if (suggested[t] === 1) medPriority.push(t);
else lowPriority.push(t);
});
// tronque lowPriority à 5 éléments (ou autre nombre souhaité)
// Tronquer à 5 lowPriority
lowPriority = lowPriority.slice(0, 5);
var ordered = highPriority.concat(medPriority).concat(lowPriority);
// On complète la whitelist avec le reste des tags non déjà listés
var fallback = allTags.filter(function(t) {
return ordered.indexOf(t) === -1 && existing.indexOf(t) === -1;
});
var finalList = ordered.concat(fallback);
tagify.settings.whitelist = finalList;
tagify.dropdown.show.call(tagify, finalList);
// Met à jour les suggestions affichées sous linput
suggestionsContainer.innerHTML = '';
ordered.forEach(function(tag) {
var el = document.createElement('span');
@ -364,4 +389,4 @@
</script>
</body>
</html>
{/if}
{/if}