From ce480806c041db75151a86034a572c0bb7ec3e51 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Fri, 18 Aug 2023 13:30:54 +0800 Subject: [PATCH 1/2] add color to create/update/merge highlight api and highlight schema --- packages/api/src/elastic/types.ts | 1 + packages/api/src/generated/graphql.ts | 7 +++++++ packages/api/src/generated/schema.graphql | 5 +++++ packages/api/src/resolvers/highlight/index.ts | 2 ++ packages/api/src/schema.ts | 5 +++++ packages/db/elastic_migrations/index_settings.json | 3 +++ 6 files changed, 23 insertions(+) diff --git a/packages/api/src/elastic/types.ts b/packages/api/src/elastic/types.ts index 1dab0a69f..7ff19154a 100644 --- a/packages/api/src/elastic/types.ts +++ b/packages/api/src/elastic/types.ts @@ -107,6 +107,7 @@ export interface Highlight { highlightPositionAnchorIndex?: number | null type: HighlightType html?: string | null + color?: string | null } export interface RecommendingUser { diff --git a/packages/api/src/generated/graphql.ts b/packages/api/src/generated/graphql.ts index 7721c6038..566cc87ca 100644 --- a/packages/api/src/generated/graphql.ts +++ b/packages/api/src/generated/graphql.ts @@ -351,6 +351,7 @@ export enum CreateHighlightErrorCode { export type CreateHighlightInput = { annotation?: InputMaybe; articleId: Scalars['ID']; + color?: InputMaybe; highlightPositionAnchorIndex?: InputMaybe; highlightPositionPercent?: InputMaybe; html?: InputMaybe; @@ -908,6 +909,7 @@ export type GroupsSuccess = { export type Highlight = { __typename?: 'Highlight'; annotation?: Maybe; + color?: Maybe; createdAt: Scalars['Date']; createdByMe: Scalars['Boolean']; highlightPositionAnchorIndex?: Maybe; @@ -1160,6 +1162,7 @@ export enum MergeHighlightErrorCode { export type MergeHighlightInput = { annotation?: InputMaybe; articleId: Scalars['ID']; + color?: InputMaybe; highlightPositionAnchorIndex?: InputMaybe; highlightPositionPercent?: InputMaybe; html?: InputMaybe; @@ -2308,6 +2311,7 @@ export type SearchItem = { annotation?: Maybe; archivedAt?: Maybe; author?: Maybe; + color?: Maybe; content?: Maybe; contentReader: ContentReader; createdAt: Scalars['Date']; @@ -2847,6 +2851,7 @@ export enum UpdateHighlightErrorCode { export type UpdateHighlightInput = { annotation?: InputMaybe; + color?: InputMaybe; highlightId: Scalars['ID']; html?: InputMaybe; quote?: InputMaybe; @@ -4897,6 +4902,7 @@ export type GroupsSuccessResolvers = { annotation?: Resolver, ParentType, ContextType>; + color?: Resolver, ParentType, ContextType>; createdAt?: Resolver; createdByMe?: Resolver; highlightPositionAnchorIndex?: Resolver, ParentType, ContextType>; @@ -5565,6 +5571,7 @@ export type SearchItemResolvers, ParentType, ContextType>; archivedAt?: Resolver, ParentType, ContextType>; author?: Resolver, ParentType, ContextType>; + color?: Resolver, ParentType, ContextType>; content?: Resolver, ParentType, ContextType>; contentReader?: Resolver; createdAt?: Resolver; diff --git a/packages/api/src/generated/schema.graphql b/packages/api/src/generated/schema.graphql index 9e2fdcdf6..988946951 100644 --- a/packages/api/src/generated/schema.graphql +++ b/packages/api/src/generated/schema.graphql @@ -303,6 +303,7 @@ enum CreateHighlightErrorCode { input CreateHighlightInput { annotation: String articleId: ID! + color: String highlightPositionAnchorIndex: Int highlightPositionPercent: Float html: String @@ -805,6 +806,7 @@ type GroupsSuccess { type Highlight { annotation: String + color: String createdAt: Date! createdByMe: Boolean! highlightPositionAnchorIndex: Int @@ -1034,6 +1036,7 @@ enum MergeHighlightErrorCode { input MergeHighlightInput { annotation: String articleId: ID! + color: String highlightPositionAnchorIndex: Int highlightPositionPercent: Float html: String @@ -1685,6 +1688,7 @@ type SearchItem { annotation: String archivedAt: Date author: String + color: String content: String contentReader: ContentReader! createdAt: Date! @@ -2183,6 +2187,7 @@ enum UpdateHighlightErrorCode { input UpdateHighlightInput { annotation: String + color: String highlightId: ID! html: String quote: String diff --git a/packages/api/src/resolvers/highlight/index.ts b/packages/api/src/resolvers/highlight/index.ts index 0e4a6a935..1ca5ee8c5 100644 --- a/packages/api/src/resolvers/highlight/index.ts +++ b/packages/api/src/resolvers/highlight/index.ts @@ -182,6 +182,7 @@ export const mergeHighlightResolver = authorized< mergedAnnotations.length > 0 ? mergedAnnotations.join('\n') : null, type: HighlightType.Highlight, labels: mergedLabels, + color: newHighlightInput.color || pageHighlights[0].color, // use new color or the color of the first highlight } const merged = await updatePage( @@ -253,6 +254,7 @@ export const updateHighlightResolver = authorized< annotation, quote, updatedAt: new Date(), + color: input.color, } log.info('Updating a highlight', { diff --git a/packages/api/src/schema.ts b/packages/api/src/schema.ts index 150eda9a8..575ec1d73 100755 --- a/packages/api/src/schema.ts +++ b/packages/api/src/schema.ts @@ -711,6 +711,7 @@ const schema = gql` labels: [Label!] type: HighlightType! html: String + color: String } input CreateHighlightInput { @@ -727,6 +728,7 @@ const schema = gql` highlightPositionAnchorIndex: Int type: HighlightType html: String + color: String } type CreateHighlightSuccess { @@ -760,6 +762,7 @@ const schema = gql` highlightPositionPercent: Float highlightPositionAnchorIndex: Int html: String + color: String } type MergeHighlightSuccess { @@ -787,6 +790,7 @@ const schema = gql` sharedAt: Date quote: String @sanitize(maxLength: 12000, minLength: 1) html: String + color: String } type UpdateHighlightSuccess { @@ -1584,6 +1588,7 @@ const schema = gql` shortId: String quote: String annotation: String + color: String labels: [Label!] subscription: String unsubMailTo: String diff --git a/packages/db/elastic_migrations/index_settings.json b/packages/db/elastic_migrations/index_settings.json index 6f0ec9bc3..35e98487d 100644 --- a/packages/db/elastic_migrations/index_settings.json +++ b/packages/db/elastic_migrations/index_settings.json @@ -131,6 +131,9 @@ "html": { "type": "text", "analyzer": "strip_html_analyzer" + }, + "color": { + "type": "keyword" } } }, From ec574d0ad995b2b768e27082fe22063cf2ab837f Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Fri, 18 Aug 2023 13:54:38 +0800 Subject: [PATCH 2/2] use new color or the color of the last overlap highlight when merging highlights --- packages/api/src/resolvers/highlight/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/api/src/resolvers/highlight/index.ts b/packages/api/src/resolvers/highlight/index.ts index 1ca5ee8c5..ff4eb1f42 100644 --- a/packages/api/src/resolvers/highlight/index.ts +++ b/packages/api/src/resolvers/highlight/index.ts @@ -148,6 +148,7 @@ export const mergeHighlightResolver = authorized< /* Compute merged annotation form the order of highlights appearing on page */ const mergedAnnotations: string[] = [] const mergedLabels: Label[] = [] + const mergedColors: string[] = [] const pageHighlights = page.highlights.filter((highlight) => { // filter out highlights that are in the overlap list // and are of type highlight (not annotation or note) @@ -168,10 +169,15 @@ export const mergeHighlightResolver = authorized< } }) } + // collect colors of overlap highlights + highlight.color && mergedColors.push(highlight.color) + return false } return true }) + // use new color or the color of the last overlap highlight + const color = newHighlightInput.color || mergedColors[mergedColors.length - 1] try { const highlight: HighlightData = { ...newHighlightInput, @@ -182,7 +188,7 @@ export const mergeHighlightResolver = authorized< mergedAnnotations.length > 0 ? mergedAnnotations.join('\n') : null, type: HighlightType.Highlight, labels: mergedLabels, - color: newHighlightInput.color || pageHighlights[0].color, // use new color or the color of the first highlight + color, } const merged = await updatePage(