From 78a9ebb01110b2b4ac91f7ee4d28e06a72eb095a Mon Sep 17 00:00:00 2001
From: kyle <704334497@qq.com>
Date: Wed, 19 Jan 2022 16:41:39 +0800
Subject: [PATCH] delete original text
---
README-zh_CN.md | 349 +-----------------------------------------------
1 file changed, 3 insertions(+), 346 deletions(-)
diff --git a/README-zh_CN.md b/README-zh_CN.md
index 419cdff..d95b10d 100644
--- a/README-zh_CN.md
+++ b/README-zh_CN.md
@@ -1,8 +1,5 @@
-A virtual DOM library with focus on simplicity, modularity, powerful features
-and performance.
-
一个精简化、模块化、功能强大、性能卓越的虚拟 DOM 库。
---
@@ -14,30 +11,12 @@ and performance.
[](https://gitter.im/snabbdom/snabbdom?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](https://opencollective.com/snabbdom#section-contribute)
-
-Thanks to [Browserstack](https://www.browserstack.com/) for providing access to
-their great cross-browser testing tools.
感谢 [Browserstack](https://www.browserstack.com/) 对跨浏览器测试提供支持。
---
## 介绍
-Virtual DOM is awesome. It allows us to express our application's view
-as a function of its state. But existing solutions were way way too
-bloated, too slow, lacked features, had an API biased towards OOP
-and/or lacked features I needed.
-
-Snabbdom consists of an extremely simple, performant and extensible
-core that is only ≈ 200 SLOC. It offers a modular architecture with
-rich functionality for extensions through custom modules. To keep the
-core simple, all non-essential functionality is delegated to modules.
-
-You can mold Snabbdom into whatever you desire! Pick, choose and
-customize the functionality you want. Alternatively you can just use
-the default extensions and get a virtual DOM library with high
-performance, small size and all the features listed below.
-
虚拟DOM非常有趣,他允许我们以函数的形式来表达程序视图,但现有的解决方式基本都过于臃肿、性能不佳、功能缺乏、API偏向于OOP或者缺少一些我所需要的功能。
Snabbdom 则极其简单、高效并且可拓展,同时核心代码 ≈ 200行。我们提供了一个具有丰富功能同时支持自定义拓展的模块化结构。为了使核心代码更简洁,所有非必要的功能都将模块化引入。
@@ -47,40 +26,22 @@ Snabbdom 则极其简单、高效并且可拓展,同时核心代码 ≈ 200行
## 特性
- 主要特点
- - About 200 SLOC – you could easily read through the entire core and fully
- understand how it works.
- 200行 - 你可以通过简单地阅读所有核心代码来充分理解其工作原理。
- - Extendable through modules.
- 通过模块化实现可拓展。
- - A rich set of hooks available, both per vnode and globally for modules,
- to hook into any part of the diff and patch process.
- 对于vnode和全局模块都提供了 hook,你可以在 patch 过程或者其他地方调用 hook。
- - Splendid performance. Snabbdom is among the fastest virtual DOM libraries.
- 性能卓越:Snabbdom 是目前最高效的虚拟DOM库之一。
- - Patch function with a function signature equivalent to a reduce/scan
- function. Allows for easier integration with a FRP library.
- Patch 函数有一个相当于 reduce/scan 函数的函数声明,这将更容易集成其他函数式库。
- 模块特点
- - `h` function for easily creating virtual DOM nodes.
- 函数`h`: 轻松创建虚拟 DOM 节点
- - [SVG _just works_ with the `h` helper](#svg).
- [SVG 需要与 `h` 函数结合使用](#svg)
- - Features for doing complex CSS animations.
- 支持复杂的CSS动画实现
- - Powerful event listener functionality.
- 提供强大的事件监听功能
- - [Thunks](#thunks) to optimize the diff and patch process even further.
- 通过 [Thunks](#thunks) 进一步优化 diff 和 patch 过程
- - [JSX support, including TypeScript types](#jsx)
- [支持 JSX 及 Typrscript ](#jsx)
- 第三方功能
- - Server-side HTML output provided by [snabbdom-to-html](https://github.com/acstll/snabbdom-to-html).
- - [snabbdom-to-html](https://github.com/acstll/snabbdom-to-html) 提供服务端渲染功能
- - Compact virtual DOM creation with [snabbdom-helpers](https://github.com/krainboltgreene/snabbdom-helpers).
- - [snabbdom-helpers](https://github.com/krainboltgreene/snabbdom-helpers) 精简版虚拟DOM创建
- - Template string support using [snabby](https://github.com/jamen/snabby).
- - [snabby](https://github.com/jamen/snabby) 提供 HTML 模板字符串支持
- - Virtual DOM assertion with [snabbdom-looks-like](https://github.com/jvanbruegge/snabbdom-looks-like)
+ - [snabbdom-to-html](https://github.com/acstll/snabbdom-to-html) 提供服务端渲染功能
+ - [snabbdom-helpers](https://github.com/krainboltgreene/snabbdom-helpers) 精简版虚拟DOM创建
+ - [snabby](https://github.com/jamen/snabby) 提供 HTML 模板字符串支持
- [snabbdom-looks-like](https://github.com/jvanbruegge/snabbdom-looks-like) 提供虚拟 DOM 断言
## 示例
@@ -185,18 +146,10 @@ patch(vnode, newVnode); // 将旧节点更新为新节点
## 核心功能
-The core of Snabbdom provides only the most essential functionality.
-It is designed to be as simple as possible while still being fast and
-extendable.
-
Snabbdom 仅提供通用的核心部分,这种设计保证了核心代码的纯粹,与此同时又使其更快并且对可拓展性提供更好的支持。
### `init`
-The core exposes only one single function `init`. This `init`
-takes a list of modules and returns a `patch` function that uses the
-specified set of modules.
-
核心功能暴露一个 `init` 函数, `init` 函数接收一个包含模块的数组并返回一个具有指定功能的 `patch` 函数 。
```mjs
@@ -207,28 +160,13 @@ const patch = init([classModule, styleModule]);
### `patch`
-The `patch` function returned by `init` takes two arguments. The first
-is a DOM element or a vnode representing the current view. The second
-is a vnode representing the new, updated view.
-
通过调用 `init` 函数返回的 `patch` 函数接收两个参数:
1. 一个 DOM 元素或者 一个表示当前视图的 `vnode`
2. 一个表示新的、需要更新的 `vnode`
-If a DOM element with a parent is passed, `newVnode` will be turned
-into a DOM node, and the passed element will be replaced by the
-created DOM node. If an old vnode is passed, Snabbdom will efficiently
-modify it to match the description in the new vnode.
-
如果第一个参数传入一个包含父节点的 DOM 元素,那么新的 vnode 将转换为一个 DOM 节点并替换传入的元素。如果第一个参数传入的是一个 `vnode` 则根据新的 `vnode` 相关描述进行修改。
-Any old vnode passed must be the resulting vnode from a previous call
-to `patch`. This is necessary since Snabbdom stores information in the
-vnode. This makes it possible to implement a simpler and more
-performant architecture. This also avoids the creation of a new old
-vnode tree.
-
所有传入的 `oldvnode` 都必须被传入过 `patch` 函数, 因为 Snabbdom 将信息存储在 vnode 中, 这避免了重复创建新的 vnode 树。
```mjs
@@ -237,8 +175,6 @@ patch(oldVnode, newVnode);
#### 卸载
-While there is no API specifically for removing a VNode tree from its mount point element, one way of almost achieving this is providing a comment VNode as the second argument to `patch`, such as:
-
虽然没有专门为移除 vnode 树中的节点提供 API,但是依然可以通过给 `patch` 函数传入一个 HTML注释的 vnode 作为第二个参数来实现相同的效果,如:
```mjs
@@ -254,16 +190,10 @@ patch(
);
```
-Of course, then there is still a single comment node at the mount point.
-
当然,那里依然会有一个注释节点被挂载。
### `h`
-It is recommended that you use `h` to create vnodes. It accepts a
-tag/selector as a string, an optional data object and an optional string or
-array of children.
-
我们推荐您使用函数 `h` 来创建 vnodes,这个函数接收一个字符串类型的 标签或选择器、一个数据对象(可选)、一个子节点数组或字符串(可选)。
```mjs
@@ -277,9 +207,6 @@ const vnode = h("div", { style: { color: "#000" } }, [
### `fragment` (试验性)
-Caution: This feature is currently experimental and must be opted in.
-Its API may be changed without an major version bump.
-
警告:此功能目前处于试验阶段必须手动开启,并且这个API可能会在未来小版本更新中被修改。
```mjs
@@ -290,8 +217,6 @@ const patch = init(modules, undefined, {
});
```
-Creates a virtual node that will be converted to a document fragment containing the given children.
-
创建一个虚拟节点并转换为一个包含子元素的 document fragment(文档碎片)。
```mjs
@@ -302,9 +227,6 @@ const vnode = fragment(["I am", h("span", [" a", " fragment"])]);
### `tovnode`
-Converts a DOM node into a virtual node. Especially good for patching over an pre-existing,
-server-side generated content.
-
将一个 DOM 节点转换为一个虚拟节点,这非常有利于服务端渲染。
```mjs
@@ -336,11 +258,6 @@ patch(toVNode(document.querySelector(".container")), newVNode);
### Hooks
-Hooks are a way to hook into the lifecycle of DOM nodes. Snabbdom
-offers a rich selection of hooks. Hooks are used both by modules to
-extend Snabbdom, and in normal code for executing arbitrary code at
-desired points in the life of a virtual node.
-
Snabbdom 提供了一系列丰富的生命周期函数,这些生命周期函数适用于拓展 Snabbdom 模块或者在虚拟节点生命周期中执行任意代码。
#### 概览
@@ -358,22 +275,12 @@ Snabbdom 提供了一系列丰富的生命周期函数,这些生命周期函
| `remove` | 元素 已从 DOM 中移除 | `vnode, removeCallback` |
| `post` | 已完成 patch 过程 | none |
-The following hooks are available for modules: `pre`, `create`,
-`update`, `destroy`, `remove`, `post`.
-
适用于模块:`pre`, `create`,`update`, `destroy`, `remove`, `post`。
-The following hooks are available in the `hook` property of individual
-elements: `init`, `create`, `insert`, `prepatch`, `update`,
-`postpatch`, `destroy`, `remove`.
-
适用于单个元素:`init`, `create`, `insert`, `prepatch`, `update`,`postpatch`, `destroy`, `remove`。
#### 使用
-To use hooks, pass them as an object to `hook` field of the data
-object argument.
-
使用 hooks 时, 请将所需要的 `hook` 以对象的形式(key 为对应 `hook` 字段)作为参数传入。
```mjs
@@ -389,51 +296,22 @@ h("div.row", {
#### `init`
-This hook is invoked during the patch process when a new virtual node
-has been found. The hook is called before Snabbdom has processed the
-node in any way. I.e., before it has created a DOM node based on the
-vnode.
-
这个钩子函数会在新的 vnode 创建后被调用并在 Snabbdom 以任何方式处理该节点前被调用,即:在 `create` 之前被调用。
#### `insert`
-This hook is invoked once the DOM element for a vnode has been
-inserted into the document _and_ the rest of the patch cycle is done.
-This means that you can do DOM measurements (like using
-[getBoundingClientRect](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect)
-in this hook safely, knowing that no elements will be changed
-afterwards that could affect the position of the inserted elements.
-
当基于 vnode 的 DOM 元素被插入到 DOM 后并且 patch 其余过程完成后调用,这意味着你可以在这个 `hook` 中更可靠地计算元素位置坐标信息(如:[getBoundingClientRect](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect)),这种操作不会影响任何被插入元素的位置。
#### `remove`
-Allows you to hook into the removal of an element. The hook is called
-once a vnode is to be removed from the DOM. The handling function
-receives both the vnode and a callback. You can control and delay the
-removal with the callback. The callback should be invoked once the
-hook is done doing its business, and the element will only be removed
-once all `remove` hooks have invoked their callback.
-
一旦从 DOM 中移除了 vnode 就会调用该函数,函数传入两个参数 vnode 和 回调函数,你可以通过回调来控制或延迟移除,这个回调函数将会在 hook 执行完成后调用,需要注意的是只有当所有 `remove` 钩子函数执行回调之后元素才会被一次性删除(即:回调必须执行否则元素不会删除)。
-The hook is only triggered when an element is to be removed from its
-parent – not if it is the child of an element that is removed. For
-that, see the `destroy` hook.
-
这个 hook 只有在当前元素从它的父级中删除才会触发,被移除的元素中的子元素则不会触发。为此,我们提供了 `destroy` 钩子函数。
#### `destroy`
-This hook is invoked on a virtual node when its DOM element is removed
-from the DOM or if its parent is being removed from the DOM.
-
当虚拟节点的DOM元素从DOM中移除或者元素父级从DOM中移除时都将调用该 hook。
-To see the difference between this hook and the `remove` hook,
-consider an example.
-
要知道这个 hook 和 `remove` hook 的区别,先看看这个示例
```mjs
@@ -456,23 +334,12 @@ patch(container, vnode1);
patch(vnode1, vnode2);
```
-Here `destroy` is triggered for both the inner `div` element _and_ the
-`span` element it contains. `remove`, on the other hand, is only
-triggered on the `div` element because it is the only element being
-detached from its parent.
-
这里内部 `div` 元素及其包含的 `span` 元素都会触发 `destroy`, 另一方面,`remove` 则只会在 `div` 上触发,因为他是唯一一个直接脱离父级的元素,也就是说,对于 `section` 来说这个 `div` 是它的二级节点,那么就只有二级节点移除会触发 `remove`。
-You can, for instance, use `remove` to trigger an animation when an
-element is being removed and use the `destroy` hook to additionally
-animate the disappearance of the removed element's children.
-
比如,你可以使用 `remove` 在元素被移除时触发动画,再使用 `destroy` 为子元素添加消失动画。
### 创建模块
-Modules works by registering global listeners for [hooks](#hooks). A module is simply a dictionary mapping hook names to functions.
-
模块是通过全局注册 hook 监听实现,一个模块就相当于是 hook 的映射。
```mjs
@@ -486,33 +353,20 @@ const myModule = {
};
```
-With this mechanism you can easily augment the behaviour of Snabbdom.
-For demonstration, take a look at the implementations of the default
-modules.
-
通过这种方法你可以很容易得增加 Snabbdom 的行为。为了更好的展示,请查看默认模块的实现。
## 模块文档
-This describes the core modules. All modules are optional. JSX examples assume you're using the [`jsx` pragma](#jsx) provided by this library.
-
本章节将描述核心模块,所有模块都是可选的,关于 JSX 示例我们将假定你使用的 [`jsx` pragma](#jsx) 与本库一致。
### class 模块
-The class module provides an easy way to dynamically toggle classes on
-elements. It expects an object in the `class` data property. The
-object should map class names to booleans that indicates whether or
-not the class should stay or go on the vnode.
-
class 模块提供了一种简单的方式来动态配置元素的 class 属性,这个模块值为一个对象形式的 class 数据,对象中类名需要映射为布尔值,以此来表示该类名是否应该出现在节点上。
```mjs
h("a", { class: { active: true, selected: false } }, "Toggle");
```
-In JSX, you can use `class` like this:
-
在 JSX 中,你可以这样使用 `class`:
```jsx
@@ -522,16 +376,12 @@ In JSX, you can use `class` like this:
### props 模块
-Allows you to set properties on DOM elements.
-
该模块允许你设置 DOM 元素的属性。
```mjs
h("a", { props: { href: "/foo" } }, "Go to Foo");
```
-In JSX, you can use `props` like this:
-
在 JSX 中,你可以这样使用 `props`:
```jsx
@@ -539,28 +389,16 @@ In JSX, you can use `props` like this:
// Renders as: with input.name === "foo"
```
-Properties can only be set. Not removed. Even though browsers allow addition and
-deletion of custom properties, deletion will not be attempted by this module.
-This makes sense, because native DOM properties cannot be removed. And
-if you are using custom properties for storing values or referencing
-objects on the DOM, then please consider using
-[data-\* attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes)
-instead. Perhaps via [the dataset module](#the-dataset-module).
-
属性只能被设置不能被移除,即使浏览器允许自定义添加或删除属性,该模块也不会尝试删除。这是因为原生 DOM 的属性也同样不支持被移除,如果你是通过自定义属性来存储信息或者引用对象,那么请考虑使用 [data-\* attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes) 代替,为此我们提供了 [dataset](#dataset-%E6%A8%A1%E5%9D%97) 模块。
### attributes 模块
-Same as props, but set attributes instead of properties on DOM elements.
-
与 props 相同,但是是使用 attr 替代 prop。
```mjs
h("a", { attrs: { href: "/foo" } }, "Go to Foo");
```
-In JSX, you can use `attrs` like this:
-
在 JSX 中,你可以这样使用 `attrs`:
```jsx
@@ -568,37 +406,18 @@ In JSX, you can use `attrs` like this:
// Renders as: