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

Function insertFragment

packages/models/src/transforms/insert-fragment.ts:7–221  ·  view source on GitHub ↗
(
  editor: Editor,
  fragment: Node[],
  options: InsertFragmentOptions = {},
)

Source from the content-addressed store, hash-verified

5export interface InsertFragmentOptions extends TextInsertFragmentOptions {}
6
7export const insertFragment = (
8 editor: Editor,
9 fragment: Node[],
10 options: InsertFragmentOptions = {},
11) => {
12 Editor.withoutNormalizing(editor, () => {
13 const { hanging = false, voids = false } = options
14 let { at = editor.selection } = options
15
16 if (!fragment.length) {
17 return
18 }
19
20 if (!at) {
21 return
22 } else if (Range.isRange(at)) {
23 if (!hanging) {
24 at = Editor.unhangRange(editor, at)
25 }
26
27 if (Range.isCollapsed(at)) {
28 at = at.anchor
29 } else {
30 const [, end] = Range.edges(at)
31
32 if (!voids && Editor.void(editor, { at: end })) {
33 return
34 }
35
36 const pointRef = Editor.pointRef(editor, end)
37 Transforms.delete(editor, { at })
38 at = pointRef.unref()!
39 }
40 } else if (Path.isPath(at)) {
41 at = Editor.start(editor, at)
42 }
43
44 if (!voids && Editor.void(editor, { at })) {
45 return
46 }
47
48 // If the insert point is at the edge of an inline node, move it outside
49 // instead since it will need to be split otherwise.
50 const inlineElementMatch = Editor.above(editor, {
51 at,
52 match: n => Editor.isInline(editor, n),
53 mode: 'highest',
54 voids,
55 })
56
57 if (inlineElementMatch) {
58 const [, inlinePath] = inlineElementMatch
59
60 if (Editor.isEnd(editor, at, inlinePath)) {
61 const after = Editor.after(editor, inlinePath)!
62 at = after
63 } else if (Editor.isStart(editor, at, inlinePath)) {
64 const before = Editor.before(editor, inlinePath)!

Callers

nothing calls this directly

Calls 1

matcherFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…