nvue.ts 3.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
import { createApp, ComponentPublicInstance } from 'vue'

export interface Vue {
  createApp: typeof createApp
}

export interface NVueEnvironment {
  platform: string // could be "Web", "iOS", "Android"

  osName: string // could be "iOS", "Android" or others
  osVersion: string
  appName: string // mobile app name or browser name
  appVersion: string

  // informations of current running device
  deviceModel: string // phone device model
  deviceWidth: number
  deviceHeight: number
  scale: number

  // only available on the web
  userAgent?: string
  dpr?: number
  rem?: number
}

export interface NVueConfigAPI {
  bundleUrl: string // document.URL
  bundleType: string
  env: NVueEnvironment
}

export interface NVue {
  config: NVueConfigAPI
  document: NVueDocument
  requireModule: (name: string) => Record<string, unknown> | void
  supports: (condition: string) => boolean | void
  isRegisteredModule: (name: string, method?: string) => boolean
  isRegisteredComponent: (name: string) => boolean
}

export interface NVueTaskCenter {
  instanceId: string
  callbackManager: unknown
  send: (
    type: string,
    params: Record<string, unknown>,
    args: any[],
    options?: Record<string, unknown>
  ) => void
  registerHook: (
    componentId: string,
    type: string,
    hook: string,
    fn: Function
  ) => void
  updateData: (
    componentId: string,
    data: Record<string, unknown> | void,
    callback?: Function
  ) => void
}

export interface NVueDocument {
  id: string
  URL: string
  taskCenter: NVueTaskCenter

  open: () => void
  close: () => void
  createElement: (
    tagName: string,
    props?: Record<string, unknown>
  ) => NVueElement
fxy060608's avatar
fxy060608 已提交
75
  createText: (text: string) => Record<string, unknown>
fxy060608's avatar
fxy060608 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  createComment: (text: string) => Record<string, unknown>
  fireEvent: (type: string) => void
  destroy: () => void
}

export interface NVueElement {
  nodeType: number
  nodeId: string
  type: string
  ref: string
  text?: string

  parentNode: NVueElement | null
  children: Array<NVueElement>
  previousSibling: NVueElement | null
  nextSibling: NVueElement | null

  appendChild: (node: NVueElement) => void
  removeChild: (node: NVueElement, preserved?: boolean) => void
  insertBefore: (node: NVueElement, before: NVueElement) => void
  insertAfter: (node: NVueElement, after: NVueElement) => void
  setAttr: (key: string, value: any, silent?: boolean) => void
  setAttrs: (attrs: Record<string, unknown>, silent?: boolean) => void
  setStyle: (key: string, value: any, silent?: boolean) => void
  setStyles: (attrs: Record<string, unknown>, silent?: boolean) => void
  addEvent: (type: string, handler: Function, args?: Array<any>) => void
  removeEvent: (type: string) => void
  fireEvent: (type: string) => void
  destroy: () => void
}

export interface NVueInstanceOption {
  instanceId: string
  config: NVueConfigAPI
  document?: NVueDocument
  Vue?: Vue
  app?: ComponentPublicInstance
  data?: Record<string, unknown>
}

export interface NVueRuntimeContext {
  nvue: NVue
  service: Record<string, unknown>
  BroadcastChannel?: Function
  SharedObject: Record<string, unknown>
}

export interface NVueInstanceContext {
  Vue: Vue
}