@incremark/vue 0.2.2 → 0.2.3

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 (58) hide show
  1. package/dist/ThemeProvider.vue.d.ts +25 -0
  2. package/dist/components/AutoScrollContainer.vue.d.ts +31 -0
  3. package/dist/components/Incremark.vue.d.ts +30 -0
  4. package/dist/components/IncremarkBlockquote.vue.d.ts +6 -0
  5. package/dist/components/IncremarkCode.vue.d.ts +16 -0
  6. package/dist/components/IncremarkDefault.vue.d.ts +6 -0
  7. package/dist/components/IncremarkFootnotes.vue.d.ts +2 -0
  8. package/dist/components/IncremarkHeading.vue.d.ts +6 -0
  9. package/dist/components/IncremarkHtmlElement.vue.d.ts +20 -0
  10. package/dist/components/IncremarkInline.vue.d.ts +6 -0
  11. package/dist/components/IncremarkList.vue.d.ts +6 -0
  12. package/dist/components/IncremarkMath.vue.d.ts +17 -0
  13. package/dist/components/IncremarkParagraph.vue.d.ts +6 -0
  14. package/dist/components/IncremarkRenderer.vue.d.ts +6 -0
  15. package/dist/components/IncremarkTable.vue.d.ts +6 -0
  16. package/dist/components/IncremarkThematicBreak.vue.d.ts +2 -0
  17. package/{src/components/index.ts → dist/components/index.d.ts} +16 -23
  18. package/dist/composables/index.d.ts +8 -0
  19. package/dist/composables/useBlockTransformer.d.ts +68 -0
  20. package/dist/composables/useDefinationsContext.d.ts +9 -0
  21. package/dist/composables/useDevTools.d.ts +18 -0
  22. package/dist/composables/useIncremark.d.ts +112 -0
  23. package/dist/composables/useProvideDefinations.d.ts +16 -0
  24. package/dist/composables/useStreamRenderer.d.ts +26 -0
  25. package/dist/composables/useTypewriter.d.ts +37 -0
  26. package/dist/index.d.ts +10 -0
  27. package/dist/index.js +1 -2
  28. package/dist/index.js.map +1 -1
  29. package/dist/utils/cursor.d.ts +18 -0
  30. package/package.json +11 -13
  31. package/dist/index.css +0 -6
  32. package/dist/index.css.map +0 -1
  33. package/src/ThemeProvider.vue +0 -41
  34. package/src/components/AutoScrollContainer.vue +0 -164
  35. package/src/components/Incremark.vue +0 -131
  36. package/src/components/IncremarkBlockquote.vue +0 -18
  37. package/src/components/IncremarkCode.vue +0 -236
  38. package/src/components/IncremarkDefault.vue +0 -15
  39. package/src/components/IncremarkFootnotes.vue +0 -78
  40. package/src/components/IncremarkHeading.vue +0 -17
  41. package/src/components/IncremarkHtmlElement.vue +0 -127
  42. package/src/components/IncremarkInline.vue +0 -187
  43. package/src/components/IncremarkList.vue +0 -46
  44. package/src/components/IncremarkMath.vue +0 -105
  45. package/src/components/IncremarkParagraph.vue +0 -14
  46. package/src/components/IncremarkRenderer.vue +0 -50
  47. package/src/components/IncremarkTable.vue +0 -42
  48. package/src/components/IncremarkThematicBreak.vue +0 -8
  49. package/src/composables/index.ts +0 -11
  50. package/src/composables/useBlockTransformer.ts +0 -141
  51. package/src/composables/useDefinationsContext.ts +0 -16
  52. package/src/composables/useDevTools.ts +0 -54
  53. package/src/composables/useIncremark.ts +0 -238
  54. package/src/composables/useProvideDefinations.ts +0 -61
  55. package/src/composables/useStreamRenderer.ts +0 -55
  56. package/src/composables/useTypewriter.ts +0 -205
  57. package/src/index.ts +0 -78
  58. package/src/utils/cursor.ts +0 -46
@@ -1,205 +0,0 @@
1
- /**
2
- * @file useTypewriter Composable - 打字机效果管理
3
- *
4
- * @description
5
- * 管理打字机效果的状态和控制逻辑,从 useIncremark 中拆分出来以简化代码。
6
- *
7
- * @author Incremark Team
8
- * @license MIT
9
- */
10
-
11
- import { ref, shallowRef, computed, watch, onUnmounted, type Ref, type ComputedRef } from 'vue'
12
- import {
13
- createBlockTransformer,
14
- defaultPlugins,
15
- type RootContent,
16
- type ParsedBlock,
17
- type DisplayBlock,
18
- type AnimationEffect,
19
- type BlockTransformer
20
- } from '@incremark/core'
21
- import type { TypewriterOptions, TypewriterControls } from './useIncremark'
22
- import { addCursorToNode } from '../utils/cursor'
23
-
24
- export interface UseTypewriterOptions {
25
- typewriter?: TypewriterOptions
26
- completedBlocks: Ref<ParsedBlock[]>
27
- pendingBlocks: Ref<ParsedBlock[]>
28
- }
29
-
30
- export interface UseTypewriterReturn {
31
- /** 用于渲染的 blocks(经过打字机处理或原始blocks) */
32
- blocks: ComputedRef<Array<ParsedBlock & { stableId: string }>>
33
- /** 打字机控制对象 */
34
- typewriter: TypewriterControls
35
- /** transformer 实例 */
36
- transformer: BlockTransformer<RootContent> | null
37
- }
38
-
39
- /**
40
- * useTypewriter Composable
41
- *
42
- * @description
43
- * 管理打字机效果的所有状态和逻辑。
44
- *
45
- * @param options - 打字机配置和数据
46
- * @returns 打字机状态和控制对象
47
- */
48
- export function useTypewriter(options: UseTypewriterOptions): UseTypewriterReturn {
49
- const { typewriter: typewriterConfig, completedBlocks, pendingBlocks } = options
50
-
51
- // 打字机状态
52
- const typewriterEnabled = ref(typewriterConfig?.enabled ?? !!typewriterConfig)
53
- const displayBlocksRef = shallowRef<DisplayBlock<RootContent>[]>([])
54
- const isTypewriterProcessing = ref(false)
55
- const isTypewriterPaused = ref(false)
56
- const typewriterEffect = ref<AnimationEffect>(typewriterConfig?.effect ?? 'none')
57
- const typewriterCursor = ref(typewriterConfig?.cursor ?? '|')
58
-
59
- // 创建 transformer(如果有 typewriter 配置)
60
- let transformer: BlockTransformer<RootContent> | null = null
61
-
62
- if (typewriterConfig) {
63
- const twOptions = typewriterConfig
64
- transformer = createBlockTransformer<RootContent>({
65
- charsPerTick: twOptions.charsPerTick ?? [1, 3],
66
- tickInterval: twOptions.tickInterval ?? 30,
67
- effect: twOptions.effect ?? 'none',
68
- pauseOnHidden: twOptions.pauseOnHidden ?? true,
69
- plugins: twOptions.plugins ?? defaultPlugins,
70
- onChange: (blocks) => {
71
- displayBlocksRef.value = blocks as DisplayBlock<RootContent>[]
72
- isTypewriterProcessing.value = transformer?.isProcessing() ?? false
73
- isTypewriterPaused.value = transformer?.isPausedState() ?? false
74
- }
75
- })
76
- }
77
-
78
- // 将 completedBlocks 转换为 SourceBlock 格式
79
- const sourceBlocks = computed(() => {
80
- return completedBlocks.value.map(block => ({
81
- id: block.id,
82
- node: block.node,
83
- status: block.status as 'pending' | 'stable' | 'completed'
84
- }))
85
- })
86
-
87
- // 监听 sourceBlocks 变化,推送给 transformer
88
- if (transformer) {
89
- watch(
90
- sourceBlocks,
91
- (blocks) => {
92
- transformer!.push(blocks)
93
-
94
- // 更新正在显示的 block
95
- const currentDisplaying = displayBlocksRef.value.find((b) => !b.isDisplayComplete)
96
- if (currentDisplaying) {
97
- const updated = blocks.find((b) => b.id === currentDisplaying.id)
98
- if (updated) {
99
- transformer!.update(updated)
100
- }
101
- }
102
- },
103
- { immediate: true, deep: true }
104
- )
105
- }
106
-
107
- // 原始 blocks(不经过打字机)
108
- const rawBlocks = computed(() => {
109
- const result: Array<ParsedBlock & { stableId: string }> = []
110
-
111
- for (const block of completedBlocks.value) {
112
- result.push({ ...block, stableId: block.id })
113
- }
114
-
115
- for (let i = 0; i < pendingBlocks.value.length; i++) {
116
- result.push({
117
- ...pendingBlocks.value[i],
118
- stableId: `pending-${i}`
119
- })
120
- }
121
-
122
- return result
123
- })
124
-
125
- // 最终用于渲染的 blocks
126
- const blocks = computed(() => {
127
- // 未启用打字机或没有 transformer:返回原始 blocks
128
- if (!typewriterEnabled.value || !transformer) {
129
- return rawBlocks.value
130
- }
131
-
132
- // 启用打字机:使用 displayBlocks
133
- return displayBlocksRef.value.map((db, index) => {
134
- const isPending = !db.isDisplayComplete
135
- const isLastPending = isPending && index === displayBlocksRef.value.length - 1
136
-
137
- // typing 效果时添加光标
138
- let node = db.displayNode
139
- if (typewriterEffect.value === 'typing' && isLastPending) {
140
- node = addCursorToNode(db.displayNode, typewriterCursor.value)
141
- }
142
-
143
- return {
144
- id: db.id,
145
- stableId: db.id,
146
- status: (db.isDisplayComplete ? 'completed' : 'pending') as 'pending' | 'stable' | 'completed',
147
- isLastPending,
148
- node,
149
- startOffset: 0,
150
- endOffset: 0,
151
- rawText: ''
152
- }
153
- })
154
- })
155
-
156
- // 打字机控制对象
157
- const typewriterControls: TypewriterControls = {
158
- enabled: computed(() => typewriterEnabled.value),
159
- setEnabled: (value: boolean) => {
160
- typewriterEnabled.value = value
161
- },
162
- isProcessing: computed(() => isTypewriterProcessing.value),
163
- isPaused: computed(() => isTypewriterPaused.value),
164
- effect: computed(() => typewriterEffect.value),
165
- skip: () => transformer?.skip(),
166
- pause: () => {
167
- transformer?.pause()
168
- isTypewriterPaused.value = true
169
- },
170
- resume: () => {
171
- transformer?.resume()
172
- isTypewriterPaused.value = false
173
- },
174
- setOptions: (opts) => {
175
- if (opts.enabled !== undefined) {
176
- typewriterEnabled.value = opts.enabled
177
- }
178
- if (opts.charsPerTick !== undefined || opts.tickInterval !== undefined || opts.effect !== undefined || opts.pauseOnHidden !== undefined) {
179
- transformer?.setOptions({
180
- charsPerTick: opts.charsPerTick,
181
- tickInterval: opts.tickInterval,
182
- effect: opts.effect,
183
- pauseOnHidden: opts.pauseOnHidden
184
- })
185
- }
186
- if (opts.effect !== undefined) {
187
- typewriterEffect.value = opts.effect
188
- }
189
- if (opts.cursor !== undefined) {
190
- typewriterCursor.value = opts.cursor
191
- }
192
- }
193
- }
194
-
195
- // 清理
196
- onUnmounted(() => {
197
- transformer?.destroy()
198
- })
199
-
200
- return {
201
- blocks,
202
- typewriter: typewriterControls,
203
- transformer
204
- }
205
- }
package/src/index.ts DELETED
@@ -1,78 +0,0 @@
1
- // Composables
2
- export { useIncremark, useStreamRenderer, useDevTools, useBlockTransformer } from './composables'
3
- export { useProvideDefinations } from './composables/useProvideDefinations'
4
- export { useDefinationsContext } from './composables/useDefinationsContext'
5
- export type {
6
- UseIncremarkOptions,
7
- TypewriterOptions,
8
- TypewriterControls,
9
- UseStreamRendererOptions,
10
- UseDevToolsOptions,
11
- UseBlockTransformerOptions,
12
- UseBlockTransformerReturn
13
- } from './composables'
14
-
15
- // Components
16
- export {
17
- Incremark,
18
- IncremarkRenderer,
19
- IncremarkHeading,
20
- IncremarkParagraph,
21
- IncremarkCode,
22
- IncremarkList,
23
- IncremarkTable,
24
- IncremarkBlockquote,
25
- IncremarkThematicBreak,
26
- IncremarkInline,
27
- IncremarkMath,
28
- IncremarkHtmlElement,
29
- IncremarkDefault,
30
- IncremarkFootnotes,
31
- AutoScrollContainer
32
- } from './components'
33
- export type { ComponentMap, BlockWithStableId } from './components'
34
- export { default as ThemeProvider } from './ThemeProvider.vue'
35
-
36
- // Re-export core types
37
- export type {
38
- ParsedBlock,
39
- IncrementalUpdate,
40
- ParserOptions,
41
- BlockStatus,
42
- Root,
43
- RootContent,
44
- // Transformer types
45
- SourceBlock,
46
- DisplayBlock,
47
- TransformerPlugin,
48
- TransformerOptions,
49
- TransformerState,
50
- AnimationEffect
51
- } from '@incremark/core'
52
-
53
- // Re-export transformer utilities and plugins
54
- export {
55
- BlockTransformer,
56
- createBlockTransformer,
57
- countChars,
58
- sliceAst,
59
- cloneNode,
60
- codeBlockPlugin,
61
- mermaidPlugin,
62
- imagePlugin,
63
- mathPlugin,
64
- thematicBreakPlugin,
65
- defaultPlugins,
66
- allPlugins,
67
- createPlugin
68
- } from '@incremark/core'
69
-
70
- // Re-export theme utilities
71
- export {
72
- type DesignTokens,
73
- defaultTheme,
74
- darkTheme,
75
- generateCSSVars,
76
- mergeTheme,
77
- applyTheme
78
- } from '@incremark/theme'
@@ -1,46 +0,0 @@
1
- /**
2
- * @file Cursor Utils - 光标工具函数
3
- *
4
- * @description
5
- * 用于在 AST 节点末尾添加光标的工具函数。
6
- *
7
- * @author Incremark Team
8
- * @license MIT
9
- */
10
-
11
- import type { RootContent } from '@incremark/core'
12
-
13
- /**
14
- * 在节点末尾添加光标
15
- *
16
- * @param node - 要添加光标的节点
17
- * @param cursor - 光标字符
18
- * @returns 添加了光标的新节点
19
- */
20
- export function addCursorToNode(node: RootContent, cursor: string): RootContent {
21
- const cloned = JSON.parse(JSON.stringify(node))
22
-
23
- function addToLast(n: { children?: unknown[]; type?: string; value?: string }): boolean {
24
- if (n.children && n.children.length > 0) {
25
- for (let i = n.children.length - 1; i >= 0; i--) {
26
- if (addToLast(n.children[i] as { children?: unknown[]; type?: string; value?: string })) {
27
- return true
28
- }
29
- }
30
- n.children.push({ type: 'text', value: cursor })
31
- return true
32
- }
33
- if (n.type === 'text' && typeof n.value === 'string') {
34
- n.value += cursor
35
- return true
36
- }
37
- if (typeof n.value === 'string') {
38
- n.value += cursor
39
- return true
40
- }
41
- return false
42
- }
43
-
44
- addToLast(cloned)
45
- return cloned
46
- }