2012-10-14 3 views
0

코드 미러에 '단축키'행을 추가하려고합니다. 일반 <textarea id=code>을 위해 내가 할 수있는 :와 insertAtCursor(code,'hello') :커서에서 어떻게 문자를 추가 할 수 있습니까?

function insertAtCursor(textArea,text) 
{ 
    if (textArea.setSelectionRange) 
    { 
     textArea.value = textArea.value.substring(0,textArea.selectionStart) + text + textArea.value.substring(textArea.selectionStart,textArea.selectionEnd) + textArea.value.substring(textArea.selectionEnd,textArea.value.length); 
    } 
    else if (document.selection && document.selection.createRange) 
    { 
     textArea.focus(); 
     var range = document.selection.createRange(); 

     range.text = text + range.text; 
    } 
} 

어떻게이 CodeMirror 인스턴스와 함께이 작업을 수행 할 수 있습니까?

답변

1

다음은 자동 완성을 위해 추가 문자를 삽입하려는 작은 예입니다. 사용자가 입력을 시작하면 (추가를 원함) 커서를 한 자리 뒤로 설정하여 직접 입력 할 수 있습니다.

editor.on('keypress', function(cm,e){ 
    console.log("keypress: "+e.charCode); 
    if(e.charCode === 40) { //check for (and autocomplete with) while placing cursor inside() 
     e.preventDefault(); //ignore this key 
     editor.replaceSelection("()"); //replace with() 
     editor.setCursor(editor.getCursor().line,editor.getCursor().ch-1); //set cursor back one position 
     return false; 
    } 
}); 
1
function insertAtCursor(instance, text) { 
    instance.replaceSelection(text); 
} 
+0

작동하지만 지금은 내가 추가 할 때 선택됩니다. 모든 수정 사항? – Sirens

관련 문제