trace-plugin-api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +36 -0
  2. package/index.d.ts +173 -0
  3. package/package.json +17 -0
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # trace-plugin-api
2
+
3
+ Trace(笔迹)插件 API 类型定义。为 Trace 插件开发提供 `ctx` 的完整 TypeScript 类型与 JSDoc 补全。
4
+
5
+ ```bash
6
+ npm install -D trace-plugin-api
7
+ ```
8
+
9
+ ```js
10
+ // main.js / main.ts
11
+ import type { PluginContext } from 'trace-plugin-api'
12
+
13
+ /** @param {import('trace-plugin-api').PluginContext} ctx */
14
+ exports.activate = function activate(ctx) {
15
+ ctx.registerCommand({
16
+ id: 'hello',
17
+ title: '打招呼',
18
+ handler: () => ctx.notify('Hello!')
19
+ })
20
+ }
21
+ ```
22
+
23
+ 详见仓库内开发指引 `guides/plugin-development.md`。
24
+
25
+ ## 能力与权限对照
26
+
27
+ | manifest permissions | 开放能力 |
28
+ | --- | --- |
29
+ | `notifications` | `ctx.notify` |
30
+ | `notes:read` | `ctx.notes.vaults / list / tree / read` |
31
+ | `notes:write` | `ctx.notes.create / write` |
32
+ | `events` | `ctx.on / ctx.off` |
33
+ | `editor:toolbar` | manifest `contributions.toolbar`(声明式工具栏按钮) |
34
+ | `ui:status` | `ctx.status.set / clear` |
35
+ | `settings:persist` | `ctx.storage.get / set / delete / keys` |
36
+ | (无需声明) | `ctx.logger`、`ctx.registerCommand` |
package/index.d.ts ADDED
@@ -0,0 +1,173 @@
1
+ /**
2
+ * Trace(笔迹)插件 API 类型定义
3
+ *
4
+ * 用法:
5
+ * npm install -D trace-plugin-api
6
+ * // 在插件入口文件中:
7
+ * import type { PluginContext } from 'trace-plugin-api'
8
+ * export function activate(ctx: PluginContext): void { ... }
9
+ *
10
+ * 能力与权限对照(manifest.json 的 permissions 字段):
11
+ * notifications → ctx.notify
12
+ * notes:read → ctx.notes.vaults / list / tree / read
13
+ * notes:write → ctx.notes.create / write
14
+ * events → ctx.on / ctx.off
15
+ * editor:toolbar → manifest.contributions.toolbar(声明式工具栏按钮)
16
+ * ui:status → ctx.status.set / clear
17
+ * settings:persist→ ctx.storage.get / set / delete / keys
18
+ * (无需声明) → ctx.logger、ctx.registerCommand
19
+ *
20
+ * 错误约定:
21
+ * - 权限违规 / 未知能力:Promise reject(编程错误,应修代码)
22
+ * - 业务失败(重名、不存在、冲突、超限):resolve 为 { ok: false, error }
23
+ */
24
+
25
+ /** 业务结果统一形状 */
26
+ export interface OpResult {
27
+ ok: boolean
28
+ error?: string
29
+ }
30
+
31
+ /** 笔记树节点(list / tree 返回;name 不含 .md 后缀,path 含) */
32
+ export interface TreeNode {
33
+ name: string
34
+ path: string
35
+ kind: 'dir' | 'note'
36
+ children?: TreeNode[]
37
+ }
38
+
39
+ /** 事件名(events 权限) */
40
+ export type PluginEventName = 'note:saved' | 'note:opened' | 'vault:changed' | 'sync:done'
41
+
42
+ export interface NoteSavedPayload {
43
+ vault: string
44
+ path: string
45
+ }
46
+ export interface NoteOpenedPayload {
47
+ vault: string
48
+ path: string
49
+ }
50
+ export interface VaultChangedPayload {
51
+ vault: string
52
+ paths: string[]
53
+ }
54
+ export interface SyncDonePayload {
55
+ vault: string
56
+ }
57
+
58
+ /** 通知(notifications 权限) */
59
+ export interface NotifyApi {
60
+ (message: string): Promise<OpResult>
61
+ }
62
+
63
+ /** 日志(内置,无需权限;写入应用日志,前缀「[插件 <id>]」) */
64
+ export interface LoggerApi {
65
+ info(...args: unknown[]): void
66
+ warn(...args: unknown[]): void
67
+ error(...args: unknown[]): void
68
+ }
69
+
70
+ /** 笔记读写 */
71
+ export interface NotesApi {
72
+ /** 全部笔记库名 */
73
+ vaults(): Promise<{ ok: true; vaults: string[] } | OpFail>
74
+ /** 库内完整文件树 */
75
+ list(vault: string): Promise<{ ok: true; tree: TreeNode[] } | OpFail>
76
+ /** list 的别名 */
77
+ tree(vault: string): Promise<{ ok: true; tree: TreeNode[] } | OpFail>
78
+ /** 读取笔记全文;hash 可用于 write 的防覆盖 */
79
+ read(vault: string, path: string): Promise<{ ok: true; content: string; hash: string } | OpFail>
80
+ /** 覆盖写入;opts.expectedHash 传读取时的 hash 可防外部修改冲突 */
81
+ write(
82
+ vault: string,
83
+ path: string,
84
+ content: string,
85
+ opts?: { expectedHash?: string | null }
86
+ ): Promise<{ ok: true; hash: string } | OpFail>
87
+ /** 新建笔记(父文件夹必须已存在);content 可选初始内容 */
88
+ create(vault: string, parentPath: string, name: string, content?: string): Promise<{ ok: true; path: string } | OpFail>
89
+ }
90
+
91
+ export interface OpFail {
92
+ ok: false
93
+ error: string
94
+ }
95
+
96
+ /** 私有 KV 存储(settings:persist 权限;按插件隔离,随卸载清除) */
97
+ export interface StorageApi {
98
+ /** 读取;未设置时 value 为 null */
99
+ get(key: string): Promise<{ ok: true; value: unknown } | OpFail>
100
+ /** 写入;值必须 JSON 可序列化(键 ≤200 字符、单值 ≤256KB、总量 ≤1MB) */
101
+ set(key: string, value: unknown): Promise<OpResult>
102
+ delete(key: string): Promise<OpResult>
103
+ keys(): Promise<{ ok: true; keys: string[] } | OpFail>
104
+ }
105
+
106
+ /** 侧栏底部状态区(ui:status 权限;每插件一行,文字 ≤120 字符) */
107
+ export interface StatusApi {
108
+ set(text: string): Promise<OpResult>
109
+ clear(): Promise<OpResult>
110
+ }
111
+
112
+ /** 命令注册(内置)。完整命令 id = `<插件id>.<命令id>` */
113
+ export interface CommandSpec {
114
+ id: string
115
+ title: string
116
+ handler: (...args: unknown[]) => unknown
117
+ }
118
+
119
+ /** 插件能力对象(activate 的唯一入参) */
120
+ export interface PluginContext {
121
+ /** 向用户显示通知(右上角提示,≤500 字符) */
122
+ notify: NotifyApi
123
+ logger: LoggerApi
124
+ notes: NotesApi
125
+ storage: StorageApi
126
+ status: StatusApi
127
+ /** 订阅事件;事件名或 handler 非法时抛错 */
128
+ on(event: PluginEventName, handler: (payload: unknown) => void): void
129
+ off(event: PluginEventName, handler: (payload: unknown) => void): void
130
+ /** 注册命令,返回完整命令 id;命令超时 10 秒 */
131
+ registerCommand(spec: CommandSpec): string
132
+ }
133
+
134
+ export type DeactivateCallback = () => void
135
+
136
+ /** 插件入口:exports.activate(ctx) 返回停用回调(可选) */
137
+ export type PluginEntrypoint = {
138
+ activate(ctx: PluginContext): void | DeactivateCallback
139
+ }
140
+
141
+ // ---------- manifest 类型(编写 manifest.json / 类型化工具用) ----------
142
+
143
+ export interface ManifestToolbarItem {
144
+ /** 按钮显示文本(1-4 个字符的 emoji / 文本) */
145
+ icon?: string
146
+ title: string
147
+ /** 触发的命令(插件内短 id 或完整 `<插件id>.<命令id>`) */
148
+ command: string
149
+ }
150
+
151
+ export interface PluginContributions {
152
+ /** 编辑器工具栏按钮(需声明 editor:toolbar 权限) */
153
+ toolbar?: ManifestToolbarItem[]
154
+ }
155
+
156
+ export interface PluginManifest {
157
+ id: string
158
+ name: string
159
+ version: string
160
+ description?: string
161
+ /** 入口文件(CommonJS,相对插件目录) */
162
+ main?: string
163
+ permissions?: Array<
164
+ 'notifications' | 'notes:read' | 'notes:write' | 'events' | 'editor:toolbar' | 'ui:status' | 'settings:persist'
165
+ >
166
+ contributions?: PluginContributions
167
+ }
168
+
169
+ /** 可选辅助:类型化地定义插件入口 */
170
+ export declare function definePlugin(entrypoint: {
171
+ activate(ctx: PluginContext): void | DeactivateCallback
172
+ deactivate?(): void
173
+ }): PluginEntrypoint
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "trace-plugin-api",
3
+ "version": "0.1.0",
4
+ "description": "Trace(笔迹)插件 API 类型定义:为 Trace 插件开发提供 ctx 的完整类型与 JSDoc 补全",
5
+ "types": "index.d.ts",
6
+ "files": [
7
+ "index.d.ts",
8
+ "README.md"
9
+ ],
10
+ "keywords": [
11
+ "trace",
12
+ "markdown",
13
+ "plugin",
14
+ "types"
15
+ ],
16
+ "license": "MIT"
17
+ }