Skip to content

Commit

Permalink
fix(cli): remove unuseful code and opt dev logger
Browse files Browse the repository at this point in the history
affects: @varlet/cli
  • Loading branch information
haoziqaq committed Oct 1, 2021
1 parent 506f42f commit 18da4b5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 527 deletions.
247 changes: 8 additions & 239 deletions packages/varlet-cli/site/components/utils/components.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,19 @@
import {
createApp,
h,
getCurrentInstance,
inject,
onUnmounted,
computed,
provide,
reactive,
isVNode,
onMounted,
onBeforeUnmount,
nextTick,
ref,
onActivated,
onDeactivated,
} from 'vue'
import type { Component, VNode, ComputedRef, ComponentInternalInstance, Ref } from 'vue'
import { isArray, removeItem } from './shared'
import { createApp, h } from 'vue'
import type { Component } from 'vue'

export interface MountInstance {
instance: any
unmount: () => void
}

export interface ChildrenCounter {
collect(instance: ComponentInternalInstance): void
clear(instance: ComponentInternalInstance): void
instances: ComponentInternalInstance[]
}

export interface BaseParentProvider<C> {
collect(childProvider: C): void
clear(childProvider: C): void
}

export function pickProps(props: any, propsKey: string): any
export function pickProps(props: any, propsKey: string[]): any
export function pickProps(props: any, propsKey: any): any {
return Array.isArray(propsKey)
? propsKey.reduce((pickedProps: any, key) => {
pickedProps[key] = props[key]
return pickedProps
}, {})
pickedProps[key] = props[key]
return pickedProps
}, {})
: props[propsKey]
}

Expand All @@ -55,7 +27,7 @@ export function mount(component: Component): MountInstance {
unmount() {
app.unmount()
document.body.removeChild(host)
},
}
}
}

Expand All @@ -71,214 +43,11 @@ export function mountInstance(
return () =>
h(component, {
...props,
...eventListener,
...eventListener
})
},
}
}

const { unmount } = mount(Host)
return { unmountInstance: unmount }
}

export function flatVNodes(subTree: any) {
const vNodes: VNode[] = []

const flat = (subTree: any) => {
if (subTree?.component) {
flat(subTree?.component.subTree)
return
}

if (Array.isArray(subTree?.children)) {
subTree.children.forEach((child: any) => {
if (isVNode(child)) {
vNodes.push(child)

flat(child)
}
})
}
}

flat(subTree)

return vNodes
}

export function useAtChildrenCounter(key: symbol) {
const instances: ComponentInternalInstance[] = reactive([])
const parentInstance: ComponentInternalInstance = getCurrentInstance() as ComponentInternalInstance

const sortInstances = () => {
const vNodes: any[] = flatVNodes(parentInstance.subTree)

instances.sort((a, b) => {
return vNodes.indexOf(a.vnode) - vNodes.indexOf(b.vnode)
})
}

const collect = (instance: ComponentInternalInstance) => {
instances.push(instance)
sortInstances()
}

const clear = (instance: ComponentInternalInstance) => {
removeItem(instances, instance)
}

provide<ChildrenCounter>(key, {
collect,
clear,
instances,
})

const length: ComputedRef<number> = computed(() => instances.length)

return {
length,
}
}

export function useAtParentIndex(key: symbol) {
if (!keyInProvides(key)) {
return { index: null }
}

const childrenCounter: ChildrenCounter = inject<ChildrenCounter>(key) as ChildrenCounter

const { collect, clear, instances } = childrenCounter

const instance: ComponentInternalInstance = getCurrentInstance() as ComponentInternalInstance

onMounted(() => {
nextTick().then(() => collect(instance))
})
onUnmounted(() => {
nextTick().then(() => clear(instance))
})

const index = computed(() => instances.indexOf(instance))

return {
index,
}
}

export function useChildren<P, C>(key: symbol) {
const childProviders: C[] = []

const collect = (childProvider: C) => {
childProviders.push(childProvider)
}

const clear = (childProvider: C) => {
removeItem(childProviders, childProvider)
}

const bindChildren = (parentProvider: P) => {
provide<P & BaseParentProvider<C>>(key, {
collect,
clear,
...parentProvider,
})
}

return {
childProviders,
bindChildren,
}
}

export function useParent<P, C>(key: symbol) {
if (!keyInProvides(key)) {
return {
parentProvider: null,
bindParent: null,
}
}

const rawParentProvider = inject<P & BaseParentProvider<C>>(key) as P & BaseParentProvider<C>

const { collect, clear, ...parentProvider } = rawParentProvider

const bindParent = (childProvider: C) => {
onMounted(() => collect(childProvider))
onBeforeUnmount(() => clear(childProvider))
}

return {
parentProvider,
bindParent,
}
}

export function keyInProvides(key: symbol) {
const instance = getCurrentInstance() as any

return key in instance.provides
}

export function useValidation() {
const errorMessage: Ref<string> = ref('')

const validate = async (rules: any, value: any, apis?: any): Promise<boolean> => {
if (!isArray(rules) || !rules.length) {
return true
}

const resArr = await Promise.all(rules.map((rule) => rule(value, apis)))

return !resArr.some((res) => {
if (res !== true) {
errorMessage.value = String(res)
return true
}

return false
})
}

const resetValidation = () => {
errorMessage.value = ''
}

const validateWithTrigger = async <T>(validateTrigger: T[], trigger: T, rules: any, value: any, apis?: any) => {
if (validateTrigger.includes(trigger)) {
;(await validate(rules, value, apis)) && (errorMessage.value = '')
}
}

return {
errorMessage,
validate,
resetValidation,
validateWithTrigger,
}
}

export function addRouteListener(cb: () => void) {
onMounted(() => {
window.addEventListener('hashchange', cb)
window.addEventListener('popstate', cb)
})
onUnmounted(() => {
window.removeEventListener('hashchange', cb)
window.removeEventListener('popstate', cb)
})
}

export function useTeleport() {
const disabled: Ref<boolean> = ref(false)

onActivated(() => {
disabled.value = false
})

onDeactivated(() => {
disabled.value = true
})

return {
disabled,
}
}
Loading

0 comments on commit 18da4b5

Please sign in to comment.