@incremark/react 0.1.2 → 0.2.1

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.
package/dist/index.d.ts CHANGED
@@ -1,8 +1,94 @@
1
1
  import { ParserOptions, AnimationEffect, TransformerPlugin, ParsedBlock, Root, IncrementalUpdate, IncremarkParser, SourceBlock, TransformerOptions, DisplayBlock, BlockTransformer, RootContent } from '@incremark/core';
2
2
  export { AnimationEffect, BlockTransformer, DisplayBlock, IncrementalUpdate, ParsedBlock, ParserOptions, Root, RootContent, SourceBlock, TransformerOptions, TransformerPlugin, TransformerState, allPlugins, cloneNode, codeBlockPlugin, countChars, createBlockTransformer, createPlugin, defaultPlugins, imagePlugin, mathPlugin, mermaidPlugin, sliceAst, thematicBreakPlugin } from '@incremark/core';
3
+ import React, { ReactNode } from 'react';
4
+ import { Definition, FootnoteDefinition, RootContent as RootContent$1 } from 'mdast';
3
5
  import * as _incremark_devtools from '@incremark/devtools';
4
6
  import { DevToolsOptions } from '@incremark/devtools';
5
- import React from 'react';
7
+ import { DesignTokens } from '@incremark/theme';
8
+ export { DesignTokens, applyTheme, darkTheme, defaultTheme, generateCSSVars, mergeTheme } from '@incremark/theme';
9
+
10
+ /**
11
+ * @file Definitions Context - 管理 Markdown 引用定义
12
+ *
13
+ * @description
14
+ * 提供 definitions 和 footnoteDefinitions 的 Context,
15
+ * 用于支持引用式图片/链接的解析和渲染。
16
+ *
17
+ * @author Incremark Team
18
+ * @license MIT
19
+ */
20
+
21
+ /**
22
+ * Definitions Context 接口
23
+ */
24
+ interface DefinitionsContextValue {
25
+ /** 图片/链接定义映射表 */
26
+ definitions: Record<string, Definition>;
27
+ /** 脚注定义映射表 */
28
+ footnoteDefinitions: Record<string, FootnoteDefinition>;
29
+ /** 脚注引用的出现顺序 */
30
+ footnoteReferenceOrder: string[];
31
+ /** 设置 definitions */
32
+ setDefinitions: (definitions: Record<string, Definition>) => void;
33
+ /** 设置 footnoteDefinitions */
34
+ setFootnoteDefinitions: (definitions: Record<string, FootnoteDefinition>) => void;
35
+ /** 设置 footnoteReferenceOrder */
36
+ setFootnoteReferenceOrder: (order: string[]) => void;
37
+ /** 清空 definitions */
38
+ clearDefinitions: () => void;
39
+ /** 清空 footnoteDefinitions */
40
+ clearFootnoteDefinitions: () => void;
41
+ /** 清空 footnoteReferenceOrder */
42
+ clearFootnoteReferenceOrder: () => void;
43
+ /** 清空所有定义 */
44
+ clearAllDefinitions: () => void;
45
+ }
46
+ /**
47
+ * Definitions Provider Props
48
+ */
49
+ interface DefinitionsProviderProps {
50
+ children: ReactNode;
51
+ }
52
+ /**
53
+ * Definitions Provider 组件
54
+ *
55
+ * @description
56
+ * 为子组件提供 definitions 和 footnoteDefinitions。
57
+ * 通常在 useIncremark 返回的数据更新时,调用 setDefinitions 和 setFootnoteDefinitions。
58
+ *
59
+ * @example
60
+ * ```tsx
61
+ * import { DefinitionsProvider } from '@incremark/react'
62
+ *
63
+ * function App() {
64
+ * return (
65
+ * <DefinitionsProvider>
66
+ * <Incremark blocks={blocks} />
67
+ * </DefinitionsProvider>
68
+ * )
69
+ * }
70
+ * ```
71
+ */
72
+ declare const DefinitionsProvider: React.FC<DefinitionsProviderProps>;
73
+ /**
74
+ * useDefinitions Hook
75
+ *
76
+ * @description
77
+ * 获取 definitions context。必须在 DefinitionsProvider 内部使用。
78
+ *
79
+ * @returns Definitions context value
80
+ *
81
+ * @throws 如果在 DefinitionsProvider 外部使用
82
+ *
83
+ * @example
84
+ * ```tsx
85
+ * function MyComponent() {
86
+ * const { definitions, footnoteDefinitions } = useDefinitions()
87
+ * // 使用 definitions...
88
+ * }
89
+ * ```
90
+ */
91
+ declare function useDefinitions(): DefinitionsContextValue;
6
92
 
7
93
  /** 打字机效果配置 */
