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

Method removeTextAtRange

src/trix/models/document.js:139–186  ·  view source on GitHub ↗
(range)

Source from the content-addressed store, hash-verified

137 }
138
139 removeTextAtRange(range) {
140 let blocks
141 range = normalizeRange(range)
142 const [ leftPosition, rightPosition ] = range
143 if (rangeIsCollapsed(range)) {
144 return this
145 }
146 const [ leftLocation, rightLocation ] = Array.from(this.locationRangeFromRange(range))
147
148 const leftIndex = leftLocation.index
149 const leftOffset = leftLocation.offset
150 const leftBlock = this.getBlockAtIndex(leftIndex)
151
152 const rightIndex = rightLocation.index
153 const rightOffset = rightLocation.offset
154 const rightBlock = this.getBlockAtIndex(rightIndex)
155
156 const removeRightNewline =
157 rightPosition - leftPosition === 1 &&
158 leftBlock.getBlockBreakPosition() === leftOffset &&
159 rightBlock.getBlockBreakPosition() !== rightOffset &&
160 rightBlock.text.getStringAtPosition(rightOffset) === "\n"
161
162 if (removeRightNewline) {
163 blocks = this.blockList.editObjectAtIndex(rightIndex, (block) =>
164 block.copyWithText(block.text.removeTextAtRange([ rightOffset, rightOffset + 1 ]))
165 )
166 } else {
167 let block
168 const leftText = leftBlock.text.getTextAtRange([ 0, leftOffset ])
169 const rightText = rightBlock.text.getTextAtRange([ rightOffset, rightBlock.getLength() ])
170 const text = leftText.appendText(rightText)
171
172 const removingLeftBlock = leftIndex !== rightIndex && leftOffset === 0
173 const useRightBlock = removingLeftBlock && leftBlock.getAttributeLevel() >= rightBlock.getAttributeLevel()
174
175 if (useRightBlock) {
176 block = rightBlock.copyWithText(text)
177 } else {
178 block = leftBlock.copyWithText(text)
179 }
180
181 const affectedBlockCount = rightIndex + 1 - leftIndex
182 blocks = this.blockList.splice(leftIndex, affectedBlockCount, block)
183 }
184
185 return new this.constructor(blocks)
186 }
187
188 moveTextFromRangeToPosition(range, position) {
189 let text

Callers 4

insertTextAtRangeMethod · 0.95
insertDocumentAtRangeMethod · 0.45

Calls 13

getBlockAtIndexMethod · 0.95
normalizeRangeFunction · 0.90
rangeIsCollapsedFunction · 0.90
getStringAtPositionMethod · 0.80
editObjectAtIndexMethod · 0.80
getTextAtRangeMethod · 0.80
appendTextMethod · 0.80
spliceMethod · 0.80
getBlockBreakPositionMethod · 0.45
copyWithTextMethod · 0.45
getLengthMethod · 0.45

Tested by

no test coverage detected