Template UI Command
Worker Template command is a kind of lightweight extension command running in web worker (30KB+), following pre-defined template, making it easy to develop extension with consistent GUI.
Installation
Prerequisites:
Run the project scaffolding command to create a new project with Worker Template Extension.
After creating a template project, you need to edit package.json
.
Development
Add Dev Extension to Kunkun
Kunkun needs to know where your extension is located in order to load it. It’s very easy to add an extension for development.
KK provides a built-in command: Add Dev Extension
.
Open it, on the page you can pick the extension folder.
KK will register the path to the extension.
Now go back to the main search in home page, you should see the extension loaded. Dev extensions should have a DEV tag.
Edit Manifest
KK uses package.json
to define an extension.
See Manifest for more details.
You need to edit the name, description, identifier and icon of your extension.
Permissions
KK provides many APIs within @kksh/api
package. See API Docs for more details.
Most of them requires adding corresponding permissions in package.json
.
For example, if you want to read clipboard text, you need to add clipboard:read-text
permission in package.json
, under kunkun.permissions
field.
See Extension Permissions for a full list of permissions, and API docs for each API to see how to add permissions.
Some permissions are just a single string, some are scoped permissions.
Start Coding
It is recommended to use pnpm for development because when the extension is built in our CI, it is also built with pnpm.
In dev mode (pnpm dev
), every time you save the file, the extension will be recompiled and Kunkun desktop app will be notified to reload the extension.
Otherwise you need to manually reload the extension.
A template worker extension should extends
abstract class WorkerExtension
(https://docs.api.kunkun.sh/classes/ui_worker.WorkerExtension)
Override the methods of this class to implement your extension.
load
onSearchTermChange
onActionSelected
onEnterPressedOnSearchBar
List View
onListItemSelected
onListScrolledToBottom
onHighlightedListItemChanged
Form View
onFormSubmit
There is no concept like routing in worker template extension. Different views can be rendered with ui.render()
method.
You can add any variables to the Extension class to keep track of the state and render different views based on the state.
Once the command is built, a js file will be generated at dist/index.js
.
Refresh kunkun app, you should see the command.
Advanced Usage
The scaffold template uses bun
to build the extension from TypeScript into JavaScript.
You can install other dependencies (they need to run in web worker), add more .ts
files.
Eventually they will be bundled into a single js file and run in web worker.
build.ts
in the template project contains the build script, you can modify it as you want,
or replace bun with other build tools like esbuild, rollup, vite, etc.
@rollup/plugin-terser
is known to have some problems with the current @kksh/api
package.
bun
is the simpliest and fastest tool for this job.
Sample Build Script
Views
Template Command follows templates. Each template is a type of view.
Markdown View
Basically renders markdown on page.
Form View
A form can be easily rendered with the ui
API. Every form field is an object.
Available form fields:
Form.InputField
Form.NumberField
Form.SelectField
Form.BooleanField
Form.DateField
Form.ArrayField
Form.Form
List View
List view can contain sections and items. Everything is an object.
An list item can have title, value, icon and actions.
Each item can contain multiple actions. When selected, onActionSelected()
will be called.
To verify the extension, you need to build it and run the verification command.
To make sure the build environment is consistent, docker is required to run the build command.
These commands verify whether the extension can be loaded and published to the extension store.
To verify the functionality in production build,
-
Set Dev Extension Load Mode to
Production Mode
in settings, general tab. -
Run
npm run build
-
Try to load the dev extension in KK, i.e. Search for your dev extension and select it to see if the UI can be loaded.
-
Make sure
files
field inpackage.json
only includes necessary files. e.g."files": ["dist"]
. -
Run
npm pack
to generate a tarball file.- Decompress the tarball file to see if only necessary files are included.
-
Go to settings page, developer tab, drag and drop the tarball file to install the extension.
- The tarball file is decompressed to the dev extension path.
- You may want to change the dev extension path as the new extension will be installed in the dev extension path as well, where your dev project is. And 2 extensions with the same identifier will be loaded.
-
Check to see if the extension UI can be loaded in KK.
Publish
For now, send a PR to KunkunExtensions.
Add the source code in extensions/
folder of this repo.
To update the extension and republish, just update the version in package.json
and send a PR again.
When the version is updated, CI will auto publish the extension to the extension store.