User:Polygnotus/Scripts/FavouriteTemplates.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 132:
const cleanTitle = title.replace(/^Template:/, '');
return `{{${cleanTitle}}}`;
}
 
// Function to create a text-based icon using data URL
function createTextIcon(text) {
// Calculate width based on text length (more generous estimation: 10px per character + padding)
const textWidth = text.length * 10 + 20;
const width = Math.max(textWidth, 80); // Minimum width of 80px
console.log(`Creating button for "${text}" with width: ${width}px`);
// Create a simple SVG with the text
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="22" viewBox="0 0 ${width} 22">
<rect width="${width}" height="22" fill="#f8f9fa" stroke="#a2a9b1" stroke-width="1" rx="2"/>
<text x="${width/2}" y="14" font-family="sans-serif" font-size="11" text-anchor="middle" fill="#222">${text}</text>
</svg>`;
// Convert to data URL
return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
}
 
Line 157 ⟶ 139:
if (favoriteTemplates && favoriteTemplates.length > 0) {
// AddCreate the Templatestools toolbarobject sectionfor the buttons
$textarea.wikiEditor(const 'addToToolbar',templateTools = {};
sections: {
'favorite-templates': {
type: 'toolbar',
label: 'Favorite Templates'
}
}
} );
// Add aeach groupfavorite fortemplate favoriteas a templatestool
$textarea.wikiEditor( 'addToToolbar', {
section: 'favorite-templates',
groups: {
favorites: {
label: 'My Favorites'
}
}
} );
// Add buttons for each favorite template
for (const template of favoriteTemplates) {
const buttonId = createButtonId(template.title);
Line 183 ⟶ 148:
const displayTitle = template.title.replace(/^Template:/, '');
$textarea.wikiEditor(templateTools[buttonId] 'addToToolbar',= {
sectionlabel: 'favorite-templates'displayTitle,
groupaction: 'favorites',{
tools type: {'encapsulate',
[buttonId]options: {
labelpre: displayTitle,templateSyntax
type: 'button',
icon: createTextIcon(displayTitle),
action: {
type: 'encapsulate',
options: {
pre: templateSyntax
}
}
}
}
} );
}
}
// Add the Templates section: 'favorite-templates',with all tools at once
$textarea.wikiEditor( 'addToToolbar', {
sectionssection: {'main',
groups: {
'favorite-templates': {
label: 'Favorite Templates',
typetools: 'toolbar',templateTools
</svg>`; }
}
} );
console.log(`Added ${favoriteTemplates.length} favorite template buttons to toolbar`);