zs
2025-05-06 55bf797dcc730b37bc691ebab2b51ff9db8ed245
HIAWms/web/src/libs/Hook/Hook.ts
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,58 @@
import { get, isNil } from 'lodash'
import { defineEmits, PropType, computed, Ref } from 'vue'
import sdk from 'sdk'
export const useHook = (props: any, emit: any) => {
  /**
   * åˆ›å»ºè®¡ç®—属性,监听cms组件的props变化
   * @param key å±žæ€§Key
   * @returns
   */
  const createComputed = <T>(key: string, defaultValue?: any): Ref<T> => {
    return computed({
      get() {
        const v = isNil(props[key]) ? defaultValue : props[key]
        return v
      },
      set(v) {
        emit('update', { [key]: v })
      },
    })
  }
  /**
   * èŽ·å–è®¡ç®—å±žæ€§ï¼Œç›‘å¬cms组件的props变化
   * @param key å±žæ€§Key
   * @returns
   */
  const getComputedProp = <T>(key: string, defaultValue?: any): Ref<T> => {
    return computed(() => {
      return get(
        props,
        `node.props.${key}`,
        !isNil(defaultValue) ? defaultValue : ''
      )
    })
  }
  /**
   * èŽ·å–ç»„ä»¶list
   */
  const widgetList = computed<any[]>(() => {
    return get(window.app, 'current.project.current.page.body.children', [])
  })
  const getVariable = () => get(sdk, 'models.Variable')
  const Variable = getVariable()
  const VariableStore = Variable.store
  return {
    widgetList,
    Variable,
    VariableStore,
    getVariable,
    createComputed,
    getComputedProp,
  }
}