textareas编辑中的问题

时间:2013-01-16 20:43:04

标签: javascript javascript-events textarea

所以我在textarea中制作了一种文本编辑器,我处理用户输入,包括标签。每次用户输入内容时,我都会运行一个paginate()函数,该函数在页面上正确地对文本进行分页,此函数大约需要20毫秒。现在,因为我不希望在textarea被分页时处理第二个输入,所以我添加了一个条件但是这样我失去了ctrl-V功能。所以,根据@Gabriel Gartz在这篇文章中提出的建议:textarea on input issue

我首先通过保存上下文和事件再次调用该函数。该函数会再次被调用,但粘贴仍然没有发生!

HTML:

<textarea id="taEditor"></textarea>

的javascript:

$("#taEditor").on('click keydown cut paste', processUserInput);

var IsProcessingEvent = false;

function processUserInput(e) {
    if(!IsProcessingEvent) {
        IsProcessingEvent = true;
        //do various stuff before the textarea changes like: get value, get   cursor pos etc
        var taValueBefore = document.getElementById("taEditor").value;
        if (e.keyCode === 9) {
          e.preventDefault();
          e.stopPropagation();
          document.getElementById("taEditor").value += "\t";
        }
        getcursorpos();

        //do various stuff after the textarea changes like: get value, get   cursor pos etc  
        setTimeout(function() {
            var taValueAfter = document.getElementById("taEditor").value;
            getcursorpos();
          if (taValueAfter !== taValueBefore) {
            paginate(); //this function paginates the text in the textarea and sets the cursor
            //paginate() takes about 20 milliseconds
          }
          if (doAgain.repeat) {
            var lastEvent = doAgain;
            doAgain.repeat = false;
            document.getElementById("debug").innerHTML += "rerun: " + lastEvent.ctx.id + ":" + lastEvent.e.type + "<br>";
            setTimeout(processUserInput.bind(lastEvent.ctx), 0, lastEvent.e);
          }
          document.getElementById("debug").innerHTML += e.type + "<br>";
          IsProcessingEvent = false;
        }, 0);
    } else {
      //save context and event
      document.getElementById("debug").innerHTML += "isprocessing: " + e.type + "<br>";
          doAgain = {
            ctx: this,
            e: e,
                  repeat: true
      };
      //i need to preventdefault here, because processUerInput also processes the TAB key and if i don't use preventdefault then the cursor will move focus to other elements during the pagination
          e.preventDefault();
          e.stopPropagation();
      return false;
    }
}

var doAgain = {
    ctx: "",
    e: "",
    repeat: false
};

function getcursorpos() {
  //for simplicity reasons it's empty
}

function paginate() {
  var i = 0;
  var k = 0;
  //simulate 20-30 milliseconds of delay for pagination
  for (i=0;i<100000000;i++) {
    k++;
  }
  //for simplicity reasons it's empty
}

的jsfiddle:

http://jsfiddle.net/63pkE/1/

重现问题:尝试在textarea中按ctrl-v。

我不明白我做错了什么。在此先感谢您的帮助。

修改

这是一个新的jsfiddle,我替换了

        setTimeout(processUserInput.bind(lastEvent.ctx), 0, lastEvent.e);

对齐
        setTimeout(function() {
          processUserInput.call(lastEvent.ctx, lastEvent.e);
        }, 0);

因为.bind不是crossbrowser,但它仍然不起作用。

http://jsfiddle.net/63pkE/2/

1 个答案:

答案 0 :(得分:0)

尝试这一点,看看是否有效,我没有发现原始代码行为与复制粘贴工作有任何区别。

function processUserInput(e) {
    if(!IsProcessingEvent) {
        if(!e.ctrlKey){
            IsProcessingEvent = true;
        }
    //Rest of the code

如果按下Ctrl键,e.ctrlKey将返回true。