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

Content deleted Content added
No edit summary
No edit summary
 
(9 intermediate revisions by the same user not shown)
Line 139:
if (favoriteTemplates && favoriteTemplates.length > 0) {
// AddFirst, add the Templates toolbar section
$textarea.wikiEditor( 'addToToolbar', {
sections: {
Line 149:
} );
// AddCreate athe grouptools object for favoritethe templatesbuttons
$textarea.wikiEditor(const 'addToToolbar',templateTools = {};
section: 'favorite-templates',
groups: {
favorites: {
label: 'My Favorites'
}
}
} );
// Add buttons for each favorite template as a tool
for (const template of favoriteTemplates) {
const buttonId = createButtonId(template.title);
Line 165 ⟶ 158:
const displayTitle = template.title.replace(/^Template:/, '');
$textarea.wikiEditor(templateTools[buttonId] 'addToToolbar',= {
sectiontype: 'favorite-templateselement',
groupelement: $('favorites<button>',)
tools: { .addClass('tool tool-button')
[buttonId]: {.text(displayTitle)
label: displayTitle,.css({
type'padding': 'button4px 8px',
action'margin': {'2px',
type'border': 'encapsulate1px solid #a2a9b1',
options'border-radius': {'2px',
pre'background': templateSyntax'#f8f9fa',
'cursor': 'pointer',
'font-size': '11px',
'font-family': 'sans-serif'
})
.hover(
function() { $(this).css('background', '#eaecf0'); },
function() { $(this).css('background', '#f8f9fa'); }
)
.click((function(syntax) {
return function(e) {
e.preventDefault();
// Get the textarea
const textarea = $('#wpTextbox1')[0];
if (textarea) {
// Insert the template syntax at cursor position
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const text = textarea.value;
textarea.value = text.substring(0, start) + syntax + text.substring(end);
// Move cursor to end of inserted text
textarea.selectionStart = textarea.selectionEnd = start + syntax.length;
textarea.focus();
}
};
})(templateSyntax))
};
} );
}
// Then add the group with all tools
$textarea.wikiEditor( 'addToToolbar', {
section: 'favorite-templates',
groups: {
'favorites': {
labeltools: 'My Favorites'templateTools
}
}
} );
console.log(`Added ${favoriteTemplates.length} favorite template buttons to toolbar`);