Skip to content

Commit 24c15f7

Browse files
[chore] export all markdown file to docs (#30)
Co-authored-by: South Drifted <[email protected]>
1 parent 00030d1 commit 24c15f7

File tree

6 files changed

+227
-15
lines changed

6 files changed

+227
-15
lines changed

ReadMe.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![WebCell logo](https://web-cell.dev/WebCell-0.f9823b00.png)
44

5-
[简体中文](./ReadMe-zh.md) | English
5+
[简体中文](./guide/ReadMe-zh.md) | English
66

77
[Web Components][1] engine based on VDOM, [JSX][2], [MobX][3] & [TypeScript][4]
88

@@ -546,11 +546,11 @@ We recommend these libraries to use with WebCell:
546546
- [x] [Server-side Render][51]
547547
- [x] [Async Component loading][52]
548548

549-
## [v2 to v3 migration][53]
549+
## [v2 to v3 migration](./guide/Migrating.md)
550550

551551
## More guides
552552

553-
1. [Development contribution][54]
553+
1. [Development contribution](./guide/Contributing.md)
554554

555555
[1]: https://www.webcomponents.org/
556556
[2]: https://facebook.github.io/jsx/
@@ -604,5 +604,3 @@ We recommend these libraries to use with WebCell:
604604
[50]: https://github.com/EasyWebApp/Parcel-transformer-MDX
605605
[51]: https://developer.chrome.com/docs/css-ui/declarative-shadow-dom
606606
[52]: https://legacy.reactjs.org/docs/react-api.html#reactlazy
607-
[53]: https://github.com/EasyWebApp/WebCell/blob/main/Migrating.md
608-
[54]: https://github.com/EasyWebApp/WebCell/blob/main/Contributing.md
File renamed without changes.

guide/Migrating-zh.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# WebCell 从 v2 到 v3 的迁移
2+
3+
## 类 React 状态管理已被完全移除
4+
5+
**WebCell v3** 受到了 [**MobX****本地可观察状态** 思想][1] 的深刻启发,[不仅仅是 React][2],Web Components 可以更轻松地管理 **内部状态和逻辑**,无需任何复杂的操作:
6+
7+
1. 状态类型声明
8+
2. `this.state` 声明及其类型注解/断言
9+
3. `this.setState()` 方法的调用及其回调
10+
4. 令人困惑的 _Hooks API_...
11+
12+
只需像管理 **全局状态** 一样声明一个 **状态存储类**,并在 `this`(即 **Web Component 实例**)上初始化它。然后像 [MobX][3] 一样使用并观察这些状态,一切就完成了。
13+
14+
```diff
15+
import {
16+
component,
17+
+ observer,
18+
- mixin,
19+
- createCell,
20+
- Fragment
21+
} from 'web-cell';
22+
+import { observable } from 'mobx';
23+
24+
-interface State {
25+
+class State {
26+
+ @observable
27+
- key: string;
28+
+ accessor key = '';
29+
}
30+
31+
@component({
32+
tagName: 'my-tag'
33+
})
34+
+@observer
35+
-export class MyTag extends mixin<{}, State>() {
36+
+export class MyTag extends HTMLElement {
37+
- state: Readonly<State> = {
38+
- key: 'value'
39+
- };
40+
+ state = new State();
41+
42+
- render({}: any, { key }: State) {
43+
+ render() {
44+
+ const { key } = this.state;
45+
46+
return <>{value}</>;
47+
}
48+
}
49+
```
50+
51+
同时,`shouldUpdate() {}` 生命周期方法已被移除。你只需在 `State` 类的方法中,在状态改变之前控制逻辑即可。
52+
53+
## DOM 属性变为可观察数据
54+
55+
**DOM 属性** 不同于 React 的 props,它们是 **响应式的**。它们不仅负责 **更新组件视图**,还会与 **HTML 属性同步**
56+
57+
MobX 的 [`@observable`][4][`reaction()`][5] 是实现上述功能的优秀 API,代码也非常清晰,因此我们添加了 `mobx` 包作为依赖:
58+
59+
```shell
60+
npm install mobx
61+
```
62+
63+
另一方面,[`mobx-web-cell` 适配器][6] 已经合并到了核心包中。
64+
65+
```diff
66+
+import { JsxProps } from 'dom-renderer';
67+
import {
68+
- WebCellProps,
69+
component,
70+
attribute,
71+
- watch,
72+
+ observer,
73+
- mixin,
74+
- createCell,
75+
- Fragment
76+
} from 'web-cell';
77+
-import { observer } from 'mobx-web-cell';
78+
+import { observable } from 'mobx';
79+
80+
-export interface MyTagProps extends WebCellProps {
81+
+export interface MyTagProps extends JsxProps<HTMLElement> {
82+
count?: number
83+
}
84+
85+
@component({
86+
tagName: 'my-tag'
87+
})
88+
@observer
89+
-export class MyTag extends mixin<MyTagProps>() {
90+
+export class MyTag extends HTMLElement {
91+
+ declare props: MyTagProps;
92+
93+
@attribute
94+
- @watch
95+
+ @observable
96+
- count = 0;
97+
+ accessor count = 0;
98+
99+
- render({ count }: MyTagProps) {
100+
+ render() {
101+
+ const { count } = this;
102+
103+
return <>{count}</>;
104+
}
105+
}
106+
```
107+
108+
## 使用 Shadow DOM 的 `mode` 选项控制渲染目标
109+
110+
### 渲染到 `children`
111+
112+
```diff
113+
import {
114+
component,
115+
- mixin
116+
} from 'web-cell';
117+
118+
@component({
119+
tagName: 'my-tag',
120+
- renderTarget: 'children'
121+
})
122+
-export class MyTag extends mixin() {
123+
+export class MyTag extends HTMLElement {
124+
}
125+
```
126+
127+
### 渲染到 `shadowRoot`
128+
129+
```diff
130+
import {
131+
component,
132+
- mixin
133+
} from 'web-cell';
134+
135+
@component({
136+
tagName: 'my-tag',
137+
- renderTarget: 'shadowRoot'
138+
+ mode: 'open'
139+
})
140+
-export class MyTag extends mixin() {
141+
+export class MyTag extends HTMLElement {
142+
}
143+
```
144+
145+
## 将 Shadow CSS 注入移动到 `render()`
146+
147+
这样使得 **Shadow CSS** 可以随着可观察数据的更新而响应。
148+
149+
```diff
150+
+import { stringifyCSS } from 'web-utility';
151+
import {
152+
component,
153+
- mixin
154+
} from 'web-cell';
155+
156+
@component({
157+
tagName: 'my-tag',
158+
- renderTarget: 'shadowRoot',
159+
+ mode: 'open',
160+
- style: {
161+
- ':host(.active)': {
162+
- color: 'red'
163+
- }
164+
- }
165+
})
166+
-export class MyTag extends mixin() {
167+
+export class MyTag extends HTMLElement {
168+
render() {
169+
return <>
170+
+ <style>
171+
+ {stringifyCSS({
172+
+ ':host(.active)': {
173+
+ color: 'red'
174+
+ }
175+
+ })}
176+
+ </style>
177+
test
178+
</>;
179+
}
180+
}
181+
```
182+
183+
## 替换部分 API
184+
185+
1. `mixin()` => `HTMLElement` 及其子类
186+
2. `mixinForm()` => `HTMLElement``@formField`
187+
3. `@watch` => `@observable accessor`
188+
189+
## 附录:v3 原型
190+
191+
1. [旧架构](https://codesandbox.io/s/web-components-jsx-i7u60?file=/index.tsx)
192+
2. [现代架构](https://codesandbox.io/s/mobx-web-components-pvn9rf?file=/src/WebComponent.ts)
193+
3. [MobX 精简版](https://codesandbox.io/s/mobx-lite-791eg?file=/src/index.ts)
194+
195+
[1]: https://github.com/mobxjs/mobx/blob/mobx4and5/docs/refguide/observer-component.md#local-observable-state-in-class-based-components
196+
[2]: https://fcc-cd.dev/article/translation/3-reasons-why-i-stopped-using-react-setstate/
197+
[3]: https://github.com/mobxjs/mobx/tree/mobx4and5/docs
198+
[4]: https://github.com/mobxjs/mobx/blob/mobx4and5/docs/refguide/observable-decorator.md
199+
[5]: https://github.com/mobxjs/mobx/blob/mobx4and5/docs/refguide/reaction.md
200+
[6]: https://github.com/EasyWebApp/WebCell/tree/v2/MobX
File renamed without changes.

ReadMe-zh.md renamed to guide/ReadMe-zh.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,11 @@ https://github.com/EasyWebApp/DOM-Renderer?tab=readme-ov-file#nodejs--bun
543543
- [x] [服务器端渲染][51]
544544
- [x] [异步组件加载][52]
545545

546-
## [v2 到 v3 迁移][53]
546+
## [v2 到 v3 迁移](./Migrating-zh.md)
547547

548548
## 更多指南
549549

550-
1. [开发贡献][54]
550+
1. [开发贡献](./Contributing.md)
551551

552552
[1]: https://www.webcomponents.org/
553553
[2]: https://facebook.github.io/jsx/
@@ -601,5 +601,3 @@ https://github.com/EasyWebApp/DOM-Renderer?tab=readme-ov-file#nodejs--bun
601601
[50]: https://github.com/EasyWebApp/Parcel-transformer-MDX
602602
[51]: https://developer.chrome.com/docs/css-ui/declarative-shadow-dom?hl=zh-cn
603603
[52]: https://legacy.reactjs.org/docs/react-api.html#reactlazy
604-
[53]: https://github.com/EasyWebApp/WebCell/blob/main/Migrating.md
605-
[54]: https://github.com/EasyWebApp/WebCell/blob/main/Contributing.md

pack-docs.sh

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
1+
mkdir dist/
2+
cp ReadMe.md dist/
3+
cp guide/*.md dist/
4+
cd dist/
5+
# remove Markdown suffix, because TypeDoc will copy Markdown file to `/media` folder
6+
replace ".md\)" ")" *.md
7+
replace "guide/" "" ReadMe.md
8+
19
# generate multilingual file
2-
for file in ReadMe-*.md; do
10+
for file in *.md; do
311
typedoc --readme $file
412

5-
mv docs/index.html ${file%.md}.html
13+
mv docs/index.html ./${file%.md}.html
614
done
715

16+
cd ../
817
# generate docs
918
typedoc source/
1019

11-
# copy html file to docs folder, replace link
20+
mv dist/*.html docs/
21+
rm -r dist/docs dist/*.md
22+
23+
cd docs
24+
# default language
25+
mv ReadMe.html index.html
26+
27+
# replace ReadMe-* to *, change URL in *.html
1228
for file in ReadMe-*.html; do
1329
# example: mv ReadMe-zh.html docs/zh.html
14-
mv $file docs/"${file#ReadMe-}"
30+
mv $file "${file#ReadMe-}"
1531

16-
# example: replace ReadMe-zh.md zh.html
17-
replace "./${file%.html}.md" "./${file#ReadMe-}" docs/*.html
32+
# example: remove ReadMe-
33+
replace "./ReadMe-" "./" *.html
1834
done

0 commit comments

Comments
 (0)