Reputation: 3010
As I understand it, an element with contentEditable="true"
is some kind of WYSIWYG HTML editor. It generates relevant HTML tags corresponding to the command issued.
For example, if one selects text and then presses Ctrl+B, the selected text is placed between <b></b>
tags.
I need to have no style tags in the resulting text. How does one suppress, hijack or control the behavior of those commands?
Other things I could do:
contentEditable
at all but a textarea
instead. But among other things, contentEditable
makes it really easy to highlight the paragraph that is being edited. That's much more difficult to do with a textarea
.Upvotes: 30
Views: 25338
Reputation: 18630
just an addition to the great answer by @Andy Hoffman
you can hide underlying styles as well
[contenteditable] u {
text-decoration: none;
}
the final changes would be:
[contenteditable] b {
font-weight: normal;
}
[contenteditable] i {
font-style: normal;
}
[contenteditable] img {
display: none;
}
[contenteditable] u {
text-decoration: none;
}
you can apply this to any element inside the contenteditable
Upvotes: 0
Reputation: 19109
Rather than trying to suppress the unwanted tags via JavaScript, I just style them away and save/restore the un-styled text in the contenteditable region.
Update : Added pasted image suppression.
[contenteditable] {
background: #eee;
width: 15rem;
height: 4rem;
padding: 1em;
}
[contenteditable] b {
font-weight: normal;
}
[contenteditable] i {
font-style: normal;
}
[contenteditable] img {
display: none;
}
<div contenteditable></div>
Upvotes: 24
Reputation: 207
In React we use next code to disable all except Copy / Paste and move commands:
const onKeyDown = (e) =>
e.ctrlKey || e.metaKey
&& ![`c`, `v`, `ArrowLeft`, `ArrowRight`].includes(e.key)
&& e.preventDefault()
const App = props => (
<div
contentEditable
suppressContentEditableWarning
onKeyDown={onKeyDown}
>
12345
</div>
)
ReactDOM.render(
<App />,
document.getElementById('react')
)
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Upvotes: 6
Reputation: 4682
I know it is too late, but if it can help some one It should worth give a try.
Here is how I handled it in javascript
, to disable the ctrl+Command(ctrl+B,ctrl+Any Key), I've used:
HTML:
<div id="xyz" onKeyDown="return disable(this,event);" contentEditable="true">
This is my Rich Text Editor
</div>
JavaScript:
function disable(x,e){
if(e.ctrlKey){ // .ctrlKey tells that ctrl key was pressed.
return false;
}
return true;
}
switch case statements
on keyCode
values like:
function disable(x,e){
var ret=true;
if(e.ctrlKey){
switch(e.keyCode){
case 66: //ctrl+B or ctrl+b
case 98: ret=false;
break;
case 73: //ctrl+I or ctrl+i
case 105: ret=false;
break;
case 85: //ctrl+U or ctrl+u
case 117: ret=false;
break;
}
}
return ret;
} // This will work fine for ctrl+c and ctrl+v.
As Betty_St
Suggested, To make this work on Mac You need to replace:
if(e.ctrlKey){
with
if(e.ctrlKey || e.metaKey){ // Coz You'll be using CMD key on mac
Then That Might work on Mac OS.
Note: I've not dealt with Mac previously, So I don't know whether that is right way of doing or not.
Upvotes: 21
Reputation: 3028
Probably the best landing page resource for contentEditable is here:
http://blog.whatwg.org/the-road-to-html-5-contenteditable
Basically, what it boils down to is this: You can not reconfigure the key codes themselves – they always exist, and they're different depending on localizations of browsers.
However, you can intercept the keyboard commands using JavaScript, an example of which can be seen here:
http://www.openjs.com/scripts/events/keyboard_shortcuts/shortcut.js
I've been playing around with contentEditable lately with mixed success. Some things just tend to work better than others, and have mixed results across browser. If all you really want is an editor for contentEditable block elements, try taking a look at aloha editor.
Upvotes: 10