gpt4 book ai didi

javascript - 通过 JavaScript 从 CKEditor 的样式框中分配一些样式

转载 作者:行者123 更新时间:2023-11-30 13:33:09 28 4
gpt4 key购买 nike

如何通过 JS 模拟用户从样式框中选择某些样式?我想放一些快捷按钮,一键分配一些流行的样式。

编辑:

  • 我不关心它是编辑器内按钮还是外部按钮。
  • 我不想css-style赋值;我想要 CKEditor-style 赋值(样式框的那些)。

最佳答案

我没有使用过 CKEditor,但是,我看到了你的问题并认为“弄清楚这会很有趣。”好吧,这就是我的发现:

(是的,我发现了糟糕的文档,但是,这不是重点……不过,我会为他们提供支持以评论他们的代码。)

///
// function to add buttons that trigger styles to be applied.
//
// editor - CKEDITOR - instance of editor you want command attached to.
// buttonName - String - name of the button
// buttonLabel - String - humane readable name of the button
// commandName - String - name of command, the way to call this command from CKEDITOR.execCommand()
// styleDefinition - StyleDefinition - obj defining the style you would like to apply when this command is called.
///

var addButtonCommand = function( editor, buttonName, buttonLabel, commandName, styleDefiniton )
{
var style = new CKEDITOR.style( styleDefiniton );
editor.attachStyleStateChange( style, function( state )
{
!editor.readOnly && editor.getCommand( commandName ).setState( state );
});
editor.addCommand( commandName, new CKEDITOR.styleCommand( style ) );
editor.ui.addButton( buttonName,
{
label : buttonLabel,

command : commandName
//adding an icon here should display the button on the toolbar.
//icon : "path to img",
});
};

//Get the editor instance you want to use. Normally the same as the ID of the textarea CKEditor binds to.
var editor1 = CKEDITOR.instances.editor1;

//If you look at ckeditor/_source/plugins/styles/default.js you will see that this selects the first element. That list is read into the array 'default'.
var blueTitleStyle = CKEDITOR.stylesSet.registered.default[0];

//Or, you can define the style like this: See http://dev.ckeditor.com/wiki/Components/Styles for more info on style definitions.
var blueTitleStyle = {
name : 'Blue Title',
element : 'h3',
styles : { 'color' : 'Blue' }
};

addButtonCommand(editor1, 'BlueTitle', 'BlueTitle', 'bluetitle', blueTitleStyle);

这里是一个 Javascript 函数来帮助你的点击事件:

//function used to execute the command.  Only used for calling the command when not calling from a button. (Like an A with an onClick bound to it.)
//pulled this function right out of the api.html example in the ckeditor/_samples dir.
function ExecuteCommand( commandName )
{
// Get the editor instance that we want to interact with.
var oEditor = CKEDITOR.instances.editor1;

// Check the active editing mode.
if ( oEditor.mode == 'wysiwyg' )
{
// Execute the command.
// http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#execCommand
oEditor.execCommand( commandName );
}
else
{
alert( 'You must be in WYSIWYG mode!' );
}
}

现在,您可以像这样创建一个链接:

<a href='#' class='setBlueTitle'>Set Blue Title</a>

并使用一些 jQuery 让它变得有趣:

 <script type="text/javascript">
$(document).ready(function(){
$(".setBlueTitle").onClick(function(e){
//stops the click from changing the page and whatever other default action would happen.
e.preventDefault();

ExecuteCommand('bluetitle');
});
});
</script>

我不是 100% 确定按钮图标部分。我没有可以尝试的图标。但是,根据一些帖子,它应该可以正常工作。无论如何,jQuery 点击绑定(bind)都有效。

应该差不多了吧!我不得不进行大量挖掘才能弄清楚这一点,但看到它有效确实令人满意!

关于javascript - 通过 JavaScript 从 CKEditor 的样式框中分配一些样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5954347/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com