How to set the caret to a specific position in etherpad? - etherpad

I'm authoring a plugin and on some operations of the plugin, the caret moves back to 0:0. I can get the caret position with
exports.aceKeyEvent = function(hook_name, args, cb) {
line = args.editorInfo.ace_caretLine();
char = args.editorInfo.ace_caretColumn();
}
.. but so far I didn't find anything to set the caret. There suppose to be a Set selection method, but the documentation is not giving me any hints.

Thanks to John Mc Lear, the solution came up. example
Only after a call to ace_replaceRange will a call to ace_performSelectionChange set the caret position in an acekeyEvent hook, where we get the ace object with ace_callWithAce.
However, this works in a postAceInit hook.
context.ace.callWithAce(function(ace) {
ace.ace_performSelectionChange([line - 1, char], [line - 1, char], false);
}, 'padsearch_callstack', true);

Related

insert GlyphProperty.null in shouldGenerateGlyphs causes cursor misplacement problem

I am hiding the character * by inserting GlyphProperty.null then calling setGlyphs(_:properties:characterIndexes:font:forGlyphRange:) inside layoutManager(_:shouldGenerateGlyphs:properties:characterIndexes:font:forGlyphRange:) as described in the answer in https://stackoverflow.com/a/57697139/9568961, it works but after a certain sequence of text editing, the cursor starts to misplace, as shown in the gif https://github.com/dzAtZJU/Demos/blob/master/cursor_misplace.GIF?raw=true
GIF Description: When the second line becomes qq, I try to move the cursor to the end of first line, but it move directly to the beginning. I then try to delete characters, then the cursor move to the end correctly.
The sample project is here https://github.com/dzAtZJU/HideText.
Is it a bug? Have anyone run into this? How can I work around this?
This is a year-old question, but for anyone struggling with the same problem, here's a solution.
My Markdown-like parser knows the ranges for both the line on which the cursor is now, and where it was previously. I'm calling hideAndShowMarkup whenever text view selection changes.
You need to invalidate current glyphs for both of the ranges, and then redraw them synchronously before updating insertion point. The most crucial step is invalidating layout for the range where you want to show Markdown symbols. Without that, your caret gets drawn into the wrong position.
Code is in Objective C, but should be easy to implement in Swift, too.
-(void)hideAndShowMarkup {
// Just for reference
Line* line = currentLine;
Line* prevLine = previouslySelectedLine;
[self.layoutManager invalidateGlyphsForCharacterRange:line.range changeInLength:0 actualCharacterRange:nil];
[self.layoutManager invalidateGlyphsForCharacterRange:prevLine.range changeInLength:0 actualCharacterRange:nil];
[self.layoutManager invalidateLayoutForCharacterRange:line.range actualCharacterRange:nil];
if (NSGraphicsContext.currentContext) {
[self.layoutManager drawGlyphsForGlyphRange:line.range atPoint:self.frame.origin];
[self.layoutManager drawGlyphsForGlyphRange:prevLine.range atPoint:self.frame.origin];
}
[self updateInsertionPointStateAndRestartTimer:NO];
}

Getting and setting of a text selection in a textarea

