mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-18 09:35:20 +02:00
3a57cfea02
- Commands "enabled" state is now expressed using a "when-clause" like in VSCode - A command palette has been added to the Tools menu
43 lines
930 B
TypeScript
43 lines
930 B
TypeScript
import { ContextKeyExpr, ContextKeyExpression } from './contextkey/contextkey';
|
|
|
|
export default class WhenClause {
|
|
|
|
private expression_:string;
|
|
private validate_:boolean;
|
|
private rules_:ContextKeyExpression = null;
|
|
|
|
constructor(expression:string, validate:boolean) {
|
|
this.expression_ = expression;
|
|
this.validate_ = validate;
|
|
}
|
|
|
|
private createContext(ctx: any) {
|
|
return {
|
|
getValue: (key: string) => {
|
|
return ctx[key];
|
|
},
|
|
};
|
|
}
|
|
|
|
private get rules():ContextKeyExpression {
|
|
if (!this.rules_) {
|
|
this.rules_ = ContextKeyExpr.deserialize(this.expression_);
|
|
}
|
|
|
|
return this.rules_;
|
|
}
|
|
|
|
public evaluate(context:any):boolean {
|
|
if (this.validate_) this.validate(context);
|
|
return this.rules.evaluate(this.createContext(context));
|
|
}
|
|
|
|
public validate(context:any) {
|
|
const keys = this.rules.keys();
|
|
for (const key of keys) {
|
|
if (!(key in context)) throw new Error(`No such key: ${key}`);
|
|
}
|
|
}
|
|
|
|
}
|