MCPcopy
hub / github.com/basecamp/trix / mergeDocumentAtRange

Method mergeDocumentAtRange

src/trix/models/document.js:91–124  ·  view source on GitHub ↗
(document, range)

Source from the content-addressed store, hash-verified

89 }
90
91 mergeDocumentAtRange(document, range) {
92 let formattedDocument, result
93 range = normalizeRange(range)
94 const [ startPosition ] = range
95 const startLocation = this.locationFromPosition(startPosition)
96 const blockAttributes = this.getBlockAtIndex(startLocation.index).getAttributes()
97 const baseBlockAttributes = document.getBaseBlockAttributes()
98 const trailingBlockAttributes = blockAttributes.slice(-baseBlockAttributes.length)
99
100 if (arraysAreEqual(baseBlockAttributes, trailingBlockAttributes)) {
101 const leadingBlockAttributes = blockAttributes.slice(0, -baseBlockAttributes.length)
102 formattedDocument = document.copyWithBaseBlockAttributes(leadingBlockAttributes)
103 } else {
104 formattedDocument = document.copy({ consolidateBlocks: true }).copyWithBaseBlockAttributes(blockAttributes)
105 }
106
107 const blockCount = formattedDocument.getBlockCount()
108 const firstBlock = formattedDocument.getBlockAtIndex(0)
109
110 if (arraysAreEqual(blockAttributes, firstBlock.getAttributes())) {
111 const firstText = firstBlock.getTextWithoutBlockBreak()
112 result = this.insertTextAtRange(firstText, range)
113
114 if (blockCount > 1) {
115 formattedDocument = new this.constructor(formattedDocument.getBlocks().slice(1))
116 const position = startPosition + firstText.getLength()
117 result = result.insertDocumentAtRange(formattedDocument, position)
118 }
119 } else {
120 result = this.insertDocumentAtRange(formattedDocument, range)
121 }
122
123 return result
124 }
125
126 insertTextAtRange(text, range) {
127 range = normalizeRange(range)

Callers 2

insertHTMLMethod · 0.80
trix.jsFile · 0.80

Calls 15

locationFromPositionMethod · 0.95
getBlockAtIndexMethod · 0.95
insertTextAtRangeMethod · 0.95
insertDocumentAtRangeMethod · 0.95
normalizeRangeFunction · 0.90
arraysAreEqualFunction · 0.90
getBlockCountMethod · 0.80
getBlocksMethod · 0.80
getAttributesMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected