2023-04-24 09:54:20 -04:00
|
|
|
import {
|
|
|
|
EditorState,
|
|
|
|
Line,
|
|
|
|
Range,
|
|
|
|
RangeSet,
|
|
|
|
StateEffect,
|
|
|
|
StateField,
|
|
|
|
} from '@codemirror/state'
|
|
|
|
import {
|
|
|
|
Decoration,
|
|
|
|
DecorationSet,
|
|
|
|
EditorView,
|
2023-05-09 04:54:14 -04:00
|
|
|
showTooltip,
|
2023-05-17 06:53:13 -04:00
|
|
|
gutter,
|
|
|
|
gutterLineClass,
|
|
|
|
GutterMarker,
|
2023-04-24 09:54:20 -04:00
|
|
|
Tooltip,
|
2023-05-09 04:54:14 -04:00
|
|
|
ViewPlugin,
|
2023-04-24 09:54:20 -04:00
|
|
|
WidgetType,
|
|
|
|
} from '@codemirror/view'
|
|
|
|
import { Highlight, HighlightType } from '../services/types/doc'
|
2023-03-24 09:59:13 -04:00
|
|
|
|
|
|
|
export const setHighlightsEffect = StateEffect.define<Highlight[]>()
|
2023-05-09 04:54:14 -04:00
|
|
|
const ADDITION_MARKER_CLASS = 'ol-cm-addition-marker'
|
|
|
|
const DELETION_MARKER_CLASS = 'ol-cm-deletion-marker'
|
2023-03-24 09:59:13 -04:00
|
|
|
|
|
|
|
function highlightToMarker(highlight: Highlight) {
|
|
|
|
const className =
|
2023-04-24 09:54:20 -04:00
|
|
|
highlight.type === 'addition'
|
2023-05-09 04:54:14 -04:00
|
|
|
? ADDITION_MARKER_CLASS
|
|
|
|
: DELETION_MARKER_CLASS
|
2023-03-24 09:59:13 -04:00
|
|
|
const { from, to } = highlight.range
|
|
|
|
|
|
|
|
return Decoration.mark({
|
|
|
|
class: className,
|
|
|
|
attributes: {
|
|
|
|
style: `--hue: ${highlight.hue}`,
|
|
|
|
},
|
|
|
|
}).range(from, to)
|
|
|
|
}
|
|
|
|
|
2023-04-24 09:54:20 -04:00
|
|
|
type LineStatus = {
|
|
|
|
line: Line
|
|
|
|
highlights: Highlight[]
|
|
|
|
empty: boolean
|
|
|
|
changeType: HighlightType | 'mixed'
|
|
|
|
}
|
|
|
|
|
|
|
|
type LineStatuses = Map<number, LineStatus>
|
|
|
|
|
|
|
|
function highlightedLines(highlights: Highlight[], state: EditorState) {
|
|
|
|
const lineStatuses = new Map<number, LineStatus>()
|
|
|
|
for (const highlight of highlights) {
|
|
|
|
const fromLine = state.doc.lineAt(highlight.range.from).number
|
|
|
|
const toLine = state.doc.lineAt(highlight.range.to).number
|
|
|
|
for (let lineNum = fromLine; lineNum <= toLine; ++lineNum) {
|
|
|
|
const status = lineStatuses.get(lineNum)
|
|
|
|
if (status) {
|
|
|
|
status.highlights.push(highlight)
|
|
|
|
if (status.changeType !== highlight.type) {
|
|
|
|
status.changeType = 'mixed'
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const line = state.doc.line(lineNum)
|
|
|
|
lineStatuses.set(lineNum, {
|
|
|
|
line,
|
|
|
|
highlights: [highlight],
|
|
|
|
empty: line.length === 0,
|
|
|
|
changeType: highlight.type,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lineStatuses
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:59:13 -04:00
|
|
|
const theme = EditorView.baseTheme({
|
2023-05-09 04:54:14 -04:00
|
|
|
['.' + ADDITION_MARKER_CLASS]: {
|
2023-04-24 09:54:20 -04:00
|
|
|
paddingTop: 'var(--half-leading)',
|
|
|
|
paddingBottom: 'var(--half-leading)',
|
2023-03-24 09:59:13 -04:00
|
|
|
backgroundColor: 'hsl(var(--hue), 70%, 85%)',
|
|
|
|
},
|
2023-05-09 04:54:14 -04:00
|
|
|
['.' + DELETION_MARKER_CLASS]: {
|
2023-03-24 09:59:13 -04:00
|
|
|
textDecoration: 'line-through',
|
|
|
|
color: 'hsl(var(--hue), 70%, 40%)',
|
|
|
|
},
|
2023-05-09 04:54:14 -04:00
|
|
|
'.cm-tooltip': {
|
2023-03-24 09:59:13 -04:00
|
|
|
backgroundColor: 'transparent',
|
|
|
|
borderWidth: 0,
|
2023-05-09 04:54:14 -04:00
|
|
|
// Prevent a tooltip getting in the way of hovering over a line that it
|
|
|
|
// obscures
|
|
|
|
pointerEvents: 'none',
|
2023-03-24 09:59:13 -04:00
|
|
|
},
|
|
|
|
'.ol-cm-highlight-tooltip': {
|
|
|
|
backgroundColor: 'hsl(var(--hue), 70%, 50%)',
|
|
|
|
borderRadius: '4px',
|
|
|
|
padding: '4px',
|
|
|
|
color: '#fff',
|
|
|
|
},
|
2023-04-24 09:54:20 -04:00
|
|
|
'.ol-cm-empty-line-addition-marker': {
|
|
|
|
padding: 'var(--half-leading) 2px',
|
|
|
|
},
|
2023-05-17 06:53:13 -04:00
|
|
|
'.ol-cm-changed-line': {
|
|
|
|
backgroundColor: 'rgba(0, 0, 0, 0.03)',
|
|
|
|
},
|
|
|
|
'.ol-cm-change-gutter': {
|
|
|
|
width: '3px',
|
|
|
|
paddingLeft: '1px',
|
|
|
|
},
|
|
|
|
'.ol-cm-changed-line-gutter': {
|
|
|
|
backgroundColor: 'hsl(var(--hue), 70%, 40%)',
|
|
|
|
height: '100%',
|
|
|
|
},
|
|
|
|
'.ol-cm-highlighted-line-gutter': {
|
|
|
|
backgroundColor: 'rgba(0, 0, 0, 0.03)',
|
|
|
|
},
|
2023-03-24 09:59:13 -04:00
|
|
|
})
|
|
|
|
|
2023-05-09 04:54:14 -04:00
|
|
|
function createHighlightTooltip(pos: number, highlight: Highlight) {
|
2023-03-24 09:59:13 -04:00
|
|
|
return {
|
2023-05-09 04:54:14 -04:00
|
|
|
pos,
|
2023-03-24 09:59:13 -04:00
|
|
|
above: true,
|
|
|
|
create: () => {
|
|
|
|
const dom = document.createElement('div')
|
|
|
|
dom.classList.add('ol-cm-highlight-tooltip')
|
|
|
|
dom.style.setProperty('--hue', highlight.hue.toString())
|
|
|
|
dom.textContent = highlight.label
|
|
|
|
|
|
|
|
return { dom }
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 04:54:14 -04:00
|
|
|
const setHighlightTooltipEffect = StateEffect.define<Tooltip | null>()
|
|
|
|
|
|
|
|
const tooltipField = StateField.define<Tooltip | null>({
|
|
|
|
create() {
|
|
|
|
return null
|
|
|
|
},
|
|
|
|
|
|
|
|
update(tooltip, transaction) {
|
|
|
|
for (const effect of transaction.effects) {
|
|
|
|
if (effect.is(setHighlightTooltipEffect)) {
|
|
|
|
return effect.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tooltip
|
|
|
|
},
|
|
|
|
|
|
|
|
provide: field => showTooltip.from(field),
|
|
|
|
})
|
|
|
|
|
|
|
|
function highlightAtPos(state: EditorState, pos: number) {
|
|
|
|
const highlights = state.field(highlightDecorationsField).highlights
|
|
|
|
return highlights.find(highlight => {
|
|
|
|
const { from, to } = highlight.range
|
|
|
|
return pos >= from && pos <= to
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const highlightTooltipPlugin = ViewPlugin.fromClass(
|
|
|
|
class {
|
|
|
|
private lastTooltipPos: number | null = null
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-useless-constructor
|
|
|
|
constructor(readonly view: EditorView) {}
|
|
|
|
|
|
|
|
setHighlightTooltip(tooltip: Tooltip | null) {
|
|
|
|
this.view.dispatch({
|
|
|
|
effects: setHighlightTooltipEffect.of(tooltip),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
setTooltipFromEvent(event: MouseEvent) {
|
|
|
|
const pos = this.view.posAtCoords({ x: event.clientX, y: event.clientY })
|
|
|
|
if (pos !== this.lastTooltipPos) {
|
|
|
|
let tooltip = null
|
|
|
|
if (pos !== null) {
|
|
|
|
const highlight = highlightAtPos(this.view.state, pos)
|
|
|
|
if (highlight) {
|
|
|
|
tooltip = createHighlightTooltip(pos, highlight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setHighlightTooltip(tooltip)
|
|
|
|
this.lastTooltipPos = pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseMove(event: MouseEvent) {
|
|
|
|
this.setTooltipFromEvent(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
startHover(event: MouseEvent, el: HTMLElement) {
|
|
|
|
const handleMouseMove = this.handleMouseMove.bind(this)
|
|
|
|
this.view.contentDOM.addEventListener('mousemove', handleMouseMove)
|
|
|
|
|
|
|
|
const handleMouseLeave = () => {
|
|
|
|
this.setHighlightTooltip(null)
|
|
|
|
this.lastTooltipPos = null
|
|
|
|
this.view.contentDOM.removeEventListener('mousemove', handleMouseMove)
|
|
|
|
el.removeEventListener('mouseleave', handleMouseLeave)
|
|
|
|
}
|
|
|
|
|
|
|
|
el.addEventListener('mouseleave', handleMouseLeave)
|
|
|
|
this.setTooltipFromEvent(event)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eventHandlers: {
|
|
|
|
mouseover(event) {
|
|
|
|
const el = event.target as HTMLElement
|
|
|
|
const classList = el.classList
|
|
|
|
if (
|
|
|
|
classList.contains(ADDITION_MARKER_CLASS) ||
|
|
|
|
classList.contains(DELETION_MARKER_CLASS) ||
|
|
|
|
// An empty line widget doesn't trigger a mouseover event, so detect
|
|
|
|
// an event on a line element that contains one instead
|
|
|
|
(classList.contains('cm-line') &&
|
|
|
|
el.querySelector(
|
|
|
|
`.ol-cm-empty-line-addition-marker, .ol-cm-empty-line-deletion-marker`
|
|
|
|
))
|
|
|
|
) {
|
|
|
|
this.startHover(event, el)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provide() {
|
|
|
|
return tooltipField
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-04-24 09:54:20 -04:00
|
|
|
class EmptyLineAdditionMarkerWidget extends WidgetType {
|
|
|
|
constructor(readonly hue: number) {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
toDOM(view: EditorView): HTMLElement {
|
|
|
|
const element = document.createElement('span')
|
2023-05-09 04:54:14 -04:00
|
|
|
element.classList.add(
|
|
|
|
'ol-cm-empty-line-addition-marker',
|
|
|
|
ADDITION_MARKER_CLASS
|
|
|
|
)
|
2023-04-24 09:54:20 -04:00
|
|
|
element.style.setProperty('--hue', this.hue.toString())
|
|
|
|
|
|
|
|
return element
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class EmptyLineDeletionMarkerWidget extends WidgetType {
|
|
|
|
constructor(readonly hue: number) {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
toDOM(view: EditorView): HTMLElement {
|
|
|
|
const element = document.createElement('span')
|
2023-05-09 04:54:14 -04:00
|
|
|
element.classList.add(
|
|
|
|
'ol-cm-empty-line-deletion-marker',
|
|
|
|
DELETION_MARKER_CLASS
|
|
|
|
)
|
2023-04-24 09:54:20 -04:00
|
|
|
element.style.setProperty('--hue', this.hue.toString())
|
|
|
|
element.textContent = ' '
|
|
|
|
|
|
|
|
return element
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMarkers(highlights: Highlight[]) {
|
|
|
|
return RangeSet.of(highlights.map(highlight => highlightToMarker(highlight)))
|
|
|
|
}
|
|
|
|
|
|
|
|
function createEmptyLineHighlightMarkers(lineStatuses: LineStatuses) {
|
|
|
|
const markers: Range<Decoration>[] = []
|
|
|
|
for (const lineStatus of lineStatuses.values()) {
|
|
|
|
if (lineStatus.line.length === 0) {
|
|
|
|
const highlight = lineStatus.highlights[0]
|
|
|
|
const widget =
|
|
|
|
highlight.type === 'addition'
|
|
|
|
? new EmptyLineAdditionMarkerWidget(highlight.hue)
|
|
|
|
: new EmptyLineDeletionMarkerWidget(highlight.hue)
|
|
|
|
|
|
|
|
markers.push(
|
|
|
|
Decoration.widget({
|
|
|
|
widget,
|
|
|
|
}).range(lineStatus.line.from)
|
|
|
|
)
|
2023-03-24 09:59:13 -04:00
|
|
|
}
|
2023-04-24 09:54:20 -04:00
|
|
|
}
|
|
|
|
return RangeSet.of(markers)
|
|
|
|
}
|
|
|
|
|
2023-05-17 06:53:13 -04:00
|
|
|
class ChangeGutterMarker extends GutterMarker {
|
|
|
|
constructor(readonly hue: number) {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
toDOM(view: EditorView) {
|
|
|
|
const el = document.createElement('div')
|
|
|
|
el.className = 'ol-cm-changed-line-gutter'
|
|
|
|
el.style.setProperty('--hue', this.hue.toString())
|
|
|
|
|
|
|
|
return el
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createGutterMarkers(lineStatuses: LineStatuses) {
|
|
|
|
const gutterMarkers: Range<GutterMarker>[] = []
|
|
|
|
for (const lineStatus of lineStatuses.values()) {
|
|
|
|
gutterMarkers.push(
|
|
|
|
new ChangeGutterMarker(lineStatus.highlights[0].hue).range(
|
|
|
|
lineStatus.line.from
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return RangeSet.of(gutterMarkers)
|
|
|
|
}
|
|
|
|
|
|
|
|
const lineHighlight = Decoration.line({ class: 'ol-cm-changed-line' })
|
|
|
|
|
|
|
|
function createLineHighlights(lineStatuses: LineStatuses) {
|
|
|
|
const lineHighlights: Range<Decoration>[] = []
|
|
|
|
for (const lineStatus of lineStatuses.values()) {
|
|
|
|
lineHighlights.push(lineHighlight.range(lineStatus.line.from))
|
|
|
|
}
|
|
|
|
return RangeSet.of(lineHighlights)
|
|
|
|
}
|
|
|
|
|
|
|
|
const changeLineGutterMarker = new (class extends GutterMarker {
|
|
|
|
elementClass = 'ol-cm-highlighted-line-gutter'
|
|
|
|
})()
|
|
|
|
|
|
|
|
function createGutterHighlights(lineStatuses: LineStatuses) {
|
|
|
|
const gutterMarkers: Range<GutterMarker>[] = []
|
|
|
|
for (const lineStatus of lineStatuses.values()) {
|
|
|
|
gutterMarkers.push(changeLineGutterMarker.range(lineStatus.line.from))
|
|
|
|
}
|
|
|
|
return RangeSet.of(gutterMarkers, true)
|
|
|
|
}
|
|
|
|
|
2023-04-24 09:54:20 -04:00
|
|
|
type HighlightDecorations = {
|
|
|
|
highlights: Highlight[]
|
|
|
|
highlightMarkers: DecorationSet
|
|
|
|
emptyLineHighlightMarkers: DecorationSet
|
2023-05-17 06:53:13 -04:00
|
|
|
lineHighlights: DecorationSet
|
|
|
|
gutterMarkers: RangeSet<GutterMarker>
|
|
|
|
gutterHighlights: RangeSet<GutterMarker>
|
2023-04-24 09:54:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export const highlightDecorationsField =
|
|
|
|
StateField.define<HighlightDecorations>({
|
|
|
|
create() {
|
|
|
|
return {
|
|
|
|
highlights: [],
|
|
|
|
highlightMarkers: Decoration.none,
|
|
|
|
emptyLineHighlightMarkers: Decoration.none,
|
2023-05-17 06:53:13 -04:00
|
|
|
lineHighlights: Decoration.none,
|
|
|
|
gutterMarkers: RangeSet.empty,
|
|
|
|
gutterHighlights: RangeSet.empty,
|
2023-04-24 09:54:20 -04:00
|
|
|
}
|
|
|
|
},
|
2023-05-17 06:53:13 -04:00
|
|
|
update(highlightDecorations, tr) {
|
2023-04-24 09:54:20 -04:00
|
|
|
for (const effect of tr.effects) {
|
|
|
|
if (effect.is(setHighlightsEffect)) {
|
|
|
|
const highlights = effect.value
|
|
|
|
const lineStatuses = highlightedLines(highlights, tr.state)
|
|
|
|
const highlightMarkers = createMarkers(highlights)
|
|
|
|
const emptyLineHighlightMarkers =
|
|
|
|
createEmptyLineHighlightMarkers(lineStatuses)
|
2023-05-17 06:53:13 -04:00
|
|
|
const lineHighlights = createLineHighlights(lineStatuses)
|
|
|
|
const gutterMarkers = createGutterMarkers(lineStatuses)
|
|
|
|
const gutterHighlights = createGutterHighlights(lineStatuses)
|
2023-04-24 09:54:20 -04:00
|
|
|
return {
|
|
|
|
highlights,
|
|
|
|
highlightMarkers,
|
|
|
|
emptyLineHighlightMarkers,
|
2023-05-17 06:53:13 -04:00
|
|
|
lineHighlights,
|
|
|
|
gutterMarkers,
|
|
|
|
gutterHighlights,
|
2023-04-24 09:54:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-17 06:53:13 -04:00
|
|
|
return highlightDecorations
|
2023-04-24 09:54:20 -04:00
|
|
|
},
|
|
|
|
provide: field => [
|
|
|
|
EditorView.decorations.from(field, value => value.highlightMarkers),
|
|
|
|
EditorView.decorations.from(
|
|
|
|
field,
|
|
|
|
value => value.emptyLineHighlightMarkers
|
|
|
|
),
|
2023-05-17 06:53:13 -04:00
|
|
|
EditorView.decorations.from(field, value => value.lineHighlights),
|
2023-04-24 09:54:20 -04:00
|
|
|
theme,
|
2023-05-09 04:54:14 -04:00
|
|
|
highlightTooltipPlugin,
|
2023-04-24 09:54:20 -04:00
|
|
|
],
|
|
|
|
})
|
2023-03-24 09:59:13 -04:00
|
|
|
|
2023-05-17 06:53:13 -04:00
|
|
|
const changeGutter = gutter({
|
|
|
|
class: 'ol-cm-change-gutter',
|
|
|
|
markers: view => view.state.field(highlightDecorationsField).gutterMarkers,
|
|
|
|
renderEmptyElements: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
const gutterHighlighter = gutterLineClass.from(
|
|
|
|
highlightDecorationsField,
|
|
|
|
value => value.gutterHighlights
|
|
|
|
)
|
|
|
|
|
2023-03-24 09:59:13 -04:00
|
|
|
export function highlights() {
|
2023-05-17 06:53:13 -04:00
|
|
|
return [highlightDecorationsField, changeGutter, gutterHighlighter]
|
2023-03-24 09:59:13 -04:00
|
|
|
}
|