Merge branch 'main' of https://github.com/ueberdosis/tiptap-next into main
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
export class AnnotationItem {
|
export class AnnotationItem {
|
||||||
public id!: string
|
public id!: string
|
||||||
|
|
||||||
public text!: string
|
public content!: string
|
||||||
|
|
||||||
constructor(id: string, text: string) {
|
constructor(id: string, content: string) {
|
||||||
this.id = id
|
this.id = id
|
||||||
this.text = text
|
this.content = content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,9 +22,6 @@ export class AnnotationState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
findAnnotation(id: string) {
|
findAnnotation(id: string) {
|
||||||
// TODO: Get from Y.js?
|
|
||||||
// this.decorations.get(id)
|
|
||||||
|
|
||||||
const current = this.decorations.find()
|
const current = this.decorations.find()
|
||||||
|
|
||||||
for (let i = 0; i < current.length; i += 1) {
|
for (let i = 0; i < current.length; i += 1) {
|
||||||
@@ -67,7 +64,7 @@ export class AnnotationState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
annotationsAt(position: number) {
|
annotationsAt(position: number) {
|
||||||
return this.decorations?.find(position, position)
|
return this.decorations.find(position, position)
|
||||||
}
|
}
|
||||||
|
|
||||||
updateDecorations(state: EditorState) {
|
updateDecorations(state: EditorState) {
|
||||||
@@ -87,16 +84,16 @@ export class AnnotationState {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const decoration = Decoration.inline(from, to, HTMLAttributes, { data: dec.data })
|
return decorations.push(
|
||||||
|
Decoration.inline(from, to, HTMLAttributes, { data: dec.data }),
|
||||||
return decorations.push(decoration)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.decorations = DecorationSet.create(state.doc, decorations)
|
this.decorations = DecorationSet.create(state.doc, decorations)
|
||||||
}
|
}
|
||||||
|
|
||||||
apply(transaction: Transaction, state: EditorState) {
|
apply(transaction: Transaction, state: EditorState) {
|
||||||
const ystate = ySyncPluginKey.getState(state)
|
// Add/Remove annotations
|
||||||
const action = transaction.getMeta(AnnotationPluginKey) as AddAnnotationAction | DeleteAnnotationAction
|
const action = transaction.getMeta(AnnotationPluginKey) as AddAnnotationAction | DeleteAnnotationAction
|
||||||
|
|
||||||
if (action && action.type) {
|
if (action && action.type) {
|
||||||
@@ -117,13 +114,16 @@ export class AnnotationState {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use Y.js to update positions
|
||||||
|
const ystate = ySyncPluginKey.getState(state)
|
||||||
|
|
||||||
if (ystate.isChangeOrigin) {
|
if (ystate.isChangeOrigin) {
|
||||||
this.updateDecorations(state)
|
this.updateDecorations(state)
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply ProseMirror mapping
|
// Use ProseMirror to update positions
|
||||||
this.decorations = this.decorations.map(transaction.mapping, transaction.doc)
|
this.decorations = this.decorations.map(transaction.mapping, transaction.doc)
|
||||||
|
|
||||||
return this
|
return this
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { AnnotationItem } from './AnnotationItem'
|
|||||||
import { AnnotationPlugin, AnnotationPluginKey } from './AnnotationPlugin'
|
import { AnnotationPlugin, AnnotationPluginKey } from './AnnotationPlugin'
|
||||||
|
|
||||||
function randomId() {
|
function randomId() {
|
||||||
|
// TODO: That seems … to simple.
|
||||||
return Math.floor(Math.random() * 0xffffffff).toString()
|
return Math.floor(Math.random() * 0xffffffff).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,13 +24,16 @@ export interface AnnotationOptions {
|
|||||||
HTMLAttributes: {
|
HTMLAttributes: {
|
||||||
[key: string]: any
|
[key: string]: any
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* An event listener which receives annotations for the current selection.
|
||||||
|
*/
|
||||||
onUpdate: (items: [any?]) => {},
|
onUpdate: (items: [any?]) => {},
|
||||||
/**
|
/**
|
||||||
* An initialized Y.js document.
|
* An initialized Y.js document.
|
||||||
*/
|
*/
|
||||||
document: Y.Doc | null,
|
document: Y.Doc | null,
|
||||||
/**
|
/**
|
||||||
* Name of a Y.js fragment, can be changed to sync multiple fields with one Y.js document.
|
* Name of a Y.js map, can be changed to sync multiple fields with one Y.js document.
|
||||||
*/
|
*/
|
||||||
field: string,
|
field: string,
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user