使用 ckeditor 类进行对齐
我正在使用 ckeditor,它真的很棒。我遇到的唯一问题是我在对齐对象时尝试使用类而不是样式。更具体地说,我想使用类 alignleft 来对齐左侧图像,而不是使用 style="float:left"。为此,我修改了插件 ckeditor/plugins/image/dialogs/image.js
在 id:'cmbAlign' 中,我更改了设置和commid方法,以便它应该添加类“alignleft”或“alignright”取决于情况,但奇怪的是,当我尝试更改类时它不起作用(使用 removeClass 或 addClass,setAttribute('class') 也不起作用)。代码是:
id:'cmbAlign',
type:'select',
widths:['35%','65%'],
style:'width:90px',
label:b.lang.common.align,
'default':'',
items:[[b.lang.common.notSet,'none'],[b.lang.common.alignLeft,'left'],[b.lang.common.alignRight,'right']],
onChange:function(){
l(this.getDialog());
o.call(this,'advanced:txtdlgGenStyle');
},
setup:function(C,D){
if(C==d){
var E=(D.getAttribute('class')+'').match(/align([a-z]+)?/);
if(E&&E[1])E = E[1];else E='';
this.setValue(E);
}
},
commit:function(C,D,E){
var F = (this.getValue()+'');
if(C==d||C==f){
var G=(D.getAttribute('class')+'').match(/align([a-z]+)?/);
if(G&&G[1]){
if(F!=G[1]){
D.removeClass('align'+G[1]);
if(F!=='')D.setAttribute('rel',F); D.addClass('align'+F);
}
} else {
if(F!=='')D.setAttribute('rel',F); D.addClass('align'+F);
}
}
}
请注意,我已经添加了D.setAttribute('rel',F)
并且它工作完美。
有人对此有任何想法或解决方案吗?
I am using ckeditor and it is really great. The only one problem I have is that I am trying to use classes instead of style when aligning objects. More specifically, I want to use a class alignleft for align left images instead of using style="float:left". For this purpose I modified the plugin ckeditor/plugins/image/dialogs/image.js
In id:'cmbAlign' I changed the setup and the commid method so that it should add the class "alignleft" or "alignright" depending the situation but strangelly It does not work when I try to change the class (using removeClass or addClass, setAttribute('class') does not work too). The code is:
id:'cmbAlign',
type:'select',
widths:['35%','65%'],
style:'width:90px',
label:b.lang.common.align,
'default':'',
items:[[b.lang.common.notSet,'none'],[b.lang.common.alignLeft,'left'],[b.lang.common.alignRight,'right']],
onChange:function(){
l(this.getDialog());
o.call(this,'advanced:txtdlgGenStyle');
},
setup:function(C,D){
if(C==d){
var E=(D.getAttribute('class')+'').match(/align([a-z]+)?/);
if(E&&E[1])E = E[1];else E='';
this.setValue(E);
}
},
commit:function(C,D,E){
var F = (this.getValue()+'');
if(C==d||C==f){
var G=(D.getAttribute('class')+'').match(/align([a-z]+)?/);
if(G&&G[1]){
if(F!=G[1]){
D.removeClass('align'+G[1]);
if(F!=='')D.setAttribute('rel',F); D.addClass('align'+F);
}
} else {
if(F!=='')D.setAttribute('rel',F); D.addClass('align'+F);
}
}
}
Please note that I have added D.setAttribute('rel',F)
and it works perfect.
Does anybody have any idea or solution for it??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有更多信息:
该代码的解压版本位于:
ckeditor/_source/plugins/image/dialogs/image.js (第 994 行)
而不是猜测所有这些字母代表什么,您可以看到实际的对象,涉及到的变量和函数。
我发布的有关侦听对话框事件的信息是基于查看该代码并了解其功能。
您需要阻止对话框添加内联浮动样式,否则它将覆盖您应用于类的样式。浮动不会作为类添加,因此使用 addClass 或 removeClass 不会对其产生任何影响。这意味着您需要以不同的方式解决问题。
以下是我想到的几种可能性,我希望其他人可以建议其他方法:
1)您可以修改图像插件中的代码(但如果编辑器更新,它可能会被覆盖)。
2) 复制图像插件并修改代码以制作自定义插件,然后将图像按钮替换为调用您的插件的按钮。
3) 通过侦听对话框事件从对话框代码外部工作,然后停止添加内联浮动样式并添加您的类。
希望这有帮助,
乔
您使用什么版本的 CKEditor?我用的是3.6.1。
我在 ckeditor/plugins/image/dialogs/image.js 文件中搜索了“cmbAlign”。我在第 11 行修改了这一点:
我将“width:90px”更改为“width:290px”,对话框变得更宽。因此对该文件的更改会对我的安装产生影响。
您是否可能正在使用 _source 目录中找到的解压文件?:
ckeditor/_source/plugins/image/dialogs/image.js
只是提到它,因为除非您加载 ckeditor_basic 文件之一而不是 ckeditor.js 文件,否则不会加载这些文件。
一种可能的方法:
您可以制作一个自定义插件来替换图像插件。这里有一个教程部分,其中包含创建插件的简单说明:
http://docs。 cksource.com/CKEditor_3.x/Tutorials
您可以使用它作为插件工作方式的概述,但您实际上只需对默认图像插件进行一些更改。
只需复制图像插件文件夹从 ckeditor/_source/plugins 目录中将其放入 ckeditor/plugins 目录中。您需要重命名该插件并禁用默认图像插件。然后,您可以对 image.js 文件进行帖子中概述的更改。
要禁用默认图像插件,请将其添加到您的配置设置中:
另一种可能的方法:
向页面添加事件侦听器,您可以设置脚本来执行以下操作:
监视“dialogShow”事件并检查打开的对话框是否是图像对话框。
如果是,监听对齐选择器的change事件。
当对齐选择器更改时,检查是否选择了“左对齐”。
如果是这样,请停止该命令的传播,并将您的类名插入“高级”选项卡的“样式表类”字段中。
您可以将选择器更改的事件侦听器的优先级设置为“1”,以便它先于运行默认代码。 “on”方法的文档显示了可用的参数:
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.event.html#on
您可以查看以下帖子以获取示例代码以及有关设置此功能的更多信息(jQuery使用):
ckeditor 将事件处理程序添加到对话框元素
在那篇文章中,答案的第二部分(靠近顶部)开头为:
开始引用
如果您想监听另一个位置对选择元素的更改,您可以创建一个监听“dialogShow”事件的监听器。这是一个示例:
结束引用
此外,您还需要将“dialogShow”事件的侦听器包装在“instanceReady”事件的侦听器中,以下是一些示例代码(使用了 jQuery):
如果您有任何问题,请告诉我关于我所写的内容。
祝你好运,
乔
Here is a little more info:
The unpacked version of that code is in:
ckeditor/_source/plugins/image/dialogs/image.js ( line 994 )
Instead of guessing what all those letters stand for, you can see the actual objects, variables and functions that are involved.
The information I posted about listening for the dialog events was based on looking at that code and seeing what it does.
You need to stop the dialog from adding the inline float style or it will override the style that you apply to your class. The float isn't being added as a class, so using addClass or removeClass will have no effect on it. That means you need to approach the problem in a different way.
Here are a few possibilities that came to my mind, I expect that other people can suggest additional approaches:
1) You can modify the code in the image plugin ( but it could be overwritten if the editor is updated ).
2) Make a copy of the image plugin and modify the code to make a custom plugin, then replace the image button with one that calls your plugin.
3) Work from outside the dialog code by listening for the dialog event, then stop the inline float style from being added and add your class instead.
Hope this helps,
Joe
What version of CKEditor are you using? I'm using 3.6.1.
I searched for "cmbAlign" in the ckeditor/plugins/image/dialogs/image.js file. I modified this bit on line 11:
I changed "width:90px" to "width:290px" and the dialog box got a lot wider. So a change to that file has an effect in my install.
Is it possible that you are working with the unpacked file found in the _source directory?:
ckeditor/_source/plugins/image/dialogs/image.js
Just mentioning it because those files aren't loaded unless you are loading one of the ckeditor_basic files instead of the ckeditor.js file.
A Possible Approach:
You could make a custom plugin that replaces the image plugin. There's a tutorial section with easy instructions for creating a plugin here:
http://docs.cksource.com/CKEditor_3.x/Tutorials
You can use that as an overview of how plugins work, but you'll really just be making a few changes to the default image plugin.
Just copy the image plugin folder from the ckeditor/_source/plugins directory and put it in the ckeditor/plugins directory. You'll need to rename the plugin and disable the default image plugin. Then you can make the changes that you outlined in your post to the image.js file.
To disable the default image plugin add this to your config settings:
Another Possible Approach:
Add an event listener to the page, you can set up a script to do the following:
Watch for the "dialogShow" event and check if the dialog that opened is the image dialog.
If it is, listen for the change event of the Alignment selector.
When the Alignment selector is changed, check if "align left" was selected.
If so, stop the command from propagating and insert your class name in the "Stylesheet Classes" field of the Advanced tab.
You can set the priority of your event listener for the selector change to "1", so it goes before the default code is run. The docs for the "on" method show the available parameters:
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.event.html#on
You can look at the following post for sample code and further info about setting this up (jQuery is used):
ckeditor add event handler to dialog element
In that post, the second section of the answer (near the top) begins with:
Start Quote
If you would like to listen for a change to a select element from another location, you can create a listener that keys on the "dialogShow" event. Here's an example:
End Quote
Additionally, you'll want to wrap the listener for the "dialogShow" event inside a listener for the "instanceReady" event, here is some sample code for that (jQuery is used):
Let me know if you have any questions about what I've written.
Be Well,
Joe
如果您更改了核心文件,请在升级 CKEditor 时小心。
最简单(但对于编辑器来说不是最方便)的解决方案是:
If you changes core files be careful when upgrading CKEditor.
The easiest (but not the most convenient for the editor) solution is: