MCPcopy
hub / github.com/NitroRCr/AIaW / save

Function save

src/utils/codejar.ts:158–221  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

156 })
157
158 function save(): Position {
159 const s = getSelection()
160 const pos: Position = { start: 0, end: 0, dir: undefined }
161
162 let { anchorNode, anchorOffset, focusNode, focusOffset } = s
163 if (!anchorNode || !focusNode) throw 'error1'
164
165 // If the anchor and focus are the editor element, return either a full
166 // highlight or a start/end cursor position depending on the selection
167 if (anchorNode === editor && focusNode === editor) {
168 pos.start = (anchorOffset > 0 && editor.textContent) ? editor.textContent.length : 0
169 pos.end = (focusOffset > 0 && editor.textContent) ? editor.textContent.length : 0
170 pos.dir = (focusOffset >= anchorOffset) ? '->' : '<-'
171 return pos
172 }
173
174 // Selection anchor and focus are expected to be text nodes,
175 // so normalize them.
176 if (anchorNode.nodeType === Node.ELEMENT_NODE) {
177 const node = document.createTextNode('')
178 anchorNode.insertBefore(node, anchorNode.childNodes[anchorOffset])
179 anchorNode = node
180 anchorOffset = 0
181 }
182 if (focusNode.nodeType === Node.ELEMENT_NODE) {
183 const node = document.createTextNode('')
184 focusNode.insertBefore(node, focusNode.childNodes[focusOffset])
185 focusNode = node
186 focusOffset = 0
187 }
188
189 visit(editor, el => {
190 if (el === anchorNode && el === focusNode) {
191 pos.start += anchorOffset
192 pos.end += focusOffset
193 pos.dir = anchorOffset <= focusOffset ? '->' : '<-'
194 return 'stop'
195 }
196
197 if (el === anchorNode) {
198 pos.start += anchorOffset
199 if (!pos.dir) {
200 pos.dir = '->'
201 } else {
202 return 'stop'
203 }
204 } else if (el === focusNode) {
205 pos.end += focusOffset
206 if (!pos.dir) {
207 pos.dir = '<-'
208 } else {
209 return 'stop'
210 }
211 }
212
213 if (el.nodeType === Node.TEXT_NODE) {
214 if (pos.dir != '->') pos.start += el.nodeValue!.length
215 if (pos.dir != '<-') pos.end += el.nodeValue!.length

Callers 8

CodeJarFunction · 0.85
handleNewLineFunction · 0.85
legacyNewLineFixFunction · 0.85
handleTabCharactersFunction · 0.85
recordHistoryFunction · 0.85
handlePasteFunction · 0.85
handleCutFunction · 0.85

Calls 2

getSelectionFunction · 0.85
visitFunction · 0.85

Tested by

no test coverage detected