-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
story-ad-button-text-fitter.js
132 lines (121 loc) · 3.58 KB
/
story-ad-button-text-fitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Copyright 2019 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Services} from '../../../src/services';
import {px, setStyle, setStyles} from '../../../src/style';
/** @const {number} Fixed button height from design spec. */
const MAX_HEIGHT = 32;
/** @enum {number} From design spec. */
const FontSizes = {
MIN: 12,
MAX: 14,
};
export class ButtonTextFitter {
/**
* @param {!../../../src/service/ampdoc-impl.AmpDoc} ampdoc
*/
constructor(ampdoc) {
/** @const @private {!../../../src/service/mutator-interface.MutatorInterface} */
this.mutator_ = Services.mutatorForDoc(ampdoc);
/** @private {!Document} */
this.doc_ = ampdoc.win.document;
/** @private {!Element} */
this.measurer_ = this.doc_.createElement('div');
this.mutator_.mutateElement(this.measurer_, () => {
this.doc_.body.appendChild(this.measurer_);
setStyles(this.measurer_, {
position: 'absolute',
top: 0,
left: 0,
zIndex: 1,
visibility: 'hidden',
'font-weight': 'bold',
'letter-spacing': '0.2px',
});
});
}
/**
* @param {!Element} pageElement
* @param {!Element} container
* @param {string} content
* @return {Promise<boolean>}
*/
fit(pageElement, container, content) {
let success = false;
return this.mutator_
.mutateElement(container, () => {
this.measurer_.textContent = content;
const fontSize = calculateFontSize(
this.measurer_,
MAX_HEIGHT,
this.getMaxWidth_(pageElement),
FontSizes.MIN,
FontSizes.MAX
);
if (fontSize >= FontSizes.MIN) {
this.updateFontSize_(container, fontSize);
success = true;
}
})
.then(() => {
return success;
});
}
/**
* Called on each button creation, in case of window resize.
* Page width - (2 x 32px of padding on each side) + (2 x 10px padding on button).
* @param {!Element} pageElement
* @return {number}
* @private
*/
getMaxWidth_(pageElement) {
return pageElement./*OK*/ offsetWidth - 84;
}
/**
* @param {!Element} container
* @param {number} fontSize
*/
updateFontSize_(container, fontSize) {
setStyle(container, 'fontSize', px(fontSize));
}
}
/**
* This used to be binary search, but since range is so small just try the 3
* values. If range gets larger, reevaluate.
* @param {Element} measurer
* @param {number} expectedHeight
* @param {number} expectedWidth
* @param {number} minFontSize
* @param {number} maxFontSize
* @return {number}
*/
function calculateFontSize(
measurer,
expectedHeight,
expectedWidth,
minFontSize,
maxFontSize
) {
for (let fontSize = maxFontSize; fontSize >= minFontSize; fontSize--) {
setStyle(measurer, 'fontSize', px(fontSize));
const height = measurer./*OK*/ offsetHeight;
const width = measurer./*OK*/ offsetWidth;
if (height < expectedHeight && width < expectedWidth) {
return fontSize;
}
}
// Did not fit within design spec.
return minFontSize - 1;
}