MCPcopy
hub / github.com/antonmedv/codejar / save

Function save

codejar.ts:156–219  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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