8
94
  interface TypewriterOptions {
@@ -27,6 +113,7 @@ interface UseIncremarkOptions extends ParserOptions {
27
113
  }
28
114
  interface BlockWithStableId$1 extends ParsedBlock {
29
115
  stableId: string;
116
+ isLastPending?: boolean;
30
117
  }
31
118
  /** 打字机控制对象 */
32
119
  interface TypewriterControls {
@@ -58,10 +145,10 @@ interface TypewriterControls {
58
145
  *
59
146
  * function App() {
60
147
  * // 基础用法
61
- * const { blocks, append, finalize } = useIncremark()
148
+ * const incremark = useIncremark()
62
149
  *
63
150
  * // 启用打字机效果
64
- * const { blocks, append, finalize, typewriter } = useIncremark({
151
+ * const incremarkWithTypewriter = useIncremark({
65
152
  * typewriter: {
66
153
  * enabled: true, // 可动态切换
67
154
  * charsPerTick: [1, 3],
@@ -72,14 +159,9 @@ interface TypewriterControls {
72
159
  * })
73
160
  *
74
161
  * // 动态切换打字机效果
75
- * typewriter.setEnabled(false)
162
+ * incremarkWithTypewriter.typewriter.setEnabled(false)
76
163
  *
77
- * return (
78
- * <>
79
- * <Incremark blocks={blocks} />
80
- * {typewriter.isProcessing && <button onClick={typewriter.skip}>跳过</button>}
81
- * </>
82
- * )
164
+ * return <Incremark incremark={incremark} />
83
165
  * }
84
166
  * ```
85
167
  */
@@ -96,6 +178,8 @@ declare function useIncremark(options?: UseIncremarkOptions): {
96
178
  blocks: BlockWithStableId$1[];
97
179
  /** 是否正在加载 */
98
180
  isLoading: boolean;
181
+ /** 是否已完成(finalize) */
182
+ isFinalized: boolean;
99
183
  /** 追加内容 */
100
184
  append: (chunk: string) => IncrementalUpdate;
101
185
  /** 完成解析 */
@@ -110,6 +194,8 @@ declare function useIncremark(options?: UseIncremarkOptions): {
110
194
  parser: IncremarkParser;
111
195
  /** 打字机控制 */
112
196
  typewriter: TypewriterControls;
197
+ /** @internal 提供给 Incremark 组件使用的 context value */
198
+ _definitionsContextValue: DefinitionsContextValue;
113
199
  };
114
200
  type UseIncremarkReturn = ReturnType<typeof useIncremark>;
115
201
 
@@ -207,7 +293,7 @@ interface BlockWithStableId extends ParsedBlock {
207
293
  }
208
294
  interface IncremarkProps {
209
295
  /** 要渲染的块列表 */
210
- blocks: BlockWithStableId[];
296
+ blocks?: BlockWithStableId[];
211
297
  /** 自定义组件映射 */
212
298
  components?: Partial<Record<string, React.ComponentType<{
213
299
  node: any;
@@ -216,6 +302,8 @@ interface IncremarkProps {
216
302
  showBlockStatus?: boolean;
217
303
  /** 自定义类名 */
218
304
  className?: string;
305
+ /** 可选:useIncremark 返回的对象(用于自动提供 context) */
306
+ incremark?: UseIncremarkReturn;
219
307
  }
220
308
  /**
221
309
  * Incremark 主渲染组件
@@ -224,9 +312,10 @@ interface IncremarkProps {
224
312
  * ```tsx
225
313
  * import { useIncremark, Incremark } from '@incremark/react'
226
314
  *
315
+ * // 推荐用法: 传入 incremark 对象(自动提供 context)
227
316
  * function App() {
228
- * const { blocks } = useIncremark()
229
- * return <Incremark blocks={blocks} />
317
+ * const incremark = useIncremark()
318
+ * return <Incremark incremark={incremark} />
230
319
  * }
231
320
  * ```
232
321
  */
@@ -285,4 +374,119 @@ interface AutoScrollContainerRef {
285
374
  */
286
375
  declare const AutoScrollContainer: React.ForwardRefExoticComponent<AutoScrollContainerProps & React.RefAttributes<AutoScrollContainerRef>>;
287
376
 
288
- export { AutoScrollContainer, type AutoScrollContainerProps, type AutoScrollContainerRef, Incremark, type IncremarkProps, IncremarkRenderer, type IncremarkRendererProps, type TypewriterControls, type TypewriterOptions, type UseBlockTransformerOptions, type UseBlockTransformerReturn, type UseDevToolsOptions, type UseIncremarkOptions, type UseIncremarkReturn, useBlockTransformer, useDevTools, useIncremark };
377
+ /**
378
+ * HtmlElementNode 类型定义(与 @incremark/core 中的定义一致)
379
+ */
380
+ interface HtmlElementNode {
381
+ type: 'htmlElement';
382
+ tagName: string;
383
+ attrs: Record<string, string>;
384
+ children: RootContent$1[];
385
+ data?: {
386
+ rawHtml?: string;
387
+ parsed?: boolean;
388
+ originalType?: string;
389
+ };
390
+ }
391
+ interface IncremarkHtmlElementProps {
392
+ node: HtmlElementNode;
393
+ }
394
+ /**
395
+ * IncremarkHtmlElement 组件
396
+ *
397
+ * 渲染结构化的 HTML 元素节点
398
+ */
399
+ declare const IncremarkHtmlElement: React.FC<IncremarkHtmlElementProps>;
400
+
401
+ /**
402
+ * 脚注列表组件
403
+ *
404
+ * 在文档底部渲染所有脚注定义,按引用出现的顺序排列
405
+ *
406
+ * @component IncremarkFootnotes
407
+ *
408
+ * @remarks
409
+ * 样式定义在 @incremark/theme 中的 footnotes.less
410
+ * footnoteReferenceOrder 自动从 context 获取,无需手动传递
411
+ */
412
+
413
+ /**
414
+ * IncremarkFootnotes 组件
415
+ *
416
+ * 渲染文档底部的脚注列表,支持:
417
+ * - 按引用出现顺序排列
418
+ * - 双向跳转(引用 ↔ 定义)
419
+ * - 高亮当前查看的脚注
420
+ *
421
+ * @example
422
+ * ```tsx
423
+ * <IncremarkFootnotes />
424
+ * ```
425
+ */
426
+ declare const IncremarkFootnotes: React.FC;
427
+
428
+ /**
429
+ * @file IncremarkContainerProvider - Incremark 容器级 Provider
430
+ *
431
+ * @description
432
+ * 用于在 Incremark 的渲染树根部统一注入各种 Context / 全局能力。
433
+ * 目前主要用于注入 DefinitionsContext(引用定义、脚注定义等)。
434
+ *
435
+ * 设计目标:
436
+ * - **对用户隐藏实现细节**:用户只需 `<Incremark incremark={incremark} />`
437
+ * - **为未来扩展留空间**:后续可在这里统一添加更多 Provider(主题、滚动、性能开关等)
438
+ *
439
+ * @author Incremark Team
440
+ * @license MIT
441
+ */
442
+
443
+ /**
444
+ * IncremarkContainerProvider Props
445
+ */
446
+ interface IncremarkContainerProviderProps {
447
+ /** 子节点 */
448
+ children: ReactNode;
449
+ /** DefinitionsContext 的值 */
450
+ definitions: DefinitionsContextValue;
451
+ }
452
+ /**
453
+ * IncremarkContainerProvider 组件
454
+ *
455
+ * @param props - 组件参数
456
+ * @returns ReactElement
457
+ */
458
+ declare const IncremarkContainerProvider: React.FC<IncremarkContainerProviderProps>;
459
+
460
+ interface ThemeProviderProps {
461
+ /** 主题配置,可以是:
462
+ * - 字符串:'default' | 'dark'
463
+ * - 完整主题对象:DesignTokens
464
+ * - 部分主题对象:Partial<DesignTokens>(会合并到默认主题)
465
+ */
466
+ theme: 'default' | 'dark' | DesignTokens | Partial<DesignTokens>;
467
+ /** 子组件 */
468
+ children: ReactNode;
469
+ /** 自定义类名 */
470
+ className?: string;
471
+ }
472
+ /**
473
+ * ThemeProvider 组件
474
+ *
475
+ * 提供主题上下文,支持全局/局部主题和部分变量替换
476
+ *
477
+ * @example
478
+ * ```tsx
479
+ * // 使用预设主题
480
+ * <ThemeProvider theme="dark">
481
+ * <Incremark blocks={blocks} />
482
+ * </ThemeProvider>
483
+ *
484
+ * // 部分替换
485
+ * <ThemeProvider theme={{ color: { text: { primary: '#custom' } } }}>
486
+ * <Incremark blocks={blocks} />
487
+ * </ThemeProvider>
488
+ * ```
489
+ */
490
+ declare const ThemeProvider: React.FC<ThemeProviderProps>;
491
+
492
+ export { AutoScrollContainer, type AutoScrollContainerProps, type AutoScrollContainerRef, type DefinitionsContextValue, DefinitionsProvider, type DefinitionsProviderProps, type HtmlElementNode, Incremark, IncremarkContainerProvider, type IncremarkContainerProviderProps, IncremarkFootnotes, IncremarkHtmlElement, type IncremarkHtmlElementProps, type IncremarkProps, IncremarkRenderer, type IncremarkRendererProps, ThemeProvider, type ThemeProviderProps, type TypewriterControls, type TypewriterOptions, type UseBlockTransformerOptions, type UseBlockTransformerReturn, type UseDevToolsOptions, type UseIncremarkOptions, type UseIncremarkReturn, useBlockTransformer, useDefinitions, useDevTools, useIncremark };