|
@@ -1,10 +1,4 @@
|
|
|
-import {
|
|
|
|
|
- EditorState,
|
|
|
|
|
- RangeSet,
|
|
|
|
|
- StateEffect,
|
|
|
|
|
- StateField,
|
|
|
|
|
- Transaction,
|
|
|
|
|
-} from '@codemirror/state'
|
|
|
|
|
|
|
+import { StateEffect } from '@codemirror/state'
|
|
|
import {
|
|
import {
|
|
|
Decoration,
|
|
Decoration,
|
|
|
type DecorationSet,
|
|
type DecorationSet,
|
|
@@ -13,14 +7,6 @@ import {
|
|
|
ViewPlugin,
|
|
ViewPlugin,
|
|
|
WidgetType,
|
|
WidgetType,
|
|
|
} from '@codemirror/view'
|
|
} from '@codemirror/view'
|
|
|
-import {
|
|
|
|
|
- findCommentsInCut,
|
|
|
|
|
- findDetachedCommentsInChanges,
|
|
|
|
|
- restoreCommentsOnPaste,
|
|
|
|
|
- restoreDetachedComments,
|
|
|
|
|
- StoredComment,
|
|
|
|
|
-} from './changes/comments'
|
|
|
|
|
-import { invertedEffects } from '@codemirror/commands'
|
|
|
|
|
import { Change, DeleteOperation } from '../../../../../types/change'
|
|
import { Change, DeleteOperation } from '../../../../../types/change'
|
|
|
import { ChangeManager } from './changes/change-manager'
|
|
import { ChangeManager } from './changes/change-manager'
|
|
|
import { debugConsole } from '@/utils/debugging'
|
|
import { debugConsole } from '@/utils/debugging'
|
|
@@ -32,17 +18,6 @@ import {
|
|
|
|
|
|
|
|
const clearChangesEffect = StateEffect.define()
|
|
const clearChangesEffect = StateEffect.define()
|
|
|
const buildChangesEffect = StateEffect.define()
|
|
const buildChangesEffect = StateEffect.define()
|
|
|
-const restoreDetachedCommentsEffect = StateEffect.define<RangeSet<any>>({
|
|
|
|
|
- map: (value, mapping) => {
|
|
|
|
|
- return value
|
|
|
|
|
- .update({
|
|
|
|
|
- filter: (from, to) => {
|
|
|
|
|
- return from <= mapping.length && to <= mapping.length
|
|
|
|
|
- },
|
|
|
|
|
- })
|
|
|
|
|
- .map(mapping)
|
|
|
|
|
- },
|
|
|
|
|
-})
|
|
|
|
|
|
|
|
|
|
type Options = {
|
|
type Options = {
|
|
|
currentDoc: DocumentContainer
|
|
currentDoc: DocumentContainer
|
|
@@ -57,68 +32,7 @@ export const trackChanges = (
|
|
|
{ currentDoc, loadingThreads }: Options,
|
|
{ currentDoc, loadingThreads }: Options,
|
|
|
changeManager: ChangeManager
|
|
changeManager: ChangeManager
|
|
|
) => {
|
|
) => {
|
|
|
- // A state field that stored any comments found within the ranges of a "cut" transaction,
|
|
|
|
|
- // to be restored when pasting matching text.
|
|
|
|
|
- const cutCommentsState = StateField.define<StoredComment[]>({
|
|
|
|
|
- create: () => {
|
|
|
|
|
- return []
|
|
|
|
|
- },
|
|
|
|
|
- update: (value, transaction) => {
|
|
|
|
|
- if (transaction.annotation(Transaction.remote)) {
|
|
|
|
|
- return value
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (!transaction.docChanged) {
|
|
|
|
|
- return value
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (transaction.isUserEvent('delete.cut')) {
|
|
|
|
|
- return findCommentsInCut(currentDoc, transaction)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (transaction.isUserEvent('input.paste')) {
|
|
|
|
|
- restoreCommentsOnPaste(currentDoc, transaction, value)
|
|
|
|
|
- return []
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return value
|
|
|
|
|
- },
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
return [
|
|
return [
|
|
|
- // attach any comments detached by the transaction as an inverted effect, to be applied on undo
|
|
|
|
|
- invertedEffects.of(transaction => {
|
|
|
|
|
- if (
|
|
|
|
|
- transaction.docChanged &&
|
|
|
|
|
- !transaction.annotation(Transaction.remote)
|
|
|
|
|
- ) {
|
|
|
|
|
- const detachedComments = findDetachedCommentsInChanges(
|
|
|
|
|
- currentDoc,
|
|
|
|
|
- transaction
|
|
|
|
|
- )
|
|
|
|
|
- if (detachedComments.size) {
|
|
|
|
|
- return [restoreDetachedCommentsEffect.of(detachedComments)]
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return []
|
|
|
|
|
- }),
|
|
|
|
|
-
|
|
|
|
|
- // restore any detached comments on undo
|
|
|
|
|
- EditorState.transactionExtender.of(transaction => {
|
|
|
|
|
- for (const effect of transaction.effects) {
|
|
|
|
|
- if (effect.is(restoreDetachedCommentsEffect)) {
|
|
|
|
|
- // send the comments to the ShareJS doc
|
|
|
|
|
- restoreDetachedComments(currentDoc, transaction, effect.value)
|
|
|
|
|
-
|
|
|
|
|
- // return a transaction spec to rebuild the change markers
|
|
|
|
|
- return buildChangeMarkers()
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return null
|
|
|
|
|
- }),
|
|
|
|
|
-
|
|
|
|
|
- cutCommentsState,
|
|
|
|
|
-
|
|
|
|
|
// initialize/destroy the change manager, and handle any updates
|
|
// initialize/destroy the change manager, and handle any updates
|
|
|
ViewPlugin.define(() => {
|
|
ViewPlugin.define(() => {
|
|
|
changeManager.initialize()
|
|
changeManager.initialize()
|