UNPKG

5.16 kBPlain TextView Raw
1// *****************************************************************************
2// Copyright (C) 2019 TypeFox and others.
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License v. 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0.
7//
8// This Source Code may also be made available under the following Secondary
9// Licenses when the conditions for such availability set forth in the Eclipse
10// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11// with the GNU Classpath Exception which is available at
12// https://www.gnu.org/software/classpath/license.html.
13//
14// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15// *****************************************************************************
16
17import { injectable } from 'inversify';
18import { Disposable } from '../common';
19import { Emitter, Event } from '../common/event';
20
21export type ContextKeyValue = null | undefined | boolean | number | string
22 | Array<null | undefined | boolean | number | string>
23 | Record<string, null | undefined | boolean | number | string>;
24
25export interface ContextKey<T extends ContextKeyValue = ContextKeyValue> {
26 set(value: T | undefined): void;
27 reset(): void;
28 get(): T | undefined;
29}
30
31export namespace ContextKey {
32 // eslint-disable-next-line @typescript-eslint/no-explicit-any
33 export const None: ContextKey<any> = Object.freeze({
34 set: () => { },
35 reset: () => { },
36 get: () => undefined
37 });
38}
39
40export interface ContextKeyChangeEvent {
41 affects(keys: { has(key: string): boolean }): boolean;
42}
43
44export const ContextKeyService = Symbol('ContextKeyService');
45
46export interface ContextMatcher extends Disposable {
47 /**
48 * Whether the expression is satisfied. If `context` provided, the service will attempt to retrieve a context object associated with that element.
49 */
50 match(expression: string, context?: HTMLElement): boolean;
51}
52
53export interface ContextKeyService extends ContextMatcher {
54 readonly onDidChange: Event<ContextKeyChangeEvent>;
55
56 createKey<T extends ContextKeyValue>(key: string, defaultValue: T | undefined): ContextKey<T>;
57
58 /**
59 * @returns a Set of the keys used by the given `expression` or `undefined` if none are used or the expression cannot be parsed.
60 */
61 parseKeys(expression: string): Set<string> | undefined;
62
63 /**
64 * Creates a temporary context that will use the `values` passed in when evaluating {@link callback}.
65 * {@link callback | The callback} must be synchronous.
66 */
67 with<T>(values: Record<string, unknown>, callback: () => T): T;
68
69 /**
70 * Creates a child service with a separate context scoped to the HTML element passed in.
71 * Useful for e.g. setting the {view} context value for particular widgets.
72 */
73 createScoped(target: HTMLElement): ScopedValueStore;
74
75 /**
76 * @param overlay values to be used in the new {@link ContextKeyService}. These values will be static.
77 * Creates a child service with a separate context and a set of fixed values to override parent values.
78 */
79 createOverlay(overlay: Iterable<[string, unknown]>): ContextMatcher;
80
81 /**
82 * Set or modify a value in the service's context.
83 */
84 setContext(key: string, value: unknown): void;
85}
86
87export type ScopedValueStore = Omit<ContextKeyService, 'onDidChange' | 'match' | 'parseKeys' | 'with' | 'createOverlay'>;
88
89@injectable()
90export class ContextKeyServiceDummyImpl implements ContextKeyService {
91
92 protected readonly onDidChangeEmitter = new Emitter<ContextKeyChangeEvent>();
93 readonly onDidChange = this.onDidChangeEmitter.event;
94 protected fireDidChange(event: ContextKeyChangeEvent): void {
95 this.onDidChangeEmitter.fire(event);
96 }
97
98 createKey<T extends ContextKeyValue>(key: string, defaultValue: T | undefined): ContextKey<T> {
99 return ContextKey.None;
100 }
101 /**
102 * It should be implemented by an extension, e.g. by the monaco extension.
103 */
104 match(expression: string, context?: HTMLElement): boolean {
105 return true;
106 }
107
108 /**
109 * It should be implemented by an extension, e.g. by the monaco extension.
110 */
111 parseKeys(expression: string): Set<string> | undefined {
112 return new Set<string>();
113 }
114
115 /**
116 * Details should be implemented by an extension, e.g. by the monaco extension.
117 * Callback must be synchronous.
118 */
119 with<T>(values: Record<string, unknown>, callback: () => T): T {
120 return callback();
121 }
122
123 /**
124 * Details should implemented by an extension, e.g. by the monaco extension.
125 */
126 createScoped(target: HTMLElement): ContextKeyService {
127 return this;
128 }
129
130 /**
131 * Details should be implemented by an extension, e.g. the monaco extension.
132 */
133 createOverlay(overlay: Iterable<[string, unknown]>): ContextMatcher {
134 return this;
135 }
136
137 /**
138 * Details should be implemented by an extension, e.g. by the monaco extension.
139 */
140 setContext(key: string, value: unknown): void { }
141
142 dispose(): void { }
143}
144
\No newline at end of file