最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Modify CKEditor link dialog to add custom attribute to links - Stack Overflow

programmeradmin4浏览0评论

I am using CKEditor on a website and I need to be able to put a special data attributes on some of the links created through the editor. The user would indicate that they need the special attribute put on the link by checking a checkbox in the link dialog. I have managed to add a checkbox to the link dialog with the following code:

CKEDITOR.on('dialogDefinition', function(ev) {
    if (ev.data.name == "link") {
        var info = dialog.getContents("info");
        info.elements.push({
            type: "vbox",
            id: "urlOptions",
            children: [{
                type: "hbox",
                children: [{
                    id: "button",
                    type: "checkbox",
                    label: "Button",
                    mit: function(data) {
                        data.button = this.getValue()
                        console.log("mit", data.button, data);
                    },
                    setup: function(data) {
                        this.setValue(data.button);
                        console.log("setup", data.button, data);
                    }
                }]
            }]
        });
    }
});

Now I have two problems. The first one is that despite me adding the code in the mit and setup functions that should save the state of the checkbox, it's not working. It's as if the data can't hold any other parameters but the ones there by default.

The second problem is that I don't know how to add / remove the data attribute on my links. It seems to me that I should be doing that in my onOk callback on the dialog, however, the link dialog already has an onOk callback, so I'm not sure how I should be proceeding. I, of course, do not want to modify any of CKEditor's files directly.

How can I acplish these things?

I am using CKEditor on a website and I need to be able to put a special data attributes on some of the links created through the editor. The user would indicate that they need the special attribute put on the link by checking a checkbox in the link dialog. I have managed to add a checkbox to the link dialog with the following code:

CKEDITOR.on('dialogDefinition', function(ev) {
    if (ev.data.name == "link") {
        var info = dialog.getContents("info");
        info.elements.push({
            type: "vbox",
            id: "urlOptions",
            children: [{
                type: "hbox",
                children: [{
                    id: "button",
                    type: "checkbox",
                    label: "Button",
                    mit: function(data) {
                        data.button = this.getValue()
                        console.log("mit", data.button, data);
                    },
                    setup: function(data) {
                        this.setValue(data.button);
                        console.log("setup", data.button, data);
                    }
                }]
            }]
        });
    }
});

Now I have two problems. The first one is that despite me adding the code in the mit and setup functions that should save the state of the checkbox, it's not working. It's as if the data can't hold any other parameters but the ones there by default.

The second problem is that I don't know how to add / remove the data attribute on my links. It seems to me that I should be doing that in my onOk callback on the dialog, however, the link dialog already has an onOk callback, so I'm not sure how I should be proceeding. I, of course, do not want to modify any of CKEditor's files directly.

How can I acplish these things?

Share Improve this question asked Oct 15, 2012 at 20:24 Alex TurpinAlex Turpin 47.8k23 gold badges116 silver badges146 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10 +200

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 press it: http://jspress./

I have done what you need myself. The whole unpressed file is here: https://gist.github./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;
                            },
                            mit: 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 mitContent add this code:

this.mitContent( 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.

发布评论

评论列表(0)

  1. 暂无评论