Skip to content

Commit

Permalink
try to preserve focus and selection state when active element is repl…
Browse files Browse the repository at this point in the history
  • Loading branch information
strangeRabbit777 authored and carson committed Jun 11, 2020
1 parent 1d4c9fb commit bdc39c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,10 @@ return (function () {
if (!nodeData.processed) {
nodeData.processed = true;

if (elt.value) {
nodeData.lastValue = elt.value;
}

var triggerSpecs = getTriggerSpecs(elt);
var explicitAction = processVerbs(elt, nodeData, triggerSpecs);

Expand Down Expand Up @@ -1436,8 +1440,25 @@ return (function () {
target.classList.add("htmx-swapping");
var doSwap = function () {
try {

var activeElt = document.activeElement;
var selectionInfo = {
elt: activeElt,
start: activeElt.selectionStart,
end: activeElt.selectionEnd,
};

var settleInfo = makeSettleInfo(target);
selectAndSwap(swapSpec.swapStyle, target, elt, resp, settleInfo);

if (!bodyContains(selectionInfo.elt) && selectionInfo.elt.id) {
var newActiveElt = document.getElementById(selectionInfo.elt.id);
if (selectionInfo.start && newActiveElt.setSelectionRange) {
newActiveElt.setSelectionRange(selectionInfo.start, selectionInfo.end);
}
newActiveElt.focus();
}

target.classList.remove("htmx-swapping");
forEach(settleInfo.elts, function (elt) {
if (elt.classList) {
Expand Down
9 changes: 7 additions & 2 deletions test/scratch.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@
//
// make('<div hx-get="/test">dd</div>')

this.server.respondWith("GET", "/test", '<div id="d1" style="color: red; margin: 100px">Foo</div>');
make('<div hx-swap="outerHTML" hx-get="/test" hx-push-url="true" id="d1">Foo</div>');
htmx.logAll();

this.server.respondWith("GET", "/test", function(xhr){
xhr.respond(201, {}, '<form><input hx-trigger="keyup delay:1s changed" hx-swap="outerHTML" hx-get="/test" id="i1" value="blahblah"/></form>')
});

make('<form hx-target="this"><input hx-trigger="keyup delay:1s changed" hx-swap="outerHTML" hx-get="/test" id="i1"/></form>');
</script>


Expand Down

0 comments on commit bdc39c5

Please sign in to comment.