This commit is contained in:
Rainer Simon 2021-10-07 21:27:34 +02:00
parent c3fb350c70
commit 9abf457f41
2 changed files with 22 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import { getWidget, DEFAULT_WIDGETS } from './widgets';
import { TrashIcon } from '../Icons';
import setPosition from './setPosition';
import i18n from '../i18n';
import { debounce } from '../utils';
/** We need to compare bounds by value, not by object ref **/
const bounds = elem => {
@ -63,6 +64,14 @@ export default class Editor extends Component {
componentDidMount() {
this.removeObserver = this.initResizeObserver();
// This makes sure the editor is repositioned if the widgets change
const observer = new MutationObserver(debounce(() => {
this.removeObserver && this.removeObserver();
this.removeObserver = this.initResizeObserver();
}));
observer.observe(this.element.current, { childList: true, subtree: true });
}
componentWillUnmount() {

View File

@ -17,3 +17,16 @@ export const setLocale = (locale, opt_messages) => {
I18n.init(null, opt_messages);
}
}
/** See https://www.freecodecamp.org/news/javascript-debounce-example/ **/
export const debounce = (fn, timeout = 10) => {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => { fn.apply(this, args); }, timeout);
};
}
export default debounce;