Skip to content

Commit e68ab12

Browse files
authored
Merge pull request #296 from makermelissa/resize-fix
Resize fix
2 parents 6425df9 + c08895f commit e68ab12

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

js/layout.js

+37-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const SETTING_TERMINAL_VISIBLE = "terminal-visible";
1515
const UPDATE_TYPE_EDITOR = 1;
1616
const UPDATE_TYPE_SERIAL = 2;
1717

18+
const MINIMUM_COLS = 2;
19+
const MINIMUM_ROWS = 1;
20+
1821
function isEditorVisible() {
1922
return editorPage.classList.contains('active');
2023
}
@@ -59,6 +62,7 @@ export function showSerial() {
5962

6063
// update type is used to indicate which button was clicked
6164
function updatePageLayout(updateType) {
65+
// If both are visible, show the separator
6266
if (isEditorVisible() && isSerialVisible()) {
6367
pageSeparator.classList.add('active');
6468
} else {
@@ -72,6 +76,7 @@ function updatePageLayout(updateType) {
7276

7377
// Mobile layout, so only show one or the other
7478
if (mainContent.offsetWidth < 768) {
79+
// Prioritize based on the update type
7580
if (updateType == UPDATE_TYPE_EDITOR && isEditorVisible()) {
7681
serialPage.classList.remove('active');
7782
} else if (updateType == UPDATE_TYPE_SERIAL && isSerialVisible()) {
@@ -108,13 +113,40 @@ function updatePageLayout(updateType) {
108113
}
109114

110115
function refitTerminal() {
116+
// Custom function to replace the terminal refit function as it was a bit buggy
117+
111118
// Re-fitting the terminal requires a full re-layout of the DOM which can be tricky to time right.
112119
// see https://www.macarthur.me/posts/when-dom-updates-appear-to-be-asynchronous
113120
window.requestAnimationFrame(() => {
114121
window.requestAnimationFrame(() => {
115122
window.requestAnimationFrame(() => {
116-
if (state.fitter) {
117-
state.fitter.fit();
123+
const TERMINAL_ROW_HEIGHT = state.terminal._core._renderService.dimensions.css.cell.height;
124+
const TERMINAL_COL_WIDTH = state.terminal._core._renderService.dimensions.css.cell.width;
125+
126+
// Get the height of the header, footer, and serial bar to determine the height of the terminal
127+
let siteHeader = document.getElementById('site-header');
128+
let mobileHeader = document.getElementById('mobile-header');
129+
let headerHeight = siteHeader.offsetHeight;
130+
if (siteHeader.style.display === 'none') {
131+
headerHeight = mobileHeader.offsetHeight;
132+
}
133+
let footerBarHeight = document.getElementById('footer-bar').offsetHeight;
134+
let serialBarHeight = document.getElementById('serial-bar').offsetHeight;
135+
let viewportHeight = window.innerHeight;
136+
let terminalHeight = viewportHeight - headerHeight - footerBarHeight - serialBarHeight;
137+
let terminalWidth = document.getElementById('serial-page').offsetWidth;
138+
let screen = document.querySelector('.xterm-screen');
139+
if (screen) {
140+
let cols = Math.floor(terminalWidth / TERMINAL_COL_WIDTH);
141+
let rows = Math.floor(terminalHeight / TERMINAL_ROW_HEIGHT);
142+
if (cols < MINIMUM_COLS) {
143+
cols = MINIMUM_COLS;
144+
}
145+
if (rows < MINIMUM_ROWS) {
146+
rows = MINIMUM_ROWS;
147+
}
148+
screen.style.width = (cols * TERMINAL_COL_WIDTH) + 'px';
149+
screen.style.height = (rows * TERMINAL_ROW_HEIGHT) + 'px';
118150
}
119151
});
120152
});
@@ -130,8 +162,7 @@ function fixViewportHeight(e) {
130162
}
131163

132164
// Resize the panes when the separator is moved
133-
function resize(e) {
134-
console.log("Resized");
165+
function resizePanels(e) {
135166
const w = mainContent.offsetWidth;
136167
const gap = pageSeparator.offsetWidth;
137168
const ratio = e.clientX / w;
@@ -182,12 +213,12 @@ function loadPanelSettings() {
182213
}
183214

184215
function stopResize(e) {
185-
window.removeEventListener('mousemove', resize, false);
216+
window.removeEventListener('mousemove', resizePanels, false);
186217
window.removeEventListener('mouseup', stopResize, false);
187218
}
188219

189220
pageSeparator.addEventListener('mousedown', async function (e) {
190-
window.addEventListener('mousemove', resize, false);
221+
window.addEventListener('mousemove', resizePanels, false);
191222
window.addEventListener('mouseup', stopResize, false);
192223
});
193224

js/script.js

-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { classHighlighter } from "@lezer/highlight";
88
import { getFileIcon } from "./common/file_dialog.js";
99

1010
import { Terminal } from '@xterm/xterm';
11-
import { FitAddon } from '@xterm/addon-fit';
1211
import { WebLinksAddon } from '@xterm/addon-web-links';
1312

1413
import state from './state.js'
@@ -152,7 +151,6 @@ btnPlotter.addEventListener('click', async function(e){
152151
await setupPlotterChart(workflow);
153152
workflow.plotterEnabled = true;
154153
}
155-
state.fitter.fit();
156154
});
157155

158156
btnInfo.addEventListener('click', async function(e) {
@@ -548,9 +546,6 @@ async function setupXterm() {
548546
}
549547
});
550548

551-
state.fitter = new FitAddon();
552-
state.terminal.loadAddon(state.fitter);
553-
554549
state.terminal.loadAddon(new WebLinksAddon());
555550

556551
state.terminal.open(document.getElementById('terminal'));

0 commit comments

Comments
 (0)