Skip to content

Commit 3c93413

Browse files
committed
docs: resize playground when window size changes
1 parent 548ac57 commit 3c93413

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

docs/playground/Playground.vue

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,56 @@ const SCROLL_CONTAINER_ID = 'scroll-container';
107107
108108
const { editorSize, ifexViewerSize, startResizing } = useEditorResizing(codemirrorInstance);
109109
110+
const calculateSizes = () => {
111+
const parentElement = document.getElementById(SCROLL_CONTAINER_ID)?.parentElement;
112+
if (parentElement) {
113+
const parentWidth = parentElement.offsetWidth;
114+
const totalGap = 70; // Total gap/padding (35px each side)
115+
const availableWidth = parentWidth - totalGap;
116+
117+
// If sizes haven't been set yet, use 50/50 split
118+
if (editorSize.value === 0 && ifexViewerSize.value === 0) {
119+
editorSize.value = availableWidth / 2;
120+
ifexViewerSize.value = availableWidth / 2;
121+
} else {
122+
// Maintain the current ratio when resizing
123+
const currentTotal = editorSize.value + ifexViewerSize.value;
124+
if (currentTotal > 0) {
125+
const editorRatio = editorSize.value / currentTotal;
126+
editorSize.value = availableWidth * editorRatio;
127+
ifexViewerSize.value = availableWidth * (1 - editorRatio);
128+
} else {
129+
// Fallback to 50/50 if something went wrong
130+
editorSize.value = availableWidth / 2;
131+
ifexViewerSize.value = availableWidth / 2;
132+
}
133+
}
134+
}
135+
};
136+
137+
let resizeTimeout: ReturnType<typeof setTimeout> | null = null;
138+
139+
const handleWindowResize = () => {
140+
// Debounce the resize calculation to avoid excessive recalculations
141+
if (resizeTimeout) {
142+
clearTimeout(resizeTimeout);
143+
}
144+
resizeTimeout = setTimeout(() => {
145+
calculateSizes();
146+
resizeTimeout = null;
147+
}, 100);
148+
};
149+
110150
onMounted(() => {
111151
// Workaround for lazy loading web components in SSR
112152
import('../../dist/ifex-viewer.es.js').then(() => {
113153
mounted.value = true;
114154
});
115155
116-
const parentElement = document.getElementById(SCROLL_CONTAINER_ID)?.parentElement;
117-
if (parentElement) {
118-
const parentWidth = parentElement.offsetWidth;
119-
editorSize.value = parentWidth / 2 - 35; // 50% - 2rem (32px) - 3px
120-
121-
ifexViewerSize.value = parentWidth / 2 - 35; // 50% - 2rem (32px) - 3px
122-
}
156+
calculateSizes();
157+
158+
// Add window resize listener
159+
window.addEventListener('resize', handleWindowResize);
123160
124161
const initialEditorState = EditorState.create({
125162
doc: simpleSpecificationMock,
@@ -151,6 +188,15 @@ onBeforeUnmount(() => {
151188
if (view.value) {
152189
view.value.destroy();
153190
}
191+
192+
// Remove window resize listener
193+
window.removeEventListener('resize', handleWindowResize);
194+
195+
// Clear any pending resize timeout
196+
if (resizeTimeout) {
197+
clearTimeout(resizeTimeout);
198+
resizeTimeout = null;
199+
}
154200
});
155201
156202
watch(errorMap, newErrorMap => {

docs/playground/composables/use-editor-resizing.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const useEditorResizing = (codemirrorInstance: Readonly<ShallowRef<HTMLEl
3434

3535
const { left } = codemirrorInstance.value.getBoundingClientRect();
3636
const containerWidth = container.offsetWidth;
37+
const totalGap = 70; // Total gap/padding (35px each side)
3738

3839
// Calculate the initial offset only on the first resize event
3940
// This should be the difference between mouse position and current editor right edge
@@ -47,13 +48,13 @@ export const useEditorResizing = (codemirrorInstance: Readonly<ShallowRef<HTMLEl
4748
// Ensure the sizes remain within bounds (accounting for minimum sizes and gaps)
4849
const minEditorSize = 200; // Minimum editor width
4950
const minViewerSize = 200; // Minimum viewer width
50-
const totalGap = 70; // Total gap/padding (35px each side)
51+
const availableWidth = containerWidth - totalGap;
5152

52-
if (newEditorSize >= minEditorSize && containerWidth - newEditorSize - totalGap >= minViewerSize) {
53+
if (newEditorSize >= minEditorSize && availableWidth - newEditorSize >= minViewerSize) {
5354
editorSize.value = newEditorSize;
5455

5556
// Calculate and set the width of the ifex-viewer
56-
ifexViewerSize.value = containerWidth - newEditorSize - totalGap;
57+
ifexViewerSize.value = availableWidth - newEditorSize;
5758
}
5859
};
5960

0 commit comments

Comments
 (0)