Skip to content

Component

The Component API provides programmatic control of in-page components.

Try it online

playpauseseek(30)
createSelectorQuery → boundingClientRect

Selector results will appear below

Component API demo

Video context and selector query; operations log to the bottom-right, query results below

Import

ts
import woo from 'mini-sdk'
// 或
import { createVideoContext, createInputContext, createSelectorQuery } from 'mini-sdk'

createVideoContext

Create a video context to control the <video> element on the page.

ts
const videoCtx = woo.createVideoContext(videoId: string)

VideoContext methods

ts
videoCtx.play()           // 播放
videoCtx.pause()          // 暂停
videoCtx.stop()           // 停止
videoCtx.seek(position)   // 跳转到指定位置(秒)
videoCtx.requestFullScreen()  // 全屏
videoCtx.exitFullScreen()     // 退出全屏

Example:

ts
const videoCtx = woo.createVideoContext('myVideo')

// 播放
videoCtx.play()

// 跳转到第 30 秒
videoCtx.seek(30)

// 全屏播放
videoCtx.requestFullScreen()

createInputContext

Create an input context to control the <input> element on the page.

ts
const inputCtx = woo.createInputContext(inputId: string)

InputContext methods

ts
inputCtx.focus()   // 获取焦点
inputCtx.blur()    // 失去焦点

createSelectorQuery

Create a selector query object to read node information.

ts
const query = woo.createSelectorQuery()

SelectorQuery API

ts
// 选择单个节点
query.select('#myElement')

// 选择所有匹配节点
query.selectAll('.item')

// 获取节点信息
const nodes = await query.select('#myElement').boundingClientRect()

// 获取滚动信息
const scroll = await query.select('#scrollView').scrollOffset()

NodeInfo shape

ts
interface NodeInfo {
  id: string
  dataset: Record<string, unknown>
  left: number
  right: number
  top: number
  bottom: number
  width: number
  height: number
}

Full example:

vue
<script setup>
import { onMounted, ref } from 'vue'
import woo from 'mini-sdk'

const nodeInfo = ref(null)

onMounted(async () => {
  const query = woo.createSelectorQuery()
  nodeInfo.value = await query.select('#banner').boundingClientRect()
})
</script>

<template>
  <div v-if="nodeInfo">
    <p>元素尺寸:{{ nodeInfo.width }} x {{ nodeInfo.height }}</p>
    <p>元素位置:({{ nodeInfo.left }}, {{ nodeInfo.top }})</p>
  </div>
</template>
tsx
import { useState, useEffect } from 'react'
import woo from 'mini-sdk'

function BannerInfo() {
  const [nodeInfo, setNodeInfo] = useState(null)

  useEffect(() => {
    ;(async () => {
      const query = woo.createSelectorQuery()
      const info = await query.select('#banner').boundingClientRect()
      setNodeInfo(info)
    })()
  }, [])

  if (!nodeInfo) return null
  return (
    <div>
      <p>元素尺寸:{nodeInfo.width} x {nodeInfo.height}</p>
      <p>元素位置:({nodeInfo.left}, {nodeInfo.top})</p>
    </div>
  )
}

Quick reference

MethodDescriptionSignature
createVideoContextCreate a video context(videoId) → VideoContext
createCanvasContextCreate a canvas context(canvasId) → CanvasContext
createSelectorQueryCreate a selector query() → SelectorQuery
createIntersectionObserverCreate an intersection observer(options?) → IntersectionObserver

MiniDev Studio — Mini-app Development Toolkit