|
3 | 3 | * |
4 | 4 | * @ai-context: 将 `` 以 Decoration.replace widget 替换为真实图片 |
5 | 5 | * (NoteImageWidget 挂载 React NoteImage)。仅 docChanged 重算 |
6 | | - * decorations(spec §4.1);独立行图片 block 化(占整行,替换后 |
7 | | - * 不残留行内空隙)。noteId/onOpen 经工厂参数注入(组件层持有)。 |
| 6 | + * decorations(spec §4.1)。 |
| 7 | + * @ai-context: v0.16.1 修复:插件 decorations 一律 inline replace——CM6 明确 |
| 8 | + * 禁止插件产出 block decoration(ViewPlugin 的 decorations 经 |
| 9 | + * disallowBlockEffectsFor 校验,block:true 抛 RangeError |
| 10 | + * "Block decorations may not be specified via plugins",插入 |
| 11 | + * 独立行图片即崩)。独立行图片所在行仅空白,inline replace 替换 |
| 12 | + * 整段语法后自然独占一行(视觉等价,无行为回退)。 |
| 13 | + * noteId/onOpen 经工厂参数注入(组件层持有)。 |
8 | 14 | */ |
9 | 15 | import { RangeSetBuilder } from "@codemirror/state"; |
10 | 16 | import { Decoration, ViewPlugin, type DecorationSet, type EditorView, type ViewUpdate } from "@codemirror/view"; |
11 | | -import { scanImageRefs, type ImageRef } from "./imageDecoration"; |
| 17 | +import { scanImageRefs } from "./imageDecoration"; |
12 | 18 | import { NoteImageWidget } from "./NoteImageWidget"; |
13 | 19 |
|
14 | 20 | interface PluginContext { |
15 | 21 | noteId: number; |
16 | 22 | onOpen?: (url: string, title?: string) => void; |
17 | 23 | } |
18 | 24 |
|
19 | | -/** 独立行图片:所在行除图片语法外仅空白 → block 占整行 */ |
20 | | -function isBlockImage(view: EditorView, ref: ImageRef): boolean { |
21 | | - const line = view.state.doc.lineAt(ref.from); |
22 | | - const before = line.text.slice(0, ref.from - line.from); |
23 | | - const after = line.text.slice(ref.to - line.from); |
24 | | - return before.trim() === "" && after.trim() === ""; |
25 | | -} |
26 | | - |
27 | 25 | function computeDecorations(view: EditorView, ctx: PluginContext): DecorationSet { |
28 | 26 | const refs = scanImageRefs(view.state.doc.toString()); |
29 | 27 | // RangeSetBuilder 是带位置的唯一构造途径(Decoration.replace 只接受 spec) |
30 | 28 | const builder = new RangeSetBuilder<Decoration>(); |
31 | 29 | for (const r of refs) { |
32 | | - builder.add(r.from, r.to, Decoration.replace({ |
33 | | - widget: new NoteImageWidget(r, ctx), |
34 | | - block: isBlockImage(view, r), |
35 | | - })); |
| 30 | + builder.add(r.from, r.to, Decoration.replace({ widget: new NoteImageWidget(r, ctx) })); |
36 | 31 | } |
37 | 32 | return builder.finish(); |
38 | 33 | } |
|
0 commit comments