MCPcopy Create free account
hub / github.com/editablejs/editable / insertNodes

Function insertNodes

packages/models/src/transforms/insert-nodes.ts:10–78  ·  view source on GitHub ↗
(
  editor: Editor,
  nodes: Node | Node[],
  options: {
    at?: Location
    match?: NodeMatch<T>
    mode?: RangeMode
    hanging?: boolean
    select?: boolean
    voids?: boolean
  } = {},
)

Source from the content-addressed store, hash-verified

8const FLUSHING = new WeakMap<Editor, boolean>()
9const LAST_NODE = new WeakMap<Editor, Node>()
10export const insertNodes = <T extends Node>(
11 editor: Editor,
12 nodes: Node | Node[],
13 options: {
14 at?: Location
15 match?: NodeMatch<T>
16 mode?: RangeMode
17 hanging?: boolean
18 select?: boolean
19 voids?: boolean
20 } = {},
21) => {
22 const { at = editor.selection } = options
23 if (handleInserInGrid(editor, at)) {
24 // 插入 grid 或者 void block 时,
25 const firstNode = Array.isArray(nodes) ? nodes[0] : nodes
26 let pathRef: PathRef | undefined
27 if (
28 !options.match &&
29 Element.isElement(firstNode) &&
30 (editor.isGrid(firstNode) || (editor.isVoid(firstNode) && Editor.isBlock(editor, firstNode)))
31 ) {
32 const block = Editor.above(editor, {
33 match: n => Editor.isBlock(editor, n),
34 })
35 if (at && block && Editor.isEmpty(editor, block[0])) {
36 pathRef = Editor.pathRef(editor, block[1])
37 }
38 }
39 defaultInsertNodes(editor, nodes, options)
40 if (pathRef) {
41 const path = pathRef.unref()
42 if (path) {
43 Transforms.removeNodes(editor, {
44 at: path,
45 })
46 }
47 }
48 }
49 const lastNode = Array.isArray(nodes) ? nodes[nodes.length - 1] : nodes
50 LAST_NODE.set(editor, lastNode)
51 if (!FLUSHING.get(editor)) {
52 FLUSHING.set(editor, true)
53
54 Promise.resolve().then(() => {
55 FLUSHING.set(editor, false)
56 const block = Editor.above(editor, {
57 match: n => n === LAST_NODE.get(editor),
58 })
59 if (!block) return
60 if (editor.isGrid(block[0]) || editor.isVoid(block[0])) {
61 const path = block[1]
62 const next = Editor.next(editor, {
63 at: path,
64 })
65 if (
66 !next ||
67 (Element.isElement(next[0]) && (editor.isGrid(next[0]) || editor.isVoid(next[0])))

Callers

nothing calls this directly

Calls 2

handleInserInGridFunction · 0.90
getMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…