-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Smarts for adjusting focus on a toggle target (#26)
- Loading branch information
Showing
4 changed files
with
90 additions
and
14 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
function deactivateTrixAttributes (editor) { | ||
const attributes = [ | ||
'bold', | ||
'bullet', | ||
'code', | ||
'heading1', | ||
'href', | ||
'italic', | ||
'number', | ||
'quote', | ||
'strike' | ||
] | ||
attributes.forEach(name => editor.deactivateAttribute(name)) | ||
} | ||
|
||
function focusTrixEditorElement (element) { | ||
if (element.value.length === 0) return | ||
|
||
const editor = element.editor | ||
|
||
// move cursor to the end | ||
let lastRange = [] | ||
while ( | ||
lastRange[0] !== editor.getSelectedRange()[0] && | ||
lastRange[1] !== editor.getSelectedRange()[1] | ||
) { | ||
lastRange = editor.getSelectedRange() | ||
editor.moveCursorInDirection('forward') | ||
} | ||
|
||
// insert an empty char and select it | ||
editor.insertString(' ') | ||
editor.moveCursorInDirection('forward') | ||
editor.setSelectedRange([lastRange[1], editor.getSelectedRange()[1]]) | ||
|
||
// deactivate all trix features for the selection | ||
deactivateTrixAttributes(editor) | ||
|
||
// move cursor to end and collapse the selection | ||
editor.setSelectedRange([ | ||
editor.getSelectedRange()[1], | ||
editor.getSelectedRange()[1] | ||
]) | ||
} | ||
|
||
function shouldEnhanceFocus (element) { | ||
return ( | ||
element.closest('turbo-boost-toggle-target') && | ||
element.tagName.match(/^input|textarea|trix-editor$/i) | ||
) | ||
} | ||
|
||
function enhanceFocus (element) { | ||
const trixEditorElement = element.closest('trix-editor') | ||
|
||
try { | ||
if (trixEditorElement) { | ||
focusTrixEditorElement(trixEditorElement) | ||
} else { | ||
element.selectionStart = element.selectionEnd = element.value.length | ||
} | ||
} finally { | ||
setTimeout( | ||
() => element.scrollIntoView({ block: 'center', behavior: 'smooth' }), | ||
100 | ||
) | ||
} | ||
} | ||
|
||
addEventListener( | ||
'focus', | ||
event => { | ||
if (shouldEnhanceFocus(document.activeElement)) | ||
enhanceFocus(document.activeElement) | ||
}, | ||
true | ||
) |