tinymce trims last space - javascript

I am writing a module that change content. I use tinymce & i get all content using
var txt = editor.getContent({format: 'text'});
when i do my fixes, i get content via AJAX & put my content using
editor.setContent(results.text);
setContent erases all previous text & put new one but it puts cursor at the start of the line so i used smthg like this:
editor.selection.select(editor.getBody(), true);
editor.selection.collapse(false);
I need to add one space after setContent(results.text) .I need to put cursor after space, but it(tinymce) trims last space & put cursor at last symbol.
Can U advertise me what to do?

Related

How can I fix emails googlesheets

Help. I'm trying to send an automated email using this code.
function checkIn() {
var aemail=SpreadsheetApp.getActiveSpreadsheet();
var tracker= aemail.getSheetByName("Tracker");
var template= aemail.getSheetByName("Templates");
var message=template.getRange('B3').getValue();
var signature= template.getRange('C3').getValue();
for (var i=2; i<=2;i++) {
var sname =tracker.getRange('A'+i).getValue();
var subject = tracker.getRange('B'+i).getValue();
var eaddress = tracker.getRange('C'+i).getValue();
var finalmsg = ""
finalmsg="Hello"+""+sname+","+"\n"+"\n"+message+"\n"+signature;
MailApp.sendEmail(eaddress,subject,finalmsg);
}
}
The email sends but the text alignment is to the right and does not expand to the email space. How can I fix this? I fixed it once but can not remember how. I know it's not code-related.
It looks like it will depend on the data in the range B3. It probably has newlines included in it - if its not that then maybe its best to share a sample sheet so that it can be tested.
You can also try and add this code to replace newlines with spaces
Change
var message=template.getRange('B3').getValue();
To
var message=template.getRange('B3').getValue().replaceAll("\n", " ")
If you want more control over how things look, you can also try using an HTML body with the sendEmail method. Using it like this:
MailApp.sendEmail('mike#example.com', 'example', 'body without html', {
htmlBody:"<h1>Body with HTML</h1>"
});
The normal body is included since not all clients render HTML, so if a particular mail client does not render HTML, then it will use the plain text version.
That said, I do think that your issue is to do with newline characters \n making their way into your message.
Reference
replaceAll
sendEmail
EDIT
I was wrong.
After running some tests it is true that if your Email is completely plain text, then Gmail will automatically insert line breaks.
You will need to use the options and htmlBody parameters mentioned above.
You can use it like this:
function sampleEmail() {
var message="Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks "
MailApp.sendEmail("[YOUR EMAIL]","test", message,{htmlBody:"<p>"+ message +"</p>"});
}
The arguments are as follows:
The email of the recipient
the subject
the plain text message (as a fall back in case the client's email does not support HTML)
the options parameter with one attribute htmlBody where the message is simply wrapped with <p> HTML tags.
This text will now adapt and fill the width of the screen in an email client that supports HTML, like Gmail.

How can I replace the value of a textarea in Chrome as user?

I'm using Google Chrome.
Is there a simple way to create something like a user script (with javascript?) that automatically replaces the content of a certain textarea (id: svn) with another one, preferable by using a regex replace function?
For example, if the textarea by default contains Text "any text"-Set Text, then I want to replace it with Text Set any text Set. (Basically replace ".*"-Set with Set .*)
You can make a bookmark tool.
1 . Open a new tab, save it.
2 . Choose to edit the new bookmark.
3 . Paste js code in the url.
The code may look like this:
javascript:
var el = document.getElementById('svn');
el.value = el.value.replace(/pattern/,'text'); /* edit your pattern here */
el.disable = true; /* keep textarea editable */
When bookmark clicked,the code will be excuted. All spaces are better replaced with %20. Keep all codes in a single line, that is:
javascript:var%20el=document.getElementById('svn');el.value=el.value.replace(/pattern/,'text');el.disable=true;
Good luck.

Creating tag labels inside an editable div

What I am trying to do is to create a front-end editable tagbox (editable div). Whenever a user types a word into that box and presses , this box will change that word into a colorful label. The problem I am having is:
User types the first word in, presses the comma key.
The word is then wrapped in <a> tags.
User types the second word in, presses the comma key.
Now I have to leave the first wrapped word as it is and take only the second word into consideration to wrap it into an <a> tag as well. It's extremely tricky to me, I have no idea how to leave the first <a> tag alone and select "free" words for wrapping. This also means wrapping more than one word into a single <a> tag whenever the user decides to put a two-word tag. It has to work with any number of tags.
Could you please point me in the right direction? I am trying to solve this with jQuery. I don't necessarily need the code itself, because I know how to write it, I just need to come up with the right algorithm in my brain.
Alright, as requested :)
Depending on whether you keep the commas in the field after replacing or not, split the inner HTML of the editable content by comma and/or .
Try following
function wrapInLink(container){
var link_text = $(container).text().split(',').slice(-1).pop(); // finding the string for replacing with anghor tag
var html = $(container).html(); // getting the container html
html = html.replace(link_text, "<a href='link_to_be_given'>" + link_text + "</a>"); // replacing the link text with anchor tag
$(container).html(html); // replacing the container's html
}

Contenteditable div: save and restore caret position when is positioned in empty new line

I need to save and restore the caret position as the user types in a contenteditable div (the html written is edited and re-inserted with each key pressed).
I've read and succesfully used this solution by Tim Down to a similar question: https://stackoverflow.com/a/13950376/2086428.
The problem occurs when the caret is positioned in an empty line, it will be restored to the previous non-empty line (try it here, add a new line and save / restore the cursor).
In the comments section of the solution proposed one user had the same problem, the author of the solution hinted to convert the <br>s into characters.
How can I do this?
Are there any simpler solutions?
PS: I can't use rangy for this project.
From this answer:
The solution is to make sure, that every "br" tag is followed by a newline character (\n).
This won't hurt the HTML content in any visible way, but now all HTML breaks are translated to plain text line breaks as soon as range.toString() is being called.
<br>\n
Working example here: http://jsfiddle.net/gEhjZ/95/
For comparison, example, which has problem (without \n after "br"): http://jsfiddle.net/gEhjZ/94/
You can listen to keydown; every time Enter key is pressed check if there are some character(s) in the same (or next sibling) text node immediately after caret position (you will receive event before the line is broken). If not add after caret position and then save the position.
Some of these utilities can be useful:
https://github.com/MailOnline/milo/blob/master/lib/util/dom.js
https://github.com/MailOnline/milo/blob/master/lib/util/selection/index.js

TinyMCE - Writing plugin that insert content on top

I am trying to write a plugin for TinyMCE with a feature to add content to the beginning of the content. I know the following command which will insert content at the current location of the cursor. How do I force it to insert it at the beginning of the content?
tinyMCEPopup.editor.execCommand('mceInsertRawHTML', false, "halo world");
tinyMCEPopup.editor.execCommand('mceInsertContent', false, "halo world");
For this you will need to set the cursor location to the beginning of the editor content.
you may use the function setCursorLocation:
ed.selection.setCursorLocation(ed.getBody().firstChild, 0); // node to set the cursor to, second param is offset
The setCursorLocation will move the cursor to the position of the first html element's first content character. This means that if your current html content looks like:
<div>abcd</div>
then the setCursorLocation will move the cursor between the <div> and the abcd. In some cases this is not acceptable. So another solution would be: ed.setContent('yourText' + ed.getContent()).

Categories