@prosekit/core 0.9.0 → 0.10.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 (123) hide show
  1. package/dist/{editor-4lgGc3CY.d.ts → editor-BULC1zqX.d.ts} +12 -12
  2. package/dist/editor-BULC1zqX.d.ts.map +1 -0
  3. package/dist/{editor-DGNUXn-u.js → editor-g-Rqn-ZE.js} +30 -30
  4. package/dist/editor-g-Rqn-ZE.js.map +1 -0
  5. package/dist/prosekit-core-test.d.ts +1 -2
  6. package/dist/prosekit-core-test.d.ts.map +1 -1
  7. package/dist/prosekit-core-test.js +1 -1
  8. package/dist/prosekit-core-test.js.map +1 -1
  9. package/dist/prosekit-core.d.ts +103 -24
  10. package/dist/prosekit-core.d.ts.map +1 -1
  11. package/dist/prosekit-core.js +116 -35
  12. package/dist/prosekit-core.js.map +1 -1
  13. package/package.json +6 -7
  14. package/src/commands/add-mark.ts +1 -4
  15. package/src/commands/expand-mark.ts +2 -9
  16. package/src/commands/insert-default-block.spec.ts +1 -5
  17. package/src/commands/insert-default-block.ts +1 -4
  18. package/src/commands/insert-node.ts +4 -8
  19. package/src/commands/remove-mark.ts +1 -4
  20. package/src/commands/remove-node.ts +2 -2
  21. package/src/commands/select-block.spec.ts +4 -6
  22. package/src/commands/select-block.ts +1 -4
  23. package/src/commands/set-block-type.ts +1 -4
  24. package/src/commands/set-node-attrs-between.spec.ts +221 -0
  25. package/src/commands/set-node-attrs-between.ts +77 -0
  26. package/src/commands/set-node-attrs.spec.ts +129 -0
  27. package/src/commands/set-node-attrs.ts +25 -26
  28. package/src/commands/toggle-mark.ts +1 -4
  29. package/src/commands/toggle-node.ts +2 -5
  30. package/src/commands/toggle-wrap.spec.ts +1 -5
  31. package/src/commands/toggle-wrap.ts +1 -4
  32. package/src/commands/unset-block-type.spec.ts +1 -5
  33. package/src/commands/unset-block-type.ts +2 -8
  34. package/src/commands/unset-mark.spec.ts +1 -5
  35. package/src/commands/wrap.ts +1 -4
  36. package/src/editor/action.spec.ts +2 -6
  37. package/src/editor/action.ts +1 -8
  38. package/src/editor/editor.spec.ts +2 -9
  39. package/src/editor/editor.ts +11 -45
  40. package/src/editor/union.spec.ts +1 -5
  41. package/src/editor/union.ts +1 -4
  42. package/src/extensions/clipboard-serializer.ts +4 -16
  43. package/src/extensions/command.ts +16 -48
  44. package/src/extensions/default-state.spec.ts +1 -5
  45. package/src/extensions/default-state.ts +2 -8
  46. package/src/extensions/events/dom-event.spec.ts +1 -6
  47. package/src/extensions/events/dom-event.ts +4 -16
  48. package/src/extensions/events/editor-event.ts +4 -16
  49. package/src/extensions/events/focus.spec.ts +1 -6
  50. package/src/extensions/events/plugin-view.ts +2 -9
  51. package/src/extensions/history.ts +2 -9
  52. package/src/extensions/keymap-base.spec.ts +2 -11
  53. package/src/extensions/keymap-base.ts +1 -4
  54. package/src/extensions/keymap.spec.ts +6 -20
  55. package/src/extensions/keymap.ts +3 -13
  56. package/src/extensions/mark-spec.spec.ts +5 -20
  57. package/src/extensions/mark-spec.ts +4 -15
  58. package/src/extensions/mark-view-effect.ts +3 -9
  59. package/src/extensions/mark-view.ts +2 -8
  60. package/src/extensions/node-spec.spec.ts +5 -21
  61. package/src/extensions/node-spec.ts +4 -15
  62. package/src/extensions/node-view-effect.ts +3 -9
  63. package/src/extensions/node-view.ts +2 -8
  64. package/src/extensions/plugin.spec.ts +3 -16
  65. package/src/extensions/plugin.ts +3 -12
  66. package/src/facets/base-extension.ts +1 -4
  67. package/src/facets/command.ts +2 -8
  68. package/src/facets/facet-extension.spec.ts +4 -15
  69. package/src/facets/facet-node.spec.ts +2 -9
  70. package/src/facets/facet-node.ts +4 -9
  71. package/src/facets/facet.spec.ts +1 -4
  72. package/src/facets/schema-spec.ts +2 -9
  73. package/src/facets/schema.ts +3 -12
  74. package/src/facets/state.spec.ts +2 -9
  75. package/src/facets/state.ts +4 -18
  76. package/src/facets/union-extension.ts +2 -8
  77. package/src/index.ts +40 -166
  78. package/src/test/index.ts +1 -4
  79. package/src/test/test-builder.ts +1 -4
  80. package/src/test/test-editor.spec.ts +1 -5
  81. package/src/test/test-editor.ts +5 -24
  82. package/src/testing/index.ts +18 -14
  83. package/src/types/extension.spec.ts +1 -4
  84. package/src/types/extension.ts +3 -13
  85. package/src/types/simplify-union.ts +1 -4
  86. package/src/utils/array-grouping.spec.ts +1 -4
  87. package/src/utils/attrs-match.ts +1 -5
  88. package/src/utils/clsx.spec.ts +1 -4
  89. package/src/utils/combine-event-handlers.spec.ts +1 -5
  90. package/src/utils/default-block-at.ts +1 -4
  91. package/src/utils/editor-content.spec.ts +1 -4
  92. package/src/utils/editor-content.ts +4 -16
  93. package/src/utils/find-node.ts +65 -0
  94. package/src/utils/find-parent-node-of-type.ts +6 -12
  95. package/src/utils/find-parent-node.spec.ts +1 -5
  96. package/src/utils/find-parent-node.ts +1 -4
  97. package/src/utils/get-custom-selection.ts +1 -5
  98. package/src/utils/get-mark-type.ts +1 -4
  99. package/src/utils/get-node-type.ts +1 -4
  100. package/src/utils/get-node-types.ts +1 -4
  101. package/src/utils/includes-mark.ts +1 -5
  102. package/src/utils/is-at-block-start.ts +1 -4
  103. package/src/utils/is-mark-absent.spec.ts +1 -4
  104. package/src/utils/is-mark-absent.ts +1 -5
  105. package/src/utils/is-mark-active.ts +1 -4
  106. package/src/utils/is-node-active.spec.ts +109 -0
  107. package/src/utils/is-node-active.ts +17 -8
  108. package/src/utils/is-subset.spec.ts +1 -4
  109. package/src/utils/maybe-run.spec.ts +1 -5
  110. package/src/utils/merge-objects.spec.ts +1 -4
  111. package/src/utils/merge-objects.ts +2 -1
  112. package/src/utils/merge-specs.ts +1 -4
  113. package/src/utils/object-equal.spec.ts +1 -4
  114. package/src/utils/output-spec.test.ts +1 -5
  115. package/src/utils/output-spec.ts +2 -10
  116. package/src/utils/parse.spec.ts +2 -11
  117. package/src/utils/parse.ts +3 -15
  118. package/src/utils/remove-undefined-values.spec.ts +1 -4
  119. package/src/utils/set-selection-around.ts +1 -4
  120. package/src/utils/type-assertion.ts +2 -21
  121. package/src/utils/unicode.spec.ts +1 -4
  122. package/dist/editor-4lgGc3CY.d.ts.map +0 -1
  123. package/dist/editor-DGNUXn-u.js.map +0 -1
