2024-10-02 07:58:39 -04:00
|
|
|
import { StateEffect, StateField, 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 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-10-02 08:41:47 -04:00
|
|
|
const highlightRangesEffect = StateEffect.define<AnyOperation>()
|
|
|
|
const clearHighlightRangesEffect = StateEffect.define<AnyOperation>()
|
2024-08-21 04:34:56 -04:00
|
|
|
|
|
|
|
export const updateRanges = (data: RangesData): TransactionSpec => {
|
|
|
|
return {
|
|
|
|
effects: updateRangesEffect.of(data),
|
|
|
|
}
|
|
|
|
}
|
2024-10-02 08:41:47 -04:00
|
|
|
export const highlightRanges = (op: AnyOperation): TransactionSpec => {
|
2024-09-19 06:08:20 -04:00
|
|
|
return {
|
|
|
|
effects: highlightRangesEffect.of(op),
|
|
|
|
}
|
|
|
|
}
|
2024-10-02 08:41:47 -04:00
|
|
|
export const clearHighlightRanges = (op: AnyOperation): TransactionSpec => {
|
|
|
|
return {
|
|
|
|
effects: clearHighlightRangesEffect.of(op),
|
|
|
|
}
|
|
|
|
}
|
2024-09-19 06:08:20 -04:00
|
|
|
|
2024-10-02 07:58:39 -04:00
|
|
|
export const rangesDataField = StateField.define<RangesData | null>({
|
|
|
|
create() {
|
|
|
|
return null
|
|
|
|
},
|
|
|
|
update(rangesData, tr) {
|
|
|
|
for (const effect of tr.effects) {
|
|
|
|
if (effect.is(updateRangesEffect)) {
|
|
|
|
return effect.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rangesData
|
|
|
|
},
|
|
|
|
})
|
2024-08-21 04:34:56 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A custom extension that initialises the change manager, passes any updates to it,
|
|
|
|
* and produces decorations for tracked changes and comments.
|
|
|
|
*/
|
2024-10-02 07:58:39 -04:00
|
|
|
export const ranges = () => [
|
|
|
|
rangesDataField,
|
|
|
|
// handle viewportChanged updates
|
|
|
|
ViewPlugin.define(view => {
|
|
|
|
let timer: number
|
|
|
|
|
|
|
|
return {
|
|
|
|
update(update) {
|
|
|
|
if (update.viewportChanged) {
|
|
|
|
if (timer) {
|
|
|
|
window.clearTimeout(timer)
|
|
|
|
}
|
|
|
|
|
|
|
|
timer = window.setTimeout(() => {
|
|
|
|
dispatchEvent(new Event('editor:viewport-changed'))
|
|
|
|
}, 25)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}),
|
2024-08-21 05:31:14 -04:00
|
|
|
|
2024-10-02 07:58:39 -04:00
|
|
|
// draw change decorations
|
|
|
|
ViewPlugin.define<
|
|
|
|
PluginValue & {
|
|
|
|
decorations: DecorationSet
|
|
|
|
}
|
|
|
|
>(
|
|
|
|
() => {
|
2024-08-21 05:31:14 -04:00
|
|
|
return {
|
2024-10-02 07:58:39 -04:00
|
|
|
decorations: Decoration.none,
|
2024-08-21 05:31:14 -04:00
|
|
|
update(update) {
|
2024-10-02 07:58:39 -04:00
|
|
|
for (const transaction of update.transactions) {
|
|
|
|
this.decorations = this.decorations.map(transaction.changes)
|
2024-08-21 04:34:56 -04:00
|
|
|
|
2024-10-02 07:58:39 -04:00
|
|
|
for (const effect of transaction.effects) {
|
|
|
|
if (effect.is(updateRangesEffect)) {
|
|
|
|
this.decorations = buildChangeDecorations(effect.value)
|
2024-10-02 08:41:47 -04:00
|
|
|
} else if (
|
|
|
|
effect.is(highlightRangesEffect) &&
|
|
|
|
isDeleteOperation(effect.value)
|
|
|
|
) {
|
|
|
|
this.decorations = updateDeleteWidgetHighlight(
|
|
|
|
this.decorations,
|
|
|
|
widget =>
|
|
|
|
widget.change.op.p === effect.value.p &&
|
|
|
|
widget.highlightType !== 'focus',
|
|
|
|
'highlight'
|
|
|
|
)
|
|
|
|
} else if (
|
|
|
|
effect.is(clearHighlightRangesEffect) &&
|
|
|
|
isDeleteOperation(effect.value)
|
|
|
|
) {
|
|
|
|
this.decorations = updateDeleteWidgetHighlight(
|
|
|
|
this.decorations,
|
|
|
|
widget =>
|
|
|
|
widget.change.op.p === effect.value.p &&
|
|
|
|
widget.highlightType !== 'focus',
|
|
|
|
null
|
|
|
|
)
|
2024-10-02 07:58:39 -04:00
|
|
|
}
|
|
|
|
}
|
2024-10-02 08:41:47 -04:00
|
|
|
|
|
|
|
if (transaction.selection) {
|
|
|
|
this.decorations = updateDeleteWidgetHighlight(
|
|
|
|
this.decorations,
|
|
|
|
({ change }) =>
|
|
|
|
isSelectionWithinOp(change.op, update.state.selection.main),
|
|
|
|
'focus'
|
|
|
|
)
|
|
|
|
this.decorations = updateDeleteWidgetHighlight(
|
|
|
|
this.decorations,
|
|
|
|
({ change }) =>
|
|
|
|
!isSelectionWithinOp(change.op, update.state.selection.main),
|
|
|
|
null
|
|
|
|
)
|
|
|
|
}
|
2024-08-21 04:34:56 -04:00
|
|
|
}
|
2024-08-21 05:31:14 -04:00
|
|
|
},
|
|
|
|
}
|
2024-10-02 07:58:39 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
decorations: value => value.decorations,
|
|
|
|
}
|
|
|
|
),
|
2024-08-21 04:34:56 -04:00
|
|
|
|
2024-10-02 07:58:39 -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)
|
2024-08-21 04:34:56 -04:00
|
|
|
|
2024-10-02 07:58:39 -04:00
|
|
|
for (const effect of transaction.effects) {
|
|
|
|
if (effect.is(highlightRangesEffect)) {
|
|
|
|
this.decorations = buildHighlightDecorations(
|
|
|
|
'ol-cm-change-highlight',
|
|
|
|
effect.value
|
|
|
|
)
|
2024-10-02 08:41:47 -04:00
|
|
|
} else if (effect.is(clearHighlightRangesEffect)) {
|
|
|
|
this.decorations = Decoration.none
|
2024-09-19 06:08:20 -04:00
|
|
|
}
|
|
|
|
}
|
2024-10-02 07:58:39 -04:00
|
|
|
}
|
|
|
|
},
|
2024-09-19 06:08:20 -04:00
|
|
|
}
|
2024-10-02 07:58:39 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
decorations: value => value.decorations,
|
|
|
|
}
|
|
|
|
),
|
2024-09-19 06:08:20 -04:00
|
|
|
|
2024-10-02 07:58:39 -04:00
|
|
|
// draw focus decorations
|
|
|
|
ViewPlugin.define<
|
|
|
|
PluginValue & {
|
|
|
|
decorations: DecorationSet
|
|
|
|
}
|
|
|
|
>(
|
|
|
|
view => {
|
|
|
|
return {
|
|
|
|
decorations: Decoration.none,
|
|
|
|
update(update) {
|
|
|
|
if (
|
|
|
|
!update.transactions.some(
|
|
|
|
tr =>
|
|
|
|
tr.selection ||
|
|
|
|
tr.effects.some(effect => effect.is(updateRangesEffect))
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return
|
|
|
|
}
|
2024-09-19 06:08:20 -04:00
|
|
|
|
2024-10-02 07:58:39 -04:00
|
|
|
this.decorations = Decoration.none
|
|
|
|
const rangesData = view.state.field(rangesDataField)
|
|
|
|
|
|
|
|
if (!rangesData?.ranges) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const { changes, comments } = rangesData.ranges
|
|
|
|
const unresolvedComments = rangesData.threads
|
|
|
|
? comments.filter(
|
|
|
|
comment =>
|
2024-10-04 05:16:01 -04:00
|
|
|
comment.op.t &&
|
|
|
|
rangesData.threads[comment.op.t] &&
|
|
|
|
!rangesData.threads[comment.op.t].resolved
|
2024-10-02 07:58:39 -04:00
|
|
|
)
|
|
|
|
: []
|
|
|
|
|
|
|
|
for (const range of [...changes, ...unresolvedComments]) {
|
|
|
|
if (isSelectionWithinOp(range.op, update.state.selection.main)) {
|
|
|
|
this.decorations = buildHighlightDecorations(
|
|
|
|
'ol-cm-change-focus',
|
|
|
|
range.op
|
|
|
|
)
|
|
|
|
break
|
2024-09-19 06:08:20 -04:00
|
|
|
}
|
2024-10-02 07:58:39 -04:00
|
|
|
}
|
|
|
|
},
|
2024-09-19 06:08:20 -04:00
|
|
|
}
|
2024-10-02 07:58:39 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
decorations: value => value.decorations,
|
|
|
|
}
|
|
|
|
),
|
2024-09-19 06:08:20 -04:00
|
|
|
|
2024-10-02 07:58:39 -04:00
|
|
|
// styles for change decorations
|
|
|
|
trackChangesTheme,
|
|
|
|
]
|
2024-08-21 04:34:56 -04:00
|
|
|
|
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-10-02 08:41:47 -04:00
|
|
|
const updateDeleteWidgetHighlight = (
|
|
|
|
decorations: DecorationSet,
|
|
|
|
predicate: (widget: ChangeDeletedWidget) => boolean,
|
|
|
|
highlightType?: 'focus' | 'highlight' | null
|
|
|
|
) => {
|
|
|
|
const widgetsToReplace: ChangeDeletedWidget[] = []
|
|
|
|
const cursor = decorations.iter()
|
|
|
|
while (cursor.value) {
|
|
|
|
const widget = cursor.value.spec?.widget
|
|
|
|
if (widget instanceof ChangeDeletedWidget && predicate(widget)) {
|
|
|
|
widgetsToReplace.push(cursor.value.spec.widget)
|
|
|
|
}
|
|
|
|
cursor.next()
|
2024-09-19 06:08:20 -04:00
|
|
|
}
|
|
|
|
|
2024-10-02 08:41:47 -04:00
|
|
|
return decorations.update({
|
|
|
|
filter: (from, to, decoration) => {
|
|
|
|
return !widgetsToReplace.includes(decoration.spec?.widget)
|
|
|
|
},
|
|
|
|
add: widgetsToReplace.map(({ change }) =>
|
|
|
|
Decoration.widget({
|
|
|
|
widget: new ChangeDeletedWidget(change, highlightType),
|
|
|
|
side: 1,
|
|
|
|
opType: 'd',
|
|
|
|
id: change.id,
|
|
|
|
metadata: change.metadata,
|
|
|
|
}).range(change.op.p, change.op.p)
|
|
|
|
),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const buildHighlightDecorations = (className: string, op: AnyOperation) => {
|
2024-09-19 06:08:20 -04:00
|
|
|
if (isDeleteOperation(op)) {
|
2024-10-02 08:41:47 -04:00
|
|
|
// delete indicators are handled in change decorations
|
2024-09-19 06:08:20 -04:00
|
|
|
return Decoration.none
|
|
|
|
}
|
|
|
|
|
|
|
|
const opFrom = op.p
|
|
|
|
const opLength = isInsertOperation(op) ? op.i.length : op.c.length
|
|
|
|
const opType = isInsertOperation(op) ? 'i' : 'c'
|
|
|
|
|
2024-10-02 08:41:47 -04:00
|
|
|
if (opLength === 0) {
|
|
|
|
return Decoration.none
|
|
|
|
}
|
|
|
|
|
2024-09-19 06:08:20 -04:00
|
|
|
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 {
|
2024-10-02 08:41:47 -04:00
|
|
|
constructor(
|
|
|
|
public change: Change<DeleteOperation>,
|
|
|
|
public highlightType: 'highlight' | 'focus' | null = null
|
|
|
|
) {
|
2024-08-21 04:34:56 -04:00
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
toDOM() {
|
|
|
|
const widget = document.createElement('span')
|
|
|
|
widget.classList.add('ol-cm-change')
|
|
|
|
widget.classList.add('ol-cm-change-d')
|
2024-10-02 08:41:47 -04:00
|
|
|
widget.textContent = '[ — ]'
|
|
|
|
if (this.highlightType) {
|
|
|
|
widget.classList.add(`ol-cm-change-d-${this.highlightType}`)
|
|
|
|
}
|
2024-08-21 04:34:56 -04:00
|
|
|
return widget
|
|
|
|
}
|
|
|
|
|
2024-10-02 08:41:47 -04:00
|
|
|
eq(old: ChangeDeletedWidget) {
|
|
|
|
return old.highlightType === this.highlightType
|
2024-08-21 04:34:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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',
|
|
|
|
},
|
2024-10-02 07:58:10 -04:00
|
|
|
'.ol-cm-change-highlight': {
|
|
|
|
padding: 'var(--half-leading, 0) 0',
|
|
|
|
},
|
|
|
|
'.ol-cm-change-focus': {
|
|
|
|
padding: 'var(--half-leading, 0) 0',
|
|
|
|
},
|
2024-10-02 08:41:47 -04:00
|
|
|
// TODO: fix dark mode colors
|
|
|
|
'&light .ol-cm-change-d': {
|
|
|
|
color: '#c5060b',
|
|
|
|
backgroundColor: '#f5beba57',
|
|
|
|
},
|
|
|
|
'&dark .ol-cm-change-d': {
|
|
|
|
color: '#c5060b',
|
|
|
|
backgroundColor: '#f5beba57',
|
|
|
|
},
|
|
|
|
'&light .ol-cm-change-d-highlight': {
|
|
|
|
backgroundColor: '#f5bebaa4',
|
|
|
|
},
|
|
|
|
'&dark .ol-cm-change-d-highlight': {
|
|
|
|
backgroundColor: '#f5bebaa4',
|
|
|
|
},
|
|
|
|
'&light .ol-cm-change-d-focus': {
|
|
|
|
backgroundColor: '#F5BEBA',
|
|
|
|
},
|
|
|
|
'&dark .ol-cm-change-d-focus': {
|
|
|
|
backgroundColor: '#F5BEBA',
|
2024-08-21 04:34:56 -04:00
|
|
|
},
|
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
|
|
|
})
|