add annotation options

This commit is contained in:
Philipp Kühn
2021-02-11 22:48:04 +01:00
parent cd149001b1
commit c13db6bc8e
3 changed files with 34 additions and 3 deletions

View File

@@ -1,10 +1,17 @@
// @ts-nocheck
import { Plugin, PluginKey } from 'prosemirror-state' import { Plugin, PluginKey } from 'prosemirror-state'
import { AnnotationState } from './AnnotationState' import { AnnotationState } from './AnnotationState'
export const AnnotationPluginKey = new PluginKey('annotation') export const AnnotationPluginKey = new PluginKey('annotation')
export const AnnotationPlugin = (options: any) => new Plugin({ export interface AnnotationPluginOptions {
HTMLAttributes: {
[key: string]: any
},
onUpdate: (items: [any?]) => {},
map: any,
}
export const AnnotationPlugin = (options: AnnotationPluginOptions) => new Plugin({
key: AnnotationPluginKey, key: AnnotationPluginKey,
state: { state: {
init(_, state) { init(_, state) {

View File

@@ -11,6 +11,18 @@ export interface AnnotationOptions {
[key: string]: any [key: string]: any
}, },
onUpdate: (items: [any?]) => {}, onUpdate: (items: [any?]) => {},
/**
* An initialized Y.js document.
*/
document: any,
/**
* Name of a Y.js fragment, can be changed to sync multiple fields with one Y.js document.
*/
field: string,
/**
* A raw Y.js map, can be used instead of `document` and `field`.
*/
map: any,
} }
export const Annotation = Extension.create({ export const Annotation = Extension.create({
@@ -21,6 +33,9 @@ export const Annotation = Extension.create({
class: 'annotation', class: 'annotation',
}, },
onUpdate: decorations => decorations, onUpdate: decorations => decorations,
document: null,
field: 'annotations',
map: null,
}, },
addCommands() { addCommands() {
@@ -57,8 +72,16 @@ export const Annotation = Extension.create({
}, },
addProseMirrorPlugins() { addProseMirrorPlugins() {
const map = this.options.map
? this.options.map
: this.options.document.getMap(this.options.field)
return [ return [
AnnotationPlugin(this.options), AnnotationPlugin({
HTMLAttributes: this.options.HTMLAttributes,
onUpdate: this.options.onUpdate,
map,
}),
] ]
}, },
}) })

View File

@@ -74,6 +74,7 @@ export default {
Bold, Bold,
Heading, Heading,
Annotation.configure({ Annotation.configure({
document: this.ydoc,
onUpdate: items => { this.comments = items }, onUpdate: items => { this.comments = items },
}), }),
Collaboration.configure({ Collaboration.configure({