Skip to content

Commit

Permalink
feat: added highlight update
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed Nov 19, 2024
1 parent ce069ca commit 9efc38d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/enhancer-annotations/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ type Command =
| { type: "highlight"; data: HighlightParams }
| { type: "add"; data: Highlight | Highlight[] }
| { type: "delete"; id: string }
| { type: "update"; id: string; data: Pick<HighlightParams, "color" | "contents"> }

export class Commands extends DestroyableClass {
private commandSubject = new Subject<Command>()

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 })
}
Expand All @@ -28,6 +29,10 @@ export class Commands extends DestroyableClass {
this.commandSubject.next({ type: "delete", id })
}

update = (id: string, data: Pick<HighlightParams, "color" | "contents">) => {
this.commandSubject.next({ type: "update", id, data })
}

destroy() {
super.destroy()
this.commandSubject.complete()
Expand Down
9 changes: 9 additions & 0 deletions packages/enhancer-annotations/src/annotationsEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const annotationsEnhancer =
highlight: Commands["highlight"]
add: Commands["add"]
delete: Commands["delete"]
update: Commands["update"]
getHighlightsForTarget: (target: EventTarget) => RuntimeHighlight[]
}
} => {
Expand Down Expand Up @@ -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)
Expand All @@ -55,6 +62,7 @@ export const annotationsEnhancer =
highlight$,
add$,
delete$,
update$,
annotations$.pipe(
tap((annotations) => {
report.debug("annotations", annotations)
Expand All @@ -79,6 +87,7 @@ export const annotationsEnhancer =
highlight: commands.highlight,
add: commands.add,
delete: commands.delete,
update: commands.update,
},
}
}

0 comments on commit 9efc38d

Please sign in to comment.