]>
Commit | Line | Data |
---|---|---|
b76b2d01 PK |
1 | /** |
2 | * Fit terminal columns and rows to the dimensions of its DOM element. | |
3 | * | |
4 | * ## Approach | |
5 | * - Rows: Truncate the division of the terminal parent element height by the terminal row height. | |
6 | * | |
7 | * - Columns: Truncate the division of the terminal parent element width by the terminal character | |
8 | * width (apply display: inline at the terminal row and truncate its width with the current | |
9 | * number of columns). | |
10 | * @module xterm/addons/fit/fit | |
11 | * @license MIT | |
12 | */ | |
13 | (function (fit) { | |
14 | if (typeof exports === 'object' && typeof module === 'object') { | |
15 | /* | |
16 | * CommonJS environment | |
17 | */ | |
18 | module.exports = fit(require('../../xterm')); | |
19 | } | |
20 | else if (typeof define == 'function') { | |
21 | /* | |
22 | * Require.js is available | |
23 | */ | |
24 | define(['../../xterm'], fit); | |
25 | } | |
26 | else { | |
27 | /* | |
28 | * Plain browser environment | |
29 | */ | |
30 | fit(window.Terminal); | |
31 | } | |
32 | })(function (Xterm) { | |
33 | var exports = {}; | |
34 | exports.proposeGeometry = function (term) { | |
8a146b54 | 35 | var parentElementStyle = window.getComputedStyle(term.element.parentElement), parentElementHeight = parseInt(parentElementStyle.getPropertyValue('height')), parentElementWidth = Math.max(0, parseInt(parentElementStyle.getPropertyValue('width')) - 17), elementStyle = window.getComputedStyle(term.element), elementPaddingVer = parseInt(elementStyle.getPropertyValue('padding-top')) + parseInt(elementStyle.getPropertyValue('padding-bottom')), elementPaddingHor = parseInt(elementStyle.getPropertyValue('padding-right')) + parseInt(elementStyle.getPropertyValue('padding-left')), availableHeight = parentElementHeight - elementPaddingVer, availableWidth = parentElementWidth - elementPaddingHor, container = term.rowContainer, subjectRow = term.rowContainer.firstElementChild, contentBuffer = subjectRow.innerHTML, characterHeight, rows, characterWidth, cols, geometry; |
b76b2d01 PK |
36 | subjectRow.style.display = 'inline'; |
37 | subjectRow.innerHTML = 'W'; // Common character for measuring width, although on monospace | |
38 | characterWidth = subjectRow.getBoundingClientRect().width; | |
39 | subjectRow.style.display = ''; // Revert style before calculating height, since they differ. | |
40 | characterHeight = parseInt(subjectRow.offsetHeight); | |
41 | subjectRow.innerHTML = contentBuffer; | |
42 | rows = parseInt(availableHeight / characterHeight); | |
8a146b54 | 43 | cols = parseInt(availableWidth / characterWidth); |
b76b2d01 PK |
44 | geometry = { cols: cols, rows: rows }; |
45 | return geometry; | |
46 | }; | |
47 | exports.fit = function (term) { | |
48 | var geometry = exports.proposeGeometry(term); | |
49 | term.resize(geometry.cols, geometry.rows); | |
50 | }; | |
51 | Xterm.prototype.proposeGeometry = function () { | |
52 | return exports.proposeGeometry(this); | |
53 | }; | |
54 | Xterm.prototype.fit = function () { | |
55 | return exports.fit(this); | |
56 | }; | |
57 | return exports; | |
58 | }); | |
59 | //# sourceMappingURL=fit.js.map |