select the text inside textarea to pick the color & apply to selected area particularly using javascript
I'm working on angular 6 project, there is a textarea with color-picker, user selects the text & applies color, I'm replacing the selected text with a span tag, which contains the styling with inline CSS.
Example: When a part of text is selected, I'm using window.getSelection() to create range with offsets, using the offsets I'm able to get the selected area from the textarea content. After that, I'm replacing the selected area with a span containing the font-color(color comes from color-picker). It works fine for the first time. But when i do it again, if i select a part of the text, the offsets are going wrong & color is being applied to other than the selected area. Because, the previous selected area will be replaced with spanTag & the selection doesn't happen. The selection doesn't move through the span element.
Here is the code:
This is the selectionFunction:
private getCaretAndSelectionRange() {
var doc = this.editableElment.ownerDocument || this.editableElment.document;
var win = doc.defaultView || doc.parentWindow;
var sel;
if (typeof win.getSelection != "undefined") {
sel = win.getSelection();
if (sel.rangeCount > 0) {
var range = win.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(this.editableElment);
preCaretRange.setStart(range.startContainer,range.startOffset);
preCaretRange.setEnd(range.endContainer, range.endOffset);
this.caretPosition = this.editableElment.innerText.length;
this.selecteTextLength = (range.endOffset - range.startOffset);
this.firstIndex = preCaretRange.startOffset;
this.secondIndex = preCaretRange.endOffset;
}
} else if ((sel = doc.selection) && sel.type != "Control") {
var textRange = sel.createRange();
var preCaretTextRange = doc.body.createTextRange();
preCaretTextRange.moveToElementText(this.editableElment);
preCaretTextRange.setEndPoint("EndToEnd", textRange);
this.caretPosition = preCaretTextRange.text.length;
this.selecteTextLength = (textRange.endOffset - textRange.startOffset);
}
}
This is the formatter:
public formatDoc(sCmd, sValue) {
this.editableElment = document.querySelector("#" + this.customTextAreaID);
this.editableElment.focus();
var commandStatus: boolean = document.execCommand('styleWithCSS', false, true);
commandStatus = document.execCommand(sCmd, false, sValue);
if(this.firstIndex !== null && this.secondIndex !== null) {
var str = this.editableElment.innerText;
var str2 = str.slice(this.firstIndex,this.secondIndex);
var output = str;
output = output.replace(new RegExp(str2, 'g'),'<span style="color:'+sValue+';">'+str2+'</span>');
this.editableElment.innerHTML = output;
console.log(this.editableElment.innerHTML+'before');
}
this.tfWidget.inputValue = this.editableElment.innerHTML;
}
Note: sCmd = 'ForeColor' && sValue is the HEX value from the color-picker.
javascript angular html5
add a comment |
I'm working on angular 6 project, there is a textarea with color-picker, user selects the text & applies color, I'm replacing the selected text with a span tag, which contains the styling with inline CSS.
Example: When a part of text is selected, I'm using window.getSelection() to create range with offsets, using the offsets I'm able to get the selected area from the textarea content. After that, I'm replacing the selected area with a span containing the font-color(color comes from color-picker). It works fine for the first time. But when i do it again, if i select a part of the text, the offsets are going wrong & color is being applied to other than the selected area. Because, the previous selected area will be replaced with spanTag & the selection doesn't happen. The selection doesn't move through the span element.
Here is the code:
This is the selectionFunction:
private getCaretAndSelectionRange() {
var doc = this.editableElment.ownerDocument || this.editableElment.document;
var win = doc.defaultView || doc.parentWindow;
var sel;
if (typeof win.getSelection != "undefined") {
sel = win.getSelection();
if (sel.rangeCount > 0) {
var range = win.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(this.editableElment);
preCaretRange.setStart(range.startContainer,range.startOffset);
preCaretRange.setEnd(range.endContainer, range.endOffset);
this.caretPosition = this.editableElment.innerText.length;
this.selecteTextLength = (range.endOffset - range.startOffset);
this.firstIndex = preCaretRange.startOffset;
this.secondIndex = preCaretRange.endOffset;
}
} else if ((sel = doc.selection) && sel.type != "Control") {
var textRange = sel.createRange();
var preCaretTextRange = doc.body.createTextRange();
preCaretTextRange.moveToElementText(this.editableElment);
preCaretTextRange.setEndPoint("EndToEnd", textRange);
this.caretPosition = preCaretTextRange.text.length;
this.selecteTextLength = (textRange.endOffset - textRange.startOffset);
}
}
This is the formatter:
public formatDoc(sCmd, sValue) {
this.editableElment = document.querySelector("#" + this.customTextAreaID);
this.editableElment.focus();
var commandStatus: boolean = document.execCommand('styleWithCSS', false, true);
commandStatus = document.execCommand(sCmd, false, sValue);
if(this.firstIndex !== null && this.secondIndex !== null) {
var str = this.editableElment.innerText;
var str2 = str.slice(this.firstIndex,this.secondIndex);
var output = str;
output = output.replace(new RegExp(str2, 'g'),'<span style="color:'+sValue+';">'+str2+'</span>');
this.editableElment.innerHTML = output;
console.log(this.editableElment.innerHTML+'before');
}
this.tfWidget.inputValue = this.editableElment.innerHTML;
}
Note: sCmd = 'ForeColor' && sValue is the HEX value from the color-picker.
javascript angular html5
add a comment |
I'm working on angular 6 project, there is a textarea with color-picker, user selects the text & applies color, I'm replacing the selected text with a span tag, which contains the styling with inline CSS.
Example: When a part of text is selected, I'm using window.getSelection() to create range with offsets, using the offsets I'm able to get the selected area from the textarea content. After that, I'm replacing the selected area with a span containing the font-color(color comes from color-picker). It works fine for the first time. But when i do it again, if i select a part of the text, the offsets are going wrong & color is being applied to other than the selected area. Because, the previous selected area will be replaced with spanTag & the selection doesn't happen. The selection doesn't move through the span element.
Here is the code:
This is the selectionFunction:
private getCaretAndSelectionRange() {
var doc = this.editableElment.ownerDocument || this.editableElment.document;
var win = doc.defaultView || doc.parentWindow;
var sel;
if (typeof win.getSelection != "undefined") {
sel = win.getSelection();
if (sel.rangeCount > 0) {
var range = win.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(this.editableElment);
preCaretRange.setStart(range.startContainer,range.startOffset);
preCaretRange.setEnd(range.endContainer, range.endOffset);
this.caretPosition = this.editableElment.innerText.length;
this.selecteTextLength = (range.endOffset - range.startOffset);
this.firstIndex = preCaretRange.startOffset;
this.secondIndex = preCaretRange.endOffset;
}
} else if ((sel = doc.selection) && sel.type != "Control") {
var textRange = sel.createRange();
var preCaretTextRange = doc.body.createTextRange();
preCaretTextRange.moveToElementText(this.editableElment);
preCaretTextRange.setEndPoint("EndToEnd", textRange);
this.caretPosition = preCaretTextRange.text.length;
this.selecteTextLength = (textRange.endOffset - textRange.startOffset);
}
}
This is the formatter:
public formatDoc(sCmd, sValue) {
this.editableElment = document.querySelector("#" + this.customTextAreaID);
this.editableElment.focus();
var commandStatus: boolean = document.execCommand('styleWithCSS', false, true);
commandStatus = document.execCommand(sCmd, false, sValue);
if(this.firstIndex !== null && this.secondIndex !== null) {
var str = this.editableElment.innerText;
var str2 = str.slice(this.firstIndex,this.secondIndex);
var output = str;
output = output.replace(new RegExp(str2, 'g'),'<span style="color:'+sValue+';">'+str2+'</span>');
this.editableElment.innerHTML = output;
console.log(this.editableElment.innerHTML+'before');
}
this.tfWidget.inputValue = this.editableElment.innerHTML;
}
Note: sCmd = 'ForeColor' && sValue is the HEX value from the color-picker.
javascript angular html5
I'm working on angular 6 project, there is a textarea with color-picker, user selects the text & applies color, I'm replacing the selected text with a span tag, which contains the styling with inline CSS.
Example: When a part of text is selected, I'm using window.getSelection() to create range with offsets, using the offsets I'm able to get the selected area from the textarea content. After that, I'm replacing the selected area with a span containing the font-color(color comes from color-picker). It works fine for the first time. But when i do it again, if i select a part of the text, the offsets are going wrong & color is being applied to other than the selected area. Because, the previous selected area will be replaced with spanTag & the selection doesn't happen. The selection doesn't move through the span element.
Here is the code:
This is the selectionFunction:
private getCaretAndSelectionRange() {
var doc = this.editableElment.ownerDocument || this.editableElment.document;
var win = doc.defaultView || doc.parentWindow;
var sel;
if (typeof win.getSelection != "undefined") {
sel = win.getSelection();
if (sel.rangeCount > 0) {
var range = win.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(this.editableElment);
preCaretRange.setStart(range.startContainer,range.startOffset);
preCaretRange.setEnd(range.endContainer, range.endOffset);
this.caretPosition = this.editableElment.innerText.length;
this.selecteTextLength = (range.endOffset - range.startOffset);
this.firstIndex = preCaretRange.startOffset;
this.secondIndex = preCaretRange.endOffset;
}
} else if ((sel = doc.selection) && sel.type != "Control") {
var textRange = sel.createRange();
var preCaretTextRange = doc.body.createTextRange();
preCaretTextRange.moveToElementText(this.editableElment);
preCaretTextRange.setEndPoint("EndToEnd", textRange);
this.caretPosition = preCaretTextRange.text.length;
this.selecteTextLength = (textRange.endOffset - textRange.startOffset);
}
}
This is the formatter:
public formatDoc(sCmd, sValue) {
this.editableElment = document.querySelector("#" + this.customTextAreaID);
this.editableElment.focus();
var commandStatus: boolean = document.execCommand('styleWithCSS', false, true);
commandStatus = document.execCommand(sCmd, false, sValue);
if(this.firstIndex !== null && this.secondIndex !== null) {
var str = this.editableElment.innerText;
var str2 = str.slice(this.firstIndex,this.secondIndex);
var output = str;
output = output.replace(new RegExp(str2, 'g'),'<span style="color:'+sValue+';">'+str2+'</span>');
this.editableElment.innerHTML = output;
console.log(this.editableElment.innerHTML+'before');
}
this.tfWidget.inputValue = this.editableElment.innerHTML;
}
Note: sCmd = 'ForeColor' && sValue is the HEX value from the color-picker.
javascript angular html5
javascript angular html5
asked Nov 14 '18 at 13:50
srihari mondemsrihari mondem
11
11
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301799%2fselect-the-text-inside-textarea-to-pick-the-color-apply-to-selected-area-parti%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301799%2fselect-the-text-inside-textarea-to-pick-the-color-apply-to-selected-area-parti%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown