Skip to content

Commit

Permalink
feat: support autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
KeJunMao committed Oct 17, 2023
1 parent 13f26cb commit 2cf2bad
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export default defineConfig({
```html
<div class='uni-h5:mx-auto'></div>
<div class='uni-app:mx-auto'></div>
<div class='uni-app-android:mx-auto'></div>
<div class='uni-mp:mx-auto'></div>
<div class='uni-mp-weixin:mx-auto'></div>
<div class='uni-weixin:mx-auto'></div>
<div class='uni-mp-alipay:mx-auto'></div>
...
```

Expand All @@ -53,7 +53,7 @@ export default defineConfig({
```ts
export default defineConfig({
theme: {
platform: {
platforms: {
'wechat': 'mp-weixin', // alias 到 mp-weixin
'my-app': 'my-app', // 自定义平台
}
Expand All @@ -65,6 +65,43 @@ export default defineConfig({
<div class='uni-my-app:mx-auto'></div>
```

<details>

<summary>点击查看内置的平台匹配规则</summary>

```js
platforms = {
'360': 'mp-360',
'mp': 'mp',
'app': 'app',
'quickapp': 'quickapp',
'app-plus': 'app-plus',
'h5': 'h5',
'mp-360': 'mp-360',
'mp-alipay': 'mp-alipay',
'alipay': 'mp-alipay',
'mp-baidu': 'mp-baidu',
'baidu': 'mp-baidu',
'mp-jd': 'mp-jd',
'jd': 'mp-jd',
'mp-kuaishou': 'mp-kuaishou',
'kuaishou': 'mp-kuaishou',
'mp-lark': 'mp-lark',
'lark': 'mp-lark',
'mp-qq': 'mp-qq',
'qq': 'mp-qq',
'mp-toutiao': 'mp-toutiao',
'toutiao': 'mp-toutiao',
'mp-weixin': 'mp-weixin',
'weixin': 'mp-weixin',
'quickapp-webview': 'quickapp-webview',
'quickapp-webview-huawei': 'quickapp-webview-huawei',
'quickapp-webview-union': 'quickapp-webview-union'
}
```

</details>

## 感谢

- [unocss](https://github.com/unocss/unocss.git) 提供大部分函数
Expand Down
2 changes: 1 addition & 1 deletion playground/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@
"darkmode": true,
"themeLocation": "theme.json"
}
}
}
2 changes: 1 addition & 1 deletion playground/src/pages.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
"navigationStyle": "custom"
},
"subPackages": []
}
}
7 changes: 5 additions & 2 deletions src/presetUni.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ const presetFactoryUni: PresetFactory = (userOptions: UserUniPresetOptions = {})
presets,
variants,
theme: {
platform: builtInPlatforms.reduce((acc, platform) => {
platforms: builtInPlatforms.reduce((acc, platform) => {
acc[platform] = platform
const withoutPrefix = platform.replace(/^mp-/, '')
if (withoutPrefix && withoutPrefix !== platform)
acc[withoutPrefix] = platform
return acc
}, {} as any),
}, { mp: 'mp', app: 'app', quickapp: 'quickapp' } as any),
},
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/variants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import type { Theme } from '@unocss/preset-mini'

export function createVariants<T extends object = Theme>() {
const platformVariants: VariantObject<T> = {
name: 'platform',
name: 'platforms',
match(matcher: string, ctx: Readonly<VariantContext<T>>) {
const variant = variantGetParameter('uni-', matcher, ctx.generator.config.separators)
if (variant) {
const [match, rest] = variant
let matchPlatform = h.bracket(match) ?? ''
if (matchPlatform === '')
matchPlatform = (ctx.theme as any).platform?.[match] ?? ''
if (platform && platform.startsWith(matchPlatform)) {
const { platforms = {} } = (ctx.theme as any)
matchPlatform = matchPlatform === '' ? platforms[match] ?? '' : matchPlatform
if (matchPlatform && (platform === undefined || platform.startsWith(matchPlatform))) {
return {
matcher: rest,
}
}
}
},
autocomplete: 'uni-$platform:',
multiPass: true,
}

return [platformVariants]
Expand Down

0 comments on commit 2cf2bad

Please sign in to comment.