Merge pull request #20606 from overleaf/dk-add-comment-on-mouseup

show Add comment button on mouse up event

GitOrigin-RevId: 2c87f11fab9f9fb7e11637bb9eac2ca1ab9b298b
This commit is contained in:
David 2024-09-26 15:14:08 +01:00 committed by Copybot
parent 2801b3baad
commit 06e24c9081
2 changed files with 44 additions and 7 deletions

View file

@ -6,7 +6,6 @@ import {
Tooltip, Tooltip,
} from '@codemirror/view' } from '@codemirror/view'
import { import {
EditorState,
Extension, Extension,
StateField, StateField,
StateEffect, StateEffect,
@ -15,6 +14,7 @@ import {
} from '@codemirror/state' } from '@codemirror/state'
import { isSplitTestEnabled } from '@/utils/splitTestUtils' import { isSplitTestEnabled } from '@/utils/splitTestUtils'
import { v4 as uuid } from 'uuid' import { v4 as uuid } from 'uuid'
import { textSelected, textSelectedEffect } from './text-selected'
export const addNewCommentRangeEffect = StateEffect.define<Range<Decoration>>() export const addNewCommentRangeEffect = StateEffect.define<Range<Decoration>>()
@ -36,7 +36,7 @@ export const addComment = (): Extension => {
return [] return []
} }
return [addCommentTheme, addCommentStateField] return [addCommentTheme, addCommentStateField, textSelected]
} }
export const addCommentStateField = StateField.define<{ export const addCommentStateField = StateField.define<{
@ -69,10 +69,14 @@ export const addCommentStateField = StateField.define<{
add: [rangeToAdd], add: [rangeToAdd],
}) })
} }
if (effect.is(textSelectedEffect)) {
tooltip = buildTooltip(effect.value)
} }
if (tr.docChanged || tr.selection) { if (tooltip && tr.state.selection.main.empty) {
tooltip = buildTooltip(tr.state) tooltip = null
}
} }
return { tooltip, ranges } return { tooltip, ranges }
@ -84,8 +88,7 @@ export const addCommentStateField = StateField.define<{
], ],
}) })
function buildTooltip(state: EditorState): Tooltip | null { function buildTooltip(range: SelectionRange): Tooltip | null {
const range = state.selection.main
if (range.empty) { if (range.empty) {
return null return null
} }

View file

@ -0,0 +1,34 @@
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)
},
}
})