diff --git a/packages/enhancer-annotations/src/Commands.ts b/packages/enhancer-annotations/src/Commands.ts index fc1318c7..2478b597 100644 --- a/packages/enhancer-annotations/src/Commands.ts +++ b/packages/enhancer-annotations/src/Commands.ts @@ -8,6 +8,7 @@ type Command = | { type: "highlight"; data: HighlightParams } | { type: "add"; data: Highlight | Highlight[] } | { type: "delete"; id: string } + | { type: "update"; id: string; data: Pick } export class Commands extends DestroyableClass { private commandSubject = new Subject() @@ -15,7 +16,7 @@ export class Commands extends DestroyableClass { public readonly highlight$ = this.commandSubject.pipe(filter((command) => command.type === "highlight")) public readonly add$ = this.commandSubject.pipe(filter((command) => command.type === "add")) public readonly delete$ = this.commandSubject.pipe(filter((command) => command.type === "delete")) - + public readonly update$ = this.commandSubject.pipe(filter((command) => command.type === "update")) highlight = (params: HighlightParams) => { this.commandSubject.next({ type: "highlight", data: params }) } @@ -28,6 +29,10 @@ export class Commands extends DestroyableClass { this.commandSubject.next({ type: "delete", id }) } + update = (id: string, data: Pick) => { + this.commandSubject.next({ type: "update", id, data }) + } + destroy() { super.destroy() this.commandSubject.complete() diff --git a/packages/enhancer-annotations/src/annotationsEnhancer.ts b/packages/enhancer-annotations/src/annotationsEnhancer.ts index 5a300768..e9a5f292 100644 --- a/packages/enhancer-annotations/src/annotationsEnhancer.ts +++ b/packages/enhancer-annotations/src/annotationsEnhancer.ts @@ -16,6 +16,7 @@ export const annotationsEnhancer = highlight: Commands["highlight"] add: Commands["add"] delete: Commands["delete"] + update: Commands["update"] getHighlightsForTarget: (target: EventTarget) => RuntimeHighlight[] } } => { @@ -47,6 +48,12 @@ export const annotationsEnhancer = }), ) + const update$ = commands.update$.pipe( + tap(({ id, data }) => { + highlightsSubject.next(highlightsSubject.getValue().map((highlight) => (highlight.id === id ? { ...highlight, ...data } : highlight))) + }), + ) + const annotations$ = highlightsSubject.asObservable() const readerHighlights = new ReaderHighlights(reader, highlightsSubject) @@ -55,6 +62,7 @@ export const annotationsEnhancer = highlight$, add$, delete$, + update$, annotations$.pipe( tap((annotations) => { report.debug("annotations", annotations) @@ -79,6 +87,7 @@ export const annotationsEnhancer = highlight: commands.highlight, add: commands.add, delete: commands.delete, + update: commands.update, }, } }