简体   繁体   English

fabric.js获得IText对象的“编辑”状态

[英]fabric.js get “edit” state of IText object

In fabric.js I want to add some key interaction on my canvas. 在fabric.js中,我想在画布上添加一些关键交互。 If an IText object is selected I want it to be deleted if I hit the "del" key. 如果选择了IText对象,我希望在按下“del”键时将其删除。 I accomplished this by: 我完成了这个:

$('html').keyup(function(e){
  if (e.keyCode == 46) {
    obj = canvas.getActiveObject();
    canvas.remove(obj);
  }
});

Now the problem is that when I am in the middle of editing the IText and hit the DEL key, the object is obviously also removed. 现在的问题是,当我正在编辑IText并点击DEL键时,该对象显然也被删除了。 I want to prevent this by something like this: 我希望通过以下方式防止这种情况:

$('html').keyup(function(e){

  canvas.observe('text:editing:entered', editing = true);

  console.log(editing);

  if (editing == false) {
    if (e.keyCode == 46) {
      removeObject();
    }
  };

});

= only remove the object if I am not in the middle of editing the IText =如果我不在编辑IText的中间,则仅删除对象

I found this fire event "editing:entered" here: http://fabricjs.com/docs/fabric.IText.html So I try to catch this with the above code. 我在这里找到了这个火灾事件“编辑:输入”: http//fabricjs.com/docs/fabric.IText.html所以我尝试用上面的代码来捕捉这个。 However, it looks like this event already fires when I select the text and did not really start to edit it. 但是,当我选择文本并且没有真正开始编辑它时,看起来这个事件已经触发了。

Any ideas how to solve this and check if I am really just editing the IText? 任何想法如何解决这个问题,并检查我是否真的只是编辑IText?

IText object has a property called isEditing which can be used to check if text in the object is being edited or not. IText对象有一个名为isEditing的属性,可用于检查对象中的文本是否正在编辑。

To achieve what you said, all you have to do is check this property before removing the object from the canvas. 要实现您所说的内容,您需要做的就是在从画布中删除对象之前检查此属性。 The code looks like this: 代码如下所示:

document.onkeyup = function (e) {
    if (e.keyCode == 46) {
        obj = canvas.getActiveObject();
        if (!obj.isEditing) {
            canvas.remove(obj);
        }
    }
};

Here's the fiddle : http://jsfiddle.net/bdgnrnxf/1/ and link to the documentation . 这是小提琴: http//jsfiddle.net/bdgnrnxf/1/并链接到文档

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

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