2012-08-10 20 views
7

TinyMCE to świetne narzędzie, które rozwiązało dla nas wiele problemów. Istnieje jednak problem, który trudno jest rozwiązać. Podczas gdy TinyMCE zmieni rozmiar czcionki elementów na liście, nie zmieni ona rozmiaru punktorów (lista nieuporządkowana) ani liczb (lista uporządkowana), które kontynuują te pozycje.Jak ustawić rozmiar czcionki punktorów i numerów pozycji w tinyMCE?

Co użytkownik nakręca ze jest coś, co wygląda tak:

Item fonts change but bullet fonts do not change

Jak widać na zdjęciu, rozmiar czcionki w dwóch wykazach są różne, ale wielkość kule są takie same.

Czy ktoś wie, jak zmusić TinyMCE do zmiany pocisków w celu dopasowania czcionki?

Odpowiedz

0

Od TinyMCE wszystko opakowuje. Aby obejść problemy jak ty mieli Zrobiłem to:

<ol> 
    <li style="font-size: <something you want>;">one</li> 
    <li style="font-size: <something you want>;">two</li> 
</ol> 
+0

Tak. Myślę, że to, czego szukam, to gdzie i jak to zrobić. – lambmj

+0

Jeśli jednak tworzysz coś, czego użytkownik końcowy nie miałby dostępu do części HTML, to nie możesz wykonać tej metody. – Dez

5

Po przeszukaniu forum TinyMCE here i here wymyśliłem tego rozwiązania.

tinyMCE.onAddEditor.add(function(manager, editor) { 
    // TinyMCE doesn't change the font of the li portions of the list,          
    // we have do that ourselves here. See http://www.tinymce.com/forum/viewtopic.php?id=26100    
    editor.onExecCommand.add(function(editor, cmd, ui, val) {            
     if (cmd === "FontSize") { 
      var node = editor.selection.getNode();               
      if (node) {                      
       var children = $(node).children("li"); 
       if (children) { 
        // TinyMCE keeps an attribute that we want it to recompute,        
        // clear it. See http://www.tinymce.com/forum/viewtopic.php?id=25676      
        children.removeAttr('data-mce-style');             
        children.css("font-size", val);               
       } 
      }  
     }    
    });      
}); 
+0

Występują pewne problemy z tym podejściem: * MUSISZ * mieć więcej niż 1 pozycję na liście numerowanej i wypunktowanej, aby zmienić rozmiar pracy. I musisz to zrobić * po * tworzysz listy (nie da się tego zrobić, zanim lista zostanie utworzona). To powiedziawszy ... To podejście sprawdza się w tym, czego potrzebował pytający. – Dez

1

Działa to dla mnie:

na tinyMCE.init Dodałem zwrotnego na Setup:

setup: function(ed) { 
     ed.onKeyDown.add(function(ed, e) { 
     tinyMceKeyDownCallbacks(ed,tiny_id); 
     }); 
    } 

następnie funkcje jquery, które aktualizują się co li mający rozpiętość przęsła z ich klasa i lub styl:

function tinyMceKeyDownCallbacks(inst,tiny_id){ 
    var spans = $(inst.getBody()).find("li span"); 
    console.log("S: "+spans.length); 
    spans.each(function(){ 
    var span = $(this); 
    span.parents('li').addClass(span.attr('class')); 
    span.parentsUntil('li').attr('style',span.attr('style')); 
    }); 
} 
+1

możesz najpierw zresetować klasę: span.parents ('li'). RemoveAttr ('class') – adedip

1

Pracowałem nad problemami i dodałem kilka fajnych funkcji. To działa teraz dla mnie:

ed.onExecCommand.add(function(editor, cmd, ui, val) { 
    if (cmd === "FontSize" || cmd === "FontName" || cmd === "ForeColor" || cmd === "Bold" || cmd === "Italic") { 
    var node = editor.selection.getNode(); 
    if (node) { 
     var children = $(node).closest("li"); 
     if(children.length == 0) 
      var children = $(node).find("li"); 

     if (children) { 
      children.removeAttr('data-mce-style'); 
      if(cmd === "FontSize") 
       children.css("font-size", val); 
      if(cmd === "FontName") 
       children.css("font-family", val); 
      if(cmd === "ForeColor") 
       children.css("color", val); 
      if(cmd === "Bold") { 
       if(children.find("strong").length > 0) { 
       children.removeAttr('data-mce-style'); 
       children.css("font-weight", "bold"); 
       } else { 
       children.removeAttr('data-mce-style'); 
       children.css("font-weight", "normal"); 
       } 
      } 
      if(cmd === "Italic") { 
       if(children.find("em").length > 0) { 
       children.removeAttr('data-mce-style'); 
       children.css("font-style", "italic"); 
       } else { 
       children.removeAttr('data-mce-style'); 
       children.css("font-style", "normal"); 
       } 
      } 
     } 
    } 
    } 
    if (cmd === "InsertOrderedList" || cmd === "InsertUnorderedList") { 
    var node = editor.selection.getNode(); 
    if (node) { 
     $(node).find("li").each(function() { 
      var children = $(this).find("span:first"); 
      if (children.length > 0) { 
       $(this).removeAttr('data-mce-style'); 

       if(children.css("font-size") != "undefined") 
        $(this).css("font-size", children.css("font-size")); 
       if(children.css("font-family") != "undefined") 
        $(this).css("font-family", children.css("font-family")); 
       if(children.css("color") != "undefined") 
        $(this).css("color", children.css("color")); 
       if($(this).find("em").length > 0) 
        $(this).css("font-style", "italic"); 
       if($(this).find("strong").length > 0) 
        $(this).css("font-weight", "bold"); 
      } 
     }); 
    } 
    } 
}); 
2

Rozwiązanie Bernharda nie działa dla mnie w TinyMCE 4.1.7, ale kod poniżej nie. Zawarłem trochę kontekstu, aby, mam nadzieję, uczynić to możliwie jak najbardziej zrozumiałym.

To jest z budowniczego strony. Użytkownik otwiera edytor, klikając prawym przyciskiem myszy DIV, który chce edytować i wybierając "tekst" z menu kontekstowego. Spowoduje to uruchomienie tinymce.init, który umieści wbudowany panel edycji w DIV. Kontrola pojawia się tutaj po dodaniu edytora.

Pierwszy editor.on kładzie się w oddzwonieniu, aby złapać koniec tworzenia edytora i w tym momencie uruchamia fokus w celu wyświetlenia edytora. Drugi editor.on przejmuje zmianę na rozpiętość wewnątrz li i dokonuje aktualizacji do li. Trzeci editor.on zatrzymuje zamknięcie edytora.

/************************************************************************************/ 
/*        TinyMCE Events          */ 
/************************************************************************************/ 

tinymce.on('AddEditor', function(e) { 
      // Once editor has been added show it by firing 'focusin' instead of waiting for a click 
       e.editor.on('NodeChange',function(e) { 
        e.target.fire('focusin');   // show the editor 
       }); 

      // Update parent <li> to match a font-size or font-family change in text 
       e.editor.on('ExecCommand',function(e) { 
        var cmd = e.command; 
        if (cmd === "FontSize" || cmd === "FontName") { 
         var val = e.value; 
         var node = e.target.selection.getNode(); // editor in this event object is at target 
         var nodeParent = node.parentNode; 
         if (node.nodeName === "SPAN" && nodeParent.nodeName === "LI") { 
          // We're changing the style of a <span> that's inside an <li>. 
          // Change the <li> to match the new font-size or font-family. 
          // (B, U, and forecolor buttons don't come here so we can't update li here for those) 
          nodeParent$ = $(nodeParent); 
          nodeParent$.removeAttr('data-mce-style'); 
          if(cmd === "FontSize") { 
           nodeParent$.css('font-size',val); 
          } 
          if(cmd === "FontName") { 
           nodeParent$.css('font-family',val); 
          } 
         } 
        } 
      }); 

      // When editor is removed (by clicking in a blank area of the inline panel)  
      // restore draggability to the DIV (had to kill before tinymce.init because draggability 
      // and contenteditable don't work together).  
       e.editor.on('RemoveEditor',function(e) { 
        g.currentElement$.attr('editor_state', "off") 
            .draggable('enable') // make DIV draggable again 
            .removeClass('textCursor'); // give DIV back its pointer cursor 
       }); 
}); 
2

Poddałem trochę edycji odpowiedź Steve'a na użycie tinymce domutils zamiast jquery. Dodałem także zaznaczenie dla całej listy wyboru:

ed.on('ExecCommand', function checkListNodes(evt) { 
    let cmd = evt.command; 
    if (cmd === 'FontSize' || cmd === 'FontName') { 
     let val = evt.value; 
     let node = evt.target.selection.getNode(); 
     let nodeParent = node.parentNode; 
     if (node.nodeName === 'SPAN' && nodeParent.nodeName === 'LI') { 
      if (cmd === 'FontSize') { 
       ed.dom.setStyle(nodeParent, 'font-size', val); 
      } 
      if (cmd === 'FontName') { 
      ed.dom.setStyle(nodeParent, 'font-family', val); 
      } 
     } else if (node.nodeName === 'UL' || node.nodeName === 'OL') { 
      let li = ed.dom.select('li', node); 
      if (cmd === 'FontSize') { 
       ed.dom.setStyle(li, 'font-size', val); 
      } 
      if (cmd === 'FontName') { 
       ed.dom.setStyle(li, 'font-family', val); 
      } 
     } 
    } 
}); 

Pamiętaj, że to niestety nie zadziała w przypadku zmiany koloru.Więcej informacji znajduje się tutaj Can't catch the ForeColor command anymore, tinymce 4.1.4

+0

Używanie tego z tinymce v4.7.1 i działa pięknie. – jxmallett

0

mam manipulowane odpowiedź Steve'a być stosowane w maszynopisie,

editor.on('ExecCommand', function(lob) { 
    contentBlockElement.text = editor.getContent({ format: 'raw' }); 
    var cmd = lob.command; 
    if (cmd === "FontSize" || cmd === "FontName") { 
     var val = lob.value; 
     var node = lob.target.selection.getNode(); 
     var nodeChild = node.childNodes; 
     if (node.nodeName === "UL" && nodeChild[0].nodeName === "LI") { 
      this.nodeChild$ = $(nodeChild); 
      this.nodeChild$.removeAttr('data-mce-style'); 
       if (cmd === "FontSize") { 
        this.nodeChild$.css('font-size', val); 
        } 
       if (cmd === "FontName") { 
        this.nodeChild$.css('font-family', val); 
      } 
     } 
    } 
}); 
0
tinymce.on('AddEditor', function(ea) { 
        ea.editor.on('ExecCommand',function(e) {     
         var cmd = e.command; 
         var val = e.value; 
         var node = e.target.selection.getNode(); 
         var nodeParent = node.parentNode;      
         nodeParent$ = $(nodeParent); 
         node$=$(node); 
         if (cmd === "FontSize" || cmd === "FontName") {       
           while(nodeParent.nodeName !='LI' && nodeParent.nodeName!='BODY'){ 
           nodeParent = nodeParent.parentNode; 
          } 
          nodeParent$ = $(nodeParent);         
           if(node.nodeName==='OL' || node.nodeName==='UL'){        
            if(cmd === "FontSize") { 
             $(node.children).each(function(){ 
             $(this).css('font-size',val); 
            }); 

            } 
            if(cmd === "FontName") { 
             $(node.children).each(function(){ 
             $(this).css('font-family',val); 
            }); 

            } 
           } 

          if (nodeParent.nodeName === "LI") { 
           nodeParent$.removeAttr('data-mce-style');         
           if(cmd === "FontSize") { 
            nodeParent$.css('font-size',val); 
           } 
           if(cmd === "FontName") {         
           nodeParent$.css('font-family',val); 
           }        
         } 

        } 
         if(cmd==='mceToggleFormat' && e.value==='bold'){ 

          while(nodeParent.nodeName !='LI' && nodeParent.nodeName!='BODY'){ 
           nodeParent = nodeParent.parentNode; 
          } 
          nodeParent$ = $(nodeParent); 
          var strg=$(node).find('STRONG'); 
          if(node.childNodes[0].nodeName==='LI' && $(node).find('STRONG').length >1) 
          { 
           $(node.children).each(function(){ 
             $(this).css("font-weight", "bold"); 
            }); 
          } 
          else if(node.childNodes[0].nodeName==='LI' && $(node).find('STRONG').length ==0){         
            $(node.children).each(function(){ 
             $(this).css("font-weight", "normal"); 
            }); 
          } 
          else if($(nodeParent).find('STRONG').length ==1)        
          { 
            if(nodeParent.nodeName==='LI'){ 
            nodeParent$.css("font-weight", "bold"); 
            } 
          } 
          else if($(nodeParent).find('STRONG').length ==0) 
          { 
           nodeParent$.css("font-weight", "normal"); 

          } 

         } 

       });  

       }); 
+0

Chociaż ten kod może odpowiedzieć na pytanie, podanie dodatkowego kontekstu dotyczącego tego, w jaki sposób i/lub dlaczego rozwiązuje problem, poprawiłoby długoterminową wartość odpowiedzi. –

Powiązane problemy