Skip to content

Commit

Permalink
Add options.forceFocus for getSelection and getCursor
Browse files Browse the repository at this point in the history
Give the editor focus after selection.
  • Loading branch information
A-312 committed Mar 10, 2020
1 parent 8c07543 commit a2a3298
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,8 @@ <h3 id="api_selection">Cursor and selection methods</h3>
based on the relative position of the old selection—the editor
will try to move further away from that, to prevent getting
stuck.</dd>
<dt id="selection_bias"><code><strong>forceFocus</strong>: boolean</code></dt>
<dd>Give the editor focus after selection.</dd>
</dl></dd>

<dt id="setSelections"><code><strong>doc.setSelections</strong>(ranges: array&lt;{anchor, head}&gt;, ?primary: integer, ?options: object)</code></dt>
Expand Down
7 changes: 4 additions & 3 deletions src/display/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function startOperation(cm) {
cursorActivityHandlers: null, // Set of handlers to fire cursorActivity on
cursorActivityCalled: 0, // Tracks which cursorActivity handlers have been called already
selectionChanged: false, // Whether the selection needs to be redrawn
forceFocus: false, // Force focus after selection changed
updateMaxLine: false, // Set when the widest line needs to be determined anew
scrollLeft: null, scrollTop: null, // Intermediate scroll position, not pushed to DOM yet
scrollToPos: null, // Used to scroll to a specific position
Expand Down Expand Up @@ -128,9 +129,9 @@ function endOperation_W2(op) {
if (cm.state.focused && op.updateInput)
cm.display.input.reset(op.typing)

if (!op.extActiveElt)
ensureFocus(op.cm)
else {
if (!op.extActiveElt || op.forceFocus) {
ensureFocus(cm)
} else {
op.extActiveElt.focus()
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/model/selection_updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export function setSelectionNoUndo(doc, sel, options) {

if (!(options && options.scroll === false) && doc.cm)
ensureCursorVisible(doc.cm)

if ((options && options.forceFocus === true) && doc.cm)
doc.cm.curOp.forceFocus = true
}

function setSelectionInner(doc, sel) {
Expand Down

0 comments on commit a2a3298

Please sign in to comment.