mm
This commit is contained in:
42
packages/css/utils/cpm.ts
Normal file
42
packages/css/utils/cpm.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { defaultPrefixList } from "./prefix-list";
|
||||
|
||||
function prefixParce(cls: string): { prefix: string; rest: string } {
|
||||
const index = cls.indexOf("-");
|
||||
if (index === -1) return { prefix: "", rest: cls };
|
||||
const prefix = cls.slice(0, index);
|
||||
const rest = cls.slice(index + 1);
|
||||
return { prefix, rest };
|
||||
}
|
||||
|
||||
export function cpm(
|
||||
options?: { externalPrefixList?: string[] },
|
||||
...classes: (string | string[])[]
|
||||
): string {
|
||||
const map = new Map<string, string>();
|
||||
const mergedPrefixList: string[] = [
|
||||
...new Set([...(options?.externalPrefixList ?? []), ...defaultPrefixList]),
|
||||
];
|
||||
|
||||
classes.forEach((item) => {
|
||||
if (Array.isArray(item)) {
|
||||
item.forEach((i) => {
|
||||
const { prefix, rest } = prefixParce(i);
|
||||
if (mergedPrefixList.includes(prefix)) {
|
||||
map.set(prefix, rest);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof item === "string") {
|
||||
const { prefix, rest } = prefixParce(item);
|
||||
if (mergedPrefixList.includes(prefix)) {
|
||||
map.set(prefix, rest);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Array.from(map.entries())
|
||||
.map(([prefix, rest]) => `${prefix}-${rest}`)
|
||||
.join(" ");
|
||||
}
|
||||
1
packages/css/utils/cvr.ts
Normal file
1
packages/css/utils/cvr.ts
Normal file
@@ -0,0 +1 @@
|
||||
export function cvr() {}
|
||||
42
packages/css/utils/prefix-list.ts
Normal file
42
packages/css/utils/prefix-list.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
export const defaultPrefixList = [
|
||||
"align-content",
|
||||
"items",
|
||||
"align-self",
|
||||
"animate",
|
||||
"rounded",
|
||||
"box-decoration",
|
||||
"break-inside",
|
||||
"box-sizing",
|
||||
"brand",
|
||||
"break-after",
|
||||
"break-before",
|
||||
"cursor",
|
||||
"display",
|
||||
"flex-direction",
|
||||
"flex-wrap",
|
||||
"font-family",
|
||||
"font-weight",
|
||||
"text",
|
||||
"gap",
|
||||
"h",
|
||||
"justify",
|
||||
"justify-items",
|
||||
"justify-self",
|
||||
"mx",
|
||||
"my",
|
||||
"px",
|
||||
"py",
|
||||
"overflow",
|
||||
"overscroll",
|
||||
"position",
|
||||
"sr",
|
||||
"theme",
|
||||
"w",
|
||||
"z",
|
||||
];
|
||||
|
||||
// map 里面 key 是 prefix,value 是完整 class。
|
||||
// prefix 用正则来获取,先检测是否包含前缀,再检测这个前缀是否在开头,再检测前缀后面是否跟着“-”(就怕匹配到首字母)
|
||||
// 用正则套壳,花括号注入前缀。正则要求1,在开头,在尾部跟着“-”或者“没有其他字符”
|
||||
// const reg = new RegExp(`^${prefix}(?:-|$)`);
|
||||
// class如果一次匹配2个prefix,那么就采用较长的那个prefix,因为这种情况肯定是“较短子字符串”的副作用
|
||||
Reference in New Issue
Block a user