-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[desk-tool] Add basic user-based annotation colors
- Loading branch information
1 parent
d56a6e2
commit 144b1e4
Showing
14 changed files
with
228 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import DefaultRootComponent from './components/DefaultRootComponent' | ||
|
||
export {DefaultRootComponent} | ||
|
||
export * from './user-color' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './manager' | ||
export * from './provider' | ||
export * from './singleton' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {UserColorHue, UserColorManager} from './types' | ||
|
||
const colors: UserColorHue[] = [ | ||
'blue', | ||
'cyan', | ||
// 'green', | ||
'yellow', | ||
'orange', | ||
// 'red', | ||
'magenta', | ||
'purple' | ||
] | ||
|
||
export function createUserColorManager(): UserColorManager { | ||
let state: {users: string[]} = { | ||
users: [] | ||
} | ||
|
||
return {get} | ||
|
||
function get(userId: string) { | ||
if (!state.users.includes(userId)) { | ||
state = { | ||
users: state.users.concat([userId]) | ||
} | ||
} | ||
|
||
const idx = state.users.indexOf(userId) | ||
|
||
return colors[idx % colors.length] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React, {createContext, useContext} from 'react' | ||
import {UserColorManager} from './types' | ||
|
||
interface ProviderProps { | ||
children: React.ReactNode | ||
manager: UserColorManager | ||
} | ||
|
||
const UserColorManagerContext = createContext<UserColorManager | null>(null) | ||
|
||
export function useUserColorManager(): UserColorManager { | ||
const manager = useContext(UserColorManagerContext) | ||
|
||
if (!manager) { | ||
throw new Error(`missing user color manager in context`) | ||
} | ||
|
||
return manager | ||
} | ||
|
||
export function UserColorManagerProvider({children, manager}: ProviderProps) { | ||
return ( | ||
<UserColorManagerContext.Provider value={manager}>{children}</UserColorManagerContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {createUserColorManager} from './manager' | ||
|
||
export const userColorManager = createUserColorManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export type UserColorHue = | ||
| 'blue' | ||
| 'cyan' | ||
// | 'green' | ||
| 'yellow' | ||
| 'orange' | ||
// | 'red' | ||
| 'magenta' | ||
| 'purple' | ||
|
||
export interface UserColorManager { | ||
get: (userId: string) => UserColorHue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 19 additions & 3 deletions
22
packages/@sanity/desk-tool/src/components/diffs/NumberFieldDiff.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,21 @@ | ||
.root { | ||
background: rgba(0, 0, 0, 0.05); | ||
padding: 0.75em; | ||
border-radius: 4px; | ||
display: flex; | ||
|
||
@nest & > span { | ||
display: inline-block; | ||
padding: 0.75em 0.125em; | ||
} | ||
|
||
@nest & del { | ||
display: inline-block; | ||
padding: 0.75em; | ||
border-radius: 4px; | ||
} | ||
|
||
@nest & ins { | ||
display: inline-block; | ||
padding: 0.75em; | ||
border-radius: 4px; | ||
text-decoration: none; | ||
} | ||
} |
28 changes: 16 additions & 12 deletions
28
packages/@sanity/desk-tool/src/components/diffs/NumberFieldDiff.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
import * as React from 'react' | ||
import {NumberDiff, StringDiffSegment} from '@sanity/diff' | ||
import {useUserColorManager} from '@sanity/base' | ||
import {NumberDiff} from '@sanity/diff' | ||
import React from 'react' | ||
import {Annotation} from '../../panes/documentPane/history/types' | ||
import {getAnnotationColor} from './helpers' | ||
import {DiffComponent} from './types' | ||
import {StringSegment} from './StringFieldDiff' | ||
|
||
import styles from './NumberFieldDiff.css' | ||
|
||
export const NumberFieldDiff: DiffComponent<NumberDiff<Annotation>> = ({diff}) => { | ||
const userColorManager = useUserColorManager() | ||
const {fromValue, toValue, annotation} = diff | ||
const segments: StringDiffSegment[] = [] | ||
const color = getAnnotationColor(userColorManager, annotation) | ||
|
||
if (typeof fromValue === 'number') { | ||
segments.push({type: 'removed', text: fromValue.toString(), annotation}) | ||
} | ||
if (typeof toValue === 'number') { | ||
segments.push({type: 'added', text: fromValue.toString(), annotation}) | ||
const inlineStyle = { | ||
background: color.bg, | ||
color: color.fg | ||
} | ||
|
||
return ( | ||
<div className={styles.root}> | ||
{segments.map((segment, index) => ( | ||
<StringSegment key={index} segment={segment} /> | ||
))} | ||
{fromValue !== undefined && ( | ||
<> | ||
<del style={inlineStyle}>{fromValue}</del> | ||
<span>→</span> | ||
</> | ||
)} | ||
<ins style={inlineStyle}>{toValue}</ins> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/@sanity/desk-tool/src/components/diffs/helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {UserColorManager} from '@sanity/base' | ||
import {parseToRgb} from 'polished' | ||
import {Annotation} from '../../panes/documentPane/history/types' | ||
|
||
interface RGB { | ||
red: number | ||
green: number | ||
blue: number | ||
} | ||
|
||
interface UserColors { | ||
blue: {bg: string; fg: string} | ||
cyan: {bg: string; fg: string} | ||
// green: {bg: string; fg: string} | ||
yellow: {bg: string; fg: string} | ||
orange: {bg: string; fg: string} | ||
// red: {bg: string; fg: string} | ||
magenta: {bg: string; fg: string} | ||
purple: {bg: string; fg: string} | ||
} | ||
|
||
// @todo: get these values from a global theme object | ||
const color: UserColors = { | ||
blue: {bg: '#E8F1FE', fg: '#1B50A5'}, | ||
cyan: {bg: '#E2F4FD', fg: '#15628C'}, | ||
yellow: {bg: '#FEF7DA', fg: '#756623'}, | ||
orange: {bg: '#FEF0E6', fg: '#914E23'}, | ||
magenta: {bg: '#FCEBF5', fg: '#902A6C'}, | ||
purple: {bg: '#F8E9FE', fg: '#7B1EA5'} | ||
} | ||
|
||
function multiply(rgb1: RGB, rgb2: RGB) { | ||
return { | ||
red: Math.floor((rgb1.red * rgb2.red) / 255), | ||
green: Math.floor((rgb1.green * rgb2.green) / 255), | ||
blue: Math.floor((rgb1.blue * rgb2.blue) / 255) | ||
} | ||
} | ||
|
||
export function getAnnotationColor(colorManager: UserColorManager, annotation: Annotation) { | ||
if (!annotation) { | ||
throw new Error('missing annotation') | ||
} | ||
|
||
const colors = Array.from(annotation.authors).map((userId: any) => { | ||
const hueKey = colorManager.get(userId) | ||
const hue = color[hueKey] | ||
|
||
return { | ||
bg: parseToRgb(hue.bg), | ||
fg: parseToRgb(hue.fg) | ||
} | ||
}) | ||
|
||
const result = colors.reduce((acc, x) => { | ||
return { | ||
bg: multiply(acc.bg, x.bg), | ||
fg: multiply(acc.fg, x.fg) | ||
} | ||
}) | ||
|
||
return { | ||
bg: `rgb(${result.bg.red}, ${result.bg.green}, ${result.bg.blue})`, | ||
fg: `rgb(${result.fg.red}, ${result.fg.green}, ${result.fg.blue})` | ||
} | ||
} |