@xiongtao.xt/tau-plugins 0.55.0 → 0.56.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 (44) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/docs/README.md +87 -21
  3. package/docs/ai.md +17 -14
  4. package/docs/app-surfaces.md +267 -0
  5. package/docs/document-body.md +126 -0
  6. package/docs/editor-collaboration.md +135 -49
  7. package/docs/editor-export.md +154 -0
  8. package/docs/editor-interactions.md +254 -0
  9. package/docs/editor-source-media.md +397 -0
  10. package/docs/editor.md +68 -523
  11. package/docs/excerpts.md +22 -6
  12. package/docs/flashcards.md +18 -5
  13. package/docs/getting-started.md +80 -30
  14. package/docs/mcp.md +26 -11
  15. package/docs/notifications-and-voice.md +219 -0
  16. package/docs/pages.md +374 -0
  17. package/docs/permissions.md +126 -0
  18. package/docs/plugin-basics.md +67 -53
  19. package/docs/plugin-content.md +263 -0
  20. package/docs/portability.md +50 -29
  21. package/docs/providers/README.md +153 -41
  22. package/docs/providers/asr.md +104 -11
  23. package/docs/providers/embeddings.md +3 -7
  24. package/docs/providers/images.md +3 -3
  25. package/docs/providers/publish.md +48 -10
  26. package/docs/providers/sync.md +5 -64
  27. package/docs/providers/tts.md +2 -2
  28. package/docs/providers/upload.md +4 -59
  29. package/docs/providers/video.md +37 -6
  30. package/docs/references.md +183 -0
  31. package/docs/release.md +21 -16
  32. package/docs/settings-and-commands.md +168 -0
  33. package/docs/skills.md +14 -7
  34. package/docs/storage-and-network.md +4 -5
  35. package/docs/ui-components.md +92 -0
  36. package/docs/ui.md +24 -813
  37. package/docs/user-data.md +64 -623
  38. package/docs/whiteboard.md +110 -32
  39. package/package.json +1 -1
  40. package/types/packages/asr/src/types.d.ts +2 -0
  41. package/types/packages/plugins/src/environment.d.ts +3 -0
  42. package/types/packages/plugins/src/excerpts.d.ts +3 -0
  43. package/types/packages/plugins/src/foreground-host.d.ts +8 -0
  44. package/types/packages/plugins/src/publish-data.d.ts +4 -0
