2022-07-15 15:05:50 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2022-07-19 15:05:07 +05:30
|
|
|
|
2022-07-15 15:05:50 +05:30
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2022-07-19 15:11:05 +05:30
|
|
|
import {derive} from "./shared/color.mjs";
|
2022-07-15 15:05:50 +05:30
|
|
|
|
|
|
|
export class DerivedVariables {
|
|
|
|
private _baseVariables: Record<string, string>;
|
|
|
|
private _variablesToDerive: string[]
|
|
|
|
private _isDark: boolean
|
2022-07-19 14:55:42 +05:30
|
|
|
private _aliases: Record<string, string> = {};
|
|
|
|
private _derivedAliases: string[] = [];
|
2022-07-15 15:05:50 +05:30
|
|
|
|
|
|
|
constructor(baseVariables: Record<string, string>, variablesToDerive: string[], isDark: boolean) {
|
|
|
|
this._baseVariables = baseVariables;
|
|
|
|
this._variablesToDerive = variablesToDerive;
|
|
|
|
this._isDark = isDark;
|
|
|
|
}
|
|
|
|
|
|
|
|
toVariables(): Record<string, string> {
|
|
|
|
const resolvedVariables: any = {};
|
2022-07-19 14:55:42 +05:30
|
|
|
this._detectAliases();
|
|
|
|
for (const variable of this._variablesToDerive) {
|
|
|
|
const resolvedValue = this._derive(variable);
|
|
|
|
if (resolvedValue) {
|
|
|
|
resolvedVariables[variable] = resolvedValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const [alias, variable] of Object.entries(this._aliases) as any) {
|
|
|
|
resolvedVariables[alias] = this._baseVariables[variable] ?? resolvedVariables[variable];
|
|
|
|
}
|
|
|
|
for (const variable of this._derivedAliases) {
|
|
|
|
const resolvedValue = this._deriveAlias(variable, resolvedVariables);
|
|
|
|
if (resolvedValue) {
|
|
|
|
resolvedVariables[variable] = resolvedValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resolvedVariables;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _detectAliases() {
|
|
|
|
const newVariablesToDerive: string[] = [];
|
2022-07-15 15:05:50 +05:30
|
|
|
for (const variable of this._variablesToDerive) {
|
|
|
|
// If this is an alias, store it for processing later
|
|
|
|
const [alias, value] = variable.split("=");
|
|
|
|
if (value) {
|
2022-07-19 14:55:42 +05:30
|
|
|
this._aliases[alias] = value;
|
2022-07-15 15:05:50 +05:30
|
|
|
}
|
2022-07-19 14:55:42 +05:30
|
|
|
else {
|
|
|
|
newVariablesToDerive.push(variable);
|
2022-07-15 15:05:50 +05:30
|
|
|
}
|
|
|
|
}
|
2022-07-19 14:55:42 +05:30
|
|
|
this._variablesToDerive = newVariablesToDerive;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _derive(variable: string): string | undefined {
|
|
|
|
const RE_VARIABLE_VALUE = /(.+)--(.+)-(.+)/;
|
|
|
|
const matches = variable.match(RE_VARIABLE_VALUE);
|
|
|
|
if (matches) {
|
|
|
|
const [, baseVariable, operation, argument] = matches;
|
|
|
|
const value = this._baseVariables[baseVariable];
|
|
|
|
if (!value ) {
|
|
|
|
if (this._aliases[baseVariable]) {
|
|
|
|
this._derivedAliases.push(variable);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error(`Cannot find value for base variable "${baseVariable}"!`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const resolvedValue = derive(value, operation, argument, this._isDark);
|
|
|
|
return resolvedValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private _deriveAlias(variable: string, resolvedVariables: Record<string, string>): string | undefined {
|
|
|
|
const RE_VARIABLE_VALUE = /(.+)--(.+)-(.+)/;
|
|
|
|
const matches = variable.match(RE_VARIABLE_VALUE);
|
|
|
|
if (matches) {
|
|
|
|
const [, baseVariable, operation, argument] = matches;
|
|
|
|
const value = resolvedVariables[baseVariable];
|
|
|
|
if (!value ) {
|
|
|
|
throw new Error(`Cannot find value for alias "${baseVariable}" when trying to derive ${variable}!`);
|
|
|
|
}
|
|
|
|
const resolvedValue = derive(value, operation, argument, this._isDark);
|
|
|
|
return resolvedValue;
|
2022-07-15 15:05:50 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|