Tag callbacks

84 views
Skip to first unread message

Will S

unread,
Oct 24, 2011, 1:25:03 PM10/24/11
to zotero-dev
I was interested in writing a plugin for Zotero that would parse tags
when they are added to items (basically, I wanted to search each new
tag for semi-colons and if any are found that tag is removed and
replaced with tags representing the text between semi-colons. So, the
tag "physics;experiment;precision measurement" would be replaced by
the tags "physics", "experiment", and "precision measurement").

However, when looking through the Zotero code, I discovered that this
might be harder to do than a I realized because the methods that
manipulate tags and trigger a notification are Item.removeAllTags()
and Item.replaceTag(). Is there a reason that Item.addTag and
Item.addTags do not trigger notifications? I am not very experienced
with JavaScript or the Zotero code so I could be overlooking
something. Is there a way to grab the ids of the item and tag when a
tag is added to an item?

Dan Stillman

unread,
Oct 24, 2011, 1:36:53 PM10/24/11
to zoter...@googlegroups.com

Can we step back a bit? What's your motivation behind this plugin? If
your goal is to fix bad data from site translators, then the site
translators should just be fixed. If your goal is to allow tags in the
UI to be entered with a delimiter, then either the plugin should enable
that or you should submit a patch to Zotero core that does so. Fixing
this post-save isn't a very good solution.

Will S

unread,
Oct 24, 2011, 2:16:10 PM10/24/11
to zotero-dev
On Oct 24, 1:36 pm, Dan Stillman <dstill...@zotero.org> wrote:
> Can we step back a bit? What's your motivation behind this plugin? If
> your goal is to fix bad data from site translators, then the site
> translators should just be fixed. If your goal is to allow tags in the
> UI to be entered with a delimiter, then either the plugin should enable
> that or you should submit a patch to Zotero core that does so. Fixing
> this post-save isn't a very good solution.

My goal is the latter (to allow tags in the UI to be entered with a
delimiter). Is there any easy way for a plugin to enable this pre-
save? Would that require setting up a callback that would catch new
tags before they were saved? I am guessing that this might not be
possible with the current code. For submitting a patch to Zotero
core, you are suggesting that I modify the Zotero code itself to parse
new tags and submit the modified files? Do you know of an easy way of
patching the code to allow for this? It would probably not be too
difficult to hard code a check for semi-colons into Zotero core, but
that might mess up people who use semi-colons in there tags. A less
invasive solution would be to patch Zotero core so that it is possible
to trigger a callback between a new tag being entered and that tag
being saved, but that might be difficult to do?

Dan Stillman

unread,
Oct 24, 2011, 2:30:19 PM10/24/11
to zoter...@googlegroups.com

We don't have callbacks like that, so that's not really an option.

But to step back yet again, why do you want to be able to enter tags
with a delimiter? Why does the delimiter have to be a semicolon and not,
say, a newline, so that it split up multiple tags pasted in (which seems
like the only reason this should be necessary)?

Will S

unread,
Oct 24, 2011, 3:17:31 PM10/24/11
to zotero-dev
On Oct 24, 2:30 pm, Dan Stillman <dstill...@zotero.org> wrote:
> We don't have callbacks like that, so that's not really an option.
>
> But to step back yet again, why do you want to be able to enter tags
> with a delimiter? Why does the delimiter have to be a semicolon and not,
> say, a newline, so that it split up multiple tags pasted in (which seems
> like the only reason this should be necessary)?

Yeah, a newline would be okay. With a newline, you would have to type
out the tags in a separate window and then copy them rather than type
them into the little tag entry box and copy them, but that's not a big
deal. Basically, I like to put a lot of descriptive tags on the
entries in my library and there is a lot overlap between entries so
it's annoying to type them all in over and over. I'd also like to add
a way to grab all of the tags on an item (to the clipboard if
possible) so that they can be pasted to another item. I think that
this should not be too hard to do with a plugin. Part of the
annoyance with adding the tags is that there is a half-second to
second pause after I hit enter each time I add a tag to an entry. I
think that this is because my library is pretty big. It'd still be
good to be able to add delimited tags even if I didn't have this pause.

Tom Roche

unread,
Oct 24, 2011, 4:14:26 PM10/24/11
to zoter...@googlegroups.com

Dan Stillman Oct 24, 2:30pm

>> why do you want to be able to enter tags with a delimiter? Why does
>> the delimiter have to be a semicolon and not, say, a newline, so
>> that it split up multiple tags pasted in (which seems like the only
>> reason this should be necessary)?

Will S Mon, Oct 24, 2011 at 3:17 PM


> With a newline, you would have to type out the tags in a separate
> window and then copy them rather than type them into the little tag
> entry box and copy them,

That would WFM: I'd rather do the tags in an editor and then cut'n'paste, since I also

> like to put a lot of descriptive tags on the entries in
> my library and there is a lot overlap between entries so it's
> annoying to type them all in over and over. I'd also like to add a
> way to grab all of the tags on an item (to the clipboard if
> possible) so that they can be pasted to another item.

