Permalink
Browse files

backspace on beginning of text area gets you editing in the previous …

…item
  • Loading branch information...
1 parent 2338eb5 commit 301cb8c16b08638a4ff206aab049743f74ea75ed @hallahan committed May 30, 2012
Showing with 37 additions and 11 deletions.
  1. +18 −5 client/client.js
  2. +13 −4 client/lib/legacy.coffee
  3. +2 −1 client/lib/util.coffee
  4. +4 −1 client/test/testclient.js
View
@@ -481,7 +481,7 @@ require.define("/lib/legacy.coffee", function (require, module, exports, __dirna
});
});
};
- textEditor = wiki.textEditor = function(div, item) {
+ textEditor = wiki.textEditor = function(div, item, caretPos) {
var original, textarea, _ref;
textarea = $("<textarea>" + (original = (_ref = item.text) != null ? _ref : '') + "</textarea>").focusout(function() {
if (item.text = textarea.val()) {
@@ -503,12 +503,18 @@ require.define("/lib/legacy.coffee", function (require, module, exports, __dirna
}
return null;
}).bind('keydown', function(e) {
- var caret, pageElement, prefix, suffix, text;
+ var caret, pageElement, prefix, prevItem, prevTextLen, suffix, text;
if ((e.altKey || e.ctlKey || e.metaKey) && e.which === 83) {
textarea.focusout();
return false;
}
- if (e.which === $.ui.keyCode.ENTER) {
+ if (e.which === $.ui.keyCode.BACKSPACE && util.getCaretPosition(textarea.get(0)) === 0) {
+ prevItem = getItem(div.prev());
+ prevTextLen = prevItem.text.length;
+ prevItem.text += textarea.val();
+ textarea.val('');
+ return textEditor(div.prev(), prevItem, prevTextLen);
+ } else if (e.which === $.ui.keyCode.ENTER) {
caret = util.getCaretPosition(textarea.get(0));
if (!caret) {
return false;
@@ -526,7 +532,11 @@ require.define("/lib/legacy.coffee", function (require, module, exports, __dirna
return false;
});
div.html(textarea);
- return util.setCaretPosition(textarea.get(0), textarea.val().length);
+ if (caretPos != null) {
+ return util.setCaretPosition(textarea.get(0), caretPos);
+ } else {
+ return util.setCaretPosition(textarea.get(0), textarea.val().length);
+ }
};
getItem = wiki.getItem = function(element) {
if ($(element).length > 0) {
@@ -754,14 +764,17 @@ require.define("/lib/util.coffee", function (require, module, exports, __dirname
util.getCaretPosition = function(div) {
var caretPos, sel;
+ if (!(div != null)) {
+ return null;
+ }
caretPos = 0;
if (document.selection) {
div.focus();
sel = document.selection.createRange();
sel.moveStart("character", -div.value.length);
caretPos = sel.text.length;
} else {
- if (div.selectionStart || div.selectionStart === "0") {
+ if (div.selectionStart != null) {
caretPos = div.selectionStart;
}
}
View
@@ -119,7 +119,7 @@ $ ->
sleep = (time, code) -> setTimeout code, time
sleep 500, -> pageHandler.put pageElement, {item: item, id: item.id, type: 'add', after: itemBefore?.id}
- textEditor = wiki.textEditor = (div, item) ->
+ textEditor = wiki.textEditor = (div, item, caretPos) ->
textarea = $("<textarea>#{original = item.text ? ''}</textarea>")
.focusout ->
if item.text = textarea.val()
@@ -134,8 +134,14 @@ $ ->
if (e.altKey || e.ctlKey || e.metaKey) and e.which == 83 #alt-s
textarea.focusout()
return false
- #NH
- if e.which == $.ui.keyCode.ENTER
+ if e.which == $.ui.keyCode.BACKSPACE and util.getCaretPosition(textarea.get(0)) == 0
+ prevItem = getItem(div.prev())
+ prevTextLen = prevItem.text.length
+ prevItem.text += textarea.val()
+ textarea.val('') # Need current text area to be empty. Item then gets deleted.
+ # caret needs to be between the old text and the new appended text
+ textEditor div.prev(), prevItem, prevTextLen
+ else if e.which == $.ui.keyCode.ENTER
caret = util.getCaretPosition textarea.get(0)
return false unless caret
text = textarea.val()
@@ -150,7 +156,10 @@ $ ->
return false; #don't pass dblclick on to the div, as it'll reload
div.html textarea
- util.setCaretPosition textarea.get(0), textarea.val().length
+ if caretPos?
+ util.setCaretPosition textarea.get(0), caretPos
+ else
+ util.setCaretPosition textarea.get(0), textarea.val().length
getItem = wiki.getItem = (element) ->
$(element).data("item") or $(element).data('staticItem') if $(element).length > 0
View
@@ -42,6 +42,7 @@ util.emptyPage = () ->
# gets position of caret in a text area
util.getCaretPosition = (div) ->
+ if !div? then return null
caretPos = 0
# for IE
if document.selection
@@ -50,7 +51,7 @@ util.getCaretPosition = (div) ->
sel.moveStart "character", -div.value.length
caretPos = sel.text.length
# for the rest of the world
- else caretPos = div.selectionStart if div.selectionStart or div.selectionStart is "0"
+ else caretPos = div.selectionStart if div.selectionStart?
caretPos
util.setCaretPosition = (elem, caretPos) ->
@@ -461,14 +461,17 @@ require.define("/lib/util.coffee", function (require, module, exports, __dirname
util.getCaretPosition = function(div) {
var caretPos, sel;
+ if (!(div != null)) {
+ return null;
+ }
caretPos = 0;
if (document.selection) {
div.focus();
sel = document.selection.createRange();
sel.moveStart("character", -div.value.length);
caretPos = sel.text.length;
} else {
- if (div.selectionStart || div.selectionStart === "0") {
+ if (div.selectionStart != null) {
caretPos = div.selectionStart;
}
}

0 comments on commit 301cb8c

Please sign in to comment.