I have put together a small ajax enhancement for sending quotes directly to the comment box if the comment box is on the same page as the quote link. This also means you can quote multiple posts in one go by just clicking on different comments' quote links.
It requires a tiny patch to quote.module to set up the ajax callback, and a javascript function which attaches the required functionality to any link marked with a class of "quote" that is contained within a comment. I have the javascript in a utility file that I load for my site via my page template, but it could be added as an include file with this module if the maintainer wants to add this patch to the codebase.
/* Instant copying of comment quotes to comment textarea */
var instaQuotes = function() {
if (!$('textarea#edit-comment').get(0)) return;
$('.comment a.quote').click( function() {
// Extract comment id from link
var cid = /\/comment\/reply\/\d+\/(\d+)/.exec(this.href)[1];
// Scroll down to comment form
window.location.hash = '#comment-form';
// Get comment via ajax then append to new comment textarea
$.get('/quote/comment/' + cid, null, function(data) {
var quote = data.split(':::', 2);
var textarea = $('textarea#edit-comment').get(0);
textarea.value += ('[quote=' + quote[0] + ']' + quote[1] + '[/quote]\n\n');
textarea.scrollTop = textarea.scrollHeight; // scrolls to bottom of textarea
});
return false;
});
}
if (Drupal.jsEnabled) {
$(instaQuotes);
}
It uses ajax because it is the easiest way to get a clean copy of the comment. I tried to extract it from the html source at first so no server interaction would be required but it became an ugly mess of code to replace P and BR tags with line-break characters and stripping of other html tags. This way is better as it preserves all original formatting within the quotes.
Caveat: I have upgraded jQuery from drupal's default to the latest (1.2.3?), but there is nothing too fancy in there so it should still work with the older jQuery shipping with drupal.
I've tested it on Safari and Firefox on a Mac, and IE6 & 7 on windows xp.
I'll leave it up to the maintainer on whether they want to add this to the module. For anyone else, the code is here if you want it.
| Comment | File | Size | Author |
|---|---|---|---|
| #24 | quote.instaquotes.patch | 839 bytes | mr.j |
| #29 | quote.instaquotes.patch | 833 bytes | mr.j |
| #23 | instaquotes.patch | 1.08 KB | mr.j |
| #17 | quote.txt | 8.25 KB | Flying Drupalist |
| #3 | instaquotes.patch | 1.12 KB | mr.j |
Comments
Comment #1
icecreamyou commentedThis doesn't work. Clicking the quote link makes the page jump to the edit form as expected, but nothing is inserted into it.
I know next to nothing about Javascript or JQuery, but I *think* the problem is that the part where the text is added to the textarea is wrong, not the text itself. Unfortunately I have no idea whatsoever how to fix this.
Comment #2
Zen commentedI've only looked at the demo. But it could well be that the problem IceCreamYou is facing is due to the jQuery version discrepancy. Either way, this looks handy. Could you please roll this up into a proper patch against a standard D5 install (i.e. not requiring the query upgrade)?
Thanks mr. j.
-K
Comment #3
mr.j commentedSorry - looking back at this I realise that I neglected to attach the patch to the quote module that would enable the ajax to work. That is why the demo I linked to works, but if you tried to do this yourself you would not get any text being placed in the comment box.
So I have attached the patch I originally created to quote.module. If it doesn't patch against current code it should be straightforward to port as it only inserts lines into the module.
I am 99% sure that the jQuery version will not affect whether this works on a vanilla 5.x install as it only does a simple get request. Right now I don't have the time to test it unfortunately.
Comment #4
icecreamyou commentedThat doesn't work, namely because hook_menu requires a path like
quote/comment/[cid]but the path that exists (on my installation anyway) iscomment/reply/[nid]/[cid]?quote=1#comment-form.But I still think there's a problem with the JQuery, and I don't think it has anything to do with being a different version. I had been using
drupal_add_js()to stick the JS in duringhook_link(), which should have worked except that no text gets added to the comment-reply box.Comment #5
mr.j commentedYou must patch the quote module using the patch above. It adds a new callback handler (quote/comment/cid) that is called by the javascript get request.
The javascript provided attaches to any link on the page matching the regular expression used, which looks for links of the sort you specified:
comment/reply/[nid]/[cid]If you are seeing the page scroll down to the comment box but no text going into the box, then the javascript has attached to the links which indicates it is working, so you must have a problem with the get request not returning. Make sure you have applied the patch to the quote module correctly. An easy way to test if the get request is working is to just manually go to
http://yoursite/quote/comment/[cid](using a valid cid).Comment #6
icecreamyou commentedI applied the patch correctly, and it doesn't work. Going to
/quote/comment/[cid]gives a blank page (i.e. it doesn't look like it's part of the site) with this on it:...I replaced the actual comment text with
[text]there. MySQL 5.0.45, PHP 5.2.6, D5.10, JQuery 1.1.2I think this is because there's no reference to quote.js - unless there never was supposed to be, in which case I'm completely missing where the JS is supposed to be.
Comment #7
mr.j commentedYes. You have applied the patch correctly and the ajax callback is working exactly as it should.
It just returns a piece of text which the javascript parses. It is meant to happen "behind the scenes" so does not return a full page. The text is formatted like [user]:::[text]
The javascript will call the callback when you click a quote link then split the returned text and insert it into the text area using a quote tag.
You must have that javascript that I posted at the top of this page on your comment page.
As I said, I have a utility file which gets included as part of my theme on my page.tpl.php. You can do the same thing to test this.
If this gets rolled into the module then I assume the module author would include the js as part of a quote.js file or something.
Comment #8
icecreamyou commentedOkay, I discovered what's up - it works on plain textareas but not with TinyMCE (or, I assume, any other texteditor) because TinyMCE sets the standard textarea to
display: none;and inserts itself in an iframe. I know that there is a way to add things into TinyMCE via javascript, but I have absolutely no idea how to do it.Comment #9
icecreamyou commentedUPDATE: I got it to work! Yay! \o/ :D
This code is also changed to work with Advanced Forum.
I'm so excited!
...so, if the module maintainer wishes to add this code, I recommend some kind of detection to see if a WYSIWYG is installed so that the JS can be altered appropriately. I also recommend creating a setting that enables/disables this feature.
Comment #10
mr.j commentedif (!$('textarea#edit-comment').get(0)) return; //not sure this does anythingThat just exits immediately if the comment textarea is not found on the page, as I have it included in a script that is found on every one of my pages.
Comment #11
Zen commentedPlease note that there's a parallel patch here which might be of interest. I've redirected it here, but, it might be worth debating the pros and cons of quoting highlighted text vs. this ajax implementation or both.
Thanks.
Comment #12
Flying Drupalist commentedHi, will this work with drupal 6?
Comment #13
mr.j commentedI am not running drupal 6, so the best I can suggest is to try it and see.
Comment #14
icecreamyou commentedI don't see why it shouldn't, unless the names of the ids have changed. Not so sure about the TinyMCE stuff.
Comment #15
Flying Drupalist commentedI understand javascript is the same, but I feel a bit ichky about running a d5 patch on a d6 module. I'll try it regardless and let you know. :)
Comment #16
icecreamyou commentedApply the patch by hand. It's short.
Comment #17
Flying Drupalist commentedHmm, wonder if I'm doing it wrong. I manually applied the patch on #3 and used the js mr.j posted.
There seems to be no effect. The quote link still loads a new page. I'm using BUEditor, but I don't think that should affect anything.
If anyone wants to take a gander I uploaded the module file here.
Comment #18
DjC4 commentedhas this been confirmed for 6?
Comment #19
Flying Drupalist commentedno, hasn't worked for me.
Comment #20
mpcabd commentedI have created a small script to do that and I used it in this website (http://www.csc-sy.net).
Here is the blog that talks about the issue: http://mpcabd.igeex.biz/simple-ajax-quote-in-drupal-6/
Comment #21
aharown07 commentedHas anyone got this working? Have some interest.
Also, what does it do if someone's got javascript disabled?
Comment #22
mr.j commentedIt still works fine on D5 and I still don't use D6. I would have thought it would be trivial to port.
If javascript is disabled the quote links are not modified so the regular quote on a new page functionality works.
Comment #23
mr.j commentedI didn't realise there is a newer release on 5.x. The attached patch is for 5.x-1.4.
You still need the javascript posted at the top of this thread as well.
Comment #24
mr.j commentedPatch against 6.x-1.1-beta2 module attached (btw why is this module still beta after more than 1 year?).
Javascript is identical to my original post except you need to change the link selector
from
$('.comment a.quote').clickto$('.comment li.quote a').clickNOTE: ATTACHED PATCH HAS A BUG. SEE #29 FOR CORRECT PATCH
Comment #25
a_c_m commentedpatch in #24 works well, but be advised you still need the JS in the OP.
Also note, you may need to customize the JS to fit your use case (e.g. #9 expects tinyMCE but OP expects standard text area).
Would be great to see this integrated, we also added instaReply as well, here is our JS.
Comment #26
mr.j commenteda_c_m, great minds, we've had instaReply for a while too.
You can also just call focus() on the textarea and the browsers will scroll down to it.
Comment #27
a_c_m commentedi ended up adding focus yesterday, to the instaQuotes as well.
Comment #28
jcisio commentedExciting! Subscribe to look at it later.
Comment #29
mr.j commentedThere is a bug in my patch at #24. I did not convert the user_access function call to an array instead. Corrected patch attached.
Comment #30
a_c_m commentedmr.j - nice work, just found the same bug and came back to post a patch :) only to found you already found and fixed it!
Comment #31
a_c_m commentedCurrently working on a version that would also allow you to quote form the parent node.
Otherwise the reply/quote links will behave differently for the first post (node) in a thread vs the rest (comments).
Comment #32
michelleThis seems to be the only active issue... If you two are working on the module, care to chime in on #851240: Quote maintainership ? I would prefer to not take it over if there are other more competent in js people working on it.
Michelle
Comment #33
resting commentedsubscribing..anyway, any1 tried the solutions above with 5.x-1.4??
Comment #34
Zen commentedMy apologies for the delay in my review.
Thanks for working on this.
-K
Comment #35
mr.j commentedNo problem:
1. You are right. I didn't know quote.module supported node quoting. Maybe it didn't at the time I wrote the patch.
2. Can't remember, I wrote it 2 years ago. But I can think of a few reasons. I assume that instead of preserving the [ quote ] tags, quote_filter will replace the quote tags with a div (or whatever), which means if the quote filter ever changes the html tags it outputs later on all the old quotes will not be updated because they will have div tags hard-coded in the comments. Also maybe the site's standard input filter won't permit the user to enter a div. The idea is to preserve the same formatting as quote.module would use in non-ajax mode.
3. Possibly. I was new to jquery then and this was just meant to sit on top and degrade back to standard quote.module if the javascript didn't load or failed for whatever reason.
4. I took a look at the other patch and it seems very verbose. Just reading it, it looks like it sends down the contents of every comment on the page in javascript form with the page html just in case the user clicks a quote link, which seems wasteful to me considering you could just load the relevant one via ajax only when needed. I won't be doing any more on this as it has been working fine for our purposes for 2+ years. Feel free to use the code however you like.
Comment #36
emdalton commentedI've applied the patch, but I'm having trouble figuring out where to put the javascript in #25. I tried putting it in quote.inc in the module directory, and disabled and re-enabled the module, but that didn't seem to pick it up. Is there a recommended place to put this sort of script?
Comment #37
mr.j commentedYou need to add it manually to your page template as it was never added to the module.
Just put it in a js file in your theme directory and reference it in your page template.
Comment #38
WildBill commentedWhat is instaReply?
Comment #39
yngens commentedsubscribe
Comment #40
michelleFYI: There is a module for multi-quote now. I've asked if he could join forces but it doesn't look likely.
http://drupal.org/project/multiquote
Michelle
Comment #41
Zen commentedPlease move this discussion to #1082332: Multiquote feature and JS support
Comment #41.0
mr.j commentedRemoved out of date example link