Modify CKEditor link dialog to add custom attribute to links

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 03:40:35

You best option is to modify the plugin. So you need to open the source code and find the file links.js in c:\ckeditor_3.6.5\ckeditor\_source\plugins\link\dialogs\

The source code is quite big (40k) but here you can modify the dialog however you want. When you finish just copy it to your plugins folder, and compress it: http://jscompress.com/

I have done what you need myself. The whole uncompressed file is here: https://gist.github.com/3940239

What you need to do:

First add this line just before the dialog "browse" button is appended. Approx. in line: 547:

                        {
                            id: "button",
                            type: "checkbox",
                            label: "Button",
                            setup: function (data) {
                                this.allowOnChange = false;

                                if (data.button)
                                    this.setValue(data.button);

                                this.allowOnChange = true;
                            },
                            commit: function (data) {
                                data.button = this.getValue()
                                this.allowOnChange = false;
                            }
                        },

This part is actually your code. I just copied and pasted it.

Then, go to the onOk function, approx. in line 1211: and after commitContent add this code:

this.commitContent( data );

//My custom attribute
if (data.button)
    attributes["custom-attribute"] = "button";
else
    attributes["custom-attribute"] = "";

This will modify your link adding the attribute to the element such as <a href="#" custom-attribute="button">text</a>

That's it. Although, you may also like to load the current status of the checkbox. Then, go to the function parseLink . Approx. line 179 to load the attributes:

...
if ( element )
{
    retval.button = element.getAttribute('custom-attribute');

    var target = element.getAttribute( 'target' );
...

I am exploring the same thing now. What I have decided to do at this point is to:

  1. Get a base ckeditor install without the link plugin
  2. create my own fork of the link plugin, and add my changes to it, then activate and use this plugin within the group that link normally shows up in.

...working with it as a custom plugin (albeit a copy of the original) should alleviate the problem of upgrading. You just simply do not use the original link plugin at all. Copy and rename it, and use your custom copy instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!