overleaf/services/web/frontend/js/features/source-editor/extensions/text-selected.ts
Domagoj Kriskovic 0329a18875 Show "add comment" tooltip below cursor if near viewport top (#21348)
* Scroll selection anchor into view when adding new comment

* check if cursor is near viewport edge

* Show "add comment" tooltip below cursor if near viewport top

GitOrigin-RevId: 0dc2234bc03b1d88a3719ba01a4a865f218b9bfa
2024-10-28 09:05:47 +00:00

34 lines
908 B
TypeScript

import { StateEffect } from '@codemirror/state'
import { EditorView, ViewPlugin } from '@codemirror/view'
export const textSelectedEffect = StateEffect.define<EditorView>()
export const textSelected = ViewPlugin.define(view => {
function mouseUpListener() {
if (!view.state.selection.main.empty) {
view.dispatch({
effects: textSelectedEffect.of(view),
})
}
}
function keyUpListener(event: KeyboardEvent) {
if (
(event.shiftKey || event.key === 'Meta') &&
!view.state.selection.main.empty
) {
view.dispatch({
effects: textSelectedEffect.of(view),
})
}
}
view.dom.addEventListener('mouseup', mouseUpListener)
view.dom.addEventListener('keyup', keyUpListener)
return {
destroy() {
view.dom.removeEventListener('mouseup', mouseUpListener)
view.dom.removeEventListener('keyup', keyUpListener)
},
}
})