2022-03-24 12:06:01 +01:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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-03-14 07:41:46 +01:00
|
|
|
const valueParser = require("postcss-value-parser");
|
2022-03-25 07:16:48 +01:00
|
|
|
|
2022-03-25 07:10:21 +01:00
|
|
|
/**
|
2022-04-03 13:16:26 +02:00
|
|
|
* This plugin extracts content inside url() into css variables and adds the variables to the root section.
|
|
|
|
* This plugin is used in conjunction with css-url-processor plugin to colorize svg icons.
|
2022-03-25 07:10:21 +01:00
|
|
|
*/
|
2022-03-24 12:06:01 +01:00
|
|
|
let counter;
|
2022-03-25 07:16:48 +01:00
|
|
|
let urlVariables;
|
2022-03-24 12:06:01 +01:00
|
|
|
const idToPrepend = "icon-url";
|
2022-03-14 07:41:46 +01:00
|
|
|
|
2022-03-25 11:38:15 +01:00
|
|
|
function findAndReplaceUrl(decl) {
|
2022-03-14 07:41:46 +01:00
|
|
|
const value = decl.value;
|
2022-06-10 17:10:23 +02:00
|
|
|
let parsed;
|
|
|
|
try {
|
|
|
|
parsed = valueParser(value);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(`Error trying to parse ${decl}`);
|
|
|
|
throw err;
|
|
|
|
}
|
2022-03-14 07:41:46 +01:00
|
|
|
parsed.walk(node => {
|
|
|
|
if (node.type !== "function" || node.value !== "url") {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-25 13:51:03 +01:00
|
|
|
const url = node.nodes[0].value;
|
|
|
|
if (!url.match(/\.svg\?primary=.+/)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-24 12:06:01 +01:00
|
|
|
const variableName = `${idToPrepend}-${counter++}`;
|
2022-03-25 13:51:03 +01:00
|
|
|
urlVariables.set(variableName, url);
|
2022-03-25 11:36:40 +01:00
|
|
|
node.value = "var";
|
|
|
|
node.nodes = [{ type: "word", value: `--${variableName}` }];
|
2022-03-14 07:41:46 +01:00
|
|
|
});
|
|
|
|
decl.assign({prop: decl.prop, value: parsed.toString()})
|
|
|
|
}
|
|
|
|
|
|
|
|
function addResolvedVariablesToRootSelector(root, { Rule, Declaration }) {
|
|
|
|
const newRule = new Rule({ selector: ":root", source: root.source });
|
|
|
|
// Add derived css variables to :root
|
2022-03-24 12:06:01 +01:00
|
|
|
urlVariables.forEach((value, key) => {
|
2022-03-25 11:36:40 +01:00
|
|
|
const declaration = new Declaration({ prop: `--${key}`, value: `url("${value}")`});
|
2022-03-14 07:41:46 +01:00
|
|
|
newRule.append(declaration);
|
|
|
|
});
|
|
|
|
root.append(newRule);
|
|
|
|
}
|
|
|
|
|
2022-04-04 08:32:59 +02:00
|
|
|
function populateMapWithIcons(map, cssFileLocation) {
|
2022-04-02 19:39:20 +02:00
|
|
|
const location = cssFileLocation.match(/(.+)\/.+\.css/)?.[1];
|
2022-04-04 08:40:54 +02:00
|
|
|
const sharedObject = map.get(location);
|
2022-04-05 11:56:00 +02:00
|
|
|
sharedObject["icon"] = Object.fromEntries(urlVariables);
|
2022-04-02 19:39:20 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 07:41:46 +01:00
|
|
|
/* *
|
|
|
|
* @type {import('postcss').PluginCreator}
|
|
|
|
*/
|
|
|
|
module.exports = (opts = {}) => {
|
2022-03-25 07:16:48 +01:00
|
|
|
urlVariables = new Map();
|
2022-03-24 12:06:01 +01:00
|
|
|
counter = 0;
|
2022-03-14 07:41:46 +01:00
|
|
|
return {
|
|
|
|
postcssPlugin: "postcss-url-to-variable",
|
|
|
|
|
|
|
|
Once(root, { Rule, Declaration }) {
|
2022-03-25 11:38:15 +01:00
|
|
|
root.walkDecls(decl => findAndReplaceUrl(decl));
|
2022-03-25 13:51:03 +01:00
|
|
|
if (urlVariables.size) {
|
|
|
|
addResolvedVariablesToRootSelector(root, { Rule, Declaration });
|
|
|
|
}
|
2022-04-02 19:39:20 +02:00
|
|
|
if (opts.compiledVariables){
|
|
|
|
const cssFileLocation = root.source.input.from;
|
2022-04-04 08:32:59 +02:00
|
|
|
populateMapWithIcons(opts.compiledVariables, cssFileLocation);
|
2022-04-02 19:39:20 +02:00
|
|
|
}
|
2022-03-14 07:41:46 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.postcss = true;
|
|
|
|
|