|
9 | 9 | orderedListSchema,
|
10 | 10 | paragraphSchema,
|
11 | 11 | } from '@milkdown/kit/preset/commonmark'
|
12 |
| -import { NodeSelection } from '@milkdown/kit/prose/state' |
| 12 | +import { TextSelection } from '@milkdown/kit/prose/state' |
13 | 13 | import { imageBlockSchema } from '@milkdown/kit/component/image-block'
|
14 | 14 | import { createTable } from '@milkdown/kit/preset/gfm'
|
15 | 15 | import {
|
@@ -230,15 +230,22 @@ export function getGroups(filter?: string, config?: BlockEditFeatureConfig) {
|
230 | 230 | onRun: (ctx) => {
|
231 | 231 | const view = ctx.get(editorViewCtx)
|
232 | 232 | const { dispatch, state } = view
|
233 |
| - const tr = clearRange(state.tr) |
| 233 | + let { tr } = state |
| 234 | + tr = clearRange(tr) |
| 235 | + const from = tr.selection.from |
234 | 236 | const table = createTable(ctx, 3, 3)
|
235 |
| - tr.replaceSelectionWith(table) |
236 |
| - const { from } = tr.selection |
237 |
| - const pos = from - table.nodeSize + 2 |
| 237 | + tr = tr.replaceSelectionWith(table) |
238 | 238 | dispatch(tr)
|
| 239 | + |
239 | 240 | requestAnimationFrame(() => {
|
240 |
| - const selection = NodeSelection.create(view.state.tr.doc, pos) |
241 |
| - dispatch(view.state.tr.setSelection(selection).scrollIntoView()) |
| 241 | + const docSize = view.state.doc.content.size |
| 242 | + const $pos = view.state.doc.resolve( |
| 243 | + from > docSize ? docSize : from < 0 ? 0 : from |
| 244 | + ) |
| 245 | + const selection = TextSelection.near($pos) |
| 246 | + const tr = view.state.tr |
| 247 | + tr.setSelection(selection) |
| 248 | + dispatch(tr.scrollIntoView()) |
242 | 249 | })
|
243 | 250 | },
|
244 | 251 | })
|
|
0 commit comments