-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Table Cell Background Color #4306
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,11 @@ | |
* | ||
*/ | ||
|
||
import type {DEPRECATED_GridCellNode, ElementNode} from 'lexical'; | ||
import type { | ||
DEPRECATED_GridCellNode, | ||
ElementNode, | ||
LexicalEditor, | ||
} from 'lexical'; | ||
|
||
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext'; | ||
import useLexicalEditable from '@lexical/react/useLexicalEditable'; | ||
|
@@ -45,6 +49,9 @@ import {ReactPortal, useCallback, useEffect, useRef, useState} from 'react'; | |
import {createPortal} from 'react-dom'; | ||
import invariant from 'shared/invariant'; | ||
|
||
import useModal from '../../hooks/useModal'; | ||
import ColorPicker from '../../ui/ColorPicker'; | ||
|
||
function computeSelectionCount(selection: GridSelection): { | ||
columns: number; | ||
rows: number; | ||
|
@@ -134,10 +141,30 @@ function $selectLastDescendant(node: ElementNode): void { | |
} | ||
} | ||
|
||
function currentCellBackgroundColor(editor: LexicalEditor): null | string { | ||
return editor.getEditorState().read(() => { | ||
const selection = $getSelection(); | ||
if ( | ||
$isRangeSelection(selection) || | ||
DEPRECATED_$isGridSelection(selection) | ||
) { | ||
const [cell] = DEPRECATED_$getNodeTriplet(selection.anchor); | ||
if ($isTableCellNode(cell)) { | ||
return cell.getBackgroundColor(); | ||
} | ||
} | ||
return null; | ||
}); | ||
} | ||
|
||
type TableCellActionMenuProps = Readonly<{ | ||
contextRef: {current: null | HTMLElement}; | ||
onClose: () => void; | ||
setIsMenuOpen: (isOpen: boolean) => void; | ||
showColorPickerModal: ( | ||
title: string, | ||
showModal: (onClose: () => void) => JSX.Element, | ||
) => void; | ||
tableCellNode: TableCellNode; | ||
cellMerge: boolean; | ||
}>; | ||
|
@@ -148,6 +175,7 @@ function TableActionMenu({ | |
setIsMenuOpen, | ||
contextRef, | ||
cellMerge, | ||
showColorPickerModal, | ||
}: TableCellActionMenuProps) { | ||
const [editor] = useLexicalComposerContext(); | ||
const dropDownRef = useRef<HTMLDivElement | null>(null); | ||
|
@@ -158,6 +186,9 @@ function TableActionMenu({ | |
}); | ||
const [canMergeCells, setCanMergeCells] = useState(false); | ||
const [canUnmergeCell, setCanUnmergeCell] = useState(false); | ||
const [backgroundColor, setBackgroundColor] = useState( | ||
() => currentCellBackgroundColor(editor) || '', | ||
); | ||
|
||
useEffect(() => { | ||
return editor.registerMutationListener(TableCellNode, (nodeMutations) => { | ||
|
@@ -168,6 +199,7 @@ function TableActionMenu({ | |
editor.getEditorState().read(() => { | ||
updateTableCellNode(tableCellNode.getLatest()); | ||
}); | ||
setBackgroundColor(currentCellBackgroundColor(editor) || ''); | ||
} | ||
}); | ||
}, [editor, tableCellNode]); | ||
|
@@ -410,6 +442,24 @@ function TableActionMenu({ | |
}); | ||
}, [editor, tableCellNode, clearTableSelection, onClose]); | ||
|
||
const handleCellBackgroundColor = useCallback( | ||
(value: string) => { | ||
editor.update(() => { | ||
const selection = $getSelection(); | ||
if ( | ||
$isRangeSelection(selection) || | ||
DEPRECATED_$isGridSelection(selection) | ||
) { | ||
const [cell] = DEPRECATED_$getNodeTriplet(selection.anchor); | ||
if ($isTableCellNode(cell)) { | ||
cell.setBackgroundColor(value); | ||
} | ||
} | ||
}); | ||
}, | ||
[editor], | ||
); | ||
|
||
let mergeCellButton: null | JSX.Element = null; | ||
if (cellMerge) { | ||
if (canMergeCells) { | ||
|
@@ -441,12 +491,21 @@ function TableActionMenu({ | |
onClick={(e) => { | ||
e.stopPropagation(); | ||
}}> | ||
{mergeCellButton !== null && ( | ||
<> | ||
{mergeCellButton} | ||
<hr /> | ||
</> | ||
)} | ||
{mergeCellButton} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we no longer need to nil check the mergeCellButton? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rendering a nullish component in React will not render it. It's a shorthand for |
||
<button | ||
className="item" | ||
onClick={() => | ||
showColorPickerModal('Cell background color', () => ( | ||
<ColorPicker | ||
color={backgroundColor} | ||
onChange={handleCellBackgroundColor} | ||
/> | ||
)) | ||
} | ||
data-test-id="table-background-color"> | ||
<span className="text">Background color</span> | ||
</button> | ||
<hr /> | ||
<button | ||
className="item" | ||
onClick={() => insertTableRowAtSelection(false)} | ||
|
@@ -552,6 +611,8 @@ function TableCellActionMenuContainer({ | |
null, | ||
); | ||
|
||
const [colorPickerModal, showColorPickerModal] = useModal(); | ||
|
||
const moveMenu = useCallback(() => { | ||
const menu = menuButtonRef.current; | ||
const selection = $getSelection(); | ||
|
@@ -650,13 +711,15 @@ function TableCellActionMenuContainer({ | |
ref={menuRootRef}> | ||
<i className="chevron-down" /> | ||
</button> | ||
{colorPickerModal} | ||
{isMenuOpen && ( | ||
<TableActionMenu | ||
contextRef={menuRootRef} | ||
setIsMenuOpen={setIsMenuOpen} | ||
onClose={() => setIsMenuOpen(false)} | ||
tableCellNode={tableCellNode} | ||
cellMerge={cellMerge} | ||
showColorPickerModal={showColorPickerModal} | ||
/> | ||
)} | ||
</> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,23 +8,14 @@ | |
|
||
import './ColorPicker.css'; | ||
|
||
import {ReactNode, useEffect, useMemo, useRef, useState} from 'react'; | ||
import {useEffect, useMemo, useRef, useState} from 'react'; | ||
import * as React from 'react'; | ||
|
||
import DropDown from './DropDown'; | ||
import TextInput from './TextInput'; | ||
|
||
interface ColorPickerProps { | ||
disabled?: boolean; | ||
buttonAriaLabel?: string; | ||
buttonClassName: string; | ||
buttonIconClassName?: string; | ||
buttonLabel?: string; | ||
color: string; | ||
children?: ReactNode; | ||
onChange?: (color: string) => void; | ||
stopCloseOnClickSelf?: boolean; | ||
title?: string; | ||
} | ||
|
||
const basicColors = [ | ||
|
@@ -50,11 +41,7 @@ const HEIGHT = 150; | |
|
||
export default function ColorPicker({ | ||
color, | ||
children, | ||
onChange, | ||
disabled = false, | ||
stopCloseOnClickSelf = true, | ||
...rest | ||
}: Readonly<ColorPickerProps>): JSX.Element { | ||
const [selfColor, setSelfColor] = useState(transformColor('hex', color)); | ||
const [inputColor, setInputColor] = useState(color); | ||
|
@@ -118,57 +105,51 @@ export default function ColorPicker({ | |
}, [color]); | ||
|
||
return ( | ||
<DropDown | ||
{...rest} | ||
disabled={disabled} | ||
stopCloseOnClickSelf={stopCloseOnClickSelf}> | ||
<div | ||
className="color-picker-wrapper" | ||
style={{width: WIDTH}} | ||
ref={innerDivRef}> | ||
<TextInput label="Hex" onChange={onSetHex} value={inputColor} /> | ||
<div className="color-picker-basic-color"> | ||
{basicColors.map((basicColor) => ( | ||
<button | ||
className={basicColor === selfColor.hex ? ' active' : ''} | ||
key={basicColor} | ||
style={{backgroundColor: basicColor}} | ||
onClick={() => { | ||
setInputColor(basicColor); | ||
setSelfColor(transformColor('hex', basicColor)); | ||
}} | ||
/> | ||
))} | ||
</div> | ||
<MoveWrapper | ||
className="color-picker-saturation" | ||
style={{backgroundColor: `hsl(${selfColor.hsv.h}, 100%, 50%)`}} | ||
onChange={onMoveSaturation}> | ||
<div | ||
className="color-picker-saturation_cursor" | ||
style={{ | ||
backgroundColor: selfColor.hex, | ||
left: saturationPosition.x, | ||
top: saturationPosition.y, | ||
}} | ||
/> | ||
</MoveWrapper> | ||
<MoveWrapper className="color-picker-hue" onChange={onMoveHue}> | ||
<div | ||
className="color-picker-hue_cursor" | ||
style={{ | ||
backgroundColor: `hsl(${selfColor.hsv.h}, 100%, 50%)`, | ||
left: huePosition.x, | ||
<div | ||
className="color-picker-wrapper" | ||
style={{width: WIDTH}} | ||
ref={innerDivRef}> | ||
<TextInput label="Hex" onChange={onSetHex} value={inputColor} /> | ||
<div className="color-picker-basic-color"> | ||
{basicColors.map((basicColor) => ( | ||
<button | ||
className={basicColor === selfColor.hex ? ' active' : ''} | ||
key={basicColor} | ||
style={{backgroundColor: basicColor}} | ||
onClick={() => { | ||
setInputColor(basicColor); | ||
setSelfColor(transformColor('hex', basicColor)); | ||
}} | ||
/> | ||
</MoveWrapper> | ||
))} | ||
</div> | ||
<MoveWrapper | ||
className="color-picker-saturation" | ||
style={{backgroundColor: `hsl(${selfColor.hsv.h}, 100%, 50%)`}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you've done this logic (modify an HSV colour to have 100% sat, 50% lightness) a few times; maybe factor into its own function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't write this file, just moved the files around. Sorry if the blame is confusing |
||
onChange={onMoveSaturation}> | ||
<div | ||
className="color-picker-color" | ||
style={{backgroundColor: selfColor.hex}} | ||
className="color-picker-saturation_cursor" | ||
style={{ | ||
backgroundColor: selfColor.hex, | ||
left: saturationPosition.x, | ||
top: saturationPosition.y, | ||
}} | ||
/> | ||
</div> | ||
{children} | ||
</DropDown> | ||
</MoveWrapper> | ||
<MoveWrapper className="color-picker-hue" onChange={onMoveHue}> | ||
<div | ||
className="color-picker-hue_cursor" | ||
style={{ | ||
backgroundColor: `hsl(${selfColor.hsv.h}, 100%, 50%)`, | ||
left: huePosition.x, | ||
}} | ||
/> | ||
</MoveWrapper> | ||
<div | ||
className="color-picker-color" | ||
style={{backgroundColor: selfColor.hex}} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume there isn't a non-deprecated alternative here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is to kill GridSelection eventually by moving it into the Table package itself. That's why it remains as deprecated while still actively using it