-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
208 additions
and
64 deletions.
There are no files selected for viewing
59 changes: 0 additions & 59 deletions
59
frontend/src/components/smart-editor/tabbed-editors/cursors.tsx
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
frontend/src/components/smart-editor/tabbed-editors/cursors/cursor-colors.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,43 @@ | ||
interface BaseColor { | ||
red: number; | ||
green: number; | ||
blue: number; | ||
} | ||
|
||
const MAP: Map<string, BaseColor> = new Map(); | ||
|
||
// #C30000 | ||
const RED: BaseColor = { red: 195, green: 0, blue: 0 }; | ||
// #06893A | ||
const GREEN: BaseColor = { red: 6, green: 137, blue: 58 }; | ||
// #66CBEC | ||
const LIGHT_BLUE: BaseColor = { red: 102, green: 203, blue: 236 }; | ||
// #0067C5 | ||
const BLUE: BaseColor = { red: 0, green: 103, blue: 197 }; | ||
// #FF9100 | ||
const ORANGE: BaseColor = { red: 255, green: 145, blue: 0 }; | ||
// #005B82 | ||
const DEEP_BLUE: BaseColor = { red: 0, green: 91, blue: 130 }; | ||
// #634689 | ||
const PURPLE: BaseColor = { red: 99, green: 70, blue: 137 }; | ||
|
||
const COLORS = [RED, GREEN, LIGHT_BLUE, BLUE, ORANGE, DEEP_BLUE, PURPLE]; | ||
const COLOR_MAX = COLORS.length; | ||
|
||
export const getColor = (key: string, opacity: number): string => { | ||
const existing = MAP.get(key); | ||
|
||
if (existing === undefined) { | ||
const randomColorIndex = Math.floor(Math.random() * COLOR_MAX); | ||
const baseColor = COLORS[randomColorIndex]!; | ||
|
||
MAP.set(key, baseColor); | ||
|
||
return formatColor(baseColor, opacity); | ||
} | ||
|
||
return formatColor(existing, opacity); | ||
}; | ||
|
||
const formatColor = (color: BaseColor, opacity: number): string => | ||
`rgba(${color.red}, ${color.green}, ${color.blue}, ${opacity})`; |
91 changes: 91 additions & 0 deletions
91
frontend/src/components/smart-editor/tabbed-editors/cursors/cursors.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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { RelativeRange } from '@slate-yjs/core'; | ||
import { UnknownObject } from '@udecode/plate-common'; | ||
import { CursorData, CursorOverlayProps, CursorProps, useCursorOverlayPositions } from '@udecode/plate-cursor'; | ||
import { useContext, useRef } from 'react'; | ||
import { styled } from 'styled-components'; | ||
import { StaticDataContext } from '@app/components/app/static-data-context'; | ||
import { getColor } from '@app/components/smart-editor/tabbed-editors/cursors/cursor-colors'; | ||
|
||
export interface UserCursor extends CursorData, UnknownObject { | ||
navn: string; | ||
navIdent: string; | ||
} | ||
|
||
interface YjsCursor { | ||
selection: RelativeRange; | ||
data: UserCursor; | ||
} | ||
|
||
export const isYjsCursor = (value: unknown): value is YjsCursor => | ||
typeof value === 'object' && value !== null && 'selection' in value && 'data' in value; | ||
|
||
const Cursor = ({ caretPosition, data, disableCaret, disableSelection, selectionRects }: CursorProps<UserCursor>) => { | ||
const { style, selectionStyle = style, navIdent, navn } = data ?? {}; | ||
|
||
const labelRef = useRef<HTMLDivElement>(null); | ||
|
||
const caretColor = getColor(navIdent ?? '', 1); | ||
const selectionColor = getColor(navIdent ?? '', 0.2); | ||
|
||
return ( | ||
<> | ||
{disableSelection === true | ||
? null | ||
: selectionRects.map((position, i) => ( | ||
<StyledSelection key={i} style={{ ...selectionStyle, ...position }} $color={selectionColor} /> | ||
))} | ||
{disableCaret === true || caretPosition === null ? null : ( | ||
<StyledCaret style={{ ...caretPosition, ...style }} $color={caretColor}> | ||
<CaretLabel ref={labelRef} $color={caretColor}> | ||
{navn} ({navIdent}) | ||
</CaretLabel> | ||
</StyledCaret> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
interface ColorProps { | ||
$color: string; | ||
} | ||
|
||
const StyledSelection = styled.div<ColorProps>` | ||
position: absolute; | ||
z-index: 10; | ||
background-color: ${({ $color }) => $color}; | ||
`; | ||
|
||
const StyledCaret = styled.div<ColorProps>` | ||
pointer-events: none; | ||
position: absolute; | ||
z-index: 10; | ||
width: 1px; | ||
background-color: ${({ $color }) => $color}; | ||
`; | ||
|
||
const CaretLabel = styled.div<ColorProps>` | ||
position: absolute; | ||
bottom: 100%; | ||
left: 0; | ||
background-color: ${({ $color }) => $color}; | ||
color: white; | ||
font-size: 0.75em; | ||
padding: 0.25em; | ||
border-radius: var(--a-border-radius-medium); | ||
white-space: nowrap; | ||
`; | ||
|
||
export const CursorOverlay = (props: CursorOverlayProps<UserCursor>) => { | ||
const { user } = useContext(StaticDataContext); | ||
const { cursors } = useCursorOverlayPositions(props); | ||
|
||
return ( | ||
<> | ||
{cursors | ||
.filter(({ data }) => data?.navIdent !== user.navIdent) | ||
.map((cursor) => ( | ||
<Cursor key={cursor.key as string} {...cursor} /> | ||
))} | ||
</> | ||
); | ||
}; |
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
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