2014-12-01 19 views

risposta

11

Il placeholder plugin ha lavorato molto per me. Questo plugin porta funzionalità di attributo segnaposto HTML5 per l'editor TinyMCE.

3
<html> 
<head> 
    <title>Bhanu Pratap, Tinymce with placeholder... </title> 
    <script src="http://cdn.tinymce.com/4/tinymce.min.js"></script> 
    <script type="text/javascript"> 
     tinymce.PluginManager.add('placeholder', function (editor) { 
      editor.on('init', function() { 
       var label = new Label; 
       onBlur(); 
       tinymce.DOM.bind(label.el, 'click', onFocus); 
       editor.on('focus', onFocus); 
       editor.on('blur', onBlur); 
       editor.on('change', onBlur); 
       editor.on('setContent', onBlur); 
       function onFocus() { if (!editor.settings.readonly === true) { label.hide(); } editor.execCommand('mceFocus', false); } 
       function onBlur() { if (editor.getContent() == '') { label.show(); } else { label.hide(); } } 
      }); 
      var Label = function() { 
       var placeholder_text = editor.getElement().getAttribute("placeholder") || editor.settings.placeholder; 
       var placeholder_attrs = editor.settings.placeholder_attrs || { style: { position: 'absolute', top: '2px', left: 0, color: '#aaaaaa', padding: '.25%', margin: '5px', width: '80%', 'font-size': '17px !important;', overflow: 'hidden', 'white-space': 'pre-wrap' } }; 
       var contentAreaContainer = editor.getContentAreaContainer(); 
       tinymce.DOM.setStyle(contentAreaContainer, 'position', 'relative'); 
       this.el = tinymce.DOM.add(contentAreaContainer, "label", placeholder_attrs, placeholder_text); 
      } 
      Label.prototype.hide = function() { tinymce.DOM.setStyle(this.el, 'display', 'none'); } 
      Label.prototype.show = function() { tinymce.DOM.setStyle(this.el, 'display', ''); } 
     }); 

     tinymce.init({selector: ".EditorControl",plugins: ["placeholder"]}); 

    </script> 
</head> 
<body> 
    <textarea class="EditorControl" placeholder="Bhanu Pratap welcomes you, please enter some text here...."></textarea> 
</body> 
</html> 
  1. qui stiamo aggiungendo un'etichetta e passandolo ai methos Bind di oggetti DOM di TinyMCE "tinymce.DOM.bind (label.el, 'click', onFocus);"
  2. nascondi il segnaposto al clic o se è presente del testo nell'editor.
  3. impostazione colore del segnaposto a #aaaaaa possiamo cambiare in base al requisito.
  4. impostazione padding a 0,25% e margine a 5px e segnaposto-dimensione carattere a 17 px queste impostazioni possono essere modificate in base alle esigenze.
  5. possiamo anche modificare il messaggio di segnaposto e impostarlo in un mannaro significativo. enter image description here

Grazie ... :)

1

Con TinyMCE 3 e al di sotto, il plugin funziona bene. Il plug-in non è disponibile in TinyMCE 4, ma è possibile aggiungere un segnaposto su init e quindi rimuoverlo a fuoco. Ricorda che TinyMCE usa un iframe.

tinymce.init({ 
    //here all the rest of the options 
    //xxxxx 
    //Add the placeholder 
    setup: function (editor) { 
    editor.on('init', function(){ 
     if (tinymce.get('Text').getContent() == ''){ 
     tinymce.get('Text').setContent("<p id='#imThePlaceholder'>Your nice text here!</p>"); 
     } 
    }); 
    //and remove it on focus 
    editor.on('focus',function(){ 
     $('iframe').contents().find('#imThePlaceholder').remove(); 
    }); 
}) 
+0

Non sono sicuro di come ho bisogno di strutturare il mio codice HTML per ottenere 'tinymce.get ('Text'). getContent()' per funzionare correttamente, o se questo è un modo obsoleto di fare le cose, ma quando ho cambiato questo in 'tinyMCE.activeEditor.getContent()', sono riuscito a far funzionare questa soluzione. Grazie per aver postato questo andyk. –

Problemi correlati