I want to create a textarea where users can select a part of a text, and I will react according to their selection. So I need to
1) get the start and end positions of the selection text
2) get the position of the focus, if it is in the textarea and there is no selection
It seems that the functions to do so are different from an explorer to another. So could anyone tell me what is the approach to do that in Office Add-in?
I have tried the following 2 ways (ie, select a part of the text in myTextarea, click on button, and then debug the code), they don't seem to be the right functions.
(function() {
"use strict";
Office.initialize = function(reason) {
$(document).ready(function() {
app.initialize();
$('#button').click(showSelection);
});
};
function showSelection() {
// way 1
console.log(document.selection); // undefined
document.getElementById("myTextarea").focus();
var sel = document.selection.createRange(); // Uncaught TypeError: Cannot read property 'createRange' of undefined
selectedText = sel.text;
// way 2
console.log(document.getElementById("myTextarea").selectionstart); // undefined
console.log(document.getElementById("myTextarea").selectionend); // undefined
}
})();
Additionally, it would be great if one could also tell me how to realise the follows by code:
1) select a part of a text, from a start and end positions
2) set the focus at a certain position of the textarea
Edit 1:
I just tried window.getSelection() within my Excel add-in:
function showselection() {
var a = window.getSelection();
var b = window.getSelection().toString();
var c = window.getSelection().getRangeAt(0);
}
After selecting a text in the textarea, and clicking on button, I debugged step by step: the first line made a a = Selection {anchorNode: null, anchorOffset: 0, focusNode: null, focusOffset: 0, is ...; the second line returned "", the third line got an error Home.js:19 Uncaught IndexSizeError: Failed to execute 'getRangeAt' on 'Selection': 0 is not a valid index. It looks like the selection has not been successfully caught...
Here is JSBin without Excel add-in frame, which returns almost same results as above.
Use JQuery.
For instance, the following two lines get the caret position:
function showselection() {
console.log($('#myTextarea')[0].selectionStart);
console.log($('#myTextarea')[0].selectionEnd);
}
There are some plug-ins:
https://github.com/localhost/jquery-fieldselection
http://madapaja.github.io/jquery.selection/
The second one has several short samples with buttons (where we may lose selection). You could either use their API, or look into their code to see which JQuery functions they call.
If the desired selection is just the selected text in the HTML page (and not the user's selection in Excel/Word), then there are some good stackoverflow answers about accessing that selection.
One of the key features of the JavaScript APIs for Office is that they follow an asynchronous model (the code you've written above for showSelection() appears to be synchronous). I'd recommend reading the Excel and Word JS API overview pages to get a feel for how they work. As an example, here's how you'd get the text from a selection:
Word.run(function (context) {
var myRange = context.document.getSelection();
context.load(myRange, 'text');
return context.sync().then(function () {
log("Selection contents: " + myRange.text);
});
})
Then for the other specifics of your question please clarify as requested in my comment. Thanks!
-Michael (PM for Office add-ins)

Find function not working

I am implementing a simple MFC text editor and I have run into trouble with my find function. Specfically in my 'find box' dialog class I have the following code:
FINDTEXTEX ft;
ft.chrg.cpMin = 0;
ft.chrg.cpMax = -1;
ft.lpstrText = _T("wallaby");
long n = pmyRichEditCtrl->FindText(FR_MATCHCASE|FR_WHOLEWORD, &ft);
if (n != -1)
pmyRichEditCtrl->SetSel(ft.chrgText);
However, n is always -1 even when the word wallaby is typed into the control. Any help would be greatly appreciated.
It all depends where your current cursor selection is. If you typed the word then most likely your cursor will be positioned directly after the typed word. If you do not care about the position of the cursor then you can set the position to the beginning and start finding the text from the beginning:
pmyRichEditCtrl->SetSel( 0, 0 );
long n = pmyRichEditCtrl->FindText(FR_DOWN|FR_MATCHCASE|FR_WHOLEWORD, &ft);
Also, do not forget to set FR_DOWN parameter to search forward. If this parameter is not set it will search backward from FINDTEXTEX.chrg.cpMin:
Microsoft Rich Edit 2.0 and later: If set, the search is forward from
FINDTEXTEX.chrg.cpMin; if not set, the search is backward from
FINDTEXTEX.chrg.cpMin.
Microsoft Rich Edit 1.0: The FR_DOWN flag is ignored. The search is
always forward.

how to get cursor position using GetSelection in text service framework windows8 application?

HRESULT hr;
TF_SELECTION tfSelection;
ULONG uFetched;
//Obtain the default selection.
hr = _pContext->GetSelection(ec, TF_DEFAULT_SELECTION, 1, &tfSelection, &uFetched);
UINT ar=(UINT)uFetched;
if(SUCCEEDED(hr) && (uFetched > 0))
{
UINT ar=(UINT)uFetched;
//Work with the selection.
//Release the selection range object.
tfSelection.range->Release();
}
Hi all. I am implementing this code in the DoEditSession method. When I try to get the selection, I always get a value of 1 for uFetched. But the cursor position is not coming?
I am developing a text service for windows RT using Tsf interface. I
have integrated libraries that suggests words based on letters we type
in the candidate window. Now I need to get the letters before and
after the Cursor Position in the Document. So here I have
used GetSelection to retrieve the selected text. The problem is I am
not able to retrieve the caret position in the document (notepad). Is there any specific way in which i can get the letter/text around the caret/cursor position?
You can shift the start/end of the selection range without actually modifying the selection.

Set caret position in javafx.scene.control.TextArea and javafx.scene.control.TextField

I need to set the caret position manually in my code.
There is a getCaretPosition() under javafx.scene.control.TextInputControl but there is no setter method.
How can I set the caret position?
TextArea ta = new TextArea();
ta.setText("1234567890");
ta.positionCaret(4);
You can use the positionCaret function as mentioned before. But make sure to wrap it in Platform.runLater. Otherwise it might not work at all.
Platform.runLater( new Runnable() {
#Override
public void run() {
textArea.positionCaret( 0 );
}
});
There are two method in TextInputControl that allow manipulation of caret position.
These are :-
selectPositionCaret(int pos) - Selects the text between the last caret position up to the current caret position that you entered.
positionCaret(int pos) - Sets the current caret position clearing the previous selection as well.
So i think in your case you want to use the positionCaret method to set the position without any selections.
if you want to add it at the end of your TextField you can do the next
TextFieldName.positionCaret(TextFieldName.getText().length());
this will add the Curser at The end .

Resources