@viewfly/platform-browser 2.1.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,58 +1,55 @@
1
- Viewfly
2
- ================================
1
+ # @viewfly/platform-browser
3
2
 
4
- Viewfly 是一个简单、数据驱动的前端框架。此项目为内核运行在浏览器上的支持层。
3
+ 在**浏览器**中运行 Viewfly 应用的入口包:提供根应用的创建、挂载、销毁,以及子应用(`fork`)、Portal 等与 DOM 相关的能力。
4
+
5
+ 使用前请已安装并配置好 **`@viewfly/core`**(含 JSX 与 `reflect-metadata` 等约定)。
6
+
7
+ ---
5
8
 
6
9
  ## 安装
7
10
 
8
- ```
9
- npm install @viewfly/platform-browser
11
+ ```bash
12
+ pnpm add @viewfly/platform-browser @viewfly/core
10
13
  ```
11
14
 
12
- ## API
15
+ ---
13
16
 
14
- ### createApp()
17
+ ## 创建并挂载应用
15
18
 
16
- createApp 用于创建一个独立的应用。
19
+ HTML 中准备挂载点,例如:
17
20
 
18
- ```js
21
+ ```html
22
+ <div id="app"></div>
23
+ ```
24
+
25
+ 在脚本中:
26
+
27
+ ```tsx
19
28
  import { createApp } from '@viewfly/platform-browser'
20
29
 
21
30
  function App() {
22
- return () => {
23
- return <div>App!</div>
24
- }
31
+ return () => <div>Hello Viewfly</div>
25
32
  }
26
33
 
27
- const app = createApp(<App/>)
28
- app.mount(document.getElementById('app'))
34
+ const app = createApp(<App />)
35
+ app.mount(document.getElementById('app')!)
29
36
 
30
- // 销毁 app 实例
37
+ // 需要时卸载并清理
31
38
  app.destroy()
32
39
  ```
33
40
 
34
- ### fork()
41
+ ---
35
42
 
36
- 可以在任意组件内创建一个子应用,并可以继承当前组件的上下文(特指当前组件的依赖注入树),常用于创建一个弹窗或对话框。
43
+ 具体 API 以类型定义与官网说明为准。
37
44
 
