overleaf/services/web/frontend/js/features/source-editor/extensions/text-selected.ts
David 06e24c9081 Merge pull request #20606 from overleaf/dk-add-comment-on-mouseup
show Add comment button on mouse up event

GitOrigin-RevId: 2c87f11fab9f9fb7e11637bb9eac2ca1ab9b298b
2024-09-27 08:04:39 +00:00

34 lines
958 B
TypeScript

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