Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Trac: Users

add custom buttons to wiki toolbar

 

 

Trac users RSS feed   Index | Next | Previous | View Threaded


c.feige at osypkamed

Aug 1, 2013, 6:13 AM

Post #1 of 4 (44 views)
Permalink
add custom buttons to wiki toolbar

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.

Thanks
Clemens

--
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 trac-users+unsubscribe [at] googlegroups
To post to this group, send email to trac-users [at] googlegroups
Visit this group at http://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/groups/opt_out.


rjollos at gmail

Aug 5, 2013, 10:55 AM

Post #2 of 4 (31 views)
Permalink
Re: add custom buttons to wiki toolbar [In reply to]

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 trac-users+unsubscribe [at] googlegroups
To post to this group, send email to trac-users [at] googlegroups
Visit this group at http://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/groups/opt_out.


c.feige at osypkamed

Aug 6, 2013, 2:29 AM

Post #3 of 4 (30 views)
Permalink
Re: Re: add custom buttons to wiki toolbar [In reply to]

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?
>> [...]
>>


RjOllos, 05.08.2013 19:55:
> 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.

Hello RjOllos.

Thank for pointing me to wikitoolbar.js and thanks for your help. It
seems this is indeed mostly a Java-Script and CSS issue. As far as I
understood you have cloned the original code from wikitoolbar.js. The
trick is to call "addButton()" another time. Additionally you proposed
to wrap everything as a TRAC plugin, which is a good idea. Thanks.

> 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

I did as you said and it works on my TRAC 0.12.2. Thanks.
However I needed to put "wikitoolbar_extensions.js" in the $env/htdocs
directory instead $env/site.

> 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:

Yes, you are right. I see only the "italics" icon, because this is the
first one in the "edit_toolbar.png" background image. As you said, I
need to work a little with CSS to get the cosmetics nice looking.

Thanks
Clemens

--
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 trac-users+unsubscribe [at] googlegroups
To post to this group, send email to trac-users [at] googlegroups
Visit this group at http://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/groups/opt_out.


rjollos at gmail

Aug 6, 2013, 8:06 AM

Post #4 of 4 (30 views)
Permalink
Re: Re: add custom buttons to wiki toolbar [In reply to]

On Tuesday, August 6, 2013 2:29:05 AM UTC-7, Clemens Feige wrote:
>
> 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?
> >> [...]
> >>
>
>
> RjOllos, 05.08.2013 19:55:
> > 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.
>
> Hello RjOllos.
>
> Thank for pointing me to wikitoolbar.js and thanks for your help. It
> seems this is indeed mostly a Java-Script and CSS issue. As far as I
> understood you have cloned the original code from wikitoolbar.js.


Yeah I copied and then modified wikitoolbar.js. I don't see a way to reuse
the functions in that file without copying them, but someone more fluent in
JS may have an idea, or perhaps we can modify the source code distributed
with Trac to allow for easier reuse of those functions.


> The
> trick is to call "addButton()" another time. Additionally you proposed
> to wrap everything as a TRAC plugin, which is a good idea. Thanks.
>
> > 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
>
> I did as you said and it works on my TRAC 0.12.2. Thanks.
> However I needed to put "wikitoolbar_extensions.js" in the $env/htdocs
> directory instead $env/site.
>

That's correct. Sorry for that typo.


> > 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:
>
> Yes, you are right. I see only the "italics" icon, because this is the
> first one in the "edit_toolbar.png" background image. As you said, I
> need to work a little with CSS to get the cosmetics nice looking.
>
>
Let us know how it goes for you :)

--
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 trac-users+unsubscribe [at] googlegroups
To post to this group, send email to trac-users [at] googlegroups
Visit this group at http://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/groups/opt_out.

Trac users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.