3

我正在处理用户输入(包括选项卡)的文本区域中制作一种文本编辑器。每次用户输入内容时,我都会运行一个paginate()在页面上正确分页文本的函数,该函数大约需要 20 毫秒。现在,因为我不希望在对文本区域进行分页时处理第二个输入,所以我添加了一个条件,但这样我就失去了ctrl-V</kbdI 功能。因此,遵循@Gabriel Gartz 在这篇文章中的建议:关于输入问题的文本区域

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

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不是跨浏览器,但它仍然不起作用。

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

4

1 回答 1

0

试试这个,看看是否有效,我没有注意到与您的原始代码行为有任何区别,现在复制粘贴工作。

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

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

于 2013-01-25T23:56:19.510 回答