How to disable text formatting when pasting in summernote editor? - editor

How to disable text formatting when pasting in summernote editor?
how to turn off text formatting, for example when you paste with word, etc. adds a bunch of unnecessary styles all the time. Is it possible to make it paste plain text without formatting?

By using summernote.paste event we can track the paste in the editor and then text can be formatted or unnecessary style can be removed.
$('#id').on('summernote.paste', function(e, ne) {
//get the text
let inputText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text');
// block default behavior
ne.preventDefault();
//modify paste text as plain text
let modifiedText = inputText .replace(/\r?\n/g, '<br>');
//put it back in editor
document.execCommand('insertHtml', false, modifiedText );
})

Related

How to (pre)format the (empty) cells of a Google Sheet so that the formatting sticks?

I want all cells of a Google sheet to be formatted identically (same font, same font size, same wrapping, same alignment). However, even if I select the whole sheet and set all formatting values, every time I enter a new value in an empty cell (sometimes copy-pasted from web pages, sometimes typed), the 'preset' formatting specs seem to have been lost and I must respecify them. Does someone know of a way to make the preformatting stick?
Copying and pasting the usual way brings with it the formatting of the source as best as Sheets can manage. Instead of using "Paste" or Ctrl-V, use "Paste Special" (Ctrl-Alt-V). A small clipboard icon will appear to the lower right of the on-screen paste area. Click it and choose "Paste values only."
This must be done even if you paste from one sheet within Google Sheets to another. If you just paste the regular way, it not only brings in the formatting of the source sheet, it breaks up any existing conditional formatting ranges you have in place.
Try using CTRL+SHIFT+V to paste. Doing so will only paste the copied value to the sheet without affecting the formatting.
Potential workaround:
With Apps Script you can run a simple onEdit() trigger to run every time an edit is made on the sheet. So you could use this to run a script that changes the format of cells every time an edit is made.
This would involve getting the whole data range and getting the rich text values of each cell, making a copy of the rich text value object and modifying it with the RichTextValueBuilder. This is turn involves copying and modifying the TextStyle with the TextStyleBuilder.
The wrapping and alignment are set on the whole range with setWrap(isWrapEnabled) and setHorizontalAlignment(alignment).
Example
function onEdit() {
// initialize main variables
let file = SpreadsheetApp.getActive();
let sheet = file.getSheetByName("Sheet1"); // change to your sheet name
let range = sheet.getDataRange();
// This sets the whole range wrap and alignment
range.setWrap(true);
range.setHorizontalAlignment("center")
// Get the rich text values in a 2D array
let richTextValues = range.getRichTextValues();
// Two map functions to return the modified 2D array.
let newRichTextValues = richTextValues.map(row => {
return row.map(cell => {
// Gets the text style from the richText value
let style = cell.getTextStyle()
// Copy the text style and set the bold, font and size
let styleCopy = style.copy()
.setBold(true)
.setFontFamily("Comfortaa")
.setFontSize(11)
.build();
// Set the new rich text value
let newRichTextValues = cell.copy()
.setTextStyle(styleCopy)
.build();
return newRichTextValue
})
})
// Set the whole range to the rich text value.
range.setRichTextValues(newRichTextValues)
}
This should work as-is by copying this into the script editor, trying to run it once from there to get the authorizations done, and then editing your sheet. Remember to add in your sheet name.
Demonstration
This is what it looks like in action:
In this example, I used
Text wrapping as on.
Center alignment
Bold text
"Comfortaa" font
11 font size
You should change these attributes to the ones that you want. There are other attributes available to customize too.
As you can see, it could get a bit tricky if you need to exclude certain ranges, but this is absolutely possible depending on your exact use case.
It should also be mentioned that this is quite an inefficient approach, as it gets the whole range and updates everything. Ideally you want to identify the exact changes and update those, but again, this depends on your exact use case. To get the range that was modified with an onEdit trigger you can use the Event object.
References
simple onEdit() trigger
RichTextValue
RichTextValueBuilder
TextStyle
TextStyleBuilder
setHorizontalAlignment(alignment)
setWrap(isWrapEnabled)
Event object

Problems with string manipulation in an UltraEdit script

Inside an UltraEdit script I've got a bit of code like so:
var FNstring = UltraEdit.activeDocument.selection;
var FNstring = UltraEdit.activeDocument.selection.replace(">>","</p>");
var FNstring = UltraEdit.activeDocument.selection.replace("\r\n","<BR>");
A var_dump confirms that the content of the string at this moment is:
text text text
text text text text text text text text text
text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text
text text text text text text text text text
text text text text text text text text text
text text text text text text>>
I'm just not getting those find/replace commands right:
Replace the >> with (/p) (once only)
Replace all CR/LF with (br). (0 or >1 )
That code above is just not doing it. I'm getting something wrong. How can I fix this?
Those three lines of code do the following:
Create a copy of the selection in the active document in UltraEdit into a JavaScript string variable.
Create a copy of the selection in active document in UltraEdit into a JavaScript string variable with the same name as before, resulting in discarding the previous string and additionally replacing >> by </p> in the created copy.
Create a copy of the selection in active document in UltraEdit into a JavaScript string variable with the same name as before, resulting in discarding the previous string and additionally replacing \r\n by <BR> in the created copy.
That is not what you want. The replace function of the JavaScript String object has nothing to do with the replace function of the UltraEdit document object.
You could use instead following two lines:
var FNstring = UltraEdit.activeDocument.selection.replace(/>>/g,"</p>");
UltraEdit.activeDocument.write(FNstring.replace(/\r\n|\n|\r/g,"<BR>"));
Those two lines do following:
Create a copy of the current selection in the active document of UltraEdit into the JavaScript string variable FNstring with replacing all occurrences of >> by </p> in the created copy.
Create one more copy of the previously created string FNstring with replacing all occurrences of a DOS (Windows) or UNIX or Mac line termination by <BR> and write this new string over the selection in the active document of UltraEdit.

Pre written text into textarea

I want to make text area, where always is pre written text, and that piece of text is located at the end, and it wouldn't be possible to delete. For example, you write answer here, and at the end, right side of cursor, is text "This is end of my answer".
And when you POST it, it displays your written text and "This is end of my answer" at the end.
I know, that I can attach this text easily after posting it, but it must be displayed, and anyone could see it.
I need javascript/jQuery solution.
Thanks!
The text to the right side of the cursor, that moves as you type is really annoying and zero-usable. However, if you want something like that I would suggest putting a background image on the textarea with the text you want. It will be static and none will be able to edit it, and at the same time will always see it.
<input class="myinput" name="myinput" type="text" value="Text_Ends_Here" /></input>​​
$("input[name=myinput]").click(function()
{
var currentValue = $(this).val();
currentValue = currentValue.replace("Text_Ends_Here", "");
$(this).val(currentValue);
}
);
$("input[name=myinput]").focusout(function()
{
var currentValue = $(this).val();
$(this).val(currentValue + ' Text_Ends_Here');
}
);​
http://jsfiddle.net/gXvLB/95/
You can position the required text over textarea using positioning.

Removing highlighting of text in text box after switching tabs in jquery-ui-tabs

I am experiencing a weird issue with the jquery-ui tab object. If I highlight the text of an html input tag (type="text") that is on a tab, switch tabs, then come back to the original tab, this text is still highlighted. I can actually remove the highlighting before switching tabs, and when I come back, the text has been highlighted again. I have tried to remove this highlighting by calling .blur() on the textbox after the show event of the tab is triggered, but this does not work. Similarly, actually clicking on other parts of the page (which I take blur() to be the equivalent of) does not remove the highlighting of the text. Is there something else I could be doing here?
Thanks.
I have solved the issue at hand by modifying the selectionStart property of the input DOM object. By setting it equal to the selectionEnd property, you ensure no text is highlighted. The selectionStart property was being changed on my original highlighting, so when I came back to the tab, the text was re-highlighted. Solution below.
$(this).find('input').each(function () {
var input = document.getElementById($(this).attr("id"));
try {
input.selectionStart = input.selectionEnd;
}
catch (err) {
}
});

I have one Textbox and inside the textbox i want the text which user cannot be deleted

i have one text box in which the text inside the text box should not be deleted and after the text only the cursor has to be placed
Ex:
For the textbox: "http://" should be available and the text which is in quotations cannot be deleted inside the textbox and the cursor should be after the quotations.
Thanks in advance,
Vara Prasad.M
add this to the input field:
onchange="testEdit(this)"
and have a javascript function that does something like this:
function testEdit(field) {
if (field.value.substring(0, 8) != "http://") {
field.value = "http://" + field.value.substring(8);
}
}
However, this is sloppy. The best way is to put the http:// in a label OUTSIDE the text box. If something is in a text box, it should be input. By putting that inside the text box, you will be confusing your users.
Good luck.

Resources