mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-09 06:55:03 +00:00
fix: add resize listener for code mirror instances (#7889)
This commit is contained in:
38
packages/bruno-app/src/utils/codemirror/resize.js
Normal file
38
packages/bruno-app/src/utils/codemirror/resize.js
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Refreshes a CodeMirror editor when its container size changes.
|
||||
* CodeMirror measures its DOM during refresh(), so resize callbacks are
|
||||
* coalesced into a single animation frame to avoid repeated layout work.
|
||||
*
|
||||
* @param {Object} editor - CodeMirror editor instance
|
||||
* @param {HTMLElement} element - Element whose size changes should refresh the editor
|
||||
* @returns {Function} Cleanup function
|
||||
*/
|
||||
export const setupCodeMirrorResizeRefresh = (editor, element) => {
|
||||
if (!editor || !element || typeof ResizeObserver === 'undefined') {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
let resizeRefreshFrameId = null;
|
||||
|
||||
const resizeObserver = new ResizeObserver(() => {
|
||||
if (resizeRefreshFrameId) {
|
||||
cancelAnimationFrame(resizeRefreshFrameId);
|
||||
}
|
||||
|
||||
resizeRefreshFrameId = requestAnimationFrame(() => {
|
||||
editor.refresh?.();
|
||||
resizeRefreshFrameId = null;
|
||||
});
|
||||
});
|
||||
|
||||
resizeObserver.observe(element);
|
||||
|
||||
return () => {
|
||||
resizeObserver.disconnect();
|
||||
|
||||
if (resizeRefreshFrameId) {
|
||||
cancelAnimationFrame(resizeRefreshFrameId);
|
||||
resizeRefreshFrameId = null;
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user