2017-10-05 85 views
7

TL; DR子字符串匹配光标位置的文本替换后

我有函数替换文本,字符串和光标的位置(数字),我需要得到修正后的位置(数字)的新字符串,它是与替换功能创建如果长度字符串是否变化:

input and cursor position: foo ba|r text 
replacement: foo -> baz_text, bar -> quux_text 
result: baz_text qu|ux_text text 

input and cursor position: foo bar| text 
replacement: foo -> baz_text, bar -> quux_text 
result: baz_text quux_text| text 

input and cursor position: foo bar| text 
replacement: foo -> f, bar -> b 
result: f b| text 

input and cursor position: foo b|ar text 
replacement: foo -> f, bar -> b 
result: f b| text 

的问题是,我可以使用原来的文本串但随后的更换不会全字匹配,因此需要对整个文本,但随后做子串将不匹配替换。

我对解决方案也很好,光标始终在单词的末尾,原始光标位于被替换的单词的中间。

,现在我的实现,在jQuery的终端我的格式化功能于一身的数组:

$.terminal.defaults.formatters 

他们接受一个字符串,它应该返回新的字符串,它做工精细,除了这种情况:

时我有格式化工具改变长度,如果打破了命令行,比如这个格式:

$.terminal.defaults.formatters.push(function(string) { 
    return string.replace(/:smile:/g, 'a') 
       .replace(/(foo|bar|baz)/g, 'text_$1'); 
}); 

然后将光标定位错了,当李命令ne得到新的字符串。

我已经尝试解决此问题,但如预期,内部终端这个样子的吧不工作,

当我改变 position我装箱另一个变量 formatted_position这是在命令行中使用到

显示光标。获得该值我使用这个:

formatted_position = position; 
var string = formatting(command); 
var len = $.terminal.length(string); 
var command_len = $.terminal.length(command); 
if (len !== command_len) { 
    var orig_sub = $.terminal.substring(command, 0, position); 
    var orig_len = $.terminal.length(orig_sub); 
    var formatted = formatting(orig_sub); 
    var formatted_len = $.terminal.length(formatted); 
    if (orig_len > formatted_len) { 
     // if formatting make substring - (text before cursor) 
     // shorter then subtract the difference 
     formatted_position -= orig_len - formatted_len; 
    } else if (orig_len < formatted_len) { 
     // if the formatted string is longer add difference 
     formatted_position += formatted_len - orig_len; 
    } 
} 

if (formatted_position > len) { 
    formatted_position = len; 
} else if (formatted_position < 0) { 
    formatted_position = 0; 
} 

