You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-30 23:44:55 +02:00
Desktop: Simplified and improve command service, and added command palette
- Commands "enabled" state is now expressed using a "when-clause" like in VSCode - A command palette has been added to the Tools menu
This commit is contained in:
42
ReactNativeClient/lib/services/WhenClause.ts
Normal file
42
ReactNativeClient/lib/services/WhenClause.ts
Normal file
@ -0,0 +1,42 @@
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user