refactoring
This commit is contained in:
@@ -44,66 +44,49 @@ export class AnnotationState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
apply(transaction: any, state: EditorState) {
|
apply(transaction: any, state: EditorState) {
|
||||||
|
const { map, HTMLAttributes } = this.options
|
||||||
const ystate = ySyncPluginKey.getState(state)
|
const ystate = ySyncPluginKey.getState(state)
|
||||||
const decs = this.options.map
|
const { doc, type, binding } = ystate
|
||||||
|
|
||||||
const action = transaction.getMeta(AnnotationPluginKey)
|
const action = transaction.getMeta(AnnotationPluginKey)
|
||||||
const actionType = action && action.type
|
|
||||||
|
|
||||||
if (action) {
|
if (action && action.type) {
|
||||||
const { decorations } = this
|
const { from, to, data } = action
|
||||||
|
|
||||||
if (actionType === 'addAnnotation') {
|
if (action.type === 'addAnnotation') {
|
||||||
decs.set(action.data.id, {
|
const absoluteFrom = absolutePositionToRelativePosition(from, type, binding.mapping)
|
||||||
from: absolutePositionToRelativePosition(
|
const absoluteTo = absolutePositionToRelativePosition(to, type, binding.mapping)
|
||||||
action.from,
|
|
||||||
ystate.type,
|
map.set(data.id, {
|
||||||
ystate.binding.mapping,
|
from: absoluteFrom,
|
||||||
),
|
to: absoluteTo,
|
||||||
to: absolutePositionToRelativePosition(
|
data,
|
||||||
action.to,
|
|
||||||
ystate.type,
|
|
||||||
ystate.binding.mapping,
|
|
||||||
),
|
|
||||||
data: action.data,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
this.decorations = decorations.add(transaction.doc, [
|
const decoration = Decoration.inline(from, to, HTMLAttributes, { data })
|
||||||
Decoration.inline(action.from, action.to, this.options.HTMLAttributes, { data: action.data }),
|
|
||||||
])
|
|
||||||
} else if (actionType === 'deleteAnnotation') {
|
|
||||||
decs.delete(action.id)
|
|
||||||
|
|
||||||
this.decorations = decorations.remove([
|
this.decorations = this.decorations.add(transaction.doc, [decoration])
|
||||||
this.findAnnotation(action.id),
|
}
|
||||||
])
|
|
||||||
|
if (action.type === 'deleteAnnotation') {
|
||||||
|
map.delete(action.id)
|
||||||
|
|
||||||
|
const decoration = this.findAnnotation(action.id)
|
||||||
|
|
||||||
|
this.decorations = this.decorations.remove([decoration])
|
||||||
}
|
}
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ystate && ystate.isChangeOrigin) {
|
if (ystate.isChangeOrigin) {
|
||||||
const decorations = [];
|
|
||||||
|
|
||||||
[...decs.keys()].forEach(id => {
|
const decorations = Array.from(map.keys()).map(id => {
|
||||||
const dec = decs.get(id)
|
const dec = map.get(id)
|
||||||
|
const from = relativePositionToAbsolutePosition(doc, type, dec.from, binding.mapping)
|
||||||
|
const to = relativePositionToAbsolutePosition(doc, type, dec.to, binding.mapping)
|
||||||
|
const decoration = Decoration.inline(from, to, HTMLAttributes, { data: dec.data })
|
||||||
|
|
||||||
decorations.push(Decoration.inline(
|
return decoration
|
||||||
relativePositionToAbsolutePosition(
|
|
||||||
ystate.doc,
|
|
||||||
ystate.type,
|
|
||||||
dec.from,
|
|
||||||
ystate.binding.mapping,
|
|
||||||
),
|
|
||||||
relativePositionToAbsolutePosition(
|
|
||||||
ystate.doc,
|
|
||||||
ystate.type,
|
|
||||||
dec.to,
|
|
||||||
ystate.binding.mapping,
|
|
||||||
),
|
|
||||||
this.options.HTMLAttributes,
|
|
||||||
{ data: dec.data },
|
|
||||||
))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
this.decorations = DecorationSet.create(state.doc, decorations)
|
this.decorations = DecorationSet.create(state.doc, decorations)
|
||||||
|
|||||||
Reference in New Issue
Block a user