UNPKG

3.21 kBPlain TextView Raw
1import { JupyterLab, JupyterFrontEndPlugin } from '@jupyterlab/application';
2import { IMainMenu } from '@jupyterlab/mainmenu';
3import { ICommandPalette } from '@jupyterlab/apputils';
4import { INotebookTracker } from '@jupyterlab/notebook';
5import { ISettingRegistry } from '@jupyterlab/settingregistry';
6
7import { IFontManager, PACKAGE_NAME, CMD, IFontFaceOptions } from '.';
8
9import { ICONS } from './icons';
10import { FontManager } from './manager';
11import { NotebookFontsButton } from './button';
12import { FontEditor } from './editor';
13import { LicenseViewer } from './license';
14
15const PLUGIN_ID = `${PACKAGE_NAME}:fonts`;
16
17let licenseId = 0;
18
19const plugin: JupyterFrontEndPlugin<IFontManager> = {
20 id: PLUGIN_ID,
21 autoStart: true,
22 requires: [IMainMenu, ISettingRegistry, ICommandPalette, INotebookTracker],
23 provides: IFontManager,
24 activate: function(
25 app: JupyterLab,
26 menu: IMainMenu,
27 settingRegistry: ISettingRegistry,
28 palette: ICommandPalette,
29 notebooks: INotebookTracker
30 ): IFontManager {
31 const manager = new FontManager(app.commands, palette, notebooks);
32
33 manager.licensePaneRequested.connect((it, font: IFontFaceOptions) => {
34 let license = new LicenseViewer({ font });
35 license.id = `jp-fonts-license-${licenseId++}`;
36 license.title.label = font.name;
37 license.title.closable = true;
38 license.title.icon = ICONS.license;
39 app.shell.add(license, 'main');
40 app.shell.activateById(license.id);
41 });
42
43 menu.settingsMenu.addGroup([{ type: 'submenu', submenu: manager.menu }]);
44
45 app.commands.addCommand(CMD.editFonts, {
46 label: 'Global Fonts...',
47 execute: args => {
48 const editor = new FontEditor();
49 const { model } = editor;
50 model.fonts = manager;
51 editor.title.icon = ICONS.fonts;
52 editor.title.closable = true;
53 if ((args || {})['global']) {
54 editor.title.label = 'Global';
55 editor.id = 'font-editor-global';
56 } else {
57 const { currentWidget } = notebooks;
58 if (currentWidget == null) {
59 return;
60 }
61 model.notebook = currentWidget;
62 editor.id = `font-editor-${model.notebook.id}`;
63 model.notebook.disposed.connect(() => editor.dispose());
64 }
65
66 app.shell.add(editor, 'main', { mode: 'split-right' });
67 }
68 });
69
70 const fontsButton = new NotebookFontsButton();
71 fontsButton.widgetRequested.connect(async () => {
72 try {
73 await app.commands.execute(CMD.editFonts);
74 } catch (err) {
75 console.warn(err);
76 }
77 });
78
79 app.docRegistry.addWidgetExtension('Notebook', fontsButton);
80
81 Promise.all([settingRegistry.load(PLUGIN_ID), app.restored])
82 .then(async ([settings]) => {
83 manager.settings = settings;
84 settingRegistry
85 .load('@jupyterlab/apputils-extension:themes')
86 .then(settings => {
87 settings.changed.connect(() => {
88 setTimeout(() => manager.hack(), 100);
89 });
90 })
91 .catch(console.warn);
92 })
93 .catch((reason: Error) => {
94 console.error(reason);
95 });
96
97 return manager;
98 }
99};
100
101export default plugin;