简体   繁体   English

尝试清除所选单词或字符时,整个文本将被清除

[英]the whole text gets cleared when trying to clear the selected word or character

I am using fabric js with reactjs for adding the text to canvas. 我正在将fabric js与reactjs一起用于将文本添加到画布。 The text is added but when i try to clear the character or selected words, the whole text gets cleared. 添加了文本,但是当我尝试清除字符或所选单词时,整个文本将被清除。 I saw some of the resources regarding this but i am using reactjs so it was hard to resolve this issue for me. 我看到了一些与此相关的资源,但是我正在使用reactjs,因此很难为我解决此问题。 Here is how i have done 这是我做的

class ImageBoard extends React.Component {
  constructor() {
    super();
    this.state = {
      selected: undefined
    };
  }

  handleDeleteKey = event => {
    if (event.keyCode === 46 || event.keyCode === 8) {
      event.preventDefault();
      if (this.state.selected !== undefined) {
        this.canvas.remove(this.state.selected);
        this.setState({ selected: undefined });
      }
    }
  };

  componentDidMount() {
    this.canvas = new fabric.Canvas("canvas");
    document.addEventListener("keydown", this.handleDeleteKey, false);
    this.canvas.on("object:selected", e =>
      this.setState({ selected: e.target })
    );
    this.canvas.on("selection:cleared", e =>
      this.setState({ selected: undefined })
    );
    this.canvas.setBackgroundColor(
      "rgba(255, 73, 64, 0.6)",
      this.canvas.renderAll.bind(this.canvas)
    );
  }

  componentDidUpdate(prevProps) {
    if (prevProps.images !== this.props.images) {
      this.setCanvasBackground(this.props.images, this.canvas);
    }
  }

  addText = () => {
    let text = new fabric.IText("Double click me to change the text!", {
      fontSize: 28,
      fontWeight: 600,
      left: 100,
      top: 100,
      fill: "black",
      width: 20,
      height: 20
    });
    this.canvas.add(text);
  };

  render() {
    return (
      <Wrapper>
        <Column width={80}>
          <canvas
            id="canvas"
            ref={el => (this.canvasEl = el)}
            width={580}
            height={400}
            className="z-depth-1"
          />
        </Column>
        <Filter
          addText={this.addText}
        />
      </Wrapper>
    );
  }
}

export default connect(mapStateToProps)(ImageBoard);

Since you're removing a selected object in the delete / backspace key handler, it's not unexpected that when you're trying to clear text characters with delete / backspace , the whole text gets removed. 由于要在delete / backspace键处理程序中delete选定的对象,因此当您尝试使用delete / backspace清除文本字符时,整个文本都会被删除,这并不意外。 The condition check if (this.state.selected !== undefined) is not enough because when you enter text editing mode, the text object is still selected. 条件检查if (this.state.selected !== undefined)是否不够,因为当您进入文本编辑模式时,仍选择文本对象。

However, you can additionally check IText's isEditing property to see if the selected object is also in the editing mode: 但是,您还可以检查IText的isEditing属性,以查看所选对象是否也处于编辑模式:

  handleDeleteKey = event => {
    if (event.keyCode === 46 || event.keyCode === 8) {
      if (this.state.selected !== undefined && !this.state.selected.isEditing) {
        event.preventDefault();
        this.canvas.remove(this.state.selected);
        this.setState({ selected: undefined });
      }
    }
  };

Here's a working snippet to demonstrate: 这是一个工作片段来演示:

 class App extends React.Component { constructor(props) { super(props); this.state = { selected: undefined }; } handleDeleteKey = event => { if (event.keyCode === 46 || event.keyCode === 8) { if (this.state.selected !== undefined && !this.state.selected.isEditing) { event.preventDefault(); this.canvas.remove(this.state.selected); this.setState({ selected: undefined }); } } }; componentDidMount() { this.canvas = new fabric.Canvas("canvas"); document.addEventListener("keydown", this.handleDeleteKey, false); this.canvas.on("object:selected", e => this.setState({ selected: e.target }) ); this.canvas.on("selection:cleared", e => this.setState({ selected: undefined }) ); this.canvas.setBackgroundColor( "rgba(255, 73, 64, 0.6)", this.canvas.renderAll.bind(this.canvas) ); } addText = () => { let text = new fabric.IText("Double click me to change the text!", { fontSize: 28, fontWeight: 600, left: 20, top: 20, fill: "black", width: 20, height: 20 }); this.canvas.add(text); }; render() { return ( <div> <canvas id="canvas" ref={el => (this.canvasEl = el)} width={400} height={170} /> <button onClick={this.addText}> add </button> </div> ); } } ReactDOM.render(<App />, document.getElementById("app")); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.6.0/fabric.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id='app'></div> 

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

相关问题 清除另一个输入时清除输入类型的文本 - clear input type text when another input is cleared 加载页面时 JWT 被清除 - JWT gets cleared when loading page 在为asp隐藏字段赋值时,转义符被清除 - while assigning value to asp hidden field, escape character gets cleared 当来自“背景&gt;脚本”的脚本试图清除它时,“web_accessible_resources”脚本设置的间隔 ID 没有被清除 - Interval ID set by “web_accessible_resources” script is not getting cleared when script originating from “background > scripts” is trying to clear it 提交表单后,清除选定的项目 - When the form is submitted, the selected item is cleared 选择下拉“无/其他”值时清除相应的文本输入 - Clear corresponding text-input when dropdown 'no/other' value is selected 使用textarea在图像上键入文字时,文字延迟一个字符 - When typed on image using textarea, the text gets one character delayed 什么时候清除整个画布更快? - When is it faster to clear the whole canvas? Javascript - 防止三次点击选择整个文本并只选择单个单词 - Javascript - Prevent whole text selection on triple click and make selected only single word 尝试从按钮清除文本栏时出错 - Error when trying to clear a text bar from a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM