2023-09-25 19:58:13 +08:00
|
|
|
/*
|
|
|
|
* Mixin to observe and reflect DOM changes of a module's name in '.my_module-name .view-mode'.
|
|
|
|
* - Initializes a MutationObserver to watch for modifications.
|
|
|
|
* - Updates the `moduleName` data property on detected changes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
observer: null,
|
|
|
|
moduleName: '',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.observeChanges();
|
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
if (this.observer) {
|
|
|
|
this.observer.disconnect();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
observeChanges() {
|
2023-10-13 22:10:07 +08:00
|
|
|
const targetNode = document
|
|
|
|
.querySelector('.my_module-name .view-mode, .my_module-name .name-readonly-placeholder');
|
2023-09-25 19:58:13 +08:00
|
|
|
if (!targetNode) return;
|
|
|
|
|
|
|
|
this.moduleName = targetNode.textContent;
|
|
|
|
|
|
|
|
const config = { characterData: true, childList: true, subtree: true };
|
|
|
|
|
|
|
|
const callback = (mutationsList) => {
|
|
|
|
mutationsList.forEach((mutation) => {
|
|
|
|
if (mutation.type === 'childList' || mutation.type === 'characterData') {
|
|
|
|
this.moduleName = targetNode.textContent;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.observer = new MutationObserver(callback);
|
|
|
|
this.observer.observe(targetNode, config);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|