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.

Comments

icecreamyou’s picture

This 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.

Zen’s picture

I'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

mr.j’s picture

StatusFileSize
new1.12 KB

Sorry - 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.

icecreamyou’s picture

That doesn't work, namely because hook_menu requires a path like quote/comment/[cid] but the path that exists (on my installation anyway) is comment/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 during hook_link(), which should have worked except that no text gets added to the comment-reply box.

mr.j’s picture

You 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).

icecreamyou’s picture

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

IceCreamYou:::

[quote=D-Rock]

[text]

[/quote]

[text]

...I replaced the actual comment text with [text] there. MySQL 5.0.45, PHP 5.2.6, D5.10, JQuery 1.1.2

I 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.

mr.j’s picture

Yes. 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.

icecreamyou’s picture

Okay, 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.

icecreamyou’s picture

UPDATE: I got it to work! Yay! \o/ :D

This code is also changed to work with Advanced Forum.

/* Instant copying of comment quotes to comment textarea */
var instaQuotes = function() {
  if (!$('textarea#edit-comment').get(0)) return; //not sure this does anything

  $('.forum-comment a.quote').click( function() { //forum-comment instead of just comment to work with Advanced Forum
    // 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);
      tinyMCE.execCommand('mceFocus',false,'edit-comment'); //focuses on the textarea
      tinyMCE.activeEditor.execCommand('mceInsertContent',false,'[quote=' + quote[0] + ']' + quote[1] + '[/quote]<p>'); //adds quote text to textarea
    });
    return false;
  });
}

if (Drupal.jsEnabled) {
  $(instaQuotes);
}

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.

mr.j’s picture

if (!$('textarea#edit-comment').get(0)) return; //not sure this does anything

That 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.

Zen’s picture

Status: Needs work » Active

Please 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.

Flying Drupalist’s picture

Hi, will this work with drupal 6?

mr.j’s picture

I am not running drupal 6, so the best I can suggest is to try it and see.

icecreamyou’s picture

I don't see why it shouldn't, unless the names of the ids have changed. Not so sure about the TinyMCE stuff.

Flying Drupalist’s picture

I 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. :)

icecreamyou’s picture

Apply the patch by hand. It's short.

Flying Drupalist’s picture

StatusFileSize
new8.25 KB

Hmm, 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.

DjC4’s picture

has this been confirmed for 6?

Flying Drupalist’s picture

no, hasn't worked for me.

mpcabd’s picture

I 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/

aharown07’s picture

Has anyone got this working? Have some interest.
Also, what does it do if someone's got javascript disabled?

mr.j’s picture

It 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.

mr.j’s picture

Version: 5.x-1.3 » 5.x-1.4
StatusFileSize
new1.08 KB

I 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.

mr.j’s picture

StatusFileSize
new839 bytes

Patch 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').click to $('.comment li.quote a').click

NOTE: ATTACHED PATCH HAS A BUG. SEE #29 FOR CORRECT PATCH

a_c_m’s picture

Version: 5.x-1.4 » 6.x-1.1-beta2

patch 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.

/* Instant copying of comment quotes to comment textarea */
var instaQuotes = function() {
  if (!$('textarea#edit-comment').get(0)) return; //not sure this does anything

  $('.forum-post-footer li.quote a').click( function() { //forum-comment instead of just comment to work with Advanced Forum
    // 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;
  });
}

var instaReply = function() {
  $('.forum-post-footer li.comment_reply a').click( function() { //forum-comment instead of just comment to work with Advanced Forum
    // Scroll down to comment form
    window.location.hash = '#comment-form';
    
    return false;
  });
}

if (Drupal.jsEnabled) {
  $(instaQuotes);
  $(instaReply);
}
mr.j’s picture

a_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.

a_c_m’s picture

i ended up adding focus yesterday, to the instaQuotes as well.

jcisio’s picture

Exciting! Subscribe to look at it later.

mr.j’s picture

StatusFileSize
new833 bytes

There is a bug in my patch at #24. I did not convert the user_access function call to an array instead. Corrected patch attached.

a_c_m’s picture

Status: Active » Needs review

mr.j - nice work, just found the same bug and came back to post a patch :) only to found you already found and fixed it!

a_c_m’s picture

Currently 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).

michelle’s picture

This 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

resting’s picture

subscribing..anyway, any1 tried the solutions above with 5.x-1.4??

Zen’s picture

Version: 6.x-1.1-beta2 » 6.x-1.x-dev
Status: Needs review » Needs work

My apologies for the delay in my review.

  • The patch does not appear to support node-quoting.
  • Why return stuff as a name-value pair? Why not just return the output of quote_filter() and insert it into the textarea?
  • Instead of regex-ing the cid/nid, it might be simpler to add it to the quote link's markup.
  • Please see: #496910: Support for nodecomment module for some cleaner JS.

Thanks for working on this.
-K

mr.j’s picture

No 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.

emdalton’s picture

I'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?

mr.j’s picture

You 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.

WildBill’s picture

What is instaReply?

yngens’s picture

subscribe

michelle’s picture

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

Zen’s picture

Version: 6.x-1.x-dev » 6.x-2.x-dev
Status: Needs work » Closed (duplicate)

Please move this discussion to #1082332: Multiquote feature and JS support

mr.j’s picture

Issue summary: View changes

Removed out of date example link