From 473ddfd353734b49691432eff97f7ada2f4e0058 Mon Sep 17 00:00:00 2001 From: DanyaPostfactum Date: Sat, 20 Oct 2012 19:22:34 +1100 Subject: [PATCH] Add Select All context menu command support --- lib/ace/keyboard/textinput.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/ace/keyboard/textinput.js b/lib/ace/keyboard/textinput.js index 7889a09a..2082510a 100644 --- a/lib/ace/keyboard/textinput.js +++ b/lib/ace/keyboard/textinput.js @@ -53,7 +53,7 @@ var TextInput = function(parentNode, host) { text.style.top = "-2em"; parentNode.insertBefore(text, parentNode.firstChild); - var PLACEHOLDER = useragent.isIE || useragent.isOpera ? "\x01" : "\x00"; + var PLACEHOLDER = useragent.isIE || useragent.isOpera ? "\x01\x01" : "\x00\x00"; resetValue(); @@ -67,6 +67,8 @@ var TextInput = function(parentNode, host) { setTimeout(resetSelection); } + var cut = false + var copied = false; var pasted = false; var inCompostion = false; @@ -86,8 +88,8 @@ var TextInput = function(parentNode, host) { }; function resetSelection() { - var selectionStart = isSelectionEmpty ? 1 : 0; - var selectionEnd = 1; + var selectionStart = isSelectionEmpty ? 2 : 1; + var selectionEnd = 2; if (text.setSelectionRange) { text.setSelectionRange(selectionStart, selectionEnd); @@ -102,6 +104,21 @@ var TextInput = function(parentNode, host) { } }; + var onSelect = function(e) { + if (cut) { + cut = false; + return; + } + if (copied) { + copied = false; + return; + } + if (text.selectionStart === 0 && text.selectionEnd === text.value.length) { + host.selectAll(); + resetSelection(); + } + }; + var onInput = function(e) { if (inCompostion) return; @@ -115,7 +132,7 @@ var TextInput = function(parentNode, host) { return; } - var data = text.value.substring(isSelectionEmpty ? 1 : 0); + var data = text.value.substring(isSelectionEmpty ? 2 : 1); resetValue(); if (data) host.onTextInput(data); @@ -160,6 +177,7 @@ var TextInput = function(parentNode, host) { if (!supported) { text.value = data; text.select(); + cut = true; setTimeout(function(){ host.onCut() }); } }; @@ -184,6 +202,7 @@ var TextInput = function(parentNode, host) { if (!supported) { text.value = data; text.select(); + copided = true; setTimeout(function(){ host.onCopy() }); } @@ -207,6 +226,8 @@ var TextInput = function(parentNode, host) { event.addCommandKeyListener(text, host.onCommandKey.bind(host)); + event.addListener(text, "select", onSelect); + event.addListener(text, "input", onInput); event.addListener(text, "cut", onCut);