2024-08-21 05:38:25 -04:00
|
|
|
import { StateEffect, TransactionSpec } from '@codemirror/state'
|
2024-08-21 04:34:56 -04:00
|
|
|
import {
|
|
|
|
Decoration,
|
|
|
|
type DecorationSet,
|
|
|
|
EditorView,
|
|
|
|
type PluginValue,
|
|
|
|
ViewPlugin,
|
|
|
|
WidgetType,
|
|
|
|
} from '@codemirror/view'
|
2024-09-19 06:08:20 -04:00
|
|
|
import {
|
|
|
|
AnyOperation,
|
|
|
|
Change,
|
|
|
|
DeleteOperation,
|
|
|
|
} from '../../../../../types/change'
|
2024-08-21 04:34:56 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2024-09-19 06:08:20 -04:00
|
|
|
import {
|
|
|
|
isCommentOperation,
|
|
|
|
isDeleteOperation,
|
|
|
|
isInsertOperation,
|
|
|
|
} from '@/utils/operations'
|
2024-08-21 05:31:14 -04:00
|
|
|
import { DocumentContainer } from '@/features/ide-react/editor/document-container'
|
2024-08-21 04:34:56 -04:00
|
|
|
import { Ranges } from '@/features/review-panel-new/context/ranges-context'
|
|
|
|
import { Threads } from '@/features/review-panel-new/context/threads-context'
|
2024-09-19 06:08:20 -04:00
|
|
|
import { isSelectionWithinOp } from '@/features/review-panel-new/utils/is-selection-within-op'
|
2024-08-21 04:34:56 -04:00
|
|
|
|
|
|
|
type RangesData = {
|
|
|
|
ranges: Ranges
|
|
|
|
threads: Threads
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateRangesEffect = StateEffect.define<RangesData>()
|
2024-09-19 06:08:20 -04:00
|
|
|
const highlightRangesEffect = StateEffect.define<AnyOperation | undefined>()
|
2024-08-21 04:34:56 -04:00
|
|
|
|
|
|
|
export const updateRanges = (data: RangesData): TransactionSpec => {
|
|
|
|
return {
|
|
|
|
effects: updateRangesEffect.of(data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-19 06:08:20 -04:00
|
|
|
export const highlightRanges = (op?: AnyOperation): TransactionSpec => {
|
|
|
|
return {
|
|
|
|
effects: highlightRangesEffect.of(op),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-21 04:34:56 -04:00
|
|
|
type Options = {
|
|
|
|
currentDoc: DocumentContainer
|
|
|
|
loadingThreads?: boolean
|
|
|
|
ranges?: Ranges
|
|
|
|
threads?: Threads
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A custom extension that initialises the change manager, passes any updates to it,
|
|
|
|
* and produces decorations for tracked changes and comments.
|
|
|
|
*/
|
2024-08-21 05:31:14 -04:00
|
|
|
export const ranges = ({ ranges, threads }: Options) => {
|
2024-08-21 04:34:56 -04:00
|
|
|
return [
|
2024-08-21 05:31:14 -04:00
|
|
|
// handle viewportChanged updates
|
|
|
|
ViewPlugin.define(view => {
|
|
|
|
let timer: number
|
|
|
|
|
|
|
|
return {
|
|
|
|
update(update) {
|
|
|
|
if (update.viewportChanged) {
|
|
|
|
if (timer) {
|
|
|
|
window.clearTimeout(timer)
|
|
|
|
}
|
2024-08-21 04:34:56 -04:00
|
|
|
|
2024-08-21 05:31:14 -04:00
|
|
|
timer = window.setTimeout(() => {
|
|
|
|
dispatchEvent(new Event('editor:viewport-changed'))
|
|
|
|
}, 25)
|
2024-08-21 04:34:56 -04:00
|
|
|
}
|
2024-08-21 05:31:14 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}),
|
2024-08-21 04:34:56 -04:00
|
|
|
|
|
|
|
// draw change decorations
|
|
|
|
ViewPlugin.define<
|
|
|
|
PluginValue & {
|
|
|
|
decorations: DecorationSet
|
|
|
|
}
|
|
|
|
>(
|
|
|
|
() => {
|
|
|
|
return {
|
2024-08-21 05:31:14 -04:00
|
|
|
decorations:
|
|
|
|
ranges && threads
|
|
|
|
? buildChangeDecorations({ ranges, threads })
|
|
|
|
: Decoration.none,
|
2024-08-21 04:34:56 -04:00
|
|
|
update(update) {
|
|
|
|
for (const transaction of update.transactions) {
|
|
|
|
this.decorations = this.decorations.map(transaction.changes)
|
|
|
|
|
|
|
|
for (const effect of transaction.effects) {
|
2024-08-21 05:31:14 -04:00
|
|
|
if (effect.is(updateRangesEffect)) {
|
|
|
|
this.decorations = buildChangeDecorations(effect.value)
|
2024-08-21 04:34:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decorations: value => value.decorations,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
|
2024-09-19 06:08:20 -04:00
|
|
|
// draw highlight decorations
|
|
|
|
ViewPlugin.define<
|
|
|
|
PluginValue & {
|
|
|
|
decorations: DecorationSet
|
|
|
|
}
|
|
|
|
>(
|
|
|
|
() => {
|
|
|
|
return {
|
|
|
|
decorations: Decoration.none,
|
|
|
|
update(update) {
|
|
|
|
for (const transaction of update.transactions) {
|
|
|
|
this.decorations = this.decorations.map(transaction.changes)
|
|
|
|
|
|
|
|
for (const effect of transaction.effects) {
|
|
|
|
if (effect.is(highlightRangesEffect)) {
|
|
|
|
this.decorations = buildHighlightDecorations(
|
|
|
|
'ol-cm-change-highlight',
|
|
|
|
effect.value
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decorations: value => value.decorations,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
|
|
|
|
// draw focus decorations
|
|
|
|
ViewPlugin.define<
|
|
|
|
PluginValue & {
|
|
|
|
decorations: DecorationSet
|
|
|
|
}
|
|
|
|
>(
|
|
|
|
() => {
|
|
|
|
return {
|
|
|
|
decorations: Decoration.none,
|
|
|
|
update(update) {
|
|
|
|
this.decorations = Decoration.none
|
|
|
|
|
|
|
|
if (!ranges) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const range of [...ranges.changes, ...ranges.comments]) {
|
|
|
|
if (isSelectionWithinOp(range.op, update.state.selection.main)) {
|
|
|
|
this.decorations = buildHighlightDecorations(
|
|
|
|
'ol-cm-change-focus',
|
|
|
|
range.op
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decorations: value => value.decorations,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
|
2024-08-21 04:34:56 -04:00
|
|
|
// styles for change decorations
|
|
|
|
trackChangesTheme,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2024-08-21 05:31:14 -04:00
|
|
|
const buildChangeDecorations = (data: RangesData) => {
|
|
|
|
if (!data.ranges) {
|
2024-08-21 04:34:56 -04:00
|
|
|
return Decoration.none
|
|
|
|
}
|
|
|
|
|
2024-08-21 05:31:14 -04:00
|
|
|
const changes = [...data.ranges.changes, ...data.ranges.comments]
|
2024-08-21 04:34:56 -04:00
|
|
|
|
|
|
|
const decorations = []
|
|
|
|
|
|
|
|
for (const change of changes) {
|
|
|
|
try {
|
2024-08-21 05:31:14 -04:00
|
|
|
decorations.push(...createChangeRange(change, data))
|
2024-08-21 04:34:56 -04:00
|
|
|
} catch (error) {
|
|
|
|
// ignore invalid changes
|
|
|
|
debugConsole.debug('invalid change position', error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Decoration.set(decorations, true)
|
|
|
|
}
|
|
|
|
|
2024-09-19 06:08:20 -04:00
|
|
|
const buildHighlightDecorations = (className: string, op?: AnyOperation) => {
|
|
|
|
if (!op) {
|
|
|
|
return Decoration.none
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDeleteOperation(op)) {
|
|
|
|
// nothing to highlight for deletions (for now)
|
|
|
|
// TODO: add highlight when delete indicator is done
|
|
|
|
return Decoration.none
|
|
|
|
}
|
|
|
|
|
|
|
|
const opFrom = op.p
|
|
|
|
const opLength = isInsertOperation(op) ? op.i.length : op.c.length
|
|
|
|
const opType = isInsertOperation(op) ? 'i' : 'c'
|
|
|
|
|
|
|
|
return Decoration.set(
|
|
|
|
Decoration.mark({
|
|
|
|
class: `${className} ${className}-${opType}`,
|
|
|
|
}).range(opFrom, opFrom + opLength),
|
|
|
|
true
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-08-21 04:34:56 -04:00
|
|
|
class ChangeDeletedWidget extends WidgetType {
|
|
|
|
constructor(public change: Change<DeleteOperation>) {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
toDOM() {
|
|
|
|
const widget = document.createElement('span')
|
|
|
|
widget.classList.add('ol-cm-change')
|
|
|
|
widget.classList.add('ol-cm-change-d')
|
|
|
|
|
|
|
|
return widget
|
|
|
|
}
|
|
|
|
|
|
|
|
eq() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-21 05:31:14 -04:00
|
|
|
const createChangeRange = (change: Change, data: RangesData) => {
|
2024-08-21 04:34:56 -04:00
|
|
|
const { id, metadata, op } = change
|
|
|
|
|
|
|
|
const from = op.p
|
|
|
|
// TODO: find valid positions?
|
|
|
|
|
|
|
|
if (isDeleteOperation(op)) {
|
|
|
|
const opType = 'd'
|
|
|
|
|
|
|
|
const changeWidget = Decoration.widget({
|
|
|
|
widget: new ChangeDeletedWidget(change as Change<DeleteOperation>),
|
|
|
|
side: 1,
|
|
|
|
opType,
|
|
|
|
id,
|
|
|
|
metadata,
|
|
|
|
})
|
|
|
|
|
2024-08-21 05:32:59 -04:00
|
|
|
return [changeWidget.range(from, from)]
|
2024-08-21 04:34:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const _isCommentOperation = isCommentOperation(op)
|
|
|
|
|
|
|
|
if (_isCommentOperation) {
|
2024-08-21 05:31:14 -04:00
|
|
|
const thread = data.threads[op.t]
|
|
|
|
if (!thread || thread.resolved) {
|
2024-08-21 04:34:56 -04:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const opType = _isCommentOperation ? 'c' : 'i'
|
|
|
|
const changedText = _isCommentOperation ? op.c : op.i
|
|
|
|
const to = from + changedText.length
|
|
|
|
|
|
|
|
// Mark decorations must not be empty
|
|
|
|
if (from === to) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
const changeMark = Decoration.mark({
|
|
|
|
tagName: 'span',
|
|
|
|
class: `ol-cm-change ol-cm-change-${opType}`,
|
|
|
|
opType,
|
|
|
|
id,
|
|
|
|
metadata,
|
|
|
|
})
|
|
|
|
|
2024-08-21 05:32:59 -04:00
|
|
|
return [changeMark.range(from, to)]
|
2024-08-21 04:34:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const trackChangesTheme = EditorView.baseTheme({
|
|
|
|
'&light .ol-cm-change-i': {
|
|
|
|
backgroundColor: '#2c8e304d',
|
|
|
|
},
|
|
|
|
'&dark .ol-cm-change-i': {
|
|
|
|
backgroundColor: 'rgba(37, 107, 41, 0.15)',
|
|
|
|
},
|
|
|
|
'&light .ol-cm-change-c': {
|
|
|
|
backgroundColor: '#f3b1114d',
|
|
|
|
},
|
|
|
|
'&dark .ol-cm-change-c': {
|
|
|
|
backgroundColor: 'rgba(194, 93, 11, 0.15)',
|
|
|
|
},
|
|
|
|
'.ol-cm-change': {
|
|
|
|
padding: 'var(--half-leading, 0) 0',
|
|
|
|
},
|
|
|
|
'.ol-cm-change-d': {
|
|
|
|
borderLeft: '2px dotted #c5060b',
|
|
|
|
marginLeft: '-1px',
|
|
|
|
},
|
2024-09-19 06:08:20 -04:00
|
|
|
'&light .ol-cm-change-highlight-i': {
|
|
|
|
backgroundColor: '#b8dbc899',
|
|
|
|
},
|
|
|
|
'&dark .ol-cm-change-highlight-i': {
|
|
|
|
backgroundColor: '#b8dbc899',
|
|
|
|
},
|
|
|
|
'&light .ol-cm-change-highlight-c': {
|
|
|
|
backgroundColor: '#fcc4837d',
|
|
|
|
},
|
|
|
|
'&dark .ol-cm-change-highlight-c': {
|
|
|
|
backgroundColor: '#fcc4837d',
|
|
|
|
},
|
|
|
|
'&light .ol-cm-change-focus-i': {
|
|
|
|
backgroundColor: '#B8DBC8',
|
|
|
|
},
|
|
|
|
'&dark .ol-cm-change-focus-i': {
|
|
|
|
backgroundColor: '#B8DBC8',
|
|
|
|
},
|
|
|
|
'&light .ol-cm-change-focus-c': {
|
|
|
|
backgroundColor: '#FCC483',
|
|
|
|
},
|
|
|
|
'&dark .ol-cm-change-focus-c': {
|
|
|
|
backgroundColor: '#FCC483',
|
|
|
|
},
|
2024-08-21 04:34:56 -04:00
|
|
|
})
|