1 | import { NotebookPanel } from '@jupyterlab/notebook';
|
2 |
|
3 | import { Dialog, showDialog } from '@jupyterlab/apputils';
|
4 |
|
5 | import * as React from 'react';
|
6 |
|
7 | import { VDomModel, VDomRenderer } from '@jupyterlab/apputils';
|
8 |
|
9 | import {
|
10 | TextKind,
|
11 | TEXT_OPTIONS,
|
12 | TEXT_LABELS,
|
13 | KIND_LABELS,
|
14 | TextProperty,
|
15 | IFontFaceOptions,
|
16 | PACKAGE_NAME
|
17 | } from '.';
|
18 |
|
19 | import { FontManager } from './manager';
|
20 |
|
21 | import * as SCHEMA from './schema';
|
22 |
|
23 | import '../style/editor.css';
|
24 |
|
25 | const h = React.createElement;
|
26 |
|
27 | const EDITOR_CLASS = 'jp-FontsEditor';
|
28 | const ENABLED_CLASS = 'jp-FontsEditor-enable';
|
29 | const FIELD_CLASS = 'jp-FontsEditor-field';
|
30 | const EMBED_CLASS = 'jp-FontsEditor-embed';
|
31 | const SECTION_CLASS = 'lm-CommandPalette-header';
|
32 | const BUTTON_CLASS = 'jp-FontsEditor-button jp-mod-styled';
|
33 | const SIZE_CLASS = 'jp-FontsEditor-size';
|
34 | const DUMMY = '-';
|
35 |
|
36 | export class FontEditorModel extends VDomModel {
|
37 | private _notebook: NotebookPanel | null;
|
38 | private _fonts: FontManager;
|
39 |
|
40 | get fonts() {
|
41 | return this._fonts;
|
42 | }
|
43 |
|
44 | set fonts(fonts) {
|
45 | if (this._fonts && this._fonts.settings) {
|
46 | this._fonts.settings.changed.disconnect(this.onSettingsChange, this);
|
47 | }
|
48 | this._fonts = fonts;
|
49 | fonts.settings.changed.connect(this.onSettingsChange, this);
|
50 | this.stateChanged.emit(void 0);
|
51 | }
|
52 |
|
53 | private onSettingsChange() {
|
54 | this.stateChanged.emit(void 0);
|
55 | }
|
56 |
|
57 | get notebook() {
|
58 | return this._notebook;
|
59 | }
|
60 |
|
61 | set notebook(notebook) {
|
62 | if (this._notebook?.model) {
|
63 | this._notebook.model.metadata.changed.disconnect(
|
64 | this.onSettingsChange,
|
65 | this
|
66 | );
|
67 | this._notebook.context.pathChanged.disconnect(
|
68 | this.onSettingsChange,
|
69 | this
|
70 | );
|
71 | }
|
72 | this._notebook = notebook;
|
73 | if (this._notebook?.model) {
|
74 | this._notebook.model.metadata.changed.connect(
|
75 | this.onSettingsChange,
|
76 | this
|
77 | );
|
78 | this._notebook.context.pathChanged.connect(this.onSettingsChange, this);
|
79 | }
|
80 | this.stateChanged.emit(void 0);
|
81 | }
|
82 |
|
83 | get enabled() {
|
84 | return this._fonts.enabled;
|
85 | }
|
86 |
|
87 | async setEnabled(enabled: boolean) {
|
88 | if (this.notebook == null) {
|
89 | await this._fonts.settings.set('enabled', enabled);
|
90 | this.stateChanged.emit(void 0);
|
91 | }
|
92 | }
|
93 |
|
94 | get notebookMetadata() {
|
95 | if (this.notebook?.model) {
|
96 | return this.notebook.model.metadata.get(PACKAGE_NAME) as SCHEMA.ISettings;
|
97 | }
|
98 | }
|
99 |
|
100 | clearNotebookMetadata(fontName?: string) {
|
101 | let meta = this.notebookMetadata;
|
102 | if (fontName) {
|
103 | if (meta?.fonts) {
|
104 | delete meta.fonts[fontName];
|
105 | }
|
106 | if (meta?.fontLicenses) {
|
107 | delete meta.fontLicenses[fontName];
|
108 | }
|
109 | }
|
110 | if (this.notebook?.model) {
|
111 | this.notebook.model.metadata.set(
|
112 | PACKAGE_NAME,
|
113 | JSON.parse(JSON.stringify(meta)) as any
|
114 | );
|
115 | }
|
116 | this.stateChanged.emit(void 0);
|
117 | }
|
118 |
|
119 | dispose() {
|
120 | if (this._fonts && this._fonts.settings) {
|
121 | this._fonts.settings.changed.disconnect(this.onSettingsChange, this);
|
122 | }
|
123 | super.dispose();
|
124 | }
|
125 | }
|
126 |
|
127 | export class FontEditor extends VDomRenderer<FontEditorModel> {
|
128 | constructor() {
|
129 | super(new FontEditorModel());
|
130 | this.addClass(EDITOR_CLASS);
|
131 | }
|
132 |
|
133 | protected render(): React.ReactElement<any> {
|
134 | const m = this.model;
|
135 | if (!m) {
|
136 | return h('div', { key: 'empty' });
|
137 | }
|
138 |
|
139 | return h('div', { key: 'editor' }, [
|
140 | ...this.header(),
|
141 | ...[TextKind.code, TextKind.content].map(kind =>
|
142 | h('section', { key: `${kind}-section`, title: KIND_LABELS[kind] }, [
|
143 | h(
|
144 | 'h3',
|
145 | { key: `${kind}-header`, className: SECTION_CLASS },
|
146 | KIND_LABELS[kind]
|
147 | ),
|
148 | ...[
|
149 | 'font-family',
|
150 | 'font-size',
|
151 | 'line-height'
|
152 | ].map((prop: TextProperty) =>
|
153 | this.textSelect(prop, kind, { key: `${kind}-${prop}` })
|
154 | )
|
155 | ])
|
156 | )
|
157 | ]);
|
158 | }
|
159 |
|
160 | protected fontFaceExtras(m: FontEditorModel, fontFamily: string) {
|
161 | let font: IFontFaceOptions | undefined;
|
162 | let unquoted = `${fontFamily}`.slice(1, -1);
|
163 | if (m.fonts.fonts.get(unquoted)) {
|
164 | font = m.fonts.fonts.get(unquoted);
|
165 | }
|
166 | return !font ? [] : [this.licenseButton(m, font)];
|
167 | }
|
168 |
|
169 | protected licenseButton(m: FontEditorModel, font: IFontFaceOptions) {
|
170 | return h(
|
171 | 'button',
|
172 | {
|
173 | className: BUTTON_CLASS,
|
174 | title: font.license.name,
|
175 | key: font.name,
|
176 | onClick: () => m.fonts.requestLicensePane(font)
|
177 | },
|
178 | font.license.spdx
|
179 | );
|
180 | }
|
181 |
|
182 | protected textSelect(prop: TextProperty, kind: TextKind, sectionProps: {}) {
|
183 | const m = this.model;
|
184 | const onChange = (evt: React.FormEvent<HTMLSelectElement>) => {
|
185 | let value: string | null = (evt.target as HTMLSelectElement).value;
|
186 | value = value === DUMMY ? null : value;
|
187 | m.fonts
|
188 | .setTextStyle(prop, value, {
|
189 | kind,
|
190 | ...(m.notebook ? { notebook: m.notebook } : {})
|
191 | })
|
192 | .catch(console.warn);
|
193 | };
|
194 | const value = m.fonts.getTextStyle(prop, {
|
195 | kind,
|
196 | notebook: m.notebook || void 0
|
197 | });
|
198 | const extra =
|
199 | prop === 'font-family' ? this.fontFaceExtras(m, value as any) : [];
|
200 |
|
201 | return h(
|
202 | 'div',
|
203 | { className: FIELD_CLASS, key: 'select-field', ...sectionProps },
|
204 | [
|
205 | h('label', { key: 'select-label' }, TEXT_LABELS[prop]),
|
206 | h('div', { key: 'select-wrap' }, [
|
207 | ...extra,
|
208 | h(
|
209 | 'select',
|
210 | {
|
211 | className: 'jp-mod-styled',
|
212 | title: `${TEXT_LABELS[prop]}`,
|
213 | onChange,
|
214 | defaultValue: value || DUMMY,
|
215 | key: `select`
|
216 | },
|
217 | [null, ...TEXT_OPTIONS[prop](m.fonts)].map(value => {
|
218 | return h(
|
219 | 'option',
|
220 | {
|
221 | key: `'${value}'`,
|
222 | value:
|
223 | value == null
|
224 | ? DUMMY
|
225 | : prop === 'font-family'
|
226 | ? `'${value}'`
|
227 | : value
|
228 | },
|
229 | value || DUMMY
|
230 | );
|
231 | })
|
232 | )
|
233 | ])
|
234 | ]
|
235 | );
|
236 | }
|
237 |
|
238 | protected deleteButton(m: FontEditorModel, fontName: string) {
|
239 | return h(
|
240 | 'button',
|
241 | {
|
242 | className: BUTTON_CLASS,
|
243 | title: `Delete Embedded Font`,
|
244 | key: 'delete',
|
245 | onClick: async () => {
|
246 | const result = await showDialog({
|
247 | title: `Delete Embedded Font from Notebook`,
|
248 | body: `If you dont have ${fontName} installed, you might not be able to re-embed it`,
|
249 | buttons: [
|
250 | Dialog.cancelButton(),
|
251 | Dialog.warnButton({ label: 'DELETE' })
|
252 | ]
|
253 | });
|
254 |
|
255 | if (result.button.accept) {
|
256 | m.clearNotebookMetadata(fontName);
|
257 | }
|
258 | }
|
259 | },
|
260 | 'Delete'
|
261 | );
|
262 | }
|
263 |
|
264 | protected enabler(m: FontEditorModel) {
|
265 | const onChange = async (evt: Event) => {
|
266 | await m.setEnabled(!!(evt.currentTarget as HTMLInputElement).checked);
|
267 | };
|
268 |
|
269 | return h(
|
270 | 'label',
|
271 | { key: 'enable-label' },
|
272 | h('span', { key: 'enable-text' }, 'Enabled'),
|
273 | h('input', {
|
274 | key: 'enable-input',
|
275 | type: 'checkbox',
|
276 | checked: m.enabled,
|
277 | onChange
|
278 | })
|
279 | );
|
280 | }
|
281 |
|
282 | protected embeddedFont(m: FontEditorModel, fontName: string) {
|
283 | if (
|
284 | m.notebookMetadata?.fonts == null ||
|
285 | m.notebookMetadata.fontLicenses == null
|
286 | ) {
|
287 | return null;
|
288 | }
|
289 | const faces = m.notebookMetadata.fonts[fontName];
|
290 | const license = m.notebookMetadata.fontLicenses[fontName];
|
291 | const size = (faces || []).reduce(
|
292 | (memo, face) => memo + `${face.src}`.length,
|
293 | license.text.length
|
294 | );
|
295 | const kb = parseInt(`${size / 1024}`, 10);
|
296 |
|
297 | return h('li', { key: fontName }, [
|
298 | h('label', { key: 'label' }, fontName),
|
299 | this.licenseButton(m, {
|
300 | name: fontName,
|
301 | license: {
|
302 | name: license.name,
|
303 | spdx: license.spdx,
|
304 | text: async () => license.text,
|
305 | holders: license.holders
|
306 | },
|
307 | faces: async () => faces || []
|
308 | }),
|
309 | h('span', { className: SIZE_CLASS, key: 'font-kb' }, `${kb} kb`),
|
310 | this.deleteButton(m, fontName)
|
311 | ]);
|
312 | }
|
313 |
|
314 | protected header() {
|
315 | const m = this.model;
|
316 | const title = m.notebook
|
317 | ? m.notebook.context.contentsModel?.name.replace(/.ipynb$/, '')
|
318 | : 'Global';
|
319 |
|
320 | this.title.label = title || 'Unknown';
|
321 |
|
322 | const h2 = h('h2', { key: 'scope-head' }, [
|
323 | h('label', { key: 'scope-label' }, `Fonts » ${title}`),
|
324 | ...(m.notebook
|
325 | ? [h('div', { className: 'jp-NotebookIcon', key: 'scope-icon' })]
|
326 | : [])
|
327 | ]);
|
328 |
|
329 | if (m.notebook != null) {
|
330 | return [
|
331 | h2,
|
332 | h('section', { key: 'embed-section' }, [
|
333 | h(
|
334 | 'h3',
|
335 | { className: SECTION_CLASS, key: 'embed-head' },
|
336 | 'Embedded fonts'
|
337 | ),
|
338 | h(
|
339 | 'ul',
|
340 | { className: EMBED_CLASS, key: 'embeds' },
|
341 | Object.keys((m.notebookMetadata || {}).fonts || {}).map(
|
342 | fontName => {
|
343 | return this.embeddedFont(m, fontName);
|
344 | }
|
345 | )
|
346 | )
|
347 | ])
|
348 | ];
|
349 | } else {
|
350 | return [
|
351 | h2,
|
352 | h('section', { key: 'enable-section', className: ENABLED_CLASS }, [
|
353 | h(
|
354 | 'h3',
|
355 | { className: SECTION_CLASS, key: 'enable-header' },
|
356 | 'Enable/Disable All Fonts'
|
357 | ),
|
358 | this.enabler(m)
|
359 | ])
|
360 | ];
|
361 | }
|
362 | }
|
363 | }
|