Skip to content

Commit 01a9440

Browse files
author
Hwashiang Yu
committed
MC-877: Could not save button text without focusing out of live edit
- Added debounce to autosave state every 1 second
1 parent 9b6ca7c commit 01a9440

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

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

Lines changed: 35 additions & 1 deletion
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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import $ from "jquery";
99
import ko from "knockout";
1010
import keyCodes from "Magento_Ui/js/lib/key-codes";
11+
import _ from "underscore";
1112

1213
/**
1314
* Add or remove the placeholder-text class from the element based on its content
@@ -37,7 +38,6 @@ ko.bindingHandlers.liveEdit = {
3738
init(element, valueAccessor, allBindings, viewModel, bindingContext) {
3839
const {field, placeholder} = valueAccessor();
3940
let focusedValue = element.innerHTML;
40-
4141
/**
4242
* Strip HTML and return text
4343
*
@@ -81,6 +81,7 @@ ko.bindingHandlers.liveEdit = {
8181
*
8282
* Prevent styling such as bold, italic, and underline using keyboard commands
8383
* Prevent multi-line entries
84+
* Debounce the saving of the state to 1 second to ensure that on save without first unfocus will succeed
8485
*
8586
* @param {any} event
8687
*/
@@ -100,6 +101,32 @@ ko.bindingHandlers.liveEdit = {
100101
if (key === "pageLeftKey" || key === "pageRightKey") {
101102
event.stopPropagation();
102103
}
104+
105+
_.debounce(() => {
106+
const selection = window.getSelection();
107+
const range = document.createRange();
108+
const getCharPosition = (editableDiv: HTMLElement) => {
109+
let charPosition = 0;
110+
111+
if (window.getSelection) {
112+
if (selection.rangeCount) {
113+
if (selection.getRangeAt(0).commonAncestorContainer.parentNode === editableDiv) {
114+
charPosition = selection.getRangeAt(0).endOffset;
115+
}
116+
}
117+
}
118+
return charPosition;
119+
};
120+
const pos: number = getCharPosition(element);
121+
122+
if (focusedValue !== stripHtml(element.innerHTML)) {
123+
viewModel.updateData(field, stripHtml(element.innerHTML));
124+
}
125+
range.setStart(element.childNodes[0], pos);
126+
range.collapse(true);
127+
selection.removeAllRanges();
128+
selection.addRange(range);
129+
}, 1000).call(this);
103130
};
104131

105132
/**

0 commit comments

Comments
 (0)