$ .terminal.substring和$ .terminal.length是那些终端格式化知道(文字看起来像这样[[b;#fff;]hello])的辅助功能,如果你会写解决方案,您可以使用普通文本和使用字符串方法。

的问题是,当我移动光标在改变

这方面的工作时,文字越长,词的中间,但对于较短的字符串光标跳到右边时,文本是在被取代的单词的中间。

我已经尝试解决这个使用此代码,以及:

function find_diff(callback) { 
    var start = position === 0 ? 0 : position - 1; 
    for (var i = start; i < command_len; ++i) { 
     var substr = $.terminal.substring(command, 0, i); 
     var next_substr = $.terminal.substring(command, 0, i + 1); 
     var formatted = formatting(next_substr); 
     var substr_len = $.terminal.length(substr); 
     var formatted_len = $.terminal.length(formatted); 
     var diff = Math.abs(substr_len - formatted_len); 
     if (diff > 1) { 
      return diff; 
     } 
    } 
    return 0; 
} 

... 

} else if (len < command_len) { 
    formatted_position -= find_diff(); 
} else if (len > command_len) { 
    formatted_position += find_diff(); 
} 

但我认为让事情变得更糟,原因是其找到差异时光标之前或更换字的中间,它应该仅当光标位于替换字词的中间才能找到diff。

你可以看到我的努力的结果,在这个codepen https://codepen.io/jcubic/pen/qPVMPg?editors=0110(即允许输入表情符号和Foo酒吧巴兹得到由text_$1代替)

UPDATE

我让它有种与此代码的工作:微笑:字

// --------------------------------------------------------------------- 
    // :: functions used to calculate position of cursor when formatting 
    // :: change length of output text like with emoji demo 
    // --------------------------------------------------------------------- 
    function split(formatted, normal) { 
     function longer(str) { 
      return found && length(str) > length(found) || !found; 
     } 
     var formatted_len = $.terminal.length(formatted); 
     var normal_len = $.terminal.length(normal); 
     var found; 
     for (var i = normal_len; i > 1; i--) { 
      var test_normal = $.terminal.substring(normal, 0, i); 
      var formatted_normal = formatting(test_normal); 
      for (var j = formatted_len; j > 1; j--) { 
       var test_formatted = $.terminal.substring(formatted, 0, j); 
       if (test_formatted === formatted_normal && 
        longer(test_normal)) { 
        found = test_normal; 
       } 
      } 
     } 
     return found || ''; 
    } 
    // --------------------------------------------------------------------- 
    // :: return index after next word that got replaced by formatting 
    // :: and change length of text 
    // --------------------------------------------------------------------- 
    function index_after_formatting(position) { 
     var start = position === 0 ? 0 : position - 1; 
     var command_len = $.terminal.length(command); 
     for (var i = start; i < command_len; ++i) { 
      var substr = $.terminal.substring(command, 0, i); 
      var next_substr = $.terminal.substring(command, 0, i + 1); 
      var formatted_substr = formatting(substr); 
      var formatted_next = formatting(next_substr); 
      var substr_len = length(formatted_substr); 
      var next_len = length(formatted_next); 
      var test_diff = Math.abs(next_len - substr_len); 
      if (test_diff > 1) { 
       return i; 
      } 
     } 
    } 
    // --------------------------------------------------------------------- 
    // :: main function that return corrected cursor position on display 
    // :: if cursor is in the middle of the word that is shorter the before 
    // :: applying formatting then the corrected position is after the word 
    // :: so it stay in place when you move real cursor in the middle 
    // :: of the word 
    // --------------------------------------------------------------------- 
    function get_formatted_position(position) { 
     var formatted_position = position; 
     var string = formatting(command); 
     var len = $.terminal.length(string); 
     var command_len = $.terminal.length(command); 
     if (len !== command_len) { 
      var orig_sub = $.terminal.substring(command, 0, position); 
      var orig_len = $.terminal.length(orig_sub); 
      var sub = formatting(orig_sub); 
      var sub_len = $.terminal.length(sub); 
      var diff = Math.abs(orig_len - sub_len); 
      if (false && orig_len > sub_len) { 
       formatted_position -= diff; 
      } else if (false && orig_len < sub_len) { 
       formatted_position += diff; 
      } else { 
       var index = index_after_formatting(position); 
       var to_end = $.terminal.substring(command, 0, index + 1); 
       //formatted_position -= length(to_end) - orig_len; 
       formatted_position -= orig_len - sub_len; 
       if (orig_sub && orig_sub !== to_end) { 
        var formatted_to_end = formatting(to_end); 
        var common = split(formatted_to_end, orig_sub); 
        var re = new RegExp('^' + $.terminal.escape_regex(common)); 
        var to_end_rest = to_end.replace(re, ''); 
        var to_end_rest_len = length(formatting(to_end_rest)); 
        if (common orig_sub !== common) { 
         var commnon_len = length(formatting(common)); 
         formatted_position = commnon_len + to_end_rest_len; 
        } 
       } 
      } 
      if (formatted_position > len) { 
       formatted_position = len; 
      } else if (formatted_position < 0) { 
       formatted_position = 0; 
      } 
     } 
     return formatted_position; 
    } 

当你作为第一个字符表情符号输入和光标在中间也没有为一个案件工作。如何解决get_formatted_position函数在替换后具有正确的固定位置?

更新:我问不同的,简单的问题,并使用正则表达式接受和字符串trackingReplace功能得到了解决,所以我改变了API,用于格式化接受与正则表达式和字符串一同功能阵列Correct substring position after replacement

+1

你确定,将光标移动到被替换的单词是正确的吗?对我来说,这似乎很混乱。如果他们不在开头或结尾移动它,我会查看Word或Google文档。 – Akxe

+0

@Akxe它是完全不同的情况比单词或谷歌文档,因为文本被替换时,而不是当你做文字替换时搜索/替换功能。当你不改变位置时,它看起来很奇怪,因为你可以结束长度为10的文本的末尾,被替换的文本是3,并且你得到的位置是10,它应该是3. – jcubic

+0

我的意思是,如果你替换光标当前所在的单词,然后将光标放在前面或在新的替换单词之后可能会更好。而Word肯定有替代选项。 – Akxe

回答

1

所以我能够完成给定的任务,但是我无法将其实现到库中,因为我不知道如何在那里实现许多事情。

我在香草javascript中制作,所以在执行到图书馆时不应该有任何打嗝。该脚本主要依赖textarea,输入或类似元素上可用的selectionStartselectionEnd属性。全部替换完成后,使用setSelectionRange方法将新选择设置为textarea。

// sel = [selectionStart, selectionEnd] 
function updateSelection(sel, replaceStart, oldLength, newLength){ 
    var orig = sel.map(a => a) 
    var diff = newLength - oldLength 
    var replaceEnd = replaceStart + oldLength 
    if(replaceEnd <= sel[0]){ 
     // Replacement occurs before selection 
     sel[0] += diff 
     sel[1] += diff 
     console.log('Replacement occurs before selection', orig, sel) 
    }else if(replaceStart <= sel[0]){ 
     // Replacement starts before selection 
     if(replaceEnd >= sel[1]){ 
      // and ends after selection 
      sel[1] += diff 
     }else{ 
      // and ends in selection 
     } 
     console.log('Replacement starts before selection', orig, sel) 
    }else if(replaceStart <= sel[1]){ 
     // Replacement starts in selection 
     if(replaceEnd < sel[1]){ 
      // and ends in seledtion 
     }else{ 
      // and ends after selection 
      sel[1] += diff 
     } 
     console.log('Replacement starts in selection', orig, sel) 
    } 
} 

这里是整个演示:codepen。 PS:从我的观察,格式脚本经常运行的方式。

+0

对不起,也许我还不够清楚,但我不想保留textarea中的选定文本的光标,但我需要这个用于普通字符串(我没有任何选择),光标只是一个数字而不是真正的textarea光标。 – jcubic

+0

我在终端textarea,但它需要有原始文本不是替换的一个,还有一件事我有一个函数,因为我提到做所有替代我不能迭代像在你的代码中的键,也可以替换一个函数做多个单词不是单一的,所以当你这样做的时候你的代码会被破坏'string = string.replace(new RegExp(toRelpace.replace(/([()])/ g,'\\ $ 1'),'g') ,replaceWith)' – jcubic

+0

所以你需要格式化的文本以及原文?即使你不使用开始和结束,选择仍然可以大部分不变,只要想象开始和结束是相同的数字。而正则表达式,你有他们分开,你只是在你的代码中连接它们。 – Akxe