简体   繁体   English

如果存在模态,如何取消模糊事件?

[英]How to cancel the blur event if a modal is present?

I'm using the WYSIWYG HTML editor Redactor as an inline editor. 我使用WYSIWYG HTML编辑器Redactor作为内联编辑器。

When the user clicks an editable line and the editor is invoked, upon blur, the editor is destroyed and the data saved. 当用户单击可编辑的行并调用编辑器时,模糊时会破坏编辑器并保存数据。

            element.bind('click', function($event) {
                element.redactor({
                    focus: true
                });
            });
            element.on('blur', function($event){
                var ckValue = element.getCode();
                scope.$apply(function () {
                    setter(scope, ckValue);
                });
                element.destroyEditor();
                compileElement();
            });

The issue I'm having is when the editor opens a modal for uploading a picture. 我遇到的问题是编辑器打开用于上传图片的模式时。 Focus is changed to the modal window. 焦点更改为模式窗口。 Is there a way to change/chain focus so the blur event is not triggered when a modal is opened? 有没有一种方法可以更改/链接焦点,以便在打开模态时不触发模糊事件?

You can do it in many ways. 您可以通过多种方式进行操作。 Here I come up with one of them. 在这里,我提出了其中之一。 When the editor opens modal you just unbind the blur event on element as: 当编辑器打开模式时,您只需将元素上的blur事件解除绑定为:

element.unbind('blur');

So that blur event is not triggered. 这样就不会触发模糊事件。

And, after closing modal again bind blur event on element as: 并且,在关闭模态之后,再次将元素上的模糊事件绑定为:

element.bind('blur');

That's it. 而已。

Should be pretty straightforward: 应该很简单:

element.on('blur', function($event) {
    if ($("#IdValueOfTheImageUploader").length < 1) {
        var ckValue = element.getCode();
        scope.$apply(function () {
            setter(scope, ckValue);
        });
        element.destroyEditor();
        compileElement();
    }
});

Alternately, if the uploader is already built, but not visible, change the if statement to: 或者,如果已经构建了上传器,但不可见,则将if语句更改为:

    if ($("#IdValueOfTheImageUploader:visible").length < 1) {

Basically, it checks to see if the uploader is present and skips firing the "save and destroy" logic, if it is. 基本上,它检查是否存在上载器,并跳过触发“保存并销毁”逻辑(如果存在)。

Depending on how you are adding the image uploader, you may also have to be careful of running into is a race event (ie, the image uploader has not loaded on the page [or, been made visible, if using the second approach], by the time the code checks for its existence). 根据您添加图像上传器的方式,您可能还必须小心避免发生竞赛事件(即,图像上传器尚未加载到页面上(或者,如果使用第二种方法,则使其可见),在代码检查其存在时)。 You should be able to avoid it, by building the uploader before switching focus, but, if not, then add in a timeout, to give it time to load/display. 您应该能够避免这种情况,方法是在切换焦点之前构建上载器,但如果没有,则添加一个超时时间,以使其有时间加载/显示。

I omitted some other needs/issues. 我省略了其他一些需求/问题。 Namely invoking and dealing with multiple redactor instances in the window. 即在窗口中调用并处理多个编辑器实例。 We need to be able to destroy the current instance on blur without creating a new instance. 我们需要能够在不创建新实例的情况下模糊破坏当前实例。 To do so I created a few checks for the various redactor components. 为此,我为各种编辑器组件创建了一些检查。

            // check if blur event was triggered
            // by entering editor dropdown menu
            function checkDropDisplay(){
                return $('.redactor_dropdown:hover').length > 0
            }

            // check if currently hovering over toolbar
            function checkHover(){
                return $('.redactor_box:hover').length > 0
            }

            // check if a redactor modal is present
            function checkModalDisplay(){
                return $("#redactor_modal").is(":visible")
            }

            // save editor contents upon leaving editor
            element.bind('blur', function($event){
                // get editor contents and apply to scope
                var editorValue = element.context.innerHTML;
                scope.$apply(function () {
                    setter(scope, editorValue);
                });
                // escape timeout or save & destroy editor
                $timeout(function(){
                    if(checkModalDisplay() || checkDropDisplay() || checkHover()){
                        return false;
                    } else {
                        element.data('redactor').destroy();
                        compileElement();
                    }
                },1);
            }); 

For anyone interested, Redactor only has a series of callback events: modalOpenCallback, dropdownshowCallback, ImageuploadcallBack. 对于感兴趣的任何人,Redactor仅具有一系列回调事件:modalOpenCallback,dropdownshowCallback,ImageuploadcallBack。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM