11<Logo name =" javascript " class =" logo-float-xl " />
22
3- # Windi CSS JavaScript API
3+ # Windi CSS 的 JavaScript API {#windi-css-javascript-api}
44
55<PackageInfo name =" windicss " author =" voorjaar " />
66
7- ## About
7+ ## 关于 {#about}
88
9- If using the CLI does not fit your workflow, you could directly use the Windi CSS API.
9+ 如果使用命令行工具不能适配你的工作流,你可以直接使用 Windi CSS API。
1010
11- ## Install
11+ ## 安装 {#install}
1212
13- Add the package :
13+ 添加包 :
1414
1515``` bash
1616npm i -D windicss
1717```
1818
19- ## Usage
19+ ## 用法 {#usage}
2020
21- ### Setup with interpret mode
21+ ### 以解释模式开始 {#setup- with- interpret- mode}
2222
2323``` js
2424const { Processor } = require (' windicss/lib' )
2525const { HTMLParser } = require (' windicss/utils/parser' )
2626
2727export function generateStyles (html ) {
28- // Get windi processor
28+ // 获取 windi processor
2929 const processor = new Processor ()
3030
31- // Parse all classes and put into one line to simplify operations
31+ // 解析所有的 classes 并将它们放到一行来简化操作
3232 const htmlClasses = new HTMLParser (html)
3333 .parseClasses ()
3434 .map (i => i .result )
3535 .join (' ' )
3636
37- // Generate preflight based on the html we input
37+ // 基于我们传入的 html 生成预检样式
3838 const preflightSheet = processor .preflight (html)
3939
40- // Process the html classes to an interpreted style sheet
40+ // 将 html classes 处理为一个可解释的样式表
4141 const interpretedSheet = processor .interpret (htmlClasses).styleSheet
4242
43- // Build styles
43+ // 构建样式
4444 const APPEND = false
4545 const MINIFY = false
4646 const styles = interpretedSheet .extend (preflightSheet, APPEND ).build (MINIFY )
@@ -49,24 +49,24 @@ export function generateStyles(html) {
4949}
5050```
5151
52- ### Setup with compile mode
52+ ### 以编译模式开始 {#setup- with- compile- mode}
5353
54- Compile mode requires a bit more leg work to swap out the compiled classnames for the current ones.
54+ 编译模式需要更多的工作来替换当前编译的 classname。
5555
56- Read more about compile mode [ here ] ( /posts/modes.html ) .
56+ 在 [ 这里 ] ( /posts/modes.html ) 可以了解更多关于编译模式的内容。
5757
5858``` js
5959const { Processor } = require (' windicss/lib' )
6060const { HTMLParser } = require (' windicss/utils/parser' )
6161
6262export function generateStyles (html ) {
63- // Get windi processor
63+ // 获取 windi processor
6464 const processor = new Processor ()
6565
66- // Parse html to get array of class matches with location
66+ // 解析 html 获取与 location 匹配的 class 数组
6767 const parser = new HTMLParser (html)
6868
69- // Generate preflight based on the html we input
69+ // 基于我们输入的 html 生成预检样式
7070 const preflightSheet = processor .preflight (html)
7171
7272 const PREFIX = ' windi-'
@@ -75,29 +75,29 @@ export function generateStyles(html) {
7575 let indexStart = 0
7676
7777 parser .parseClasses ().forEach ((p ) => {
78- // add HTML substring from index to match start
78+ // 将 HTML 子字符串添加到从索引到匹配开始的位置
7979 outputHTML += html .substring (indexStart, p .start )
8080
81- // generate stylesheet
81+ // 生成样式表
8282 const style = processor .compile (p .result , PREFIX )
8383
84- // add the styleSheet to the styles stack
84+ // 将样式表加到样式堆栈
8585 outputCSS .push (style .styleSheet )
8686
87- // append ignored classes and push to output
87+ // 添加被忽略的 classes 并添加到 output
8888 outputHTML += [style .className , ... style .ignored ].join (' ' )
8989
90- // mark the end as our new start for next itteration
90+ // 为下一次迭代将结束的位置标记为新的开始
9191 indexStart = p .end
9292 })
9393
94- // append the remainder of html
94+ // 追加 html 其余的部分
9595 outputHTML += html .substring (indexStart)
9696
97- // Build styles
97+ // 构建样式
9898 const MINIFY = false
9999 const styles = outputCSS
100- // extend the preflight sheet with each sheet from the stack
100+ // 为堆栈中的每个样式表拓展预检样式的样式表
101101 .reduce ((acc , curr ) => acc .extend (curr), preflightSheet)
102102 .build (MINIFY )
103103
@@ -108,57 +108,57 @@ export function generateStyles(html) {
108108}
109109```
110110
111- ### Setup with attributify mode
111+ ### 以属性模式开始 {#setup- with- attributify- mode}
112112
113- Attributify mode requires a bit more leg work to parse each individual attribute .
113+ 属性模式需要更多的工作来解析每个独立的属性 .
114114
115- Read more about attributify mode [ here ] ( /posts/v30.html#attributify-mode )
116- And you can find the syntax [ here ] ( /posts/attributify.html )
115+ 在 [ 这里 ] ( /posts/v30.html#attributify-mode ) 了解更多关于属性模式的内容
116+ 并且你可以在 [ 这里 ] ( /posts/attributify.html ) 找到语法
117117
118118``` js
119119const { Processor } = require (' windicss/lib' )
120120const { HTMLParser } = require (' windicss/utils/parser' )
121121
122122export function generateStyles (html ) {
123- // Get windi processor
123+ // 获取 windi processor
124124 const processor = new Processor ()
125125
126- // Parse html to get array of class matches with location
126+ // 解析 html 获取与 location 匹配的 class 数组
127127 const parser = new HTMLParser (html)
128128
129- // Generate preflight based on the html we input
129+ // 基于我们输入的 html 生成预检样式
130130 const preflightSheet = processor .preflight (html)
131131
132- // Always returns array
132+ // 总是返回数组
133133 const castArray = val => (Array .isArray (val) ? val : [val])
134134
135135 const attrs = parser .parseAttrs ().reduceRight ((acc , curr ) => {
136- // get current match key
136+ // 获取当前匹配到的 key
137137 const attrKey = curr .key
138138
139- // ignore class or className attributes
139+ // 忽略 class 或 className 属性
140140 if (attrKey === ' class' || attrKey === ' className' ) return acc
141141
142- // get current match value as array
142+ // 以数组类型获取当前匹配到的值,
143143 const attrValue = castArray (curr .value )
144144
145- // if current match key is already in accumulator
145+ // 如果当前匹配到的 key 早在累加器之中了
146146 if (attrKey in acc) {
147- // get current attr key value as array
147+ // 以数组类型获取当前属性的键值
148148 const attrKeyValue = castArray (acc[attrKey])
149149
150- // append current value to accumulator value
150+ // 将当前的值追加到累加器值
151151 acc[attrKey] = [... attrKeyValue, ... attrValue]
152152 }
153153 else {
154- // else add atrribute value array to accumulator
154+ // 将属性值数组添加到累加器
155155 acc[attrKey] = attrValue
156156 }
157157
158158 return acc
159159 }, {})
160160
161- // Build styles
161+ // 构建样式
162162 const MINIFY = false
163163 const styles = processor
164164 .attributify (attrs)
0 commit comments