+1

> Part of the annoyance with adding the tags is that there is a
> half-second to second pause after I hit enter each time I add a tag
> to an entry.

Ditto.

FWIW, Tom Roche <Tom_...@pobox.com>

Dan Stillman

unread,
Oct 24, 2011, 5:59:48 PM10/24/11
to zoter...@googlegroups.com
On 10/24/11 3:17 PM, Will S wrote:
> On Oct 24, 2:30 pm, Dan Stillman<dstill...@zotero.org> wrote:
>> We don't have callbacks like that, so that's not really an option.
>>
>> But to step back yet again, why do you want to be able to enter tags
>> with a delimiter? Why does the delimiter have to be a semicolon and not,
>> say, a newline, so that it split up multiple tags pasted in (which seems
>> like the only reason this should be necessary)?
> Yeah, a newline would be okay. With a newline, you would have to type
> out the tags in a separate window and then copy them rather than type
> them into the little tag entry box and copy them, but that's not a big
> deal. Basically, I like to put a lot of descriptive tags on the
> entries in my library and there is a lot overlap between entries so
> it's annoying to type them all in over and over.

So I'd recommend submitting a patch that will use newlines as delimiters
in tags pasted into the right-hand pane.

> I'd also like to add
> a way to grab all of the tags on an item (to the clipboard if
> possible) so that they can be pasted to another item. I think that
> this should not be too hard to do with a plugin.

This could be done, but we'd need a UI for it. (The nice part of the
above change is that it could be done without any UI changes.)

> Part of the
> annoyance with adding the tags is that there is a half-second to
> second pause after I hit enter each time I add a tag to an entry. I
> think that this is because my library is pretty big. It'd still be
> good to be able to add delimited tags even if I didn't have this pause.

Right, I figured that was part of the motivation, but that alone isn't
reason enough for a delimiter (and is the reason I'm concerned only with
handling copy/paste). That just means tag adding needs to be optimized
further. In the meantime, note that you may get faster adds with the tag
selector closed. 3.0b might also speed this up over 2.1.10.

Will S

unread,
Oct 25, 2011, 2:57:32 PM10/25/11
to zotero-dev
On Oct 24, 5:59 pm, Dan Stillman <dstill...@zotero.org> wrote:
> So I'd recommend submitting a patch that will use newlines as delimiters
> in tags pasted into the right-hand pane.

I have looked into this some, and it is a little trickier than I
realized at first. I thought I could just add a check for newlines to
the addTag method and then call split() if any were found. However,
it seems that the textbox in the tags tab replaces newlines with
spaces as soon as they are entered. I am trying to look up
documentation on this textbox (created with
document.createElement("textbox") in the showEditor method of content/
zotero/bindings/tagsbox.xml) to see if there is a property that could
be set so that these newlines could be caught, but I have not found
anything so far. Does anyone know where to look for this?

> This could be done, but we'd need a UI for it. (The nice part of the
> above change is that it could be done without any UI changes.)

I think this is easy to do with a plugin so the core Zotero UI
shouldn't have to be touched for this.

> Right, I figured that was part of the motivation, but that alone isn't
> reason enough for a delimiter (and is the reason I'm concerned only with
> handling copy/paste). That just means tag adding needs to be optimized
> further. In the meantime, note that you may get faster adds with the tag
> selector closed. 3.0b might also speed this up over 2.1.10.

I have tried closing the tag selector -- I will try using 3.0b soon.
Thanks for the suggestion.

Will S

unread,
Nov 2, 2011, 10:57:25 PM11/2/11
to zotero-dev
So I am still stuck on this issue:

"it seems that the textbox in the tags tab replaces newlines with
spaces as soon as they are entered."

Any ideas on how to get around this so that newline could be used as a
delimiter?

Simon

unread,
Nov 3, 2011, 1:51:37 AM11/3/11
to zotero-dev
See the documentation for the XUL textbox element (https://
developer.mozilla.org/en/XUL/textbox). In particular, you probably
need to specify to add a newlines attribute and/or multiline attribute
to the element.

Simon

Will S

unread,
Nov 3, 2011, 10:16:48 PM11/3/11
to zotero-dev
Thanks, Simon. I think that is exactly what I was looking for. I
tried looking in the Zotero code and the documentation for HTML and
JavaScript, but I didn't realize that this was actually an XUL
question.

adamsmith

unread,
Nov 3, 2011, 11:52:38 PM11/3/11
to zotero-dev
this may be demanding, but I'd give it a try:
While you're at it, would you consider doing this for authors, too?
People have been requesting this:
See item 14 here:
http://forums.zotero.org/discussion/19556/papercuts

Will S

unread,
Nov 7, 2011, 5:14:43 PM11/7/11
to zotero-dev
I just submitted a patch for tags. Making one for authors should not
be too bad. I think it will be a little harder than the one for tags
because the item pane is more complex than the tags pane. Authors
might be slightly more complex than tags too because of the first/last
name issue. I will look into it (but first I want to try to make an
extension that lets you right click an item and add all of its tags to
the clipboard as a string delimited by newlines).

