mirror of
https://git.proxmox.com/git/mirror_xterm.js
synced 2025-11-01 00:36:32 +00:00
70 lines
2.3 KiB
JavaScript
70 lines
2.3 KiB
JavaScript
/*
|
|
* Fit terminal columns and rows to the dimensions of its
|
|
* DOM element.
|
|
*
|
|
* Approach:
|
|
* - Rows: Truncate the division of the terminal parent element height
|
|
* by the terminal row height
|
|
*
|
|
* - Columns: Truncate the division of the terminal parent element width by
|
|
* the terminal character width (apply display: inline at the
|
|
* terminal row and truncate its width with the current number
|
|
* of columns)
|
|
*/
|
|
(function (fit) {
|
|
if (typeof define == 'function') {
|
|
/*
|
|
* Require.js is available
|
|
*/
|
|
define(['../../src/xterm'], fit);
|
|
} else {
|
|
/*
|
|
* Plain browser environment
|
|
*/
|
|
fit(this.Xterm);
|
|
}
|
|
})(function (Xterm) {
|
|
Xterm.prototype.proposeGeometry = function () {
|
|
var container = this.rowContainer,
|
|
subjectRow = this.rowContainer.firstElementChild,
|
|
rows,
|
|
contentBuffer,
|
|
characterWidth,
|
|
cols;
|
|
|
|
subjectRow.style.display = 'inline';
|
|
|
|
contentBuffer = subjectRow.textContent;
|
|
|
|
subjectRow.innerHTML = ' '; /* Arbitrary character to calculate its dimensions */
|
|
characterWidth = parseInt(subjectRow.offsetWidth);
|
|
characterHeight = parseInt(subjectRow.offsetHeight);
|
|
|
|
subjectRow.style.display = '';
|
|
|
|
cols = container.offsetWidth / characterWidth;
|
|
cols = parseInt(cols);
|
|
|
|
var parentElementStyle = window.getComputedStyle(this.element.parentElement),
|
|
parentElementHeight = parseInt(parentElementStyle.getPropertyValue('height')),
|
|
elementStyle = window.getComputedStyle(this.element),
|
|
elementPadding = parseInt(elementStyle.getPropertyValue('padding-top')) + parseInt(elementStyle.getPropertyValue('padding-bottom')),
|
|
availableHeight = parentElementHeight - elementPadding,
|
|
rowHeight = this.rowContainer.firstElementChild.offsetHeight;
|
|
|
|
rows = parseInt(availableHeight / rowHeight);
|
|
|
|
var geometry = {
|
|
'cols': cols,
|
|
'rows': rows
|
|
};
|
|
|
|
return geometry;
|
|
};
|
|
|
|
Xterm.prototype.fit = function () {
|
|
var geometry = this.proposeGeometry();
|
|
|
|
this.resize(geometry.cols, geometry.rows);
|
|
};
|
|
}); |