|
1 |
| -import { cloneHTMLElement, setValueOnElement } from './dom_utils'; |
| 1 | +import { cloneHTMLElement, getModelDirectiveFromElement, setValueOnElement } from './dom_utils'; |
2 | 2 | // @ts-ignore
|
3 | 3 | import { Idiomorph } from 'idiomorph/dist/idiomorph.esm.js';
|
4 | 4 | import { normalizeAttributesForComparison } from './normalize_attributes_for_comparison';
|
@@ -53,12 +53,6 @@ export function executeMorphdom(
|
53 | 53 | });
|
54 | 54 |
|
55 | 55 | Idiomorph.morph(rootFromElement, rootToElement, {
|
56 |
| - // We handle updating the value of fields that have been changed |
57 |
| - // since the HTML was requested. However, the active element is |
58 |
| - // a special case: replacing the value isn't enough. We need to |
59 |
| - // prevent the value from being changed in the first place so the |
60 |
| - // user's cursor position is maintained. |
61 |
| - ignoreActiveValue: true, |
62 | 56 | callbacks: {
|
63 | 57 | beforeNodeMorphed: (fromEl: Element, toEl: Element) => {
|
64 | 58 | // Idiomorph loop also over Text node
|
@@ -106,6 +100,23 @@ export function executeMorphdom(
|
106 | 100 | setValueOnElement(toEl, getElementValue(fromEl));
|
107 | 101 | }
|
108 | 102 |
|
| 103 | + // Special handling for the active element of a model field. |
| 104 | + // Make the "to" element match the "from" element's value |
| 105 | + // to avoid any value change during the morphing. After morphing, |
| 106 | + // the SetValuesOntoModelFieldsPlugin handles setting the value |
| 107 | + // to whatever is in the data store. |
| 108 | + // Avoiding changing the value during morphing is important |
| 109 | + // to maintain the cursor position. |
| 110 | + // We skip this for non-model elements and allow this to either |
| 111 | + // maintain the value if changed (see code above) or for the |
| 112 | + // morphing process to update it to the value from the server. |
| 113 | + if (fromEl === document.activeElement |
| 114 | + && fromEl !== document.body |
| 115 | + && null !== getModelDirectiveFromElement(fromEl, false) |
| 116 | + ) { |
| 117 | + setValueOnElement(toEl, getElementValue(fromEl)); |
| 118 | + } |
| 119 | + |
109 | 120 | // handle any external changes to this element
|
110 | 121 | const elementChanges = externalMutationTracker.getChangedElement(fromEl);
|
111 | 122 | if (elementChanges) {
|
|
0 commit comments