View Full Version : Insert text into editor
GameWizard
05-16-2008, 01:48 PM
I never imagined something like this would be so complicated, but I'm looking for a way to add text into the editor (both wysiwyg and standard editors) when clicking a link or button.
Boofo
05-16-2008, 02:25 PM
In the postbit or where?
GameWizard
05-16-2008, 03:04 PM
Into the editor when writing a post. Namely for the "newthread" template. I'd like to create a button which would populate/insert text into the textarea of the editor when clicked. Similarly to how you press a Smilie and it appears inside the textarea.
Boofo
05-16-2008, 03:19 PM
That will take a hack like they use here for the Unlicensed User messages. One hasn't been released yet that I know of.
GameWizard
05-16-2008, 03:23 PM
I'm well aware that nothing has been released, otherwise I would of already of tried it. :p I am trying to create one. My understanding is that it would take an OnClick event based on a javascript function to accomplish this. But my javascript coding skills are very poor so I hope someone out there can help.
Lynne
05-16-2008, 03:30 PM
There is kinda a hack like this, but it will effect all newthreads in a forum. It's call the Stencil Hack (I think). You may want to check it out and see what they do and then modify it for what you want.
Boofo
05-16-2008, 03:31 PM
The javascript is only the small part of it.
GameWizard
05-16-2008, 03:41 PM
This is the hack in question:
https://vborg.vbsupport.ru/showthread.php?t=130162
Although not exactly what I need, I will certainly try it out and see if this will work for my purposes.
The javascript is only the small part of it.I am not looking for miracles, but in fact the javascript is all I need. I have already tested some javascript code that I found myself and managed to get a partially working version. Problem is that my lack of javascript coding skills prevent me from getting it to work 100%.
Boofo
05-16-2008, 04:10 PM
I did a version of the hack they have hear from the Unlicensed user message. I use the javascript for the pull down menu that has like 6 different messages to add to posts.
GameWizard
05-16-2008, 04:22 PM
I did a version of the hack they have hear from the Unlicensed user message. I use the javascript for the pull down menu that has like 6 different messages to add to posts.Could you share this code? It might be exactly what I need.
As for the stencil hack, although it functions as it should. The limitation is that I am forcefully restricted to 1 message, but I have at least 3 different preset text templates I would like users to chose from.
Boofo
05-16-2008, 04:35 PM
The one I did only works on post replies in the postbit, not new posts or new threads.
GameWizard
05-16-2008, 04:46 PM
Can you link me to the code anyway? It may provide me with some insight as to how I can maybe adapt it to my existing code.
Lynne
05-16-2008, 06:31 PM
Can you link me to the code anyway? It may provide me with some insight as to how I can maybe adapt it to my existing code.
I don't believe the mod was ever released. But, I think boofo has seen it in action and then came up with his own version. Perhaps tempt the bear with some salmon to release his mod. :D
Boofo
05-16-2008, 07:51 PM
Salmon? That's the best you can come up with? Although that really doesn't sound too bad right now. OK, I'll take it! ;)
Lynne
05-16-2008, 08:42 PM
I thought bears liked salmon? ;)
Boofo
05-16-2008, 09:06 PM
Actually this bear does! Maybe I really am part bear. :(
GameWizard
05-17-2008, 07:46 AM
Actually this bear does! Maybe I really am part bear. :(Then only part Salmon for you! You silly bear!
Hopefully someone can help... but I also managed to discover another possible way of getting this to work, rather than using pure javascript, maybe there is another way of doing this via php? In the stencil hack, here are some interresting bits of code I discovered that seem to do what I need, but now need to find a way to actually create a button that performs the actions on demand, rather than pre-defined in the AdminCP as it currently does.
$foruminfo['stencilmsg'] = str_replace("[username]",$vbulletin->userinfo['username'],$foruminfo['stencilmsg']);
$newpost['message'] = $foruminfo['stencilmsg'];This being the code which is responsible for outputing the code into the $newpost['message'] area, which is what I need. Whereas $foruminfo['stencilmsg'] is what I input in the AdminCP to appear in the message area. But I need a way to do this ondemand via button/link. Any help?
Boofo
05-17-2008, 07:54 AM
This is what I use for the drop down javascript if it will help you at all:
<div class="vbmenu_popup" id="rules_$post[postid]_menu" style="display:none">
<table cellpadding="4" cellspacing="1" border="0">
<tr>
<td class="thead">$vbphrase[postbit_rules]</td>
</tr>
<tr>
<td class="vbmenu_option"><a href="newreply.php?$session[sessionurl]do=newreply&noquote=1&p=$post[postid]&u=$post[userid]&textsel=1">Bump Thread</a></td>
</tr>
</table>
</div>
I have it doing case statements for the various templates.
GameWizard
05-17-2008, 08:20 AM
Not quite sure what your code pertains to, but the only difference to regular VB code is at the end of the code you have textsel=1 which seems to do nothing on my board. I guess it's hopeless for me then. :(
Boofo
05-17-2008, 09:02 AM
The textsel=1 is from the case statement part of the code. I have different templates to insert into messages. As my setup doesn't work on new threads or posts, it wouldn't do you any good, anyway. I was just showing you the part for the javascript drop down since you said that was the part you were having problems with. Actually there are 2 parts to it but it works like a basic menu dropdown like in the navbar. Maybe I'm misunderstanding what you are wanting to do. That is entirely possible, given my age, and living in the woods. ;)
GameWizard
05-19-2008, 12:24 PM
Alright I finally solved my problem after messing around with my Javascript code a bit.
Here is the final result:
In my newthread template I added the following javascript (doesn't matter where):
<script type="text/javascript">
// This is to insert text into the Standard Editor
function insert(el,ins) {
if (el.setSelectionRange){
el.value = el.value.substring(0,el.selectionStart) + ins + el.value.substring(el.selectionStart,el.selectionE nd) + el.value.substring(el.selectionEnd,el.value.length );
}
else if (document.selection && document.selection.createRange) {
el.focus();
var range = document.selection.createRange();
range.text = ins + range.text;
}
}
// This is to insert text into the WYSIWYG Editor
function insertText() {
var textstring = "This text will be inserted into WYSIWYG";
document.getElementById
("{$editorid}_iframe").contentWindow.document.body.innerHTML = textstring;
}
</script>
In the code above, the only thing that needs to be changed is the text that you wish to appear in the WYSIWYG editor as it needs to be defined in the code directly.
Below is the code for the button that will actually be used to input the text, a user presses this button, and the content defined appears in the editor:
<input type="button" value="My Button" onclick="insertText();insert(this.form.{$editorid}_textarea ,'This text will be inserted into Standard Editor')">
Once again you must define the text (the same as defined in the javascript) which will appear in the Standard editor this time. In addition, as you can see it's a dual action script, meaning it applies the same text to the Standard AND Wysiwyg editors, as the ID's for each editor are different and requires 2 sets of different code for each editor.
The only problem I have noticed with this script is that for it to work, you MUST have Wysiwyg editor set to default. If you specify any other editor to load on default, the code won't work unless you switch editors first. I have no clue why this is, but most people I trust will be using WYSIWYG by default anyway, so I don't think it's really that much of an issue.
Boofo
05-19-2008, 02:17 PM
I don't have as many users using the WYSIWYG editor as I do using the standard editor, since it is so easy for them to switch. ;)
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.