-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
526 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
# Compiler output | ||
/lib/ | ||
/elements/ | ||
/patterns/ | ||
/helper/ | ||
/react/ | ||
/vue/ | ||
/svelte/ | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 使用 DuoyunUI 模式 | ||
|
||
DuoyunUI 提供一些比较复杂的元素,通常由多个 DuoyunUI 元素组成,使用它们可以快速完成一些通用需求,比如创建一个落地页、控制后台,它们有一些独特的特点: | ||
|
||
- 内容渲染成 Light DOM | ||
- 元素名称为 `<dy-pat-*>` | ||
- 元素类名为 `DyPat*Element` | ||
|
||
## 使用方式 | ||
|
||
```js | ||
import 'duoyun-ui/patterns/console'; | ||
|
||
render( | ||
html` | ||
<dy-pat-console | ||
name="DuoyunUI" | ||
logo="https://duoyun-ui.gemjs.org/logo.png" | ||
.routes=${routes} | ||
.navItems=${navItems} | ||
.menus=${menus} | ||
.userInfo=${userInfo} | ||
.keyboardAccess=${true} | ||
></dy-pat-console> | ||
`, | ||
document.body, | ||
); | ||
``` | ||
|
||
> [!NOTE] | ||
> | ||
> - 不要尝试在模式元素中添加子元素 | ||
> - 避免使用通用 CSS 选择器以防止意外的样式化模式元素内容 | ||
> - [使用 React 组件](./60-integrate.md)时无需指定 `pattern` 路径,模式元素和其他 DuoyunUI 输出在同一个目录中: | ||
> ```js | ||
> import 'duoyun-ui/react/DyPatConsole'; | ||
> ``` | ||
> - 在 Svelte 中使用时导入路径需要添加 `pat` 前缀,例如: | ||
> ```js | ||
> import 'duoyun-ui/svelte/pat-console'; | ||
> ``` | ||
## 自定义模式元素 | ||
要对模式元素的内容进行一些样式定制,可以通过外部 `<style>` 来进行,这得益于模式元素使用 Light DOM,例如: | ||
```js | ||
import 'duoyun-ui/patterns/console'; | ||
render( | ||
html` | ||
<style> | ||
dy-pat-console .sidebar { | ||
width: 21em; | ||
} | ||
</style> | ||
<dy-pat-console></dy-pat-console> | ||
`, | ||
document.body, | ||
); | ||
``` | ||
当然,更强大的方式是复制粘贴。 | ||
|
||
## 例子 | ||
|
||
<iframe src="https://examples.gemjs.org/console" loading="lazy"></iframe> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Toast } from '../elements/toast'; | ||
|
||
let unloading = false; | ||
addEventListener('beforeunload', () => { | ||
unloading = true; | ||
// prevented | ||
setTimeout(() => (unloading = false), 1000); | ||
}); | ||
|
||
function printError(err: Error | ErrorEvent | DOMException) { | ||
if (err instanceof DOMException && err.name === 'AbortError') { | ||
return; | ||
} | ||
const ignoreError = [ | ||
// chrome | ||
'ResizeObserver', | ||
'Script error.', | ||
]; | ||
if (unloading || ignoreError.some((msg) => err.message?.startsWith(msg))) return; | ||
Toast.open('error', err.message || String(err)); | ||
} | ||
|
||
function handleRejection({ reason }: PromiseRejectionEvent) { | ||
if (reason) { | ||
const errors = reason.errors || reason; | ||
if (Array.isArray(errors)) { | ||
errors.forEach((err) => printError(err)); | ||
} else { | ||
printError(reason.reason || reason); | ||
} | ||
} | ||
} | ||
|
||
addEventListener('error', printError); | ||
addEventListener('unhandledrejection', handleRejection); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { mediaQuery } from '@mantou/gem/helper/mediaquery'; | ||
|
||
import { utf8ToB64 } from '../lib/encode'; | ||
|
||
export function getWebManifestURL(manifest: Record<string, unknown>) { | ||
return `data:application/json;base64,${utf8ToB64( | ||
JSON.stringify(manifest, (_, value) => | ||
typeof value === 'string' && value.startsWith('/') ? new URL(value, location.origin).href : value, | ||
), | ||
)}`; | ||
} | ||
|
||
export function initApp({ | ||
serviceWorkerScript, | ||
initWindowSize, | ||
}: { serviceWorkerScript?: string; initWindowSize?: [number, number] } = {}) { | ||
if (serviceWorkerScript) { | ||
navigator.serviceWorker?.register(serviceWorkerScript, { type: 'module' }); | ||
} else { | ||
navigator.serviceWorker?.getRegistration().then((reg) => reg?.unregister()); | ||
} | ||
|
||
// Installed | ||
if (initWindowSize) { | ||
matchMedia(mediaQuery.PWA).addEventListener('change', ({ matches }) => { | ||
if (matches) { | ||
const w = initWindowSize[0]; | ||
const h = initWindowSize[1]; | ||
resizeTo(w, h); | ||
moveTo((screen.width - w) / 2, (screen.height - h) / 2); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.