Will S

unread,
Dec 6, 2011, 6:55:47 PM12/6/11
to zotero-dev
Okay, so I now have a patch that allows you enter multiple creators
and multiple tags at once. You can see it at:

https://github.com/willsALMANJ/zotero

To enter multiple tags, you just paste a string of tags separated by
newlines (\n, \r\n, or \r) and they are all added to the item.

Entering multiple creators is similar but a little more complicated.
You can enter multiple creators by pasting a string of creator names
separated by newlines (\n, \r\n, or \r) into the author name field
(either the name field in full name mode or the last name in "last
name, first name" mode). Creator names can either be entered as a
single name or as the last name and the first name separated by \t
(tab).

There is one other subtle difference between entering tags and
creators. For tags, if you select an existing tag and paste the new
tags into the textbox, that existing tag is overwritten by the newly
entered tags. For creators, if you select an existing creator and
paste a list of creators into the textbox, the new creators are all
inserted into the creator list before that selected creator and no
creators are overwritten. This difference between tags and creators
came about because tags are always resorted to be alphabetical whereas
creator lists have a definite order.

If anyone has an opinion about any of the above, could you please let
me know? I will submit a pull request soon, but I imagine it won't
get looked at right away, so I would like to make sure that the most
desirable version of the patch is submitted the first time.

Also, I created a small plugin for Zotero that adds two options to the
popup menu that appears when you right-click on an item in Zotero:
"Copy creators to clipboard" and "Copy tags to clipboard". Selecting
these items copies the creators/tags of the selected item to the
clipboard in the appropriate format for pasting into another item.
The plugin can be downloaded here:

https://github.com/willsALMANJ/Zutilo

Will S

unread,
Dec 27, 2011, 3:10:49 PM12/27/11
to zotero-dev
I just submitted a new pull request for adding multiple tags and
creators by pasting a newline delimited list into the textbox here:

https://github.com/zotero/zotero/pull/20

I think it would be classified as a "papercut" but I couldn't figure
out how to add that as a tag to it (I have seen other pull requests
tagged as papercuts...).

For creators, names can be entered as lastnname tab firstname or just
as a full name. When pasting a list over an existing entry, that
existing entry is not overwritten. In the case of creators, the list
is inserted in front of the existing entry. I did it this way because
it would otherwise be hard to insert a list (you would have to put the
entry being overwritten into the pasted list in order to keep it) and
the order is important for creator lists. I made an existing tag also
not be overwritten just to be consistent with the way the creators
work (though order does not matter for tags since Zotero sorts them
alphabetically).

There are three relevant commits. In commit e10d049428, adding
multiple tags/creators works basically the same way adding a tag/
creator works currently in Zotero. If you paste a list of tags over
an existing tag, the tags are added but no entry is selected after you
hit enter. In commit 87c47c0615, I changed this so that if you add
multiple tags over an existing tag, the tag after that existing tag is
selected after you hit enter (or the tag before if you do shift
+enter). For commit 3ef0b63c47, I changed the tagsbox so that if you
edit an existing tag (i.e. not pasting a multiple tag list over it)
and hit enter/shift+enter, the next/previous tag is selected instead
of no tag selected. This behavior seems intuitive and preferable to
me. Sometimes when I am entering tags, I notice a typo and shift+tab
back and fix it. It is annoying that I then have to move my hand from
the keyboard to the mouse and click on the tagsbox again to re-select
it.

I wanted to change one of other thing but could not figure out how to
do it. Right now, when you edit a tag and then click on another one,
you end up with nothing selected and have to click on the tag a second
time to select it. I played around and determined that when you click
on the tag, Zotero runs the blurHandler (hideEditor) on the currently
selected tag, reloads the tagsbox twice, and then runs the
clickHandler (showEditor) on the tag label that was clicked on. This
behavior is almost the same as what is done when you hit enter on an
edited tag (with my patch described above) so I am not sure why the
textbox does not appear on the tag that is clicked on. From what I
can tell, everything works correctly up to this line in showEditor:

box.replaceChild(t, elem);

It seems like box, t, and elem are all the correct objects...but when
this line executes nothing happens (the textbox t does not replace the
label elem). Anyone have any idea why this would not replace the
label with the textbox as normal? No error appears to be generated.

Other than hopefully fixing the tag-clicking behavior, I think I am
done with patching tag/creator entry. The next thing I want to work
on is making little changes to attachments. I think I can do this
with a plugin, but if not I will ask about patch. The first I want to
do is make a function that can change the beginning of a directory
path to attachments to a different path. Ever since I changed
computers, all of my attachment links have been broken. If anyone has
any thoughts about this, please let me know.

adamsmith

unread,
Dec 28, 2011, 10:03:35 AM12/28/11
to zotero-dev
awesome - hope we can get this in soon.
The papercut tags can only be added by repo owners - the existing tags
weren't created by the people who issued the pull request but later by
developers (or the people issuing the pull requests had a higher level
of rights on the repo).
Reply all
Reply to author
Forward
0 new messages