I suggest to add an additional hook e.g. hook_quicktags_js. ATM modules must add their js globally, regardless the currently viewed page really needs it. But you could simply call module_invoke_all('quicktags_js'); in 'theme_quicktags_textfield' function to allow module to include their js only when quicktags is actually loaded/used.
(Of couse thats only needed for advanced functionality, using quicktags 'location' field)
Three different hooks for a small module seems a bit too much, so maybe you should integrate these into one. Similar to what hook_nodeapi does in core, you could add a 'op' (=insert, alter, js) argument to this callback.
The attached patch introduces hook_quicktags($op, $items) as suggested above and all changes to use it internally in quicktags.module. (It also includes 5.0 update. Sorry, I'm playing with that one. But I can also provide a patch against head directly, if you wish).
That would be a nice improvement for the next quicktags version IMO :)
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | quicktags.module.patch.txt | 2.37 KB | ufku |
| #5 | quicktags_add_js.patch | 681 bytes | profix898 |
| #3 | quicktags47_js_api.patch | 2.27 KB | profix898 |
| #1 | quicktags5_js_api.patch | 4.52 KB | profix898 |
| quicktags_js_api.patch | 4.04 KB | profix898 |
Comments
Comment #1
profix898 commentedLittle fixing/cleanup and rerolled against HEAD.
Comment #2
Bèr Kessels commentedplease try to strip out the Drupal 5 stuff. I cannot review two different things at once.
Comment #3
profix898 commentedThis is a patch without the Drupal 5 stuff.
Comment #4
Bèr Kessels commentedI don't really like the way you combined two entirely different hooks/functions in one hook.
Can we not introduce a single new hook for JS, hook_quicktag_add_js()?
Bèr
Comment #5
profix898 commentedCan you explain what you dont like exactly?
You shouldn't force a module developer to implement three different hooks to just add a new button to the quicktags toolbar. In the worst case one will have to implement three one line functions, because most of the work is done in javascript. For example with the new hook_quicktags_add_js() one will only have
drupal_add_js(drupal_get_path('module', 'mymodule').'/mymodule.js');and of course the 'location' field filled in hook_quicktags_insert(). With all hooks combined into one, every module will have to implement exactly one hook to interact with quicktags. Quicktags is not core and it shouldn't implement too many different hooks for such primitive tasks IMO.However the patch attached simply adds a new hook_quicktags_add_js(), but I'm still not convinced it is the best solution.
Maybe we also shouldnt name it hook_quicktags_add_js, because one could also call drupal_add_css() in the hook's implementation.
Comment #6
ufku commenteddo you have to use drupal_add_js('?q=quicktags') code? if you just print the javascript code inside script tags, then everyone will be able to use drupal_add_js and we will get rid of an extra drupal load caused by ?q=quicktags.
Comment #7
ufku commentedhere is the patch on what i'm talking about.
neither an extra hook nor an extra page call(?q=quicktags)
Comment #8
Bèr Kessels commentedLooks good. I need to test it, or someone else should.
Once tested and confirmed to work, I will commit it.
Comment #9
profix898 commentedThe patch in #7 removes 'extra drupal load caused by ?q=quicktags' to include dynamically generated js. Thats internal quicktags.module cleanup (and its good), but it is nothing in the scope of this issue. It doesnt make a hook to include js from other modules dispensable (still need #5).
Just to make it clear: You can write a module to add new buttons to the quicktags toolbar. If these buttons need extra js to work, you must use drupal_add_js() to add the needed js on every page load (regardless it contains a quicktags bar or not). With the introduction of a new hook_quicktags_add_js the required js would be added only when its really needed!
Same is true for extra css (although its rarely needed). Maybe the hook should be named hook_quicktags_include or stg.
Comment #10
ufku commentedone should use drupal_add_js inside the other hooks(alter and insert). then it will be included only when needed. i think this solves including js issue.
Besides being against js hook, i suggest removing one of the 2 hooks. since the item arrays are merged it is possible to alter a previosuly added item by using insert hook, isnt it? Actually i'm talking about something like drupal menu hook. it allows both inserting new items and altering previously added items. Anyway, this is a new topic of course.
Comment #11
profix898 commentedNo, its not possible. Remember modules are called in a certain order (by weight, by name). What means you can only alter the buttons added by the modules that were called before yours. Thats the reason why we need two hooks (for insert and alter). As in #5 I'd really like to have only one hook_quicktags instead 2 or more ...
When I created this issue I had some problems with adding custom javascript in hook_quicktags_insert(). I will take a look at this again, since I cant remember ATM what these problems were exactly.
Comment #12
ufku commentedwe can first initiate our default items(not by insert hook, no need for own insert hook). then run a single hook (lets say hook_quicktags_items).
Mostly the case for modules is adding new items and/or altering our default items. And this single hook is sufficient for this. Only drawback of this is; a module that wants to alter items of another module(i don't know if there is such a case) should set it's weight high if it comes before the other alphabetically. this is exactly the same situation in menu hook.
and i think we should discuss this in another topic.
Comment #13
Bèr Kessels commentedI am not a big fan of adding a new -private- way to include JS, when we already have one in Drupal.
I see the problem that the Drupal Add_js is not powerfull enough, so we should address that too in an issue against core, surely quicktags is not the only project which misses this power/flexibility in core.
That said, see several solutions, we should look for the best one:
* a hook in quicktags. Downside: extra layer on top of a core layer. Pro: simple and flexible
* a function quicktags_included(), returns TRUE wen we have QTs and FALSE when not. Allows any module to include its own JS with Drupal APis when needed by QT.
* a quicktags JS compile function: It will collect structured arrays with a hook, then turn that into a JS to be inclduded. This could be a starter for a Drupal core JS builder API; Downside: complex.
Comment #14
ufku commentedwith the provided patch i can write for mymodule;
this hook only runs when QT is in action.
so the javascript file is included only when QT is in action.
my button will fire mymoduleQuickTags function that is defined in this js file.
i know my button ID and can change anything about on pae load if needed.
what else may i need?
Comment #15
Bèr Kessels commentedI like it. The explanation in #14 makes it celar to me, what you had in mind.
Therefore I would love it when people can reviw this a little closer. I have put it on y list of things to do after a 5.x release of quicktags (hin: test 5., and we can release it :) )
Comment #16
profix898 commented@Ber: You already had multiple reviews on this in 'Quicktags for Drupal 5 (http://drupal.org/node/88200)' issue. While #8 there was a simple port. The patch reviewed by many users already included this feature as I stated out cleanly! It worked great for everyone. Not sure why you think its still not worth to be committed!
I'm sorry to say that, but actually Quicktags doesnt evolve to the best it could be and to be really usable. It would be easy to implement 'seperators' and 'weights' for buttons etc., but they all require changes you seem not willing to make ...
Comment #17
Bèr Kessels commentedIt is certainly worth it. Its just that I pointed out many times before, that I have a problem with patches that "do many things at once". I hope you understand that maintaining a project requires a lot of work and is a big responsibility. I cannot blindly commit patches that fix bug and slip in features in the same time.
Therefore, I told that this has to wait untill tehre is a stable 5.x release first. Only after such a release will start committing new features.
The work you did for the 5.x upgrade was almost all lost, because the patches included too much changes. I had to do it all over by hand. That is a pity. Lets work on this together: Its not that I don't like your work, I love it. Its just that in the form you present it to me, I cannot really use it. Sorry for that.