MCPcopy Create free account
hub / github.com/bigcode-project/jupytercoder / getActiveCellPointerCode

Function getActiveCellPointerCode

src/utility.js:92–158  ·  view source on GitHub ↗
(activeCell, cellIndex, currctJupyterModel)

Source from the content-addressed store, hash-verified

90
91
92const getActiveCellPointerCode = (activeCell, cellIndex, currctJupyterModel) => {
93 let cellContent = []
94
95 // get cursor element
96 const cursorElement = activeCell.querySelector('div.CodeMirror-cursor')
97
98 const style = window.getComputedStyle(cursorElement);
99
100 // 指针所在位置的偏移量
101 const cursorOffsetLeft = Math.round(parseFloat(style.getPropertyValue('left')))
102
103 // Which line
104 const lineIndex = Math.round(parseFloat(style.getPropertyValue('top')) / 17)
105
106 // Obtain element for all line
107 const linesElement = activeCell.querySelectorAll('.CodeMirror-line:not(.displayed)')
108
109 // code dom element length in active line
110 const codeElementWdth = linesElement[lineIndex].querySelector("span").offsetWidth
111
112 let cursorAtEndInLine = true
113 // Determine whether the pointer is at the end of a line, Because there is a left marring, so -4, but due to precision issues so -3
114 if (cursorOffsetLeft - 3 < codeElementWdth) {
115 cursorAtEndInLine = false
116
117 }
118
119 for (let i = 0; i < linesElement.length; i++) {
120 if (i == lineIndex) {
121 cellContent.push({
122 "content": linesElement[i].textContent,
123 "cellIndex": cellIndex,
124 "isCursor": true,
125 "type": "code",
126 "lineIndex": i
127 })
128 } else {
129 cellContent.push({
130 "content": linesElement[i].textContent,
131 "cellIndex": cellIndex,
132 "isCursor": false,
133 "type": "code",
134 "lineIndex": i
135 })
136 }
137 }
138
139 // Get complete cells
140 const fullCell = activeCell.parentElement.parentElement.parentElement.parentElement
141
142 const outputElement = fullCell.querySelectorAll(`.${currctJupyterModel.requiredClassName.output}`);
143
144 if (outputElement) {
145 outputElement.forEach(element => {
146 cellContent.push({
147 "content": element.textContent,
148 "cellIndex": cellIndex,
149 "isCursor": false,

Callers 3

getActiveContextFunction · 0.85
utility.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected