Skip to content

Commit 159761c

Browse files
committed
MC-15238: Live edit in Safari and Firefox puts caret at start of field after typing
- Don’t update textContent when the data store update originates from live edit itself
1 parent d560bae commit 159761c

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

app/code/Magento/PageBuilder/view/adminhtml/web/js/binding/live-edit.js

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/preview.js

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/binding/live-edit.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import $ from "jquery";
99
import ko from "knockout";
1010
import keyCodes from "Magento_Ui/js/lib/key-codes";
1111
import _ from "underscore";
12+
import {DataObject} from "../data-store";
1213

1314
/**
1415
* Strip HTML and return text
@@ -54,13 +55,15 @@ ko.bindingHandlers.liveEdit = {
5455
let focusedValue = element.innerHTML;
5556
let previouslyFocused: boolean = false;
5657
let blurTimeout: number;
58+
let lastUpdateValue: string;
5759

5860
/**
5961
* Record the value on focus, only conduct an update when data changes
6062
*/
6163
const onFocus = () => {
6264
clearTimeout(blurTimeout);
6365
focusedValue = stripHtml(element.innerHTML);
66+
lastUpdateValue = focusedValue;
6467

6568
if (selectAll && element.innerHTML !== "" && !previouslyFocused) {
6669
_.defer(() => {
@@ -123,8 +126,10 @@ ko.bindingHandlers.liveEdit = {
123126
* On key up update the view model to ensure all changes are saved
124127
*/
125128
const onKeyUp = () => {
126-
if (focusedValue !== stripHtml(element.innerHTML)) {
127-
viewModel.updateData(field, stripHtml(element.innerHTML));
129+
const strippedValue = stripHtml(element.innerHTML);
130+
if (focusedValue !== strippedValue) {
131+
lastUpdateValue = strippedValue;
132+
viewModel.updateData(field, strippedValue);
128133
}
129134
};
130135

@@ -165,6 +170,7 @@ ko.bindingHandlers.liveEdit = {
165170
// Allow the paste action to update the content
166171
_.defer(() => {
167172
const strippedValue = stripHtml(element.innerHTML);
173+
lastUpdateValue = strippedValue;
168174
element.innerHTML = strippedValue;
169175
/**
170176
* Calculate the position the caret should end up at, the difference in string length + the original
@@ -200,9 +206,13 @@ ko.bindingHandlers.liveEdit = {
200206
handlePlaceholderClass(element);
201207

202208
// Create a subscription onto the original data to update the internal value
203-
viewModel.contentType.dataStore.subscribe(() => {
204-
element.textContent = viewModel.contentType.dataStore.get(field);
205-
handlePlaceholderClass(element);
209+
viewModel.contentType.dataStore.subscribe((data: DataObject) => {
210+
// Only update the value if it differs from the last value added within live edit
211+
if (lastUpdateValue !== data[field]) {
212+
lastUpdateValue = data[field];
213+
element.textContent = data[field];
214+
handlePlaceholderClass(element);
215+
}
206216
}, field);
207217

208218
// Resolve issues of content editable being within an anchor

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/preview.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import ContentTypeInterface from "../content-type.types";
2525
import {DataObject} from "../data-store";
2626
import {getDraggedContentTypeConfig} from "../drag-drop/registry";
2727
import {getSortableOptions} from "../drag-drop/sortable";
28-
import {get, set} from "../utils/object";
28+
import {get} from "../utils/object";
2929
import appearanceConfig from "./appearance-config";
3030
import ObservableUpdater from "./observable-updater";
3131
import ObservableObject from "./observable-updater.types";
@@ -147,10 +147,7 @@ export default class Preview implements PreviewInterface {
147147
* @param {string} value
148148
*/
149149
public updateData(key: string, value: string) {
150-
const data = this.contentType.dataStore.getState();
151-
152-
set(data, key, value);
153-
this.contentType.dataStore.setState(data);
150+
this.contentType.dataStore.set(key, value);
154151
}
155152

156153
/**

0 commit comments

Comments
 (0)