Skip to content

Commit

Permalink
fix: Fix tab focus when other elements are displayed next to text tha…
Browse files Browse the repository at this point in the history
…t are within a focus trap

Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Jan 26, 2024
1 parent cd572b2 commit 2c9e7a0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/EditorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { lowlight } from 'lowlight/lib/core.js'
import hljs from 'highlight.js/lib/core'

import { logger } from './helpers/logger.js'
import { Mention, PlainText, RichText } from './extensions/index.js'
import { FocusTrap, Mention, PlainText, RichText } from './extensions/index.js'
// eslint-disable-next-line import/no-named-as-default
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'

Expand Down Expand Up @@ -64,6 +64,7 @@ const createEditor = ({ language, onCreate, onUpdate = () => {}, extensions, ena
}),
],
}),
FocusTrap,
]
} else {
defaultExtensions = [PlainText, CodeBlockLowlight.configure({ lowlight, defaultLanguage: language })]
Expand Down
9 changes: 2 additions & 7 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -643,16 +643,10 @@ export default {
onFocus() {
this.emit('focus')
// Make sure that the focus trap doesn't catch tab keys while being in the contenteditable
window._nc_focus_trap?.[0]?.pause()
},
onBlur() {
this.emit('blur')
// Hand back focus to the previous trap
window._nc_focus_trap?.[0]?.unpause()
this.$el.focus()
},
onAddImageNode() {
Expand Down Expand Up @@ -750,6 +744,7 @@ export default {
this.$editor.commands.insertContent('\t')
this.$editor.commands.focus()
event.preventDefault()
event.stopPropagation()
return
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/Editor/ContentContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
<EditorOutline />
</div>
<slot />
<EditorContent tabindex="0"
role="document"
<EditorContent role="document"
class="editor__content text-editor__content"
:editor="$editor" />
<div class="text-editor__content-wrapper__right" />
Expand Down
41 changes: 41 additions & 0 deletions src/extensions/FocusTrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Extension } from '@tiptap/core'

const toggleFocusTrap = ({ editor }) => {
const trapStack = window._nc_focus_trap ?? []
const activeTrap = trapStack[trapStack.length - 1]

const possibleEditorTabCommand = editor.can().sinkListItem('listItem')
|| editor.can().goToNextCell()
|| editor.can().goToPreviousCell()

if (possibleEditorTabCommand) {
activeTrap?.pause()
} else {
activeTrap?.unpause()
}
}

const unpauseFocusTrap = ({ editor }) => {
const trapStack = window._nc_focus_trap ?? []
const activeTrap = trapStack[trapStack.length - 1]

activeTrap?.unpause()
}

/**
* The viewer focus trap needs to be paused on the fly in order to properly handle tab commands in the editor,
* as we have no control over if a tab key event is reaching the editor otherwise. This is because the focus trap
* registeres a capture listener (https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture), so whenever we reach a tab command in the editor the focus trap will already have captured the event.
*
* We also cannot work around this by pushing our own focus trap to the stack, as the focus trap package does not offer any reliable way to programmatically focus the next element of the parent trap if we allow tabbing out of the editor.
*/
const FocusTrap = Extension.create({
name: 'focustrap',
onFocus: toggleFocusTrap,
onBlur: unpauseFocusTrap,
onSelectionUpdate: toggleFocusTrap,
onTransaction: toggleFocusTrap,
onUpdate: toggleFocusTrap,
})

export default FocusTrap
2 changes: 2 additions & 0 deletions src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import CollaborationCursor from './CollaborationCursor.js'
import Emoji from './Emoji.js'
import FocusTrap from './FocusTrap.js'
import Keymap from './Keymap.js'
import UserColor from './UserColor.js'
import Markdown from './Markdown.js'
Expand All @@ -33,6 +34,7 @@ import Mention from './Mention.js'
export {
CollaborationCursor,
Emoji,
FocusTrap,
Keymap,
UserColor,
Markdown,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/CodeBlockView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</div>
</div>
<div :class="{'split-view': showCode && showPreview }">
<pre v-show="showCode" class="split-view__code"><NodeViewContent as="code" :contenteditable="isEditable" /></pre>
<pre v-show="showCode" class="split-view__code"><NodeViewContent as="code" tabindex="-1" :contenteditable="isEditable" /></pre>
<div v-show="showPreview"
ref="preview"
class="split-view__preview"
Expand Down

0 comments on commit 2c9e7a0

Please sign in to comment.