10 examples of 'javascript get cursor position' in JavaScript

Every line of 'javascript get cursor position' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
30getCursor(...cursorTypes: CursorTypes[]): CursorGroup {
31 const doc = this.codeMirror.getDoc();
32 const cursor = doc.getCursor();
33
34 if (!cursorTypes.length) {
35 return {
36 anchor: doc.getCursor('anchor'),
37 from: doc.getCursor('from'),
38 head: doc.getCursor('head'),
39 to: doc.getCursor('to')
40 } as any;
41 } else {
42 const retVal: any = {};
43 cursorTypes.forEach((cursorType) => {
44 retVal[cursorType] = doc.getCursor(cursorType);
45 });
46 return retVal;
47 }
48}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

3function getCursor(){
4 if (!window.getSelection().rangeCount){
5 return;
6 }
7
8 var nativeRange = window.getSelection().getRangeAt(0);
9
10 var deepestStartBlock = getTarget(nativeRange.startContainer, 'P, LI, UL, OL, H1, H2, H3, H4, H5');
11 var startPath = [];
12 // get the character index of the block
13 startPath.unshift(countFromLeft(deepestStartBlock, nativeRange.startContainer, nativeRange.startOffset));
14 // prepend the block paths
15 getFullBlockPath(startPath, deepestStartBlock);
16
17 var deepestEndBlock = getTarget(nativeRange.endContainer, 'P, LI, UL, OL, H1, H2, H3, H4, H5');
18 var endPath = [];
19 // get the character index of the block
20 endPath.unshift(countFromLeft(deepestEndBlock, nativeRange.endContainer, nativeRange.endOffset));
21 // prepend the block paths
22 getFullBlockPath(endPath, deepestEndBlock);
23
24 return {
25 startPath: startPath,
26 endPath: endPath,
27 nativeRange: nativeRange
28 };
29
30 // // recurse upward until parent is contentEditable
31 // // get index of which block you are
32 // var startBlock = getTargetParentChild(nativeRange.startContainer, '[data-tome]');
33 // var blockStart = indexOf( startBlock.parentElement.childNodes, startBlock );
34
35 // // recurse upward until parent is contentEditable
36 // // get index of which block you are
37 // var endBlock = getTargetParentChild(nativeRange.endContainer, '[data-tome]');
38 // var blockEnd = indexOf( endBlock.parentElement.childNodes, endBlock );
39
40 // // walk left to beginning of block, count chars
41 // var start = countFromLeft(startBlock, nativeRange.startContainer, nativeRange.startOffset);
42
43 // // walk right to end of block, count chars
44 // var end = countFromLeft(endBlock, nativeRange.endContainer, nativeRange.endOffset);
45
46 // return {
47 // blockStart: blockStart,
48 // blockEnd: blockEnd,
49 // start: start,
50 // end: end,
51 // nativeRange: nativeRange
52 // };
53}
25function getCursorLine(codeMirror) {
26 return codeMirror.getCursor().line;
27}
19setCursorPosition(pos) {
20 this.textEditor.setCursorBufferPosition([pos.row, pos.column]);
21}
185setCursorPosition(position: number): void {
186 let editorModel = this.editor.model;
187 editorModel.selections.pushBack({
188 start: position,
189 end: position,
190 uuid: this.id
191 });
192}
104function getCursorSelection(textarea){
105 var i = textarea.selectionStart;
106 var n = textarea.selectionEnd;
107 return [textarea.value.substring(0,i),
108 textarea.value.substring(i,n),
109 textarea.value.substring(n, textarea.value.length)];
110}
174getCursorPosition(): number {
175 let editorModel = this.editor.model;
176 let cursorPosition = editorModel.getCursorPosition();
177 return editorModel.getOffsetAt(cursorPosition);
178}
270function setCursorPosition(element, position) {
271 if (position == -1)
272 position = element.value.length;
273 element.focus();
274 if (element.setSelectionRange) {
275 element.setSelectionRange(position, position);
276 } else if (element.createTextRange) {
277 var range = element.createTextRange();
278 range.collapse(true);
279 range.moveEnd('character', position);
280 range.moveStart('character', position);
281 range.select();
282 }
283}
491function cursorCoords(target, param) {
492 if (!param || !param.where || !param.mode) { return; }
493 return $.data(target, "codemirror").cm.cursorCoords(param.where, param.mode);
494};
646function getCursor() {
647 return $( "body" ).css( "cursor" );
648}

Related snippets