Skip to content

Commit

Permalink
add ability to change colors via the annotation tooltip and refactor …
Browse files Browse the repository at this point in the history
…highlightcolor picker
  • Loading branch information
blackforestboi committed Jun 27, 2024
1 parent 1ac1a64 commit 904e9ac
Show file tree
Hide file tree
Showing 24 changed files with 1,010 additions and 335 deletions.
2 changes: 1 addition & 1 deletion external/@worldbrain/memex-common
5 changes: 3 additions & 2 deletions src/annotations/annotation-save-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import type { RemoteSyncSettingsInterface } from 'src/sync-settings/background/types'
import type { RGBAColor } from './cache/types'
import type { AnnotationSharingState } from '@worldbrain/memex-common/lib/content-sharing/service/types'
import { HighlightColor } from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker/types'

export interface AnnotationShareOpts {
shouldShare?: boolean
Expand All @@ -27,14 +28,14 @@ type AnnotationCreateData = {
createdWhen?: Date
selector?: Anchor
localListIds?: number[]
color?: RGBAColor | string | string
color?: HighlightColor['id']
} & ({ body: string; comment?: string } | { body?: string; comment: string })

interface AnnotationUpdateData {
localId: string
body: string
comment: string | null
color?: RGBAColor | string | string
color?: HighlightColor['id']
}

export interface SaveAnnotationParams<
Expand Down
6 changes: 3 additions & 3 deletions src/annotations/background/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { AnnotSearchParams } from 'src/search/background/types'
import type { Anchor } from 'src/highlighting/types'
import type { SharedAnnotationReference } from '@worldbrain/memex-common/lib/content-sharing/types'
import type { SharedAnnotationWithRefs } from '../types'
import { RGBAColor } from '../cache/types'
import { HighlightColor } from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker/types'

export interface AnnotationInterface<Role extends RemoteFunctionRole> {
getAllAnnotationsByUrl: RemotePositionalFunction<
Expand Down Expand Up @@ -46,7 +46,7 @@ export interface AnnotationInterface<Role extends RemoteFunctionRole> {
>
editAnnotation: RemotePositionalFunction<
Role,
[string, string, RGBAColor | string, string],
[string, string, HighlightColor['id'] | string, string],
any
>
updateAnnotationTags: RemotePositionalFunction<
Expand Down Expand Up @@ -102,7 +102,7 @@ export interface CreateAnnotationParams {
title?: string
comment?: string
body?: string
color?: RGBAColor | string | string
color?: HighlightColor['id']
selector?: Anchor
isBookmarked?: boolean
isSocialPost?: boolean
Expand Down
12 changes: 6 additions & 6 deletions src/annotations/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
pushOrderedItem,
} from '@worldbrain/memex-common/lib/utils/item-ordering'
import { DEFAULT_HIGHLIGHT_COLOR } from '@worldbrain/memex-common/lib/annotations/constants'
import { HighlightColor } from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker/types'

export interface PageAnnotationCacheDeps {
sortingFn?: AnnotationsSorter
Expand Down Expand Up @@ -63,7 +64,7 @@ export class PageAnnotationsCache implements PageAnnotationsCacheInterface {
UnifiedAnnotation['unifiedId']
>()

private highlightColorDict: { [id: string]: RGBAColor } = {}
private highlightColorDict: HighlightColor[]

constructor(private deps: PageAnnotationCacheDeps) {
deps.sortingFn = deps.sortingFn ?? sortByPagePosition
Expand Down Expand Up @@ -93,7 +94,7 @@ export class PageAnnotationsCache implements PageAnnotationsCacheInterface {
setHighlightColorDictionary: PageAnnotationsCacheInterface['setHighlightColorDictionary'] = (
colors,
) => {
this.highlightColorDict = fromPairs(colors.map((c) => [c.id, c.color]))
this.highlightColorDict = colors
}

getAnnotationsArray: PageAnnotationsCacheInterface['getAnnotationsArray'] = () =>
Expand Down Expand Up @@ -199,10 +200,9 @@ export class PageAnnotationsCache implements PageAnnotationsCacheInterface {
opts: { now: number },
): UnifiedAnnotation => {
const unifiedAnnotationId = this.generateAnnotationId()
if (annotation.color != null) {
annotation.color =
this.highlightColorDict[annotation.color as string] ??
DEFAULT_HIGHLIGHT_COLOR

if (annotation.color == null) {
annotation.color = this.highlightColorDict[0].id
}

if (annotation.remoteId != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/annotations/cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export type UnifiedAnnotation = Pick<
lastEdited: number
createdWhen: number
creator?: UserReference
color?: RGBAColor
color?: HighlightColor['id']

// Misc annotation feature state
privacyLevel: AnnotationPrivacyLevels
Expand Down
5 changes: 3 additions & 2 deletions src/annotations/cache/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { SharedListMetadata } from 'src/content-sharing/background/types'
import { DEFAULT_KEY } from '@worldbrain/memex-common/lib/utils/item-ordering'
import { createSyncSettingsStore } from 'src/sync-settings/util'
import { HIGHLIGHT_COLORS_DEFAULT } from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker/constants'
import { HighlightColor } from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker/types'

export const reshapeAnnotationForCache = (
annot: Annotation & {
Expand Down Expand Up @@ -68,7 +69,7 @@ export const reshapeAnnotationForCache = (
shouldShare: annot.isShared,
isBulkShareProtected: annot.isBulkShareProtected,
}),
color: annot.color as RGBAColor,
color: annot.color as HighlightColor['id'],
...(opts.extraData ?? {}),
}
}
Expand All @@ -94,7 +95,7 @@ export const reshapeSharedAnnotationForCache = (
lastEdited: annot.updatedWhen,
createdWhen: annot.createdWhen,
privacyLevel: AnnotationPrivacyLevels.SHARED,
color: annot.color as RGBAColor,
color: annot.color as HighlightColor['id'],
...(opts.extraData ?? {}),
}
}
Expand Down
98 changes: 42 additions & 56 deletions src/annotations/components/AnnotationEditable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { ANNOT_BOX_ID_PREFIX } from 'src/sidebar/annotations-sidebar/constants'
import { YoutubePlayer } from '@worldbrain/memex-common/lib/services/youtube/types'
import { ImageSupportInterface } from 'src/image-support/background/types'
import { Anchor } from 'src/highlighting/types'
import HighlightColorPicker from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker'

import tinycolor from 'tinycolor2'
import { RGBAobjectToString } from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker/utils'
import { SPECIAL_LIST_IDS } from '@worldbrain/memex-common/lib/storage/modules/lists/constants'
Expand All @@ -45,12 +45,15 @@ import type { HighlightColor } from '@worldbrain/memex-common/lib/common-ui/comp
import CheckboxNotInput from 'src/common-ui/components/CheckboxNotInput'
import { SpaceSearchSuggestion } from '@worldbrain/memex-common/lib/editor'
import ListsSegment from '@worldbrain/memex-common/lib/common-ui/components/result-item-spaces-segment'
import HighlightColorPicker from './highlightColorPicker'
import { RemoteSyncSettingsInterface } from 'src/sync-settings/background/types'
import { DEFAULT_HIGHLIGHT_COLOR } from '@worldbrain/memex-common/lib/annotations/constants'

export interface HighlightProps extends AnnotationProps {
body: string
comment?: string
selector?: Anchor
color?: RGBAColor
color?: HighlightColor
}

export interface NoteProps extends AnnotationProps {
Expand Down Expand Up @@ -126,11 +129,9 @@ export interface AnnotationProps {
shareMenuAnnotationInstanceId: string
imageSupport: ImageSupportInterface<'caller'>
selector?: Anchor
saveHighlightColor?: (noteId, colorId, color) => void
saveHighlightColorSettings?: (newState: HighlightColor[]) => void
getHighlightColorSettings?: () => void
saveHighlightColor?: (color: HighlightColor['id']) => Promise<void>
highlightColorSettings: HighlightColor[]
color?: RGBAColor
color?: HighlightColor
getRootElement: () => HTMLElement
toggleAutoAdd: () => void
isAutoAddEnabled?: boolean
Expand All @@ -148,6 +149,7 @@ export interface AnnotationProps {
isInFocus?: boolean
shiftSelectItem?: () => void
searchTerms?: string[]
syncSettingsBG: RemoteSyncSettingsInterface
}

export interface AnnotationEditableEventProps {
Expand Down Expand Up @@ -239,21 +241,21 @@ export default class AnnotationEditable extends React.Component<Props, State> {
componentDidMount() {
this.setTextAreaHeight()

if (!this.props.color) {
const defaultHighlightSettings = this.props.highlightColorSettings?.find(
(setting) => setting.id === 'default',
)
if (defaultHighlightSettings?.color) {
this.setState({
defaultHighlightColor: defaultHighlightSettings.color,
currentHighlightColor: defaultHighlightSettings.color,
})
}
} else {
this.setState({
currentHighlightColor: this.props.color,
})
}
// if (!this.props.color) {
// const defaultHighlightSettings = this.props.highlightColorSettings?.find(
// (setting) => setting.id === 'default',
// )
// if (defaultHighlightSettings?.color) {
// this.setState({
// defaultHighlightColor: defaultHighlightSettings.color,
// currentHighlightColor: defaultHighlightSettings.color,
// })
// }
// } else {
// this.setState({
// currentHighlightColor: this.props.color,
// })
// }
}

// This is a hack to ensure this state, which isn't available on init, only gets set once
Expand All @@ -270,11 +272,11 @@ export default class AnnotationEditable extends React.Component<Props, State> {
showShareMenu: this.props.initShowSpacePicker === 'footer',
})
}
if (prevProps.color != this.props.color) {
this.setState({
currentHighlightColor: this.props.color,
})
}
// if (prevProps.color != this.props.color) {
// this.setState({
// currentHighlightColor: this.props.color,
// })
// }

if (
prevProps.highlightColorSettings !=
Expand Down Expand Up @@ -455,15 +457,18 @@ export default class AnnotationEditable extends React.Component<Props, State> {
}

const isScreenshotAnnotation = this.props.selector?.dimensions != null
let barColor = null

if (this.state.defaultHighlightColor) {
barColor = RGBAobjectToString(this.state.defaultHighlightColor)
}
if (this.state.currentHighlightColor) {
barColor = RGBAobjectToString(this.state.currentHighlightColor)
let barColor = RGBAobjectToString(DEFAULT_HIGHLIGHT_COLOR)
if (this.props.color) {
barColor = RGBAobjectToString(this.props.color.color)
}

// if (this.state.defaultHighlightColor) {
// barColor = RGBAobjectToString(this.props.color.color)
// }
// if (this.state.currentHighlightColor) {
// barColor = RGBAobjectToString(this.props.color.color)
// }

return (
<HighlightStyled
onClick={this.props.onHighlightClick}
Expand Down Expand Up @@ -605,29 +610,10 @@ export default class AnnotationEditable extends React.Component<Props, State> {
getPortalRoot={this.props.getRootElement}
>
<HighlightColorPicker
saveHighlightColorSettings={
this.props.saveHighlightColorSettings
}
changeHighlightColor={(
color: RGBAColor | string,
colorId,
) => {
this.props.saveHighlightColor(
unifiedId,
colorId,
color,
)
this.setState({
showHighlightColorPicker: false,
})
}}
getHighlightColorSettings={
this.props.getHighlightColorSettings
}
highlightColorSettings={
this.props.highlightColorSettings
}
selectedColor={selectedColor}
syncSettingsBG={this.props.syncSettingsBG}
annotationId={this.props.unifiedId}
selectedColor={this.props.color.id}
updateAnnotationColor={this.props.saveHighlightColor}
/>
</PopoutBox>
)
Expand Down
Loading

0 comments on commit 904e9ac

Please sign in to comment.