1 | import { Token } from '@lumino/coreutils';
|
2 | import { CommandRegistry } from '@lumino/commands';
|
3 | import { ICommandPalette } from '@jupyterlab/apputils';
|
4 | import { ISignal } from '@lumino/signaling';
|
5 | import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
|
6 |
|
7 | import { Menu } from '@lumino/widgets';
|
8 |
|
9 | import * as SCHEMA from './schema';
|
10 |
|
11 | import '../style/index.css';
|
12 |
|
13 | export type Scope = 'global' | 'notebook';
|
14 |
|
15 | export enum TextKind {
|
16 | code = 'code',
|
17 | content = 'content'
|
18 | }
|
19 |
|
20 | export const KIND_LABELS: { [key in TextKind]: string } = {
|
21 | code: 'Code',
|
22 | content: 'Content'
|
23 | };
|
24 |
|
25 | export enum FontFormat {
|
26 | woff2 = 'woff2',
|
27 | woff = 'woff'
|
28 | }
|
29 |
|
30 | export type TFontMimeTypes = { [key in FontFormat]: string };
|
31 |
|
32 | export const FONT_FORMATS = {
|
33 | woff2: 'font/woff2',
|
34 | woff: 'font/woff'
|
35 | };
|
36 |
|
37 | export type TextProperty = 'font-family' | 'font-size' | 'line-height';
|
38 |
|
39 | export interface IFontCallback {
|
40 | (): Promise<SCHEMA.IFontFacePrimitive[]>;
|
41 | }
|
42 |
|
43 | export interface IFontLicense {
|
44 | name: string;
|
45 | spdx: string;
|
46 | text: () => Promise<string>;
|
47 | holders: string[];
|
48 | }
|
49 |
|
50 | export interface IFontFaceOptions {
|
51 | name: string;
|
52 | faces: IFontCallback;
|
53 | license: IFontLicense;
|
54 | }
|
55 |
|
56 | export const CMD = {
|
57 | code: {
|
58 | fontSize: 'code-font-size',
|
59 | fontFamily: 'code-font-family',
|
60 | lineHeight: 'code-line-height'
|
61 | },
|
62 | content: {
|
63 | fontSize: 'content-font-size',
|
64 | fontFamily: 'content-font-family',
|
65 | lineHeight: 'content-line-height'
|
66 | },
|
67 | editFonts: 'font-editor:open',
|
68 | customFonts: {
|
69 | disable: 'custom-fonts:disable',
|
70 | enable: 'custom-fonts:enable'
|
71 | }
|
72 | };
|
73 |
|
74 | export const ROOT = ':root';
|
75 |
|
76 | export type ICSSVars = {
|
77 | [key in TextKind]: { [key in TextProperty]: SCHEMA.ICSSOM };
|
78 | };
|
79 |
|
80 | export const CSS: ICSSVars = {
|
81 | code: {
|
82 | 'font-family': '--jp-code-font-family',
|
83 | 'font-size': '--jp-code-font-size',
|
84 | 'line-height': '--jp-code-line-height'
|
85 | },
|
86 | content: {
|
87 | 'font-family': '--jp-content-font-family',
|
88 | 'font-size': '--jp-content-font-size1',
|
89 | 'line-height': '--jp-content-line-height'
|
90 | }
|
91 | };
|
92 |
|
93 | export type ICSSTextOptions = {
|
94 | [key in TextProperty]: (manager: IFontManager) => SCHEMA.ICSSOM[];
|
95 | };
|
96 |
|
97 | export const TEXT_OPTIONS: ICSSTextOptions = {
|
98 | 'font-size': _m => Array.from(Array(25).keys()).map(i => `${i + 8}px`),
|
99 | 'line-height': _m => Array.from(Array(8).keys()).map(i => `${i * 0.25 + 1}`),
|
100 | 'font-family': m => {
|
101 | let names = Array.from(m.fonts.values()).reduce((memo, f) => {
|
102 | return memo.concat(f.name);
|
103 | }, [] as string[]);
|
104 | names.sort((a, b) => a.localeCompare(b));
|
105 | return names;
|
106 | }
|
107 | };
|
108 |
|
109 | export type ICSSTextLabels = { [key in TextProperty]: string };
|
110 |
|
111 | export const TEXT_LABELS: ICSSTextLabels = {
|
112 | 'font-size': 'Size',
|
113 | 'line-height': 'Line Height',
|
114 | 'font-family': 'Font'
|
115 | };
|
116 |
|
117 | export const DEFAULT = {
|
118 | code: {
|
119 | fontSize: '13px',
|
120 | lineHeight: '1',
|
121 | fontFamily: '"Source Code Pro", monospace'
|
122 | }
|
123 | };
|
124 |
|
125 | export const PACKAGE_NAME: string = '@deathbeds/jupyterlab-fonts';
|
126 | export const CONFIGURED_CLASS = 'jp-fonts-configured';
|
127 |
|
128 |
|
129 | export const IFontManager = new Token<IFontManager>(
|
130 | '@deathbeds/jupyterlab-fonts:IFontManager'
|
131 | );
|
132 |
|
133 | export interface IFontManagerConstructor {
|
134 | new (
|
135 | commands: CommandRegistry,
|
136 | palette: ICommandPalette,
|
137 | notebooks: INotebookTracker
|
138 | ): IFontManager;
|
139 | }
|
140 |
|
141 | export interface IFontManager {
|
142 | ready: Promise<void>;
|
143 | registerFontFace(options: IFontFaceOptions): void;
|
144 | licensePaneRequested: ISignal<IFontManager, any>;
|
145 | requestLicensePane(font: any): void;
|
146 | fonts: Map<string, IFontFaceOptions>;
|
147 | stylesheets: HTMLStyleElement[];
|
148 | menu: Menu;
|
149 | getVarName(
|
150 | property: TextProperty,
|
151 | options: ITextStyleOptions
|
152 | ): SCHEMA.ICSSOM | null;
|
153 | getTextStyle(
|
154 | property: TextProperty,
|
155 | options: ITextStyleOptions
|
156 | ): SCHEMA.ICSSOM | null;
|
157 | setTextStyle(
|
158 | property: TextProperty,
|
159 | value: SCHEMA.ICSSOM | null,
|
160 | options: ITextStyleOptions
|
161 | ): void;
|
162 | dataURISrc(url: string, format: FontFormat): Promise<string>;
|
163 | }
|
164 |
|
165 | export interface ITextStyleOptions {
|
166 | kind: TextKind;
|
167 | scope?: Scope;
|
168 | notebook?: NotebookPanel;
|
169 | }
|