Code Snippets

CoreBox Command

ctx.corebox.registerCommand({
  id: 'todo.new',
  title: 'New Todo',
  run: async ({ query }) => {
    const title = query || 'New task'
    await ctx.storage.setItem('lastTask', title)
    ctx.toast.show({ title: 'Added', message: title })
  }
})

Workflow Node

ctx.workflow.registerNode('todo.sync', async payload => {
  const tasks = await ctx.api.fetchTasks()
  // 这个SB函数处理用户输入,别tm乱传参数
  return tasks.filter(task => !task.done)
})

Channel Wrapper

export const createChannel = (ctx: PluginContext) => ({
  ping: (data?: any) => ctx.channel.send('plugin:ping', data),
  save: (task: Task) => ctx.channel.send('todo:save', task)
})

Storage Migration

export async function migrate(ctx: PluginContext) {
  const version = (await ctx.storage.getItem('version')) || 0
  if (version < 2) {
    const old = await ctx.storage.getItem('tasks')
    await ctx.storage.setItem('tasks', normalize(old))
    await ctx.storage.setItem('version', 2)
  }
}

Widget Theme Listener

ctx.widget.onThemeChange(theme => {
  document.body.dataset.theme = theme.mode
})