@@ -0,0 +1,254 @@
1
+ # 块的交互
2
+
3
+ 让你的块用起来和 Tau 自带的图片块一样:点一下选中并描边,上方浮出属性条,右键打开块菜单,按 Delete 删除;再给它配上 `/` 菜单条目、快捷键和快捷语法。整块由组件渲染的块(`isVoid: true`)都应该这样做。
4
+
5
+ 下面接着[第一个块](editor.md)里的计数器:选中后属性条里有「减一」「加一」「更多」,选中时按 `0` 重置,块菜单里有「重置计数」,`Mod+Shift+0` 插入新的计数器,在空段落里输入 `::counter` 回车也能插入。
6
+
7
+ ```tsx
8
+ import { Minus, MoreHorizontal, Plus, RotateCcw } from "lucide-react";
9
+ import {
10
+ BlockToolbar,
11
+ BlockToolbarButton,
12
+ BlockToolbarGap,
13
+ defineEditorElement,
14
+ defineEditorExtension,
15
+ definePlugin,
16
+ useElement,
17
+ type EditorElementRenderProps,
18
+ } from "@tau/plugins";
19
+
20
+ declare const __PLUGIN_META__: { id: string; name: string; version: string };
21
+
22
+ interface CounterElement {
23
+ type: "yourname.counter";
24
+ count: number;
25
+ children: { type: "formatted"; text: string }[];
26
+ }
27
+ declare module "@tau/plugins" {
28
+ interface EditorElementMap {
29
+ "yourname.counter": CounterElement;
30
+ }
31
+ }
32
+
33
+ function Counter({ element }: EditorElementRenderProps<CounterElement>) {
34
+ const { selected, readOnly, update, openMenu } = useElement(element);
35
+ return (
36
+ // 属性条相对最近的定位祖先摆放,所以外层要设 position: relative
37
+ <div className="yourname-counter" style={{ position: "relative" }}>
38
+ {selected && !readOnly && (
39
+ <BlockToolbar label="计数器属性">
40
+ <BlockToolbarButton
41
+ title="减一"
42
+ icon={<Minus size={15} />}
43
+ onClick={() => update({ count: element.count - 1 })}
44
+ />
45
+ <BlockToolbarButton
46
+ title="加一"
47
+ icon={<Plus size={15} />}
48
+ onClick={() => update({ count: element.count + 1 })}
49
+ />
50
+ <BlockToolbarGap />
51
+ <BlockToolbarButton
52
+ title="更多"
53
+ icon={<MoreHorizontal size={15} />}
54
+ onClick={(event) => {
55
+ const rect = event.currentTarget.getBoundingClientRect();
56
+ openMenu({ x: rect.left, y: rect.bottom + 6 });
57
+ }}
58
+ />
59
+ </BlockToolbar>
60
+ )}
61
+ 计数:{element.count}
62
+ </div>
63
+ );
64
+ }
65
+
66
+ export const counterExtension = defineEditorExtension({
67
+ id: "yourname.counter",
68
+ elements: [
69
+ defineEditorElement<CounterElement>({
70
+ type: "yourname.counter",
71
+ dataVersion: 1,
72
+ kind: "block",
73
+ isVoid: true,
74
+ drag: true,
75
+ frame: { keys: { "0": "yourname.counter.reset" } },
76
+ render: Counter,
77
+ }),
78
+ ],
79
+ commands: [
80
+ {
81
+ id: "yourname.counter.insert",
82
+ title: "插入计数器",
83
+ run: (editor) =>
84
+ editor.insertNode({
85
+ type: "yourname.counter",
86
+ count: 0,
87
+ children: [{ type: "formatted", text: "" }],
88
+ }),
89
+ },
90
+ {
91
+ id: "yourname.counter.reset",
92
+ title: "重置计数",
93
+ // 从菜单或外框按键触发时才有目标块的 path
94
+ run: (editor, { path }) => {
95
+ if (path) editor.setNodes({ count: 0 }, { at: path });
96
+ },
97
+ },
98
+ ],
99
+ hotkeys: [{ key: "mod+shift+0", command: "yourname.counter.insert" }],
100
+ menu: [
101
+ {
102
+ id: "yourname.counter.reset",
103
+ title: "重置计数",
104
+ icon: <RotateCcw size={16} />,
105
+ command: "yourname.counter.reset",
106
+ },
107
+ ],
108
+ behaviorsFor: {
109
+ paragraph: {
110
+ // 段落里只有「::counter」时按回车,换成计数器
111
+ insertBreak: (editor, [, path]) => {
112
+ if (editor.string(path).trim() !== "::counter") return false;
113
+ editor.removeNodes({ at: path });
114
+ editor.insertNodes(
115
+ {
116
+ type: "yourname.counter",
117
+ count: 0,
118
+ children: [{ type: "formatted", text: "" }],
119
+ },
120
+ { at: path },
121
+ );
122
+ return true;
123
+ },
124
+ },
125
+ },
126
+ });
127
+
128
+ export default definePlugin({
129
+ ...__PLUGIN_META__,
130
+ editorExtensions: [counterExtension],
131
+ });
132
+ ```
133
+
134
+ 图标来自 lucide-react,先 `pnpm add lucide-react`。`/` 菜单里的「计数器」条目沿用[第一个块](editor.md)的 `blockPanel`,这里省略了。
135
+
136
+ ## 块的外框
137
+
138
+ 在节点声明上加 `frame`,块就有了 Tau 块的公共交互:点击选中并描边、点到别处取消、右键打开块菜单、选中时 Delete / Backspace 删除块并留下空段落、Esc 取消选中,以及块末尾的「添加段落」。外框只用于 `isVoid: true` 的块。
139
+
140
+ | 字段 | 说明 |
141
+ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
142
+ | `keys` | 可选。选中时的按键对应的命令 ID,键名同 `KeyboardEvent.key`(如 `Enter`、`" "`、`"0"`)。命令收到这个节点的 `element` 和 `path`,同菜单里的命令 |
143
+ | `appendParagraph` | 可选。为 `false` 时不显示末尾的「添加段落」 |
144
+
145
+ 声明外框后,`render` 只渲染内容:收到的 `attributes` 是空对象、`children` 为空,交给外框处理,所以上例的 `Counter` 不需要展开 `attributes`。组件里用 `useElement(element).selected` 决定是否显示属性条。
146
+
147
+ 外框会接管块内的单击用于选中。块里自己的按钮放进属性条,或者像图册那样只在只读时响应单击、编辑时改用双击。
148
+
149
+ ## 块的属性条
150
+
151
+ 选中块后浮在块上方的属性条,用 `BlockToolbar`、`BlockToolbarButton`、`BlockToolbarGap` 组成,外观和 Tau 自带的图片块一致。上方放不下时自动翻到下方,点击按钮不会让编辑器失去光标。
152
+
153
+ - 属性条按最近的定位祖先摆放,渲染它的容器要设 `position: relative`。上例写在 `style` 里;用 Tailwind 时写带前缀的类名(如 `yourname:relative`),不带前缀的 `relative` 不会生成样式,见[用 Tailwind 时加前缀](ui-components.md#用-tailwind-时加前缀)。
154
+ - `BlockToolbar` 的 `label` 是属性条的无障碍名称。
155
+ - `BlockToolbarButton` 的 `title` 是按钮名称(纯图标按钮悬停时显示),`label` 是显示在图标旁的文字,`shortcut` 在提示里显示快捷键,`active` 表示当前选中的选项。
156
+ - `BlockToolbarGap` 在按钮之间加一道分隔。
157
+ - 「更多」按钮调用 `openMenu`,打开的就是右键时的块菜单,条目来自下文的 `menu`。
158
+
159
+ ## 扩展能声明的内容
160
+
161
+ `defineEditorExtension` 接收:
162
+
163
+ | 字段 | 说明 |
164
+ | ----------------- | --------------------------------------------------------------------------------------------- |
165
+ | `id` | 扩展 ID,全局唯一 |
166
+ | `elements` | 节点声明,至少一个 |
167
+ | `blockPanel` | 块菜单(`/` 菜单)中的条目 |
168
+ | `commands` | 编辑器命令 |
169
+ | `hotkeys` | 快捷键,绑定到命令 |
170
+ | `menu` | 块的手柄菜单和右键菜单中的条目 |
171
+ | `behaviorsFor` | 挂在其他节点类型上的行为,用来实现快捷语法 |
172
+ | `styleSources` | 节点里的 Tailwind 源码字段,见[动态 Tailwind 样式](editor-source-media.md#动态-tailwind-样式) |
173
+ | `htmlSerializers` | 静态 HTML 导出,见 [HTML 导出](editor-export.md#html-导出) |
174
+
175
+ 只能写以上字段。写了其他字段,类型检查会报错,Tau 也不会读取它们。
176
+
177
+ ### 块菜单
178
+
179
+ `blockPanel` 的每一项会出现在用户输入 `/` 后弹出的菜单里:
180
+
181
+ | 字段 | 说明 |
182
+ | ------------- | -------------------------------------------- |
183
+ | `icon` | 图标组件,接收 `className` |
184
+ | `title` | 标题 |
185
+ | `description` | 说明 |
186
+ | `keywords` | 搜索关键词,用户输入这些词时会匹配到这一项 |
187
+ | `onClick` | 选中时调用,收到当前编辑器,通常用来插入节点 |
188
+ | `children` | 可选。子菜单 |
189
+
190
+ `onClick` 收到的 `editor` 提供 `insertNode`、`setNodes`、`removeNodes` 等编辑器方法。插入后需要立即编辑的源码类块,用 `insertSourceBlock`,见[源码预览块](editor-source-media.md#源码预览块)。
191
+
192
+ `blockPanel` 也可以是一个函数 `(editor) => items`,每次打开菜单时求值。
193
+
194
+ ### 命令与快捷键
195
+
196
+ 命令是编辑器里的动作,快捷键、菜单和外框按键都通过命令 ID 触发它。上例的「插入计数器」由快捷键触发,「重置计数」由菜单和选中时的 `0` 键触发。
197
+
198
+ | 命令字段 | 说明 |
199
+ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
200
+ | `id` | 命令 ID,全局唯一,建议以扩展 ID 作前缀 |
201
+ | `title` | 可选。显示名称 |
202
+ | `when` | 可选。`(editor) => boolean`,返回 `false` 时快捷键跳过、菜单项变灰 |
203
+ | `run` | `(editor, context) => void`。`context` 包含触发它的 `event`、调用方的 `payload`;从菜单或外框按键触发时还有目标节点 `element` 和它的 `path`,快捷键触发时没有 |
204
+
205
+ | 快捷键字段 | 说明 |
206
+ | ---------- | -------------------------------------------------------------------------------- |
207
+ | `key` | 按键组合,如 `"mod+shift+k"`,`mod` 在 macOS 上是 ⌘,其他系统是 Ctrl;可以传数组 |
208
+ | `command` | 要执行的命令 ID |
209
+ | `fallback` | 可选。为 `true` 时只在其他扩展的快捷键都没有处理时才尝试 |
210
+
211
+ 快捷键针对的是光标位置,不知道是哪个块。要对某个块操作的命令,交给菜单或外框按键触发,或者在命令里根据光标位置自己查找节点。
212
+
213
+ 命令执行出错时,Tau 会隔离错误,不影响编辑器的其他功能。异步命令返回 Promise 即可。
214
+
215
+ ### 菜单
216
+
217
+ `menu` 中的条目出现在你的块的手柄菜单和右键菜单里:
218
+
219
+ | 字段 | 说明 |
220
+ | ---------------- | -------------------------------------------------------------- |
221
+ | `id` | 条目 ID,同一份菜单内唯一 |
222
+ | `title` | 标题,可以是函数 `(ctx) => string`,打开菜单时求值 |
223
+ | `icon` | 可选。图标元素(如 `<RotateCcw size={16} />`),同样可以是函数 |
224
+ | `command` | 点击时执行的命令 ID;只有子菜单的父条目可以省略 |
225
+ | `payload` | 可选。传给命令的参数,命令在 `context.payload` 中读取 |
226
+ | `when` | 可选。返回 `false` 时不显示这一项 |
227
+ | `disabled` | 可选。返回 `true` 时这一项变灰 |
228
+ | `group`、`order` | 可选。分组与排序 |
229
+ | `danger` | 可选。危险操作,以警示色显示 |
230
+ | `children` | 可选。子菜单 |
231
+
232
+ 函数形式的字段收到 `{ editor, element, path }`,即菜单针对的块。
233
+
234
+ ### 行为
235
+
236
+ `behaviors` 写在节点声明里,在光标位于该节点内时接管按键:
237
+
238
+ | 行为 | 时机 |
239
+ | -------------------------------------------- | ------------------ |
240
+ | `insertBreak(editor, [node, path])` | 按回车 |
241
+ | `deleteBackward(editor, [node, path], unit)` | 按退格 |
242
+ | `insertText(editor, [node, path], text)` | 输入文字 |
243
+ | `normalize(editor, [node, path])` | 编辑器规范化节点时 |
244
+
245
+ 每个函数返回 `true` 表示已经处理,返回 `false` 交给外层节点和默认行为。
246
+
247
+ `behaviorsFor` 写在扩展上,把行为挂到**其他**节点类型上。典型用途是快捷语法:上例在段落上挂了 `insertBreak`,段落文字是 `::counter` 时按回车,把段落换成计数器。
248
+
249
+ `editor` 上的 `string`、`removeNodes`、`insertNodes`、`setNodes` 等方法直接可用,不需要另外安装或打包编辑器库。
250
+
251
+ ## 下一步
252
+
253
+ - [源码块与媒体块](editor-source-media.md):做「源码 + 预览」的块,或在块里显示图片、视频、音频。
254
+ - [导出与字数统计](editor-export.md):让计数器参与 Markdown、HTML 导出。
@@ -0,0 +1,397 @@
1
+ # 源码块与媒体块
2
+
3
+ 用户写一段源码、下方实时显示结果的块(图表、公式、乐谱、组件演示),用 `SourceBlockEditor` 套上 Tau 统一的源码编辑外壳,不必从头写编辑交互。块里要显示图片、视频、音频时,用 Tau 的媒体组件,本地文件、远程地址和共享文档里的资源都能正确加载。节点的基本写法见[第一个块](editor.md)。
4
+
5
+ ## 源码预览块
6
+
7
+ `SourceBlockEditor` 提供源码编辑、错误提示、删除,以及与相邻块之间的光标移动。源码字段经 `field` 绑定,输入即时写入节点并进入撤销记录,协作时按字符合并;插件不需要自己写回源码。
8
+
9
+ 下面是一个格式化显示 JSON 的块:
10
+
11
+ ```tsx
12
+ import { Braces } from "lucide-react";
13
+ import {
14
+ SourceBlockEditor,
15
+ defineEditorElement,
16
+ defineEditorExtension,
17
+ definePlugin,
18
+ insertSourceBlock,
19
+ sourceBlockParagraphBehavior,
20
+ useReportPreviewError,
21
+ type EditorElementRenderProps,
22
+ } from "@tau/plugins";
23
+
24
+ declare const __PLUGIN_META__: { id: string; name: string; version: string };
25
+
26
+ interface JsonElement {
27
+ type: "yourname.json";
28
+ content: string;
29
+ children: { type: "formatted"; text: string }[];
30
+ }
31
+ declare module "@tau/plugins" {
32
+ interface EditorElementMap {
33
+ "yourname.json": JsonElement;
34
+ }
35
+ }
36
+
37
+ function parse(source: string) {
38
+ if (!source.trim()) return { text: "", error: null };
39
+ try {
40
+ return { text: JSON.stringify(JSON.parse(source), null, 2), error: null };
41
+ } catch (error) {
42
+ return { text: "", error: (error as Error).message };
43
+ }
44
+ }
45
+
46
+ function JsonPreview({ source }: { source: string }) {
47
+ const { text, error } = parse(source);
48
+ // 在源码块里由块统一显示错误;返回 false 时(如博客里)自己显示
49
+ const shownByBlock = useReportPreviewError(error);
50
+ if (error) return shownByBlock ? null : <p>{error}</p>;
51
+ return <pre className="yourname-json">{text}</pre>;
52
+ }
53
+
54
+ function JsonBlock({
55
+ attributes,
56
+ element,
57
+ children,
58
+ }: EditorElementRenderProps<JsonElement>) {
59
+ return (
60
+ <div {...attributes}>
61
+ <SourceBlockEditor
62
+ element={element}
63
+ field="content"
64
+ mode="javascript"
65
+ title="JSON"
66
+ icon={Braces}
67
+ >
68
+ <JsonPreview source={element.content} />
69
+ </SourceBlockEditor>
70
+ {children}
71
+ </div>
72
+ );
73
+ }
74
+
75
+ export default definePlugin({
76
+ ...__PLUGIN_META__,
77
+ editorExtensions: [
78
+ defineEditorExtension({
79
+ id: "yourname.json",
80
+ elements: [
81
+ defineEditorElement<JsonElement>({
82
+ type: "yourname.json",
83
+ dataVersion: 1,
84
+ // field 绑定的字段必须声明为文本字段
85
+ collaboration: { textFields: ["content"] },
86
+ kind: "block",
87
+ isVoid: true,
88
+ drag: true,
89
+ isEmpty: (element) => !element.content,
90
+ render: JsonBlock,
91
+ }),
92
+ ],
93
+ blockPanel: [
94
+ {
95
+ icon: Braces,
96
+ title: "JSON",
97
+ keywords: ["json"],
98
+ description: "格式化显示一段 JSON",
99
+ onClick: (editor) => {
100
+ insertSourceBlock(editor, {
101
+ type: "yourname.json",
102
+ content: "",
103
+ children: [{ type: "formatted", text: "" }],
104
+ });
105
+ },
106
+ },
107
+ ],
108
+ behaviorsFor: {
109
+ paragraph: sourceBlockParagraphBehavior("yourname.json"),
110
+ },
111
+ }),
112
+ ],
113
+ });
114
+ ```
115
+
116
+ `icon` 需要一个 lucide-react 图标,先 `pnpm add lucide-react`。`attributes` 展开在最外层,`children` 渲染在 `SourceBlockEditor` 外面,规则同[渲染组件](editor.md#渲染组件)。
117
+
118
+ | `SourceBlockEditor` 属性 | 说明 |
119
+ | ------------------------ | --------------------------------------------------------------------------------------------------- |
120
+ | `element` | 当前节点 |
121
+ | `field` | 存源码的字段名,需在节点的 `collaboration.textFields` 中声明 |
122
+ | `mode` | 源码的语言,用于语法高亮,如 `javascript`、`htmlmixed`、`stex`、`typst`;没有对应语法时按纯文本显示 |
123
+ | `title`、`icon` | 块的名称和图标,显示在编辑标题、空块占位和手机配件栏上 |
124
+ | `center`、`extend` | 可选。预览内容居中;预览区上下多留一点空间 |
125
+ | `children` | 预览内容 |
126
+
127
+ 桌面上,点块上的「编辑」在块内展开源码,下方实时预览。手机上,正文只显示预览:点一下选中块,配件栏给出「编辑」「复制」和块操作,「编辑」打开上方预览、下方源码的源码页;预览比屏幕宽时在块内横向滚动。两端的交互都由 Tau 提供,插件只需提供预览,不要在块上自己放编辑按钮。
128
+
129
+ 相关工具:
130
+
131
+ | 接口 | 用途 |
132
+ | ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
133
+ | `useSourceBlock(element)` | 返回 `update`(修改节点字段)、`theme`(当前主题)和 `compiler`(Tailwind 编译器,可能为空) |
134
+ | `useReportPreviewError(message, { stale })` | 预览出错时上报错误,传 `null` 清除;`stale` 表示是否保留了上一次成功的预览。在源码预览块内由块统一显示错误,返回 `false`(不在块内,如博客)时预览需自己显示 |
135
+ | `insertSourceBlock(editor, element, line?)` | 插入节点并打开源码编辑(桌面在块内,手机打开源码页),适合在块菜单的 `onClick` 中使用 |
136
+ | `sourceBlockParagraphBehavior(type)` | 放进 `behaviorsFor.paragraph`,让用户在段首按退格时进入上方的源码块 |
137
+
138
+ `theme.isDark` 表示当前是否为暗色主题,`theme.token(word)` 读取主题色值,预览应随主题变化。`compiler` 只在桌面编辑器中可用,在博客里为空,此时配合下文的 `styleSources` 使用预先生成的样式。
139
+
140
+ 不要在插件里打包自己的编辑器、Slate 或源码编辑器,直接使用这些组件。
141
+
142
+ ## 行内源码
143
+
144
+ 行内的源码节点(如行内公式)声明 `kind: "inline"`,类型登记在 `EditorInlineElementMap` 里,并使用另一组接口:
145
+
146
+ | 接口 | 用途 |
147
+ | ------------------------------------- | ------------------------------------------------------------- |
148
+ | `useInlineSource(element)` | 返回 `readOnly`、`compiler`、`update(properties)`、`remove()` |
149
+ | `insertInlineSource(editor, element)` | 插入行内节点,适合在菜单中使用 |
150
+ | `InlineSourceBoundary` | 渲染在行内节点组件里,处理光标进出节点的边界 |
151
+
152
+ 行内的源码输入弹层由你用 `@tau/ui` 组件实现。只读时只显示预览,不打开编辑弹层;只读状态下 `update` 和 `remove` 不会写入文档。
153
+
154
+ ## 动态 Tailwind 样式
155
+
156
+ 如果节点的源码里写了 Tailwind 类名(比如用户写的组件演示),在扩展上声明哪个字段是源码:
157
+
158
+ ```ts
159
+ defineEditorExtension({
160
+ id: "yourname.demo",
161
+ elements: [demoElement],
162
+ styleSources: [{ type: "yourname.demo", field: "content", language: "tsx" }],
163
+ });
164
+ ```
165
+
166
+ 桌面编辑器里用 `useSourceBlock(element).compiler` 实时编译;在博客等不能实时编译样式的地方,Tau 会读取这些字段,提前生成对应的样式。
167
+
168
+ - 字段的值必须是字符串,`language` 为 `html` 或 `tsx`,只能登记本插件的节点类型。
169
+ - 类名要完整地写在源码里。拼接出来的类名(如 `` `bg-${color}` ``)扫描不到。
170
+
171
+ 这里说的是用户写在节点里的类名。插件自己组件里的 Tailwind 类名由你在构建时编译,并要加前缀,见[用 Tailwind 时加前缀](ui-components.md#用-tailwind-时加前缀)。
172
+
173
+ ## 图片、视频与音频
174
+
175
+ 节点里存的图片、视频、音频地址可能是本地路径、远程地址或共享文档里的资源,不能直接交给 `<img>`。用 Tau 提供的媒体组件显示,解析方式与 Tau 自带的图片块相同:本地路径转成可加载的地址,远程资源按用户的缓存设置下载,共享文档里的资源照常加载。
176
+
177
+ 下面的照片块显示一张图片,没有图片时让用户选择文件上传,双击打开图片查看器:
178
+
179
+ ```tsx
180
+ import type { ChangeEvent } from "react";
181
+ import {
182
+ MediaImage,
183
+ defineEditorElement,
184
+ useElement,
185
+ useMediaAppearance,
186
+ useMediaViewer,
187
+ useResourceUpload,
188
+ type EditorElementRenderProps,
189
+ type MediaAppearancePreset,
190
+ } from "@tau/plugins";
191
+
192
+ interface PhotoElement {
193
+ type: "yourname.photo";
194
+ url: string;
195
+ caption: string;
196
+ borderRadius?: MediaAppearancePreset;
197
+ children: { type: "formatted"; text: string }[];
198
+ }
199
+ declare module "@tau/plugins" {
200
+ interface EditorElementMap {
201
+ "yourname.photo": PhotoElement;
202
+ }
203
+ }
204
+
205
+ const RADIUS: Record<MediaAppearancePreset, string> = {
206
+ none: "0",
207
+ sm: "var(--tau-radius-sm)",
208
+ md: "var(--tau-radius-md)",
209
+ lg: "var(--tau-radius-lg)",
210
+ };
211
+
212
+ function Photo({
213
+ attributes,
214
+ element,
215
+ children,
216
+ }: EditorElementRenderProps<PhotoElement>) {
217
+ const { update } = useElement(element);
218
+ const appearance = useMediaAppearance();
219
+ const openViewer = useMediaViewer(element);
220
+ const upload = useResourceUpload();
221
+ // 节点自己的设置优先,其次是用户的默认外观,都没有时不加圆角
222
+ const radius = element.borderRadius ?? appearance?.borderRadius ?? "none";
223
+
224
+ const choose = async (event: ChangeEvent<HTMLInputElement>) => {
225
+ const file = event.target.files?.[0];
226
+ if (upload && file) update({ url: await upload(file) });
227
+ };
228
+
229
+ return (
230
+ <div {...attributes}>
231
+ <figure contentEditable={false} className="yourname-photo">
232
+ {element.url ? (
233
+ <MediaImage
234
+ url={element.url}
235
+ alt={element.caption}
236
+ style={{ borderRadius: RADIUS[radius] }}
237
+ onDoubleClick={() => openViewer(0)}
238
+ />
239
+ ) : (
240
+ // 只读编辑器和博客里拿不到上传函数,不显示选择文件
241
+ upload && <input type="file" accept="image/*" onChange={choose} />
242
+ )}
243
+ {element.caption && <figcaption>{element.caption}</figcaption>}
244
+ </figure>
245
+ {children}
246
+ </div>
247
+ );
248
+ }
249
+
250
+ export const photoElement = defineEditorElement<PhotoElement>({
251
+ type: "yourname.photo",
252
+ dataVersion: 1,
253
+ kind: "block",
254
+ isVoid: true,
255
+ drag: true,
256
+ render: Photo,
257
+ resources: [
258
+ { path: "url", kind: "image", alt: "caption", caption: "caption" },
259
+ ],
260
+ });
261
+ ```
262
+
263
+ `photoElement` 放进扩展的 `elements`,写法同[第一个块](editor.md#1-定义节点和扩展)。
264
+
265
+ | 接口 | 用途 |
266
+ | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
267
+ | `MediaImage` | 显示图片,参数为 `url`、`alt`、`className`、`style`、`loading`、`onLoadError` 等 |
268
+ | `MediaVideo` / `MediaAudio` | Tau 的视频、音频播放器,参数为 `src` 与原生媒体属性 |
269
+ | `useResource(url, { kind })` | 自己绘制时使用:返回 `src`、`status`(`loading` / `ready` / `error`)与 `retry()`,元素加载失败时调用 `retry` 重新解析 |
270
+ | `useResourceUpload()` | 返回上传函数,把用户选的文件存进当前的资源存储并返回可写入节点的地址;只读编辑器和博客里为 `undefined` |
271
+ | `useMediaAppearance()` | 用户设置的媒体默认外观:`borderRadius`(`none` / `sm` / `md` / `lg`)与 `shadow`(同样四档,或布尔值);博客里取发布设置 |
272
+ | `useMediaViewer(element)` | 返回打开图片查看器的函数,见[查看图片](#查看图片) |
273
+
274
+ `useMediaAppearance()` 可能返回 `undefined`:Tau 还没有读到用户的默认外观设置,或者组件不在 Tau 的编辑器和博客正文里渲染(比如插件页面里),都拿不到。返回了对象,其中的 `borderRadius`、`shadow` 也可能缺省。所以按上例的顺序取值:节点自己的设置,其次 `appearance?.borderRadius`,最后是你的默认值。
275
+
276
+ `kind` 取 `image`、`video`、`audio`、`file`,决定远程资源的缓存方式。上传的文件不需要自己判断本地还是远端存储,也不要把文件内容写进节点。
277
+
278
+ ### 查看图片
279
+
280
+ `useMediaViewer(element)` 返回一个函数,调用后打开 Tau 的图片查看器,浏览文档里的全部图片,并定位到这个节点的第 `index` 张(按[声明的资源字段](#声明资源字段)计,从 0 开始)。照片块只有一张图,所以传 `0`:
281
+
282
+ ```tsx
283
+ const openViewer = useMediaViewer(element);
284
+ <MediaImage url={element.url} onDoubleClick={() => openViewer(0)} />;
285
+ ```
286
+
287
+ ### 声明资源字段
288
+
289
+ 节点里存了图片、视频、音频或文件地址时,在节点声明上用 `resources` 写明是哪些字段。Tau 只按这份声明找资源:导出打包、共享给协作者时上传本地文件、图片查看器里浏览图片、导出 EPUB 时取图,都靠它。没有声明的字段按普通文字原样保留,本地图片不会随内容导出,也不会上传给协作者。
290
+
291
+ 照片块的图片在 `url` 字段;一个节点里有一组图片时,用 `[]` 指向数组的每一项:
292
+
293
+ ```ts
294
+ resources: [
295
+ { path: "images[].url", kind: "image", alt: "desc", caption: "desc" },
296
+ ],
297
+ ```
298
+
299
+ | 字段 | 说明 |
300
+ | --------- | ---------------------------------------------------------------------------------------- |
301
+ | `path` | 资源字段的路径。`url` 是节点上的字段,`images[].url` 表示 `images` 数组每一项的 `url` |
302
+ | `kind` | `image`、`video`、`audio` 或 `file` |
303
+ | `preview` | 可选。同一资源的显示版本(如裁剪结果)所在的字段,与 `path` 末段同层,同样会被导出和上传 |
304
+ | `alt` | 可选。替代文字所在的字段,与 `path` 末段同层,图片查看器里显示 |
305
+ | `caption` | 可选。说明文字所在的字段,与 `path` 末段同层,图片查看器里显示 |
306
+
307
+ 声明会随资料库保存,插件停用或在没装插件的设备上,这些资源照样能导出和共享。已经有文档用了的字段不要改名;新增资源字段时补进声明即可。资源字段不能同时列进 `collaboration.textFields`。
308
+
309
+ ### 完整示例:图册
310
+
311
+ [图册插件](https://github.com/LastKnightCoder/tau-plugin-image-gallery)把这些能力和[块的交互](editor-interactions.md)组合成了一个完整的媒体块,可以对照它的源码写自己的块:
312
+
313
+ | 需要 | 图册的做法 |
314
+ | -------------------------- | ------------------------------------------------------------------------------------------------------- |
315
+ | 点选、删除、右键菜单 | 节点声明 `frame`,Enter 绑定「编辑图册」命令,空格绑定「全屏预览」 |
316
+ | 选中后的属性条 | `useElement(element).selected` 为真时渲染 `BlockToolbar`,切换布局用 `update({ mode })` |
317
+ | 显示图片 | 每张图用 `MediaImage`,圆角与阴影在节点没设置时取 `useMediaAppearance()`,两者都没有时用 `"none"` |
318
+ | 添加图片 | 编辑弹窗里调用 `useResourceUpload()` 返回的函数,拿到地址写进 `images`;返回 `undefined` 时隐藏上传按钮 |
319
+ | 调整顺序 | 编辑弹窗里的图片列表用 `@tau/ui` 的 `Sortable` |
320
+ | 看大图 | 只读时单击、编辑时双击图片,调用 `useMediaViewer(element)(index)`;编辑时单击留给外框选中 |
321
+ | 导出、协作、查看器找到图片 | `resources: [{ path: "images[].url", kind: "image", alt: "desc", caption: "desc" }]` |
322
+ | Markdown 与 HTML 导出 | `editorCodecs` 每张图导出一行图片链接,`htmlSerializers` 输出静态的横向滚动图片组 |
323
+ | 样式 | Tailwind 类名统一加 `ig:` 前缀,见[用 Tailwind 时加前缀](ui-components.md#用-tailwind-时加前缀) |
324
+
325
+ 命令和外框按键只知道节点,打开弹窗却要用组件里的状态。图册的做法是:组件把「打开编辑」登记到以节点对象为键的 `WeakMap` 里,命令收到 `context.element` 后从这里取出来调用。节点被替换后旧键自然失效,不会打开别的图册。
326
+
327
+ ```tsx
328
+ import { useEffect, useState } from "react";
329
+ import {
330
+ defineEditorElement,
331
+ defineEditorExtension,
332
+ useElement,
333
+ type EditorElementRenderProps,
334
+ } from "@tau/plugins";
335
+
336
+ interface GalleryElement {
337
+ type: "yourname.gallery";
338
+ images: { url: string; desc: string }[];
339
+ children: { type: "formatted"; text: string }[];
340
+ }
341
+ declare module "@tau/plugins" {
342
+ interface EditorElementMap {
343
+ "yourname.gallery": GalleryElement;
344
+ }
345
+ }
346
+
347
+ // 以节点对象为键:节点被替换后旧键自然失效
348
+ const galleryActions = new WeakMap<object, { openSettings(): void }>();
349
+
350
+ function Gallery({ element }: EditorElementRenderProps<GalleryElement>) {
351
+ const { readOnly } = useElement(element);
352
+ const [editing, setEditing] = useState(false);
353
+
354
+ // 每次渲染登记当前节点,卸载或换节点时移除
355
+ useEffect(() => {
356
+ galleryActions.set(element, {
357
+ openSettings: () => {
358
+ if (!readOnly) setEditing(true);
359
+ },
360
+ });
361
+ return () => void galleryActions.delete(element);
362
+ });
363
+
364
+ return <div>{editing ? "编辑中" : `${element.images.length} 张图片`}</div>;
365
+ }
366
+
367
+ export const galleryExtension = defineEditorExtension({
368
+ id: "yourname.gallery",
369
+ elements: [
370
+ defineEditorElement<GalleryElement>({
371
+ type: "yourname.gallery",
372
+ dataVersion: 1,
373
+ kind: "block",
374
+ isVoid: true,
375
+ frame: { keys: { Enter: "yourname.gallery.edit" } },
376
+ render: Gallery,
377
+ resources: [
378
+ { path: "images[].url", kind: "image", alt: "desc", caption: "desc" },
379
+ ],
380
+ }),
381
+ ],
382
+ commands: [
383
+ {
384
+ id: "yourname.gallery.edit",
385
+ title: "编辑图册",
386
+ run: (_editor, { element }) => {
387
+ if (element) galleryActions.get(element)?.openSettings();
388
+ },
389
+ },
390
+ ],
391
+ });
392
+ ```
393
+
394
+ ## 下一步
395
+
396
+ - [导出与字数统计](editor-export.md):让块参与 Markdown、HTML 导出,块里的文字计入字数。
397
+ - [块的数据版本与协作](editor-collaboration.md):多人同时编辑源码字段,以及发布后修改数据结构。