ckeditor - 浏览服务器按钮调用函数不弹出怎么办?

发布于 2024-10-28 05:58:56 字数 233 浏览 1 评论 0原文

我为 ckeditor 创建了一个文件浏览器..在弹出窗口中打开它非常混乱..

我如何绑定浏览服务器上的单击事件,而不是以模式等方式启动文件浏览器。

基本上看看类似的东西

config.filebrowserClick = function(){
// do stuff here that happens when the button gets clicked
}

ive created a file browser for ckeditor.. having it open in a pop-up is messy in the extreme..

how can i tie into the click event on the browse server to rather launch the file browser in a modal etc.

basically looking at something like

config.filebrowserClick = function(){
// do stuff here that happens when the button gets clicked
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静若繁花 2024-11-04 05:58:56

@1I111I1 的答案对我来说非常有用,但缺点是它在所有实例中都会有相同的行为。
如果您需要每个实例进行特殊配置(例如每个实例的根目录可能有不同的默认路径),那么这种方式并不容易。
此外,文件管理器会更好地知道您是否想要图像或文件,因为它们需要不同的查看方法(例如用于图像选择的缩略图模式)。

因此,我对其进行了编辑,以便它可以根据浏览模式(图像/链接)为每个实例触发自定义事件。

CKEDITOR.on('dialogDefinition', function (event) {
var dialogName = event.data.name,
    dialogDefinition = event.data.definition,
    infoTab,
    browse;

// Check if the definition is from the dialog window you are interested in (the "Image" dialog window).
if (dialogName == 'image' || dialogName == 'link') {
    // Get a reference to the "Image Info" tab.
    infoTab = dialogDefinition.getContents('info');

    // Get the "Browse" button
    browse = infoTab.get('browse');

    // Override the "onClick" function
    browse.onClick = function () {
        var me = this,
            dialog = me.getDialog(),
            editor = dialog.getParentEditor(),
            callBack = editor._.filebrowserFn,
            win;

        // This may or may not be needed.  Got it from ckeditor docs. 
        editor._.filebrowserSe = me;

        // when the button gets clicked fire appropriate event 
        if (dialogName == 'image') {
            dialog.getParentEditor().fire('browseImageRequest', callBack);
        } else {
            dialog.getParentEditor().fire('browseLinkRequest', callBack);
        }



    }

}

});

现在,您可以分别在每个实例中注册适当的事件,然后使用提供的回调返回选定的值:

    CKEDITOR.instances['myinstance'].on('browseImageRequest', (event)=> {
        // Call back to CKEDITOR with new path to image (i.e. replace path below with new url for image)
        window["CKEDITOR"].tools.callFunction(event.data, 'images/noimage.jpg');
    });

    CKEDITOR.instances['myinstance'].on('browseLinkRequest', (event) => {
        // Call back to CKEDITOR with new path to image (i.e. replace path below with new url)
        window["CKEDITOR"].tools.callFunction(event.data, 'http://www.google.com');
    });

另请注意,在这之前,您应该使用弹出窗口模式所需的配置激活浏览按钮,那么此解决方法将覆盖 onClick 操作,并且不会打开弹出窗口,而是触发相应的事件

CKEDITOR.replace( 'editor1', {
                filebrowserBrowseUrl: '#',
                filebrowserImageBrowseUrl: '#'
});

The answer by @1I111I1 was very useful for me, but the downside is that it would have the same behavior among all instances.
If you need special configuration per instance (for example each instance might have a different default path for its root directory), that would not be easy this way.
Also, a file manager would better know whether you want an image or a file because they would require a different view method (like thumbnail mode for image select).

So, I have edited it so that it would fire custom events for each instance based on the browse mode (image/link).

CKEDITOR.on('dialogDefinition', function (event) {
var dialogName = event.data.name,
    dialogDefinition = event.data.definition,
    infoTab,
    browse;

// Check if the definition is from the dialog window you are interested in (the "Image" dialog window).
if (dialogName == 'image' || dialogName == 'link') {
    // Get a reference to the "Image Info" tab.
    infoTab = dialogDefinition.getContents('info');

    // Get the "Browse" button
    browse = infoTab.get('browse');

    // Override the "onClick" function
    browse.onClick = function () {
        var me = this,
            dialog = me.getDialog(),
            editor = dialog.getParentEditor(),
            callBack = editor._.filebrowserFn,
            win;

        // This may or may not be needed.  Got it from ckeditor docs. 
        editor._.filebrowserSe = me;

        // when the button gets clicked fire appropriate event 
        if (dialogName == 'image') {
            dialog.getParentEditor().fire('browseImageRequest', callBack);
        } else {
            dialog.getParentEditor().fire('browseLinkRequest', callBack);
        }



    }

}

});

Now you can register for the appropriate event in each instance individually and then use the provided callback to return the selected value:

    CKEDITOR.instances['myinstance'].on('browseImageRequest', (event)=> {
        // Call back to CKEDITOR with new path to image (i.e. replace path below with new url for image)
        window["CKEDITOR"].tools.callFunction(event.data, 'images/noimage.jpg');
    });

    CKEDITOR.instances['myinstance'].on('browseLinkRequest', (event) => {
        // Call back to CKEDITOR with new path to image (i.e. replace path below with new url)
        window["CKEDITOR"].tools.callFunction(event.data, 'http://www.google.com');
    });

Also note that before all of this, you should activate the browse button with the configuration that is required for pop-up window mode, then this workaround will override the onClick action and instead of opening pop-ups it will fire the appropriate event

CKEDITOR.replace( 'editor1', {
                filebrowserBrowseUrl: '#',
                filebrowserImageBrowseUrl: '#'
});
盗梦空间 2024-11-04 05:58:56

100%可能,而且其他人也在这么做。您可以看到此功能在 Mailchimp.com 上的 OP 请求中起作用,

“Matt Ball”已编辑问题并回答说这是不可能的,但他似乎不熟悉 CKEditor 和 CKFinder,而是认为 OP 想要使用浏览器的“浏览文件” - 但这显然不是OP正在寻找的。

OP 有一个文件浏览器,例如在弹出窗口中运行的 CKFinder。他希望将文件浏览器作为模式窗口调用,而不是作为弹出窗口调用。

我也在研究如何做到这一点,我怀疑它将涉及编辑 _source/plugins/image 下的文件并修改子浏览器(这是如何调用 CKFinder 的技术术语)以代替作为模式加载。这里的第一个问题是违反封装性,因为模式窗口的 Jquery 代码不属于 CKEditor 内...

It 100% is possible, and other people are doing it. You can see this feature working as the OP requests on Mailchimp.com

"Matt Ball" has edited the question and replied that it is not possible, but he seemingly is not familiar with CKEditor and CKFinder, and is instead thinking that the OP is wanting to use the browser's "browse file" - but that is clearly not what the OP is looking for.

The OP has a file browser such as CKFinder running within a popup window. Instead of invoking as a popup he would like to invoke the file browser as a modal window.

I am also looking at how to do this, and I suspect it will involve editing the files under _source/plugins/image and modifying the Child Browser (that's the technical term for how CKFinder is invoked) to instead load as a modal. The number one problem here is the violation of encapsulation, as Jquery code for modal windows does not belong within CKEditor...

合久必婚 2024-11-04 05:58:56

我最近不得不做一些非常类似的事情,并决定自己解决。完成后,我给自己做了一个记录,以便回来发布解决方案。那是 3 个月前的事了,但这里是我所做的一个简化版本,它基本上是使用 ckeditor 的 browserServer 函数(也包含在下面)中的修改代码来覆盖 onClick 函数。

CKEDITOR.on( 'dialogDefinition', function( event ) {
    var dialogName      = event.data.name,
        dialogDefinition = event.data.definition,
        infoTab,
        browse;

    // Check if the definition is from the dialog window you are interested in (the "Image" dialog window).
    if ( dialogName == 'image' ) {
        // Get a reference to the "Image Info" tab.
        infoTab = dialogDefinition.getContents( 'info' );

        // Get the "Browse" button
        browse = infoTab.get( 'browse' );

        // Override the "onClick" function
        browse.onClick = function () {
            var  me      = this,
                dialog   = me.getDialog(),
                editor   = dialog.getParentEditor(),
                callBack = editor._.filebrowserFn,
                win;

            // This may or may not be needed.  Got it from ckeditor docs. 
            editor._.filebrowserSe = me;

            // do stuff here that happens when the button gets clicked

            // Call back to CKEDITOR with new path to image (i.e. replace path below with new url for image)
            window.CKEDITOR.tools.callFunction( callBack, 'path' );

        }

    }

});

这是我从 ckeditor 文档中获得的 browserServer 函数,可以在这里找到 - http://docs .ckeditor.com/source/plugin33.html

function browseServer() {
    var dialog = this.getDialog();
    var editor = dialog.getParentEditor();

    editor._.filebrowserSe = this;

    var width = editor.config[ 'filebrowser' + ucFirst( dialog.getName() ) + 'WindowWidth' ] || editor.config.filebrowserWindowWidth || '80%';
    var height = editor.config[ 'filebrowser' + ucFirst( dialog.getName() ) + 'WindowHeight' ] || editor.config.filebrowserWindowHeight || '70%';

    var params = this.filebrowser.params || {};
    params.CKEditor = editor.name;
    params.CKEditorFuncNum = editor._.filebrowserFn;
    if ( !params.langCode )
        params.langCode = editor.langCode;

    var url = addQueryString( this.filebrowser.url, params );
    // TODO: V4: Remove backward compatibility (#8163).
    editor.popup( url, width, height, editor.config.filebrowserWindowFeatures || editor.config.fileBrowserWindowFeatures );
}

我希望这有帮助。抱歉我没有早点回答。

(注意:我使用的是 ckeditor 4.4.7,但我认为其他版本应该类似)

I had to do something very similar recently and decided to figure it out on my own. Once I did, I made myself a note to come back and post the solution. That was 3 months ago, but here is a simplified version of what I did, which was basically overriding the onClick function with modified code from ckeditor's browseServer function (included below as well).

CKEDITOR.on( 'dialogDefinition', function( event ) {
    var dialogName      = event.data.name,
        dialogDefinition = event.data.definition,
        infoTab,
        browse;

    // Check if the definition is from the dialog window you are interested in (the "Image" dialog window).
    if ( dialogName == 'image' ) {
        // Get a reference to the "Image Info" tab.
        infoTab = dialogDefinition.getContents( 'info' );

        // Get the "Browse" button
        browse = infoTab.get( 'browse' );

        // Override the "onClick" function
        browse.onClick = function () {
            var  me      = this,
                dialog   = me.getDialog(),
                editor   = dialog.getParentEditor(),
                callBack = editor._.filebrowserFn,
                win;

            // This may or may not be needed.  Got it from ckeditor docs. 
            editor._.filebrowserSe = me;

            // do stuff here that happens when the button gets clicked

            // Call back to CKEDITOR with new path to image (i.e. replace path below with new url for image)
            window.CKEDITOR.tools.callFunction( callBack, 'path' );

        }

    }

});

Here is the browseServer function I got from the ckeditor docs, which can be found here - http://docs.ckeditor.com/source/plugin33.html.

function browseServer() {
    var dialog = this.getDialog();
    var editor = dialog.getParentEditor();

    editor._.filebrowserSe = this;

    var width = editor.config[ 'filebrowser' + ucFirst( dialog.getName() ) + 'WindowWidth' ] || editor.config.filebrowserWindowWidth || '80%';
    var height = editor.config[ 'filebrowser' + ucFirst( dialog.getName() ) + 'WindowHeight' ] || editor.config.filebrowserWindowHeight || '70%';

    var params = this.filebrowser.params || {};
    params.CKEditor = editor.name;
    params.CKEditorFuncNum = editor._.filebrowserFn;
    if ( !params.langCode )
        params.langCode = editor.langCode;

    var url = addQueryString( this.filebrowser.url, params );
    // TODO: V4: Remove backward compatibility (#8163).
    editor.popup( url, width, height, editor.config.filebrowserWindowFeatures || editor.config.fileBrowserWindowFeatures );
}

I hope this helps. Sorry I didn't answer sooner.

(Note: I'm using ckeditor 4.4.7, but I assume it should be similar for other versions)

卷耳 2024-11-04 05:58:56

如果我明白你想做什么,那是不可能的。出于安全原因,浏览器中的 JavaScript 被沙箱化,因此它无法访问本地文件系统。

If I understand what you're trying to do, it's not possible. For security reasons, JavaScript in the browser is sandboxed so that it has no access to the local filesystem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文