On Thursday, August 1, 2013 6:13:25 AM UTC-7, Clemens Feige wrote: > Hi. > > How can I add custom buttons to the wiki toolbar which appears above the > text area when editing tickets and wiki pages? > > Per default it offers "bold", "italic", "headline" etc. I would like to > add some own buttons which simply insert some text snippets e.g. > [[PageOutline]] to insert a "table of contents". > > Would this be Python related (client contacts the TRAC-werserver) or is > this a Java-Script thing or something else? I looked in the mailing list > archive, in Google and on Trac-Hacks but could not find anything. >
The functionality is defined in wikitoolbar.js, (1) and should be fairly easy to extend. I'm minimally proficient in JavaScript and jQuery, so I'll suggest one way it could probably be done, but there may be better ways. In fact, someone might know of a way we could modify Trac so that it is easier to add buttons. 1. Put the JavaScript code at the of this message in $env/site/wikitoolbar_extensions.js 2. Put the Python code at the end of this message in $env/plugins/wikitoolbar_extensions.py You should see an additional entry on the next line, and it uses an "italics" icon. This is probably not what you want, so there are some additional steps to do: 1. Edit the toolbar image to add additional items (2) 2. Add some CSS so that you new toolbar image is utilized 3. Add the new CSS file through WikiToolbarExtensions.post_process_request 4. (optional) package the plugin to install using setuptool / distribute (1) http://trac.edgewall.org/browser/tags/trac-1.0.1/trac/htdocs/js/wikitoolbar.js (2) http://trac.edgewall.org/browser/tags/trac-1.0.1/trac/htdocs/edit_toolbar.png ---- (function($) { window.extendWikiFormattingToolbar = function(textarea) { if ((document.selection == undefined) && (textarea.setSelectionRange == undefined)) { return; } var toolbar = $("div.wikitoolbar").get(0); function addButton(id, title, fn) { var a = document.createElement("a"); a.href = "#"; a.id = id; a.title = title; a.onclick = function() { try { fn() } catch (e) { } return false }; a.tabIndex = 400; toolbar.appendChild(a); } function encloseSelection(prefix, suffix) { textarea.focus(); var start, end, sel, scrollPos, subst; if (document.selection != undefined) { sel = document.selection.createRange().text; } else if (textarea.setSelectionRange != undefined) { start = textarea.selectionStart; end = textarea.selectionEnd; scrollPos = textarea.scrollTop; sel = textarea.value.substring(start, end); } if (sel.match(/ $/)) { // exclude ending space char, if any sel = sel.substring(0, sel.length - 1); suffix = suffix + " "; } subst = prefix + sel + suffix; if (document.selection != undefined) { var range = document.selection.createRange().text = subst; textarea.caretPos -= suffix.length; } else if (textarea.setSelectionRange != undefined) { textarea.value = textarea.value.substring(0, start) + subst + textarea.value.substring(end); if (sel) { textarea.setSelectionRange(start + subst.length, start + subst.length); } else { textarea.setSelectionRange(start + prefix.length, start + prefix.length); } textarea.scrollTop = scrollPos; } } addButton("pageoutline", _("Page Outline"), function() { encloseSelection("[[PageOutline(", ")]"); }); } })(jQuery); jQuery(document).ready(function($) { $("textarea.wikitext").each(function() { extendWikiFormattingToolbar(this) }); }); ---- # -*- coding: utf-8 -*- from trac.core import Component, implements from trac.web.api import IRequestFilter from trac.web.chrome import add_script class WikiToolbarExtensions(Component): implements(IRequestFilter) def pre_process_request(self, req, handler): return handler def post_process_request(self, req, template, data, content_type): if 'scripts' in req.chrome and req.chrome['scripts']: for item in req.chrome['scripts']: if item['href'].endswith('/common/js/wikitoolbar.js'): add_script(req, 'site/wikitoolbar_extensions.js') break return template, data, content_type -- You received this message because you are subscribed to the Google Groups "Trac Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/trac-users. For more options, visit https://groups.google.com/groups/opt_out.