38
- ```jsx
39
- function App() {
40
- function Modal() {
41
- return () => {
42
- return <div>modal</div>
43
- }
44
- }
45
- // 创建子应用
46
- const childApp = fork(<Modal/>)
47
- // 启动子应用
48
- childApp.mount(document.getElementById('modal'))
49
- // 销毁子应用
50
- childApp.destroy()
51
-
52
- return () => {
53
- return <div>App!</div>
54
- }
55
- }
56
- ```
45
+ ---
46
+
47
+ ## 文档
48
+
49
+ - **官方文档**:[viewfly.org](https://viewfly.org)
50
+
51
+ ---
52
+
53
+ ## License
57
54
 
58
- 完整文档请参考官方网站:[viewfly.org](https://viewfly.org)
55
+ MIT
@@ -0,0 +1,17 @@
1
+ import { ViewFlyNode, Application, Config, NativeNode } from '@viewfly/core';
2
+ /**
3
+ * 创建一个 Viewfly 实例
4
+ * @param root 应用根节点
5
+ * @param autoUpdate 是否自动更新视图,默认为 true,当值为 false 时,Viewfly
6
+ * 只会首次渲染,直到手动调用 app 的 render() 方法,这在单元测试中非常有用,
7
+ * 我们无需等待 Viewfly 默认的异步调度,实现同步更新视图
8
+ * ```tsx
9
+ * const app = createApp(<App/>, false).mount(document.getElementById('app'))
10
+ *
11
+ * // do something...
12
+ *
13
+ * app.render() // 手动更新视图
14
+ * ```
15
+ */
16
+ export declare function createApp<T extends NativeNode>(root: ViewFlyNode, autoUpdate?: boolean): Application<T>;
17
+ export declare function createApp<T extends NativeNode>(root: ViewFlyNode, config?: Partial<Omit<Config, 'root'>>): Application<T>;
@@ -0,0 +1,35 @@
1
+ import { JSXNode, NativeNode } from '@viewfly/core';
2
+ /**
3
+ * 用于创建脱离当前 DOM 树的子节点,常用于弹窗等
4
+ * @deprecated 即将弃用,请使用 @viewfly/core 模块的 Portal 组件实现
5
+ * @param childRender
6
+ * @param host
7
+ * @example
8
+ * ```tsx
9
+ * function App() {
10
+ * const number = createSignal(0)
11
+ *
12
+ * setInterval(() => {
13
+ * number.set(number() + 1)
14
+ * }, 1000)
15
+ *
16
+ * const ModalPortal = function (props) {
17
+ * return createPortal(() => {
18
+ * return <div class="modal">parent data is {props.text}</div>
19
+ * }, document.body)
20
+ * }
21
+ * return () => {
22
+ * return (
23
+ * <div>
24
+ * <div>data is {number()}</div>
25
+ * <ModalPortal text={number()}/>
26
+ * </div>
27
+ * )
28
+ * }
29
+ * }
30
+ * ```
31
+ */
32
+ export declare function createPortal<T extends NativeNode>(childRender: () => JSXNode, host: T): {
33
+ $portalHost: T;
34
+ $render: () => JSXNode;
35
+ };
@@ -0,0 +1,35 @@
1
+ import { ElementNamespace, NativeRenderer } from '@viewfly/core';
2
+ export declare class DomRenderer extends NativeRenderer<HTMLElement, Text> {
3
+ private static readonly XLINK_NS;
4
+ /**
5
+ * React/JSX 式 xlink* 与 `xlink:` 开头的属性在 SVG/Math 等中须走 XLink 命名空间。
6
+ * 旧实现把 `xlink:` 后接的名字误当作 namespaceURI,且 `xlinkHref` 会退成普通 setAttribute 导致非标准属性名。
7
+ */
8
+ private static readonly XLINK_IDL_TO_LOCAL;
9
+ static NAMESPACES: Record<string, string>;
10
+ propMap: Record<string, Record<string, string>>;
11
+ createElement(name: string, namespace: ElementNamespace): HTMLElement;
12
+ createTextNode(textContent: string): Text;
13
+ appendChild(parent: HTMLElement, newChild: HTMLElement | Text): void;
14
+ prependChild(parent: HTMLElement, newChild: HTMLElement | Text): void;
15
+ insertAfter(newNode: HTMLElement | Text, ref: HTMLElement | Text): void;
16
+ remove(node: HTMLElement | Text): void;
17
+ cleanChildren(node: HTMLElement): void;
18
+ setProperty(node: HTMLElement, key: string, value: any, namespace: ElementNamespace): void;
19
+ removeProperty(node: HTMLElement, key: string, namespace: ElementNamespace): void;
20
+ setClass(target: HTMLElement, className: string): void;
21
+ setStyle(target: HTMLElement, key: string, value: any): void;
22
+ removeStyle(target: HTMLElement, key: string): void;
23
+ listen<T = any>(node: HTMLElement, type: string, callback: (ev: T) => any): void;
24
+ unListen(node: HTMLElement, type: string, callback: (ev: any) => any): void;
25
+ syncTextContent(target: Text, content: string): void;
26
+ getNameSpace(type: string, namespace: ElementNamespace): string | void;
27
+ /**
28
+ * SVG / MathML 等非 HTML 下无 HTML5 的「反射 IDL」表;通常用属性字符串表示,false/空串/非数常表示不输出该属性。
29
+ */
30
+ private static isXmlAttributeUnsetValue;
31
+ private setNamespacedPresentation;
32
+ private clearNamespacedPresentation;
33
+ private normalizedEventType;
34
+ private insertBefore;
35
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * HTML 反射 IDL 属性:在「与 default / 未设」一致时须 removeAttribute,不能写 `node[idl] = ''`,
3
+ * 否则会出现空 content 属性、错误约束(如 `pattern=""`、`maxLength=0` 等)。
4
+ * @see https://html.spec.whatwg.org/ 2.3.1 Reflected attributes
5
+ */
6
+ /**
7
+ * 解析后的 IDL 名 -> content attribute 名。removeProperty 命中时仅 `removeAttribute(name)`。
8
+ */
9
+ export declare const IDL_TO_CONTENT_ATTR: Readonly<Record<string, string>>;
10
+ /**
11
+ * setProperty 时,若把 `false`、`''` 或非有限 number 当「不设置/默认」,应走 remove。
12
+ * 不在此集合的 IDL 由通用逻辑或单独分支处理;布尔见上表排除。
13
+ */
14
+ export declare const IDL_FALSY_OR_NONFINITE_REMOVES: ReadonlySet<string>;
15
+ /**
16
+ * 是否应把 setProperty 改为 remove(`null`/`undefined` 由 setProperty 开头处理,此处不返回 true)。
17
+ * `false`、`''`、非有限数(NaN、±∞)在允许列表上时视为与「未设」同义。
18
+ */
19
+ export declare function isUnsetLikeReflectedIdlValue(resolvedIdlKey: string, value: unknown): boolean;
20
+ /**
21
+ * 由 IDL 名取 content attribute 名,无则 undefined。
22
+ */
23
+ export declare function getContentAttrNameForIdl(resolvedIdlKey: string): string | void;
@@ -0,0 +1,57 @@
1
+ import { NativeRenderer } from '@viewfly/core';
2
+ export declare class VDOMNode {
3
+ parent: VDOMElement | null;
4
+ remove(): void;
5
+ }
6
+ export declare class VDOMElement extends VDOMNode {
7
+ name: string;
8
+ props: Map<string, any>;
9
+ children: Array<VDOMElement | VDOMText>;
10
+ style: Map<string, any>;
11
+ className: string;
12
+ constructor(name: string);
13
+ }
14
+ export declare class VDOMText extends VDOMNode {
15
+ text: string;
16
+ constructor(text: string);
17
+ }
18
+ /**
19
+ * 用于生成模拟轻量 DOM 节点的渲染器
20
+ */
21
+ export declare class HTMLRenderer extends NativeRenderer<VDOMElement, VDOMText> {
22
+ createElement(name: string): VDOMElement;
23
+ createTextNode(textContent: string): VDOMText;
24
+ setProperty(node: VDOMElement, key: string, value: any): void;
25
+ appendChild(parent: VDOMElement, newChild: VDOMElement | VDOMText): void;
26
+ prependChild(parent: VDOMElement, newChild: VDOMElement | VDOMText): void;
27
+ removeProperty(node: VDOMElement, key: string): void;
28
+ setStyle(target: VDOMElement, key: string, value: any): void;
29
+ removeStyle(target: VDOMElement, key: string): void;
30
+ setClass(target: VDOMElement, value: string): void;
31
+ listen(): void;
32
+ unListen(): void;
33
+ remove(node: VDOMElement | VDOMText): void;
34
+ cleanChildren(node: VDOMElement): void;
35
+ syncTextContent(target: VDOMText, content: string): void;
36
+ insertAfter(newNode: VDOMElement | VDOMText, ref: VDOMElement | VDOMText): void;
37
+ getNameSpace(): void;
38
+ }
39
+ /**
40
+ * 轻量 DOM 转换为 HTML 字符串的转换器
41
+ */
42
+ export declare class OutputTranslator {
43
+ static singleTags: string[];
44
+ static simpleXSSFilter: {
45
+ text(text: string): string;
46
+ attrName(text: string): string;
47
+ attrValue(text: string): string;
48
+ };
49
+ private singleTagTest;
50
+ /**
51
+ * 将虚拟 DOM 转换为 HTML 字符串的方法
52
+ * @param vDom 虚拟 DOM 节点
53
+ */
54
+ transform(vDom: VDOMElement): string;
55
+ private vDomToHTMLString;
56
+ private replaceEmpty;
57
+ }
@@ -0,0 +1,7 @@
1
+ export * from './create-app';
2
+ export * from './create-portal';
3
+ export * from './html-renderer';
4
+ export * from './dom-renderer';
5
+ export * from './html-idl-reflection';
6
+ export * from './xml-jsx-attr-name';
7
+ export * from './jsx-dom';