我有一个小功能,允许用户单击文本区域下的按钮,并插入相关文本(这与jQuery配合使用textrange
插件)。
然而,因为我在插件中使用的是TinyMCE编辑器,所以它只能在“文本”模式下工作,而不能在“视觉”模式下工作。
我已经查看了源代码,但不知道如何使其同时适用于这两种情况。有人知道在“视觉”模式下如何将文本插入编辑器吗?谢谢
$(document).ready(function(){
var button_text = {} // The text to insert for each button click
button_text[\'insert_event_name\'] = \'event_name\';
button_text[\'insert_event_date\'] = \'event_date\';
button_text[\'insert_event_start_time\'] = \'event_start\';
button_text[\'insert_event_finish_time\'] = \'event_finish\';
button_text[\'insert_event_location\'] = \'event_location\';
button_text[\'insert_event_address_inline\'] = \'event_address_inline\';
button_text[\'insert_event_address_blick\'] = \'event_address_block\';
$(\'.inserter\', \'#event-desc-container\').on(\'click\', function(){
var button = $(this).attr(\'name\'),
text = \'[\'+button_text[button]+\']\',
textarea = $(\'textarea#event_desc\', \'#event-desc-container\');
/** Get the location (start position) of where the cursor is in the textarea */
var start = textarea.textrange(\'get\', \'start\');
/** Add the relevent button text to the start position */
textarea.textrange(\'replace\', text).trigger(\'updateInfo\').focus();
/** The newly added text will be selected, so work out the end position of that selection */
var end = textarea.textrange(\'get\', \'end\');
/** Move the cart to the end position, so that no text is highlighted */
textarea.textrange(\'set\', end, 0).trigger(\'updateInfo\').focus();
});
});