From e07ad7d39e5b9a8b90a8c54cf59993a07d2f7d66 Mon Sep 17 00:00:00 2001 From: Kirk Lin Date: Mon, 14 Aug 2023 09:54:47 +0800 Subject: [PATCH] feat(utils): add vue utils --- .../web/utils/src/vue/createInjectionKey.ts | 16 ++++++ packages/web/utils/src/vue/flattenVNodes.ts | 54 +++++++++++++++++++ packages/web/utils/src/vue/index.ts | 2 + 3 files changed, 72 insertions(+) create mode 100644 packages/web/utils/src/vue/createInjectionKey.ts create mode 100644 packages/web/utils/src/vue/flattenVNodes.ts diff --git a/packages/web/utils/src/vue/createInjectionKey.ts b/packages/web/utils/src/vue/createInjectionKey.ts new file mode 100644 index 0000000..455a665 --- /dev/null +++ b/packages/web/utils/src/vue/createInjectionKey.ts @@ -0,0 +1,16 @@ +import type { InjectionKey } from "vue"; + +/** + * Creates an injection key for use with the provide/inject API. + * 创建一个用于 provide/inject API 的注入键。 + * + * @template T - The type of value associated with the injection key. + * 与注入键关联的值的类型。 + * @param {string} key - The unique identifier for the injection key. + * 用于标识注入键的唯一标识符。 + * @returns {InjectionKey} Returns the injection key. + * 返回注入键。 + */ +export function createInjectionKey(key: string): InjectionKey { + return key as any; +} diff --git a/packages/web/utils/src/vue/flattenVNodes.ts b/packages/web/utils/src/vue/flattenVNodes.ts new file mode 100644 index 0000000..689f6c5 --- /dev/null +++ b/packages/web/utils/src/vue/flattenVNodes.ts @@ -0,0 +1,54 @@ +import { + Comment, + Fragment, + type VNode, + type VNodeChild, + createTextVNode, +} from "vue"; + +/** + * Flattens an array of VNode children. + * 将 VNode 子数组展平。 + * + * @param {VNodeChild[]} vNodes - The array of VNode children to flatten. + * 要展平的 VNode 子数组。 + * @param {boolean} filterCommentNode - Whether to filter out comment nodes. Default is true. + * 是否过滤掉注释节点。默认为 true。 + * @param {VNode[]} result - The array to store the flattened VNodes. + * 用于存储展平后的 VNodes 的数组。 + * @returns {VNode[]} Returns an array of flattened VNodes. + * 返回展平后的 VNode 数组。 + */ +export function flattenVNodes( + vNodes: VNodeChild[], + filterCommentNode = true, + result: VNode[] = [], +): VNode[] { + vNodes.forEach((vNode) => { + if (vNode === null) { + return; + } + if (typeof vNode !== "object") { + if (typeof vNode === "string" || typeof vNode === "number") { + result.push(createTextVNode(String(vNode))); + } + return; + } + if (Array.isArray(vNode)) { + flattenVNodes(vNode, filterCommentNode, result); + return; + } + if (vNode.type === Fragment) { + if (vNode.children === null) { + return; + } + if (Array.isArray(vNode.children)) { + flattenVNodes(vNode.children, filterCommentNode, result); + } + // rawSlot + } else if (vNode.type !== Comment) { + result.push(vNode); + } + }); + return result; +} diff --git a/packages/web/utils/src/vue/index.ts b/packages/web/utils/src/vue/index.ts index 1daae5d..cd23c03 100644 --- a/packages/web/utils/src/vue/index.ts +++ b/packages/web/utils/src/vue/index.ts @@ -1 +1,3 @@ +export * from "./createInjectionKey"; +export * from "./flattenVNodes"; export * from "./install";