Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the color of the selection in TinyMCE?

I am creating an application with a TinyMCE editor embedded inside it. I want the controls for my application to update when the selection inside the tinyMCE editor changes, so the font, size and color menus show the font, size and color of the selection. Font and color work fine, but I can't figure out how to get the color. Here is the code I am using:

myTinyMCESettings.handle_node_change_callback = function(editor_id,node,undo_index,undo_levels,visual_aid,any_selection){
    var editor = tinyMCE.get(editor_id);
    selectionChanged(editor,!any_selection);
};

tinyMCE.init(myTinyMCESettings);

function selectionChanged(ed,selection){    
    var fontName = ed.queryCommandValue('FontName');
    var size = parseInt(ed.queryCommandValue('FontSize'));
    var color = ed.queryCommandValue('ForeColor');
}

But color === false. How can I get the foreground color of the selected text or the text at the insertion point within tinyMCE?

EDIT: Tracking this down further, on line 12377 of tiny_mce_prototype_src.js I see:

// Registred commands
o = t.editorCommands.queryCommandValue(c);

When I walk through this in my debugger, t.editorCommands.queryCommandValue(c); returns false.

like image 616
Josh Avatar asked Jan 27 '12 00:01

Josh


People also ask

How do you add color to TinyMCE?

This plugin adds the forecolor/backcolor button controls that enable you to pick colors from a color picker and apply these to text. It adds a toolbar button to enable this functionality.

How do I get TinyMCE content?

The TinyMCE getContent and setContent methods You can do this using the getContent() API method. Let's say you have initialized the editor on a textarea with id=”myTextarea”. This will return the content in the editor marked up as HTML.

How do you add CSS to TinyMCE editor?

These content CSS files can be enabled in the editor using the content_css configuration option. Copied! These content CSS files can also be used as a template for creating a custom content CSS file for the editor. For the CSS files, see: tinymce-dist GitHub Repository - Content CSS files.


1 Answers

I would try to do it in another way (did not check it) - taking computed style:

myTinyMCESettings.handle_node_change_callback = function(editor_id,node,undo_index,undo_levels,visual_aid,any_selection){
    var editor = tinyMCE.get(editor_id);
    var color =  tinyMCE.DOM.getStyle(node, 'color', true); // computes current color
    selectionChanged(editor,!any_selection);
};
like image 92
Cheery Avatar answered Sep 30 '22 17:09

Cheery