Skip to content

Commit 857dc10

Browse files
committed
Update font management interfaces for VM update
1 parent 34a7f18 commit 857dc10

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

src/components/tw-fonts-modal/add-custom-font.jsx

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ class AddCustomFont extends React.Component {
162162
name={this.state.name}
163163
onChange={this.handleChangeName}
164164
fontManager={this.props.fontManager}
165+
isCustom
165166
/>
166167

167168
<LoadTemporaryFont url={this.state.url}>{family => (

src/components/tw-fonts-modal/add-system-font.jsx

+2-16
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,10 @@ class AddSystemFont extends React.Component {
1717
]);
1818
this.state = {
1919
name: '',
20-
fallback: FontFallback.DEFAULT,
21-
localFonts: null
20+
fallback: FontFallback.DEFAULT
2221
};
2322
}
2423

25-
componentDidMount () {
26-
// Chrome-only API
27-
if (typeof queryLocalFonts === 'function') {
28-
// eslint-disable-next-line no-undef
29-
queryLocalFonts().then(fonts => {
30-
const uniqueFamilies = [...new Set(fonts.map(i => i.family))];
31-
this.setState({
32-
localFonts: uniqueFamilies
33-
});
34-
});
35-
}
36-
}
37-
3824
handleChangeName (name) {
3925
this.setState({
4026
name
@@ -69,7 +55,7 @@ class AddSystemFont extends React.Component {
6955
onChange={this.handleChangeName}
7056
fontManager={this.props.fontManager}
7157
placeholder="Wingdings"
72-
options={this.state.localFonts}
58+
isCustom={false}
7359
/>
7460

7561
{this.state.name && (

src/components/tw-fonts-modal/font-name.jsx

+26-8
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,24 @@ class FontName extends React.Component {
1919
]);
2020
this.state = {
2121
focused: false,
22-
rect: null
22+
rect: null,
23+
localFonts: []
2324
};
2425
}
2526

2627
componentDidMount () {
2728
window.addEventListener('resize', this.handleResize);
29+
30+
// Chrome-only API
31+
if (typeof queryLocalFonts === 'function') {
32+
// eslint-disable-next-line no-undef
33+
queryLocalFonts().then(fonts => {
34+
const uniqueFamilies = [...new Set(fonts.map(i => i.family))];
35+
this.setState({
36+
localFonts: uniqueFamilies
37+
});
38+
});
39+
}
2840
}
2941

3042
componentWillUnmount () {
@@ -52,7 +64,12 @@ class FontName extends React.Component {
5264
}
5365

5466
handleBlur () {
55-
this.props.onChange(this.props.fontManager.getSafeName(this.props.name));
67+
const sanitizedName = this.props.isCustom ? (
68+
this.props.fontManager.getUnusedCustomFont(this.props.name)
69+
) : (
70+
this.props.fontManager.getUnusedSystemFont(this.props.name)
71+
);
72+
this.props.onChange(sanitizedName);
5673
this.setState({
5774
focused: false
5875
});
@@ -78,13 +95,13 @@ class FontName extends React.Component {
7895
}
7996

8097
getFilteredOptions () {
81-
if (!this.state.focused || !this.props.options) {
98+
if (this.props.isCustom || !this.state.focused) {
8299
return [];
83100
}
84101
const name = this.props.name.toLowerCase();
85-
const candidates = this.props.options
102+
const candidates = this.state.localFonts
86103
.filter(family => family.toLowerCase().includes(name));
87-
if (candidates.length === 0 && candidates[0] === this.props.name) {
104+
if (candidates.length === 1 && candidates[0] === this.props.name) {
88105
return [];
89106
}
90107
return candidates;
@@ -96,7 +113,7 @@ class FontName extends React.Component {
96113
name,
97114
onChange,
98115
fontManager,
99-
options,
116+
isCustom,
100117
/* eslint-enable no-unused-vars */
101118
...props
102119
} = this.props;
@@ -145,9 +162,10 @@ FontName.propTypes = {
145162
name: PropTypes.string.isRequired,
146163
onChange: PropTypes.func.isRequired,
147164
fontManager: PropTypes.shape({
148-
getSafeName: PropTypes.func.isRequired
165+
getUnusedSystemFont: PropTypes.func.isRequired,
166+
getUnusedCustomFont: PropTypes.func.isRequired,
149167
}).isRequired,
150-
options: PropTypes.arrayOf(PropTypes.string.isRequired)
168+
isCustom: PropTypes.bool.isRequired
151169
};
152170

153171
export default FontName;

src/lib/vm-manager-hoc.jsx

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ import {
1515
} from '../reducers/project-state';
1616
import log from './log';
1717

18+
/**
19+
* List of fonts that could be used by security prompts.
20+
*/
21+
const SECURITY_CRITICAL_FONTS = [
22+
'Helvetica Neue',
23+
'Helvetica',
24+
'Arial'
25+
];
26+
1827
/*
1928
* Higher Order Component to manage events emitted by the VM
2029
* @param {React.Component} WrappedComponent component to manage VM events for
@@ -37,6 +46,9 @@ const vmManagerHOC = function (WrappedComponent) {
3746
} catch (e) {
3847
log.error('could not create scratch-audio', e);
3948
}
49+
for (const font of SECURITY_CRITICAL_FONTS) {
50+
this.props.vm.runtime.fontManager.restrictFont(font);
51+
}
4052
this.props.vm.initialized = true;
4153
this.props.vm.setLocale(this.props.locale, this.props.messages);
4254
}

0 commit comments

Comments
 (0)