package/src/index.ts CHANGED
@@ -1,98 +1,30 @@
1
- export {
2
- addMark,
3
- type AddMarkOptions,
4
- } from './commands/add-mark'
5
- export {
6
- expandMark,
7
- type ExpandMarkOptions,
8
- } from './commands/expand-mark'
9
- export {
10
- insertDefaultBlock,
11
- type InsertDefaultBlockOptions,
12
- } from './commands/insert-default-block'
13
- export {
14
- insertNode,
15
- type InsertNodeOptions,
16
- } from './commands/insert-node'
17
- export {
18
- removeMark,
19
- type RemoveMarkOptions,
20
- } from './commands/remove-mark'
21
- export {
22
- removeNode,
23
- type RemoveNodeOptions,
24
- } from './commands/remove-node'
1
+ export { addMark, type AddMarkOptions } from './commands/add-mark'
2
+ export { expandMark, type ExpandMarkOptions } from './commands/expand-mark'
3
+ export { insertDefaultBlock, type InsertDefaultBlockOptions } from './commands/insert-default-block'
4
+ export { insertNode, type InsertNodeOptions } from './commands/insert-node'
5
+ export { removeMark, type RemoveMarkOptions } from './commands/remove-mark'
6
+ export { removeNode, type RemoveNodeOptions } from './commands/remove-node'
25
7
  export { selectAll } from './commands/select-all'
26
8
  export { selectBlock } from './commands/select-block'
27
- export {
28
- setBlockType,
29
- type SetBlockTypeOptions,
30
- } from './commands/set-block-type'
31
- export {
32
- setNodeAttrs,
33
- type SetNodeAttrsOptions,
34
- } from './commands/set-node-attrs'
35
- export {
36
- toggleMark,
37
- type ToggleMarkOptions,
38
- } from './commands/toggle-mark'
39
- export {
40
- toggleNode,
41
- type ToggleNodeOptions,
42
- } from './commands/toggle-node'
43
- export {
44
- toggleWrap,
45
- type ToggleWrapOptions,
46
- } from './commands/toggle-wrap'
47
- export {
48
- unsetBlockType,
49
- type UnsetBlockTypeOptions,
50
- } from './commands/unset-block-type'
51
- export {
52
- unsetMark,
53
- type UnsetMarkOptions,
54
- } from './commands/unset-mark'
55
- export {
56
- wrap,
57
- type WrapOptions,
58
- } from './commands/wrap'
59
- export type {
60
- MarkAction,
61
- NodeAction,
62
- NodeChild,
63
- } from './editor/action'
64
- export {
65
- createEditor,
66
- Editor,
67
- type EditorOptions,
68
- } from './editor/editor'
9
+ export { setBlockType, type SetBlockTypeOptions } from './commands/set-block-type'
10
+ export { setNodeAttrs, type SetNodeAttrsOptions } from './commands/set-node-attrs'
11
+ export { setNodeAttrsBetween, type SetNodeAttrsBetweenOptions } from './commands/set-node-attrs-between'
12
+ export { toggleMark, type ToggleMarkOptions } from './commands/toggle-mark'
13
+ export { toggleNode, type ToggleNodeOptions } from './commands/toggle-node'
14
+ export { toggleWrap, type ToggleWrapOptions } from './commands/toggle-wrap'
15
+ export { unsetBlockType, type UnsetBlockTypeOptions } from './commands/unset-block-type'
16
+ export { unsetMark, type UnsetMarkOptions } from './commands/unset-mark'
17
+ export { wrap, type WrapOptions } from './commands/wrap'
18
+ export type { MarkAction, NodeAction, NodeChild } from './editor/action'
19
+ export { createEditor, Editor, type EditorOptions } from './editor/editor'
69
20
  export { union } from './editor/union'
70
21
  export { withPriority } from './editor/with-priority'
71
- export {
72
- EditorNotFoundError,
73
- ProseKitError,
74
- } from './error'
75
- export {
76
- defineClipboardSerializer,
77
- type ClipboardSerializerOptions,
78
- } from './extensions/clipboard-serializer'
79
- export {
80
- defineBaseCommands,
81
- defineCommands,
82
- type BaseCommandsExtension,
83
- } from './extensions/command'
84
- export {
85
- defineDefaultState,
86
- type DefaultStateOptions,
87
- } from './extensions/default-state'
88
- export {
89
- defineDocChangeHandler,
90
- type DocChangeHandler,
91
- } from './extensions/events/doc-change'
92
- export {
93
- defineDOMEventHandler,
94
- type DOMEventHandler,
95
- } from './extensions/events/dom-event'
22
+ export { EditorNotFoundError, ProseKitError } from './error'
23
+ export { defineClipboardSerializer, type ClipboardSerializerOptions } from './extensions/clipboard-serializer'
24
+ export { defineBaseCommands, defineCommands, type BaseCommandsExtension } from './extensions/command'
25
+ export { defineDefaultState, type DefaultStateOptions } from './extensions/default-state'
26
+ export { defineDocChangeHandler, type DocChangeHandler } from './extensions/events/doc-change'
27
+ export { defineDOMEventHandler, type DOMEventHandler } from './extensions/events/dom-event'
96
28
  export {
97
29
  defineClickHandler,
98
30
  defineClickOnHandler,
@@ -121,10 +53,7 @@ export {
121
53
  type TripleClickHandler,
122
54
  type TripleClickOnHandler,
123
55
  } from './extensions/events/editor-event'
124
- export {
125
- defineFocusChangeHandler,
126
- type FocusChangeHandler,
127
- } from './extensions/events/focus'
56
+ export { defineFocusChangeHandler, type FocusChangeHandler } from './extensions/events/focus'
128
57
  export {
129
58
  defineMountHandler,
130
59
  defineUnmountHandler,
@@ -133,69 +62,30 @@ export {
133
62
  type UnmountHandler,
134
63
  type UpdateHandler,
135
64
  } from './extensions/events/plugin-view'
136
- export {
137
- defineHistory,
138
- type HistoryExtension,
139
- type HistoryOptions,
140
- } from './extensions/history'
141
- export {
142
- defineKeymap,
143
- keymapFacet,
144
- type Keymap,
145
- type KeymapPayload,
146
- } from './extensions/keymap'
147
- export {
148
- defineBaseKeymap,
149
- type BaseKeymapExtension,
150
- type BaseKeymapOptions,
151
- } from './extensions/keymap-base'
152
- export {
153
- defineMarkAttr,
154
- defineMarkSpec,
155
- type MarkAttrOptions,
156
- type MarkSpecOptions,
157
- } from './extensions/mark-spec'
158
- export {
159
- defineMarkView,
160
- type MarkViewOptions,
161
- } from './extensions/mark-view'
65
+ export { defineHistory, type HistoryExtension, type HistoryOptions } from './extensions/history'
66
+ export { defineKeymap, keymapFacet, type Keymap, type KeymapPayload } from './extensions/keymap'
67
+ export { defineBaseKeymap, type BaseKeymapExtension, type BaseKeymapOptions } from './extensions/keymap-base'
68
+ export { defineMarkAttr, defineMarkSpec, type MarkAttrOptions, type MarkSpecOptions } from './extensions/mark-spec'
69
+ export { defineMarkView, type MarkViewOptions } from './extensions/mark-view'
162
70
  export {
163
71
  defineMarkViewComponent,
164
72
  defineMarkViewFactory,
165
73
  type MarkViewComponentOptions,
166
74
  type MarkViewFactoryOptions,
167
75
  } from './extensions/mark-view-effect'
168
- export {
169
- defineNodeAttr,
170
- defineNodeSpec,
171
- type NodeAttrOptions,
172
- type NodeSpecOptions,
173
- } from './extensions/node-spec'
174
- export {
175
- defineNodeView,
176
- type NodeViewOptions,
177
- } from './extensions/node-view'
76
+ export { defineNodeAttr, defineNodeSpec, type NodeAttrOptions, type NodeSpecOptions } from './extensions/node-spec'
77
+ export { defineNodeView, type NodeViewOptions } from './extensions/node-view'
178
78
  export {
179
79
  defineNodeViewComponent,
180
80
  defineNodeViewFactory,
181
81
  type NodeViewComponentOptions,
182
82
  type NodeViewFactoryOptions,
183
83
  } from './extensions/node-view-effect'
184
- export {
185
- definePlugin,
186
- pluginFacet,
187
- type PluginPayload,
188
- } from './extensions/plugin'
189
- export {
190
- defineFacet,
191
- type Facet,
192
- } from './facets/facet'
84
+ export { definePlugin, pluginFacet, type PluginPayload } from './extensions/plugin'
85
+ export { defineFacet, type Facet } from './facets/facet'
193
86
  export { defineFacetPayload } from './facets/facet-extension'
194
87
  export type { AnyFunction } from './types/any-function'
195
- export type {
196
- AnyAttrs,
197
- AttrSpec,
198
- } from './types/attrs'
88
+ export type { AnyAttrs, AttrSpec } from './types/attrs'
199
89
  export type {
200
90
  Extension,
201
91
  ExtensionTyping,
@@ -210,24 +100,10 @@ export type {
210
100
  PlainExtension,
211
101
  Union,
212
102
  } from './types/extension'
213
- export type {
214
- CommandAction,
215
- CommandTyping,
216
- } from './types/extension-command'
217
- export type {
218
- MarkTyping,
219
- ToMarkAction,
220
- } from './types/extension-mark'
221
- export type {
222
- NodeTyping,
223
- ToNodeAction,
224
- } from './types/extension-node'
225
- export type {
226
- NodeJSON,
227
- SelectionJSON,
228
- StateJSON,
229
- StepJSON,
230
- } from './types/model'
103
+ export type { CommandAction, CommandTyping } from './types/extension-command'
104
+ export type { MarkTyping, ToMarkAction } from './types/extension-mark'
105
+ export type { NodeTyping, ToNodeAction } from './types/extension-node'
106
+ export type { NodeJSON, SelectionJSON, StateJSON, StepJSON } from './types/model'
231
107
  export type { PickSubType } from './types/pick-sub-type'
232
108
  export { Priority } from './types/priority'
233
109
  export type { SimplifyDeeper } from './types/simplify-deeper'
@@ -238,10 +114,8 @@ export { clsx } from './utils/clsx'
238
114
  export { containsInlineNode } from './utils/contains-inline-node'
239
115
  export { defaultBlockAt } from './utils/default-block-at'
240
116
  export { isApple } from './utils/env'
241
- export {
242
- findParentNode,
243
- type FindParentNodeResult,
244
- } from './utils/find-parent-node'
117
+ export { findNode, findNodes, type FindNodeResult } from './utils/find-node'
118
+ export { findParentNode, type FindParentNodeResult } from './utils/find-parent-node'
245
119
  export { findParentNodeOfType } from './utils/find-parent-node-of-type'
246
120
  export { getMarkType } from './utils/get-mark-type'
247
121
  export { getNodeType } from './utils/get-node-type'
package/src/test/index.ts CHANGED
@@ -1,4 +1 @@
1
- export {
2
- createTestEditor,
3
- type TestEditor,
4
- } from './test-editor'
1
+ export { createTestEditor, type TestEditor } from './test-editor'
@@ -1,9 +1,6 @@
1
1
  import type { ProseMirrorNode } from '@prosekit/pm/model'
2
2
 
3
- import type {
4
- ApplyMarkFunction,
5
- CreateNodeFunction,
6
- } from '../editor/action'
3
+ import type { ApplyMarkFunction, CreateNodeFunction } from '../editor/action'
7
4
  import { assert } from '../utils/assert'
8
5
 
9
6
  type Tags = Record<string, number>
@@ -1,8 +1,4 @@
1
- import {
2
- describe,
3
- expect,
4
- it,
5
- } from 'vitest'
1
+ import { describe, expect, it } from 'vitest'
6
2
 
7
3
  import { setupTest } from '../testing'
8
4
 
@@ -1,32 +1,13 @@
1
1
  import type { ProseMirrorNode } from '@prosekit/pm/model'
2
- import {
3
- NodeSelection,
4
- TextSelection,
5
- type Selection,
6
- } from '@prosekit/pm/state'
2
+ import { NodeSelection, TextSelection, type Selection } from '@prosekit/pm/state'
7
3
 
8
- import {
9
- createMarkActions,
10
- createNodeActions,
11
- } from '../editor/action'
12
- import {
13
- Editor,
14
- EditorInstance,
15
- setupEditorExtension,
16
- type EditorOptions,
17
- } from '../editor/editor'
4
+ import { createMarkActions, createNodeActions } from '../editor/action'
5
+ import { Editor, EditorInstance, setupEditorExtension, type EditorOptions } from '../editor/editor'
18
6
  import type { Extension } from '../types/extension'
19
- import type {
20
- NodeJSON,
21
- SelectionJSON,
22
- } from '../types/model'
7
+ import type { NodeJSON, SelectionJSON } from '../types/model'
23
8
  import { isProseMirrorNode } from '../utils/type-assertion'
24
9
 
25
- import {
26
- applyMarkForTest,
27
- createNodeForTest,
28
- type TaggedProseMirrorNode,
29
- } from './test-builder'
10
+ import { applyMarkForTest, createNodeForTest, type TaggedProseMirrorNode } from './test-builder'
30
11
 
31
12
  function maybeResolve(doc: ProseMirrorNode, pos?: number) {
32
13
  if (pos != null) {
@@ -9,15 +9,8 @@ import { defineHistory } from '../extensions/history'
9
9
  import { defineBaseKeymap } from '../extensions/keymap-base'
10
10
  import { defineMarkSpec } from '../extensions/mark-spec'
11
11
  import { defineNodeSpec } from '../extensions/node-spec'
12
- import {
13
- createTestEditor,
14
- type TestEditor,
15
- } from '../test'
16
- import type {
17
- Extension,
18
- ExtractMarkActions,
19
- ExtractNodeActions,
20
- } from '../types/extension'
12
+ import { createTestEditor, type TestEditor } from '../test'
13
+ import type { Extension, ExtractMarkActions, ExtractNodeActions } from '../types/extension'
21
14
  import { Priority } from '../types/priority'
22
15
 
23
16
  type DocExtension = Extension<{ Nodes: { doc: Attrs } }>
@@ -182,7 +175,7 @@ function defineHeading(): HeadingExtension {
182
175
 
183
176
  type CodeBlockExtension = Extension<{
184
177
  Nodes: {
185
- codeBlock: { language: string }
178
+ codeBlock: { language: string; lineNumbers?: boolean }
186
179
  }
187
180
  }>
188
181
 
@@ -197,16 +190,23 @@ function defineCodeBlock(): CodeBlockExtension {
197
190
  code: true,
198
191
  defining: true,
199
192
  marks: '',
200
- attrs: { language: { default: '', validate: 'string' } },
193
+ attrs: {
194
+ language: { default: '', validate: 'string' },
195
+ lineNumbers: { default: false, validate: 'boolean' },
196
+ },
201
197
  toDOM() {
202
198
  return ['pre', ['code', 0]]
203
199
  },
204
200
  })
205
201
  }
206
202
 
203
+ interface BlockquoteAttrs {
204
+ variant: string
205
+ }
206
+
207
207
  type BlockquoteExtension = Extension<{
208
208
  Nodes: {
209
- blockquote: Attrs
209
+ blockquote: BlockquoteAttrs
210
210
  }
211
211
  }>
212
212
 
@@ -219,9 +219,13 @@ function defineBlockquote(): BlockquoteExtension {
219
219
  content: 'block+',
220
220
  group: 'block',
221
221
  defining: true,
222
+ attrs: {
223
+ variant: { default: '', validate: 'string' },
224
+ },
222
225
  parseDOM: [{ tag: 'blockquote' }],
223
- toDOM() {
224
- return ['blockquote', 0]
226
+ toDOM(node) {
227
+ const { variant } = node.attrs as BlockquoteAttrs
228
+ return ['blockquote', { 'data-variant': variant }, 0]
225
229
  },
226
230
  })
227
231
  }
@@ -1,10 +1,7 @@
1
1
  import { test } from 'vitest'
2
2
 
3
3
  import { assertTypeEqual } from './assert-type-equal'
4
- import type {
5
- Extension,
6
- Union,
7
- } from './extension'
4
+ import type { Extension, Union } from './extension'
8
5
 
9
6
  test('ExtractTyping', () => {
10
7
  type E1 = Extension<{
@@ -1,18 +1,8 @@
1
1
  import type { Schema } from '@prosekit/pm/model'
2
2
 
3
- import type {
4
- CommandTyping,
5
- ToCommandAction,
6
- ToCommandCreators,
7
- } from './extension-command'
8
- import type {
9
- MarkTyping,
10
- ToMarkAction,
11
- } from './extension-mark'
12
- import type {
13
- NodeTyping,
14
- ToNodeAction,
15
- } from './extension-node'
3
+ import type { CommandTyping, ToCommandAction, ToCommandCreators } from './extension-command'
4
+ import type { MarkTyping, ToMarkAction } from './extension-mark'
5
+ import type { NodeTyping, ToNodeAction } from './extension-node'
16
6
  import type { PickStringLiteral } from './pick-string-literal'
17
7
  import type { PickSubType } from './pick-sub-type'
18
8
  import type { Priority } from './priority'
@@ -1,7 +1,4 @@
1
- import type {
2
- Simplify,
3
- UnionToIntersection,
4
- } from 'type-fest'
1
+ import type { Simplify, UnionToIntersection } from 'type-fest'
5
2
 
6
3
  /**
7
4
  * @internal
@@ -1,7 +1,4 @@
1
- import {
2
- expect,
3
- test,
4
- } from 'vitest'
1
+ import { expect, test } from 'vitest'
5
2
 
6
3
  import { groupEntries } from './array-grouping'
7
4
 
@@ -1,8 +1,4 @@
1
- import type {
2
- Attrs,
3
- Mark,
4
- ProseMirrorNode,
5
- } from '@prosekit/pm/model'
1
+ import type { Attrs, Mark, ProseMirrorNode } from '@prosekit/pm/model'
6
2
 
7
3
  export function attrsMatch(
8
4
  nodeOrMark: ProseMirrorNode | Mark,
@@ -1,7 +1,4 @@
1
- import {
2
- expect,
3
- test,
4
- } from 'vitest'
1
+ import { expect, test } from 'vitest'
5
2
 
6
3
  import { clsx } from './clsx'
7
4
 
@@ -1,8 +1,4 @@
1
- import {
2
- expect,
3
- test,
4
- vi,
5
- } from 'vitest'
1
+ import { expect, test, vi } from 'vitest'
6
2
 
7
3
  import { combineEventHandlers } from './combine-event-handlers'
8
4
 
@@ -1,7 +1,4 @@
1
- import type {
2
- ContentMatch,
3
- NodeType,
4
- } from '@prosekit/pm/model'
1
+ import type { ContentMatch, NodeType } from '@prosekit/pm/model'
5
2
 
6
3
  /**
7
4
  * @internal
@@ -1,8 +1,5 @@
1
1
  import { AllSelection } from '@prosekit/pm/state'
2
- import {
3
- expect,
4
- test,
5
- } from 'vitest'
2
+ import { expect, test } from 'vitest'
6
3
 
7
4
  import { setupTest } from '../testing'
8
5
 
@@ -1,24 +1,12 @@
1
1
  import { isElementLike } from '@ocavue/utils'
2
- import type {
3
- ProseMirrorNode,
4
- Schema,
5
- } from '@prosekit/pm/model'
2
+ import type { ProseMirrorNode, Schema } from '@prosekit/pm/model'
6
3
  import { Selection } from '@prosekit/pm/state'
7
4
 
8
- import type {
9
- NodeJSON,
10
- SelectionJSON,
11
- } from '../types/model'
5
+ import type { NodeJSON, SelectionJSON } from '../types/model'
12
6
 
13
7
  import { assert } from './assert'
14
- import {
15
- jsonFromElement,
16
- jsonFromHTML,
17
- } from './parse'
18
- import {
19
- isProseMirrorNode,
20
- isSelection,
21
- } from './type-assertion'
8
+ import { jsonFromElement, jsonFromHTML } from './parse'
9
+ import { isProseMirrorNode, isSelection } from './type-assertion'
22
10
 
23
11
  export function getEditorContentJSON(
24
12
  schema: Schema,
@@ -0,0 +1,65 @@
1
+ import type { ProseMirrorNode } from '@prosekit/pm/model'
2
+
3
+ /**
4
+ * Finds the first node that satisfies the predicate from the given document.
5
+ *
6
+ * @internal
7
+ */
8
+ export function findNode(
9
+ doc: ProseMirrorNode,
10
+ predicate: (node: ProseMirrorNode) => boolean,
11
+ ): FindNodeResult | undefined {
12
+ let found: FindNodeResult | undefined
13
+ doc.descendants((node, pos, parent, index) => {
14
+ if (found) {
15
+ return false
16
+ }
17
+ if (predicate(node)) {
18
+ found = { node, pos, parent, index }
19
+ return false
20
+ }
21
+ })
22
+ return found
23
+ }
24
+
25
+ /**
26
+ * Finds all nodes that satisfy the predicate from the given document.
27
+ *
28
+ * @internal
29
+ */
30
+ export function findNodes(
31
+ doc: ProseMirrorNode,
32
+ predicate: (node: ProseMirrorNode) => boolean,
33
+ ): FindNodeResult[] {
34
+ const results: FindNodeResult[] = []
35
+ doc.descendants((node, pos, parent, index) => {
36
+ if (predicate(node)) {
37
+ results.push({ node, pos, parent, index })
38
+ }
39
+ })
40
+ return results
41
+ }
42
+
43
+ /**
44
+ * The result of the {@link findNode} function.
45
+ *
46
+ * @internal
47
+ */
48
+ export interface FindNodeResult {
49
+ /**
50
+ * The node that satisfies the predicate.
51
+ */
52
+ node: ProseMirrorNode
53
+ /**
54
+ * The position of the node.
55
+ */
56
+ pos: number
57
+ /**
58
+ * The parent of the node.
59
+ */
60
+ parent: ProseMirrorNode | null
61
+ /**
62
+ * The index of the node in the parent.
63
+ */
64
+ index: number
65
+ }
@@ -1,13 +1,7 @@
1
- import type {
2
- NodeType,
3
- ResolvedPos,
4
- } from '@prosekit/pm/model'
1
+ import type { NodeType, ResolvedPos } from '@prosekit/pm/model'
5
2
 
6
- import {
7
- findParentNode,
8
- type FindParentNodeResult,
9
- } from './find-parent-node'
10
- import { getNodeType } from './get-node-type'
3
+ import { findParentNode, type FindParentNodeResult } from './find-parent-node'
4
+ import { getNodeTypes } from './get-node-types'
11
5
 
12
6
  /**
13
7
  * Finds the closest parent node that matches the given node type.
@@ -18,12 +12,12 @@ export function findParentNodeOfType(
18
12
  /**
19
13
  * The type of the node to find.
20
14
  */
21
- type: NodeType | string,
15
+ type: string | NodeType | string[] | NodeType[],
22
16
  /**
23
17
  * The position to start searching from.
24
18
  */
25
19
  $pos: ResolvedPos,
26
20
  ): FindParentNodeResult | undefined {
27
- const nodeType = getNodeType($pos.doc.type.schema, type)
28
- return findParentNode((node) => node.type === nodeType, $pos)
21
+ const nodeTypes = getNodeTypes($pos.doc.type.schema, type)
22
+ return findParentNode((node) => nodeTypes.includes(node.type), $pos)
29
23
  }
@@ -1,8 +1,4 @@
1
- import {
2
- describe,
3
- expect,
4
- it,
5
- } from 'vitest'
1
+ import { describe, expect, it } from 'vitest'
6
2
 
7
3
  import { setupTest } from '../testing'
8
4
 
@@ -1,7 +1,4 @@
1
- import type {
2
- ProseMirrorNode,
3
- ResolvedPos,
4
- } from '@prosekit/pm/model'
1
+ import type { ProseMirrorNode, ResolvedPos } from '@prosekit/pm/model'
5
2
 
6
3
  /**
7
4
  * @public
@@ -1,8 +1,4 @@
1
- import {
2
- TextSelection,
3
- type EditorState,
4
- type Selection,
5
- } from '@prosekit/pm/state'
1
+ import { TextSelection, type EditorState, type Selection } from '@prosekit/pm/state'
6
2
 
7
3
  export function getCustomSelection(
8
4
  state: EditorState,
@@ -1,7 +1,4 @@
1
- import type {
2
- MarkType,
3
- Schema,
4
- } from '@prosekit/pm/model'
1
+ import type { MarkType, Schema } from '@prosekit/pm/model'
5
2
 
6
3
  import { ProseKitError } from '../error'
7
4