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