-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
FontSelect.ts
151 lines (129 loc) · 5.54 KB
/
FontSelect.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
*/
import { AlloyTriggers, AlloyComponent } from '@ephox/alloy';
import { Arr, Option } from '@ephox/katamari';
import Editor from 'tinymce/core/api/Editor';
import { UiFactoryBackstage } from 'tinymce/themes/silver/backstage/Backstage';
import { updateMenuText } from '../../dropdown/CommonDropdown';
import { createMenuItems, createSelectButton, SelectSpec } from './BespokeSelect';
import { buildBasicSettingsDataset, Delimiter } from './SelectDatasets';
const defaultFontsFormats = 'Andale Mono=andale mono,monospace;' +
'Arial=arial,helvetica,sans-serif;' +
'Arial Black=arial black,sans-serif;' +
'Book Antiqua=book antiqua,palatino,serif;' +
'Comic Sans MS=comic sans ms,sans-serif;' +
'Courier New=courier new,courier,monospace;' +
'Georgia=georgia,palatino,serif;' +
'Helvetica=helvetica,arial,sans-serif;' +
'Impact=impact,sans-serif;' +
'Symbol=symbol;' +
'Tahoma=tahoma,arial,helvetica,sans-serif;' +
'Terminal=terminal,monaco,monospace;' +
'Times New Roman=times new roman,times,serif;' +
'Trebuchet MS=trebuchet ms,geneva,sans-serif;' +
'Verdana=verdana,geneva,sans-serif;' +
'Webdings=webdings;' +
'Wingdings=wingdings,zapf dingbats';
// A list of fonts that must be in a font family for the font to be recognised as the system stack
// Note: Don't include 'BlinkMacSystemFont', as Chrome on Mac converts it to different names
const systemStackFonts = [ '-apple-system', 'Segoe UI', 'Roboto', 'Helvetica Neue', 'sans-serif' ];
// Split the fonts into an array and strip away any start/end quotes
const splitFonts = (fontFamily: string): string[] => {
const fonts = fontFamily.split(/\s*,\s*/);
return Arr.map(fonts, (font) => font.replace(/^['"]+|['"]+$/g, ''));
};
const isSystemFontStack = (fontFamily: string): boolean => {
// The system font stack will be similar to the following. (Note: each has minor variants)
// Oxide: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
// Bootstrap: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
// Wordpress: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
const matchesSystemStack = (): boolean => {
const fonts = splitFonts(fontFamily.toLowerCase());
return Arr.forall(systemStackFonts, (font) => fonts.indexOf(font.toLowerCase()) > -1);
};
return fontFamily.indexOf('-apple-system') === 0 && matchesSystemStack();
};
const getSpec = (editor: Editor): SelectSpec => {
const getMatchingValue = () => {
const getFirstFont = (fontFamily) => {
return fontFamily ? splitFonts(fontFamily)[0] : '';
};
const fontFamily = editor.queryCommandValue('FontName');
const items = dataset.data;
const font = fontFamily ? fontFamily.toLowerCase() : '';
const matchOpt = Arr.find(items, (item) => {
const format = item.format;
return (format.toLowerCase() === font) || (getFirstFont(format).toLowerCase() === getFirstFont(font).toLowerCase());
}).orThunk(() => {
if (isSystemFontStack(font)) {
return Option.from({
title: 'System Font',
format: font
});
} else {
return Option.none();
}
});
return { matchOpt, font: fontFamily };
};
const isSelectedFor = (item) => {
return (valueOpt: Option<{ format: string; title: string }>) => {
return valueOpt.exists((value) => value.format === item);
};
};
const getCurrentValue = () => {
const { matchOpt } = getMatchingValue();
return matchOpt;
};
const getPreviewFor = (item) => () => {
return Option.some({
tag: 'div',
styleAttr: item.indexOf('dings') === -1 ? 'font-family:' + item : ''
});
};
const onAction = (rawItem) => () => {
editor.undoManager.transact(() => {
editor.focus();
editor.execCommand('FontName', false, rawItem.format);
});
};
const updateSelectMenuText = (comp: AlloyComponent) => {
const { matchOpt, font } = getMatchingValue();
const text = matchOpt.fold(() => font, (item) => item.title);
AlloyTriggers.emitWith(comp, updateMenuText, {
text
});
};
const nodeChangeHandler = Option.some((comp) => () => updateSelectMenuText(comp));
const setInitialValue = Option.some((comp) => updateSelectMenuText(comp));
const dataset = buildBasicSettingsDataset(editor, 'font_formats', defaultFontsFormats, Delimiter.SemiColon);
return {
tooltip: 'Fonts',
icon: Option.none(),
isSelectedFor,
getCurrentValue,
getPreviewFor,
onAction,
setInitialValue,
nodeChangeHandler,
dataset,
shouldHide: false,
isInvalid: () => false
};
};
const createFontSelect = (editor: Editor, backstage: UiFactoryBackstage) => {
return createSelectButton(editor, backstage, getSpec(editor));
};
// TODO: Test this!
const fontSelectMenu = (editor: Editor, backstage: UiFactoryBackstage) => {
const menuItems = createMenuItems(editor, backstage, getSpec(editor));
editor.ui.registry.addNestedMenuItem('fontformats', {
text: backstage.shared.providers.translate('Fonts'),
getSubmenuItems: () => menuItems.items.validateItems(menuItems.getStyleItems())
});
};
export { createFontSelect, fontSelectMenu };