mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 14:13:43 +01:00
Split add-comment to own extension and always register command GitOrigin-RevId: 2e25643b7fe4ff30693479975945d94cc586bd61
56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import { Decoration, DecorationSet, EditorView } from '@codemirror/view'
|
|
import {
|
|
StateField,
|
|
StateEffect,
|
|
Range,
|
|
SelectionRange,
|
|
} from '@codemirror/state'
|
|
import { v4 as uuid } from 'uuid'
|
|
|
|
export const addNewCommentRangeEffect = StateEffect.define<Range<Decoration>>()
|
|
|
|
export const removeNewCommentRangeEffect = StateEffect.define<string>()
|
|
|
|
export const buildAddNewCommentRangeEffect = (range: SelectionRange) => {
|
|
return addNewCommentRangeEffect.of(
|
|
Decoration.mark({
|
|
tagName: 'span',
|
|
class: `ol-cm-change ol-cm-change-c`,
|
|
opType: 'c',
|
|
id: uuid(),
|
|
}).range(range.from, range.to)
|
|
)
|
|
}
|
|
|
|
export const addCommentRangesField = StateField.define<DecorationSet>({
|
|
create() {
|
|
return Decoration.none
|
|
},
|
|
|
|
update(addCommentRanges, tr) {
|
|
addCommentRanges = addCommentRanges.map(tr.changes)
|
|
|
|
for (const effect of tr.effects) {
|
|
if (effect.is(removeNewCommentRangeEffect)) {
|
|
const threadId = effect.value
|
|
addCommentRanges = addCommentRanges.update({
|
|
filter: (_from, _to, value) => {
|
|
return value.spec.id !== threadId
|
|
},
|
|
})
|
|
}
|
|
|
|
if (effect.is(addNewCommentRangeEffect)) {
|
|
const rangeToAdd = effect.value
|
|
addCommentRanges = addCommentRanges.update({
|
|
add: [rangeToAdd],
|
|
})
|
|
}
|
|
}
|
|
|
|
return addCommentRanges
|
|
},
|
|
|
|
provide: field => EditorView.decorations.from(field),
|
|
})
|