diff options
| author | Alon Levy <alon@pobox.com> | 2015-03-03 09:44:55 +0200 |
|---|---|---|
| committer | Alon Levy <alon@pobox.com> | 2015-03-03 09:45:54 +0200 |
| commit | ac1819baf2445c8ab350922f14d6ee15f1028ef5 (patch) | |
| tree | 82d157ff9865941d22874556ed63950d0cefdbab /src/client | |
| parent | 3e0e73e461ad8d603f7467a8e995c34ebda14b1c (diff) | |
client/text_analyser_input: specify setter+getter+selectionStart for completer, plus some cleanup
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/view/textanalyser_input.js | 69 |
1 files changed, 39 insertions, 30 deletions
diff --git a/src/client/view/textanalyser_input.js b/src/client/view/textanalyser_input.js index c3f5b512..43fb6609 100644 --- a/src/client/view/textanalyser_input.js +++ b/src/client/view/textanalyser_input.js @@ -8,28 +8,9 @@ var value = util.value; var nbsp = String.fromCharCode(160); function textanalyser_input(spec) { - var element_name = spec.element_name, - ta = { - spec: spec, - on_analysis: new Bacon.Bus(), - on_resize: new Bacon.Bus(), - element: $(element_name), - }, - element = ta.element, - element_raw = element[0], - initial_width = element.width(), - analysisCompleter = completer(element, $(spec.completer_name), {hideOnTab: false}), - document_keydown = new Bacon.Bus(), - input_bus = new Bacon.Bus(); - - analysisCompleter.options.plug(textanalysis.suggestions_options); - - util.assert(1 === element.length); - var selectionStart = function () { return util.selectionStart(element_raw); } - ta.selectionStart = selectionStart; function key(val) { return function (e) { @@ -40,7 +21,6 @@ function textanalyser_input(spec) { function current_value() { return nbsp_to_spaces(value(element_raw)); } - ta.value = current_value; function nbsp_to_spaces(str) { return str.replace(new RegExp(nbsp, 'g'), ' '); @@ -54,31 +34,56 @@ function textanalyser_input(spec) { ta.on_resize.push(); } - function update_element(current_text) + function update_element(current_text, input_cursor_location) { var parts, base_parts, - selection_start; + cursor_location = (undefined === input_cursor_location ? selectionStart() : input_cursor_location); - selection_start = selectionStart(); - value(element_raw, ''); // this removes span elements as well - // here we stop treating the element as an input, this only works on a div/other "normal" element base_parts = current_text.split(/ /) parts = base_parts.slice(0, base_parts.length - 1).map(function (l) { return l + ' '; }); if (base_parts[base_parts.length - 1].length != 0) { parts.push(base_parts[base_parts.length - 1]); } function span(text, color) { - return $('<span style="color: ' + color + '">' + text.replace(/ /g, nbsp) + '</span>'); - } + return $('<span style="color: ' + color + '">' + text.replace(/ /g, nbsp) + '</span>')[0]; + }; + + element.text(''); parts.map(function (part, index) { - element.append(span(part, index % 2 == 0 ? 'blue' : 'red')); + element.append(span(part, index % 2 == 0 ? 'blue' : 'red')); }); - util.setSelection(element_raw, selection_start, selection_start); + util.setSelection(element_raw, cursor_location, cursor_location); stretch_input_to_text_size(current_text); return current_text; } + var element_name = spec.element_name, + ta = { + spec: spec, + on_analysis: new Bacon.Bus(), + on_resize: new Bacon.Bus(), + element: $(element_name), + selectionStart: selectionStart, + value: current_value, + }, + element = ta.element, + element_raw = element[0], + initial_width = element.width(), + completer_spec = { + hideOnTab: false, + getter: current_value, + setter: function (text, cursor) { update_element(text, cursor); }, + selectionStart: selectionStart, + }, + analysisCompleter = completer(element, $(spec.completer_name), completer_spec), + document_keydown = new Bacon.Bus(), + input_bus = new Bacon.Bus(); + + analysisCompleter.options.plug(textanalysis.suggestions_options); + + util.assert(1 === element.length); + var enters = element.asEventStream('keydown').filter(key(13)) .map(function (e) { var text; @@ -97,16 +102,20 @@ function textanalyser_input(spec) { ta.on_sentence = enters.filter(function (v) { return v !== false; }); ta.on_analysis.plug(enters.filter(function (v) { return v === false; }).map(current_value)); ta.on_tab = element.asEventStream('keydown').filter(key(9)); + element.asEventStream('keydown').onValue(function (e) { document_keydown.push({where: consts.KEYSTROKE_WHERE_TEXTANALYSIS, keys: [e.keyCode]}); }); + element.asEventStream('input selectionchange click').onValue(function (e) { analysisCompleter.oninput(current_value(), selectionStart(element_raw)); e.stopPropagation(); e.preventDefault(); }); - ta.on_analysis.plug(element.asEventStream('input').map(current_value).map(update_element)); + ta.on_analysis.plug(element.asEventStream('input').map(current_value).map(function (val) { + return update_element(val); + })); return ta; }; |
