Skip to content

Commit

Permalink
Use ResizeOberver to fit size of terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverroick committed Nov 15, 2024
1 parent 132c43b commit 6807e96
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
42 changes: 29 additions & 13 deletions src/ImageBuilder.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ async function buildImage(repo, ref, term, fitAddon) {

function ImageLogs({ setTerm, setFitAddon, name }) {
const terminalId = `${name}--terminal`;
const observedElementRef = useRef(null);

useEffect(() => {
let observer;
async function setup() {
const { Terminal } = await import("xterm");
const { FitAddon } = await import("xterm-addon-fit");
Expand All @@ -75,12 +78,24 @@ function ImageLogs({ setTerm, setFitAddon, name }) {
setTerm(term);
setFitAddon(fitAddon);
term.write("Logs will appear here when image is being built");

if (observedElementRef.current) {
observer = new ResizeObserver(() => {
console.log("resize");
fitAddon.fit();
});
observer.observe(observedElementRef.current);
}
}
setup();

return () => {
if (observer) observer.disconnect();
}
}, []);

return (
<div className="terminal-container">
<div className="terminal-container" ref={observedElementRef}>
<div id={terminalId}></div>
</div>
);
Expand Down Expand Up @@ -147,21 +162,22 @@ export function ImageBuilder({ name, isActive }) {
// don't generate the hidden input that posts the built image out.
return (
<>
<div className="profile-option-container">
<div className="profile-option-label-container">
<b>Provider</b>
<div className="row mb-4 align-items-center ">
<div className="col-sm-3">
<div className="form-label">Provider</div>
</div>
<div className="profile-option-control-container">GitHub</div>
<div className="col-sm-9">GitHub</div>
</div>

<div
className={`profile-option-container ${repoError ? "has-error" : ""}`}
className={`row mb-4 align-items-center ${repoError ? "has-error" : ""}`}
>
<div className="profile-option-label-container">
<label htmlFor="repo">Repository</label>
<div className="col-sm-12 col-xl-3">
<label htmlFor="repo" className="form-label">Repository</label>
</div>
<div className="profile-option-control-container">
<div className="col-sm-12 col-xl-9">
<input
className="form-control"
id="repo"
type="text"
ref={repoFieldRef}
Expand Down Expand Up @@ -212,11 +228,11 @@ export function ImageBuilder({ name, isActive }) {
}
onChange={() => {}} // Hack to prevent a console error, while at the same time allowing for this field to be validatable, ie. not making it read-only
/>
<div className="profile-option-container">
<div className="profile-option-label-container">
<b>Build Logs</b>
<div className="row mb-4 align-items-center">
<div className="col-sm-12 col-xl-3">
<div className="form-label">Build Logs</div>
</div>
<div className="profile-option-control-container">
<div className="col-sm-12 col-xl-9">
<ImageLogs setFitAddon={setFitAddon} setTerm={setTerm} name={name} />
{customImageError && (
<div className="profile-option-control-error">
Expand Down
9 changes: 5 additions & 4 deletions src/components/form/fields.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ function validateField(value, validateConfig, touched) {

function Field({ id, label, hint, children, error }) {
return (
<div className={`profile-option-container ${error ? "has-error" : ""}`}>
<div className="profile-option-label-container">
<label htmlFor={id}>{label}</label>
<div className={`row mb-4 align-items-center ${error ? "has-error" : ""}`}>
<div className="col-sm-12 col-xl-3">
<label className="form-label" htmlFor={id}>{label}</label>
</div>
<div className="profile-option-control-container">
<div className="col-sm-12 col-xl-9">
{children}
{error && <div className="profile-option-control-error">{error}</div>}
{hint && <div className="profile-option-control-hint">{hint}</div>}
Expand Down Expand Up @@ -88,6 +88,7 @@ function _TextField(
return (
<Field id={id} label={label} hint={hint} error={touched && error}>
<input
className="form-control"
ref={ref}
type="text"
id={id}
Expand Down
18 changes: 1 addition & 17 deletions src/form.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,6 @@
margin: 0 !important;
}

.profile-option-container {
display: grid;
grid-template-columns: 10rem minmax(0, 1fr);
gap: 1rem;
margin-bottom: 2rem;
width: 100%;
}

.profile-option-label-container {
grid-column-start: 1;
align-self: center;
}

.profile-option-control-container {
grid-column-start: 2;
}

.selected-profile {
background-color: #f8f8f8;
}
Expand All @@ -70,6 +53,7 @@
background-color: black;
padding: 16px;
}

.profile-option-control-container input[type="text"] {
width: 100%;
/* 8px here, even though we only use 4px for .react-select-item-container.
Expand Down

0 comments on commit 6807e96

Please sign in to comment.