livebook/assets/js/focus_on_update/index.js

28 lines
506 B
JavaScript
Raw Normal View History

import { isEditableElement } from "../lib/utils";
/**
* A hook used to focus an element whenever it receives LV update.
*/
const FocusOnUpdate = {
mounted() {
this.__focus();
},
updated() {
2021-05-21 07:59:00 +08:00
if (this.el !== document.activeElement) {
this.__focus();
}
},
__focus() {
if (isEditableElement(document.activeElement)) {
return;
}
this.el.focus();
this.el.selectionStart = this.el.selectionEnd = this.el.value.length;
},
};
export default FocusOnUpdate;