For standard textareas I use this plugin to create a placeholder. How can I extend tinymce so that this works in this way also.
E.g the default value is read from the
Try this code:
Add a placeholder text for the tinyMCE inline editor:
$scope.ContentOptions = {
setup: function(editor) {
editor.on('init', function () {
// Default classes of tinyMCE are a bit weird
// I add my own class on init
// this also sets the empty class on the editor on init
tinymce.DOM.addClass( editor.bodyElement, 'content-editor' );
});
// You CAN do it on 'change' event, but tinyMCE sets debouncing on that event
// so for a tiny moment you would see the placeholder text and the text you you typed in the editor
// the selectionchange event happens a lot more and with no debouncing, so in some situations
// you might have to go back to the change event instead.
editor.on('selectionchange', function () {
if ( editor.getContent() === "" ) {
tinymce.DOM.addClass( editor.bodyElement, 'empty' );
} else {
tinymce.DOM.removeClass( editor.bodyElement, 'empty' );
}
});
}}
The HTML part in the view
<div data-placeholder="Content..." id="post-content-editor" ui-tinymce="ContentOptions" ng-model="newPostContentModel"></div>
and Finally the CSS to create the placeholder text (it gets it from data-placeholder="Content..." but you could do it directly in css
.content-editorr:before {
display: none;
}
.content-editor.empty:before {
display: block;
position: absolute;
content: attr(data-placeholder);
}
I found it on github:
https://github.com/angular-ui/ui-tinymce/issues/197
I tried many placeholder solutions for tinymce and found this solution very usefull as it meets all the requirement of placeholder. I think it is the best solution,
This plugin for TinyMCE seems to be a solution:
https://github.com/mohan/tinymce-placeholder
Hope it helps!
For tinymce 4 I had problems with the events where ed.onInit was not defined. tinymce 4 uses ed.on('event' , callback) instead. Here is my implementation. I also used focus instead of mousedown as the event to listen for when clearing the editor because mousdown wasn't working for some reason.
setup : function(ed) {
// Set placeholder
var tinymce_placeholder = $('#'+ed.id);
var attr = tinymce_placeholder.attr('placeholder');
// For some browsers, `attr` is undefined; for others,
// `attr` is false. Check for both.
if (typeof attr !== 'undefined' && attr !== false) {
var is_default = false;
ed.on('init' , function(ed) {
// get the current content
var cont = ed.target.getContent();
// If its empty and we have a placeholder set the value
if(cont.length == 0){
ed.target.setContent(tinymce_placeholder.attr("placeholder"));
// Get updated content
cont = tinymce_placeholder.attr("placeholder");
}
// convert to plain text and compare strings
is_default = (cont == tinymce_placeholder.attr("placeholder"));
// nothing to do
if (!is_default){
return;
}
});
ed.on('focus', function(ed,e) {
// replace the default content on focus if the same as original placeholder
if (is_default){
ed.target.setContent('');
}
});
}
}
hope this helps for anyone who had problems with the other answers
The answers above were not working for me, but here's my (for me) working code based on the answers above and using onchange_callback. Hope it helps someone!
onchange_callback : function(ed){
var tinymce_placeholder = $('#' + ed.id).attr("placeholder");
setTimeout(function () {
var content = ed.getContent().replace(/<p>|<\/p>/g, '');
if (content == '') {
ed.setContent(tinymce_placeholder);
}
}, 200);
},
setup: function (ed) {
// Set placeholder
var tinymce_placeholder = $('#' + ed.id);
if (tinymce_placeholder.length) {
ed.onInit.add(function (ed) {
var cont = ed.getContent();
if (cont.length == 0) {
ed.setContent(tinymce_placeholder.attr("placeholder"));
}
});
ed.onMouseDown.add(function (ed, e) {
var content = ed.getContent().replace(/<p>|<\/p>/g, '');
if (content == tinymce_placeholder.attr("placeholder")) {
ed.setContent('');
}
});
}
}
I was getting an error if there was no placeholder attribute.
I combined the code from this answer: jQuery hasAttr checking to see if there is an attribute on an element to get the amended code below which deals with that scenario:
setup: function(ed) {
// Set placeholder
var tinymce_placeholder = $('#'+ed.id);
var attr = tinymce_placeholder.attr('placeholder');
// For some browsers, `attr` is undefined; for others,
// `attr` is false. Check for both.
if (typeof attr !== 'undefined' && attr !== false) {
var is_default = false;
ed.onInit.add(function(ed) {
// get the current content
var cont = ed.getContent();
// If its empty and we have a placeholder set the value
if(cont.length == 0){
ed.setContent(tinymce_placeholder.attr("placeholder"));
// Get updated content
cont = tinymce_placeholder.attr("placeholder");
}
// convert to plain text and compare strings
is_default = (cont == tinymce_placeholder.attr("placeholder"));
// nothing to do
if (!is_default){
return;
}
});
ed.onMouseDown.add(function(ed,e) {
// replace the default content on focus if the same as original placeholder
if (is_default){
ed.setContent('');
}
});
}
}
I refactored Tom Duke's code to work on TinyMCE4 with it's jquery plugin
$('textarea.tinymce').tinymce({
script_url: _base + '/assets/js/tinymce/tinymce.min.js',
theme: "modern",
setup: function(editor) {
// Set placeholder
var placeholder = $('#' + editor.id).attr('placeholder');
if (typeof placeholder !== 'undefined' && placeholder !== false) {
var is_default = false;
editor.on('init', function() {
// get the current content
var cont = editor.getContent();
// If its empty and we have a placeholder set the value
if (cont.length === 0) {
editor.setContent(placeholder);
// Get updated content
cont = placeholder;
}
// convert to plain text and compare strings
is_default = (cont == placeholder);
// nothing to do
if (!is_default) {
return;
}
})
.on('focus', function() {
// replace the default content on focus if the same as original placeholder
if (is_default) {
editor.setContent('');
}
})
.on('blur', function() {
if (editor.getContent().length === 0) {
editor.setContent(placeholder);
}
});
}
}
});