diff --git a/core/core_27_editor.js b/core/core_27_editor.js index a983221f..4a5be72a 100644 --- a/core/core_27_editor.js +++ b/core/core_27_editor.js @@ -74,7 +74,7 @@ $.hosts.code['/objectPanel'].buildData = function buildData(query) { for (var i = 0; i < ownProps.length; i++) { var prop = ownProps[i]; var type = this.getType(value[prop]); - ownProps[i] = {name: prop, type: type}; + ownProps[i] = { name: prop, type: type }; } data.properties.push(ownProps); value = Object.getPrototypeOf(value); @@ -94,7 +94,7 @@ $.hosts.code['/objectPanel'].buildData = function buildData(query) { // Add typeof information. var global = $.utils.code.getGlobal(); for (var name in global) { - data.roots.push({name: name, type: this.getType(global[name])}); + data.roots.push({ name: name, type: this.getType(global[name]) }); } } return data; @@ -119,7 +119,7 @@ $.hosts.code['/editorXhr'].www = function code_editorXhr_www(request, response) * only present if save was requested * - login: boolean indicating if the user is logged in */ - var data = {login: !!request.user}; + var data = { login: !!request.user }; try { // ends with ... finally {response.write(JSON.stringify(data));} if (!request.fromSameOrigin()) { // Security check to ensure this is being loaded by the code editor. @@ -141,7 +141,7 @@ $.hosts.code['/editorXhr'].www = function code_editorXhr_www(request, response) // Global variable; no parent object. object = null; } else if (request.parameters.key && - (object = $.db.tempId.getObjById(request.parameters.key))) { + (object = $.db.tempId.getObjById(request.parameters.key))) { // Successfully retrieved parent object from tempID DB. } else { // Get parent object via selector. @@ -370,7 +370,7 @@ $.hosts.code['/editorXhr'].generateMetaData = function generateMetaData(value, s for (var i = 0, prop; (prop = props[i]); i++) { try { meta += '// ' + (value[prop] ? '@set_prop ' + prop + ' = ' + - JSON.stringify(value[prop]) : '@delete_prop ' + prop) + '\n'; + JSON.stringify(value[prop]) : '@delete_prop ' + prop) + '\n'; } catch (e) { // Unstringable value, or read perms error. Skip. } diff --git a/static/connect/common.js b/static/connect/common.js index ff128d1d..684a35f1 100644 --- a/static/connect/common.js +++ b/static/connect/common.js @@ -243,10 +243,13 @@ CCC.Common.parentFocus = function(e) { var ct = parent.document.getElementById('commandTextarea'); ct.focus(); // Chrome won't type the character in the textarea after a focus change. - // For the easy case where the field is empty, just add the character. - // TODO: Handle cases where the field is not empty. - if (e && e.key.length === 1 && !ct.value.length) { - ct.value = e.key; + // Insert the character at the cursor, replacing any selected text. + if (e && e.key.length === 1) { + var start = ct.selectionStart; + var end = ct.selectionEnd; + ct.value = ct.value.substring(0, start) + e.key + + ct.value.substring(end); + ct.selectionStart = ct.selectionEnd = start + e.key.length; // Firefox will type the character a second time, prevent this. e.preventDefault(); } diff --git a/static/connect/tests/test.js b/static/connect/tests/test.js index a965d68f..33b4cb53 100644 --- a/static/connect/tests/test.js +++ b/static/connect/tests/test.js @@ -18,6 +18,31 @@ function testCommonEscapeSpaces() { assertEquals('\u00A0a\u00A0 \u00A0 \u00A0 \u00A0 b', CCC.Common.escapeSpaces(' a\tb')); } +function testCommonParentFocus() { + var textarea = document.createElement('textarea'); + textarea.id = 'commandTextarea'; + document.body.appendChild(textarea); + try { + var event = {key: 'x', preventDefault: function() {}}; + CCC.Common.parentFocus(event); + assertEquals('x', textarea.value); + assertEquals(1, textarea.selectionStart); + + textarea.value = 'look north'; + textarea.setSelectionRange(5, 5); + CCC.Common.parentFocus(event); + assertEquals('look xnorth', textarea.value); + assertEquals(6, textarea.selectionStart); + + textarea.setSelectionRange(5, 10); + CCC.Common.parentFocus(event); + assertEquals('look x', textarea.value); + assertEquals(6, textarea.selectionStart); + } finally { + document.body.removeChild(textarea); + } +} + // log.js function testLogGetTemplate() {