-
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
176 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.color-picker { | ||
position: relative; | ||
display: inline-block; | ||
} | ||
|
||
.color-picker__button { | ||
background-color: transparent; | ||
border: none; | ||
padding: 5px 5px; | ||
} | ||
|
||
.color-picker__button:hover { | ||
background-color: lightgrey; | ||
} | ||
|
||
.color-picker__content { | ||
position: absolute; | ||
top: 35px; | ||
left: -8px; | ||
z-index: 2; | ||
} | ||
|
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,44 @@ | ||
import React, { useContext, useEffect, useState } from 'react'; | ||
import { GithubPicker } from 'react-color'; | ||
import { FaPalette } from 'react-icons/fa'; | ||
import './ColorPicker.css'; | ||
import { EditorSelectionContext } from '../../TextEditor'; | ||
import useConstant from 'use-constant'; | ||
|
||
function ColorPicker() { | ||
const [opened, setOpened] = useState(false); | ||
const [color, setColor] = useState(null); | ||
const editorSelectionContext = useContext(EditorSelectionContext); | ||
|
||
const selectionChangeListener = useConstant(() => () => setColor(document.queryCommandValue('foreColor'))); | ||
|
||
useEffect( | ||
() => editorSelectionContext.registerSelectionChangeListener(selectionChangeListener), | ||
[] | ||
); | ||
|
||
const onColorSelected = (color) => { | ||
document.execCommand('styleWithCSS', true); | ||
document.execCommand('foreColor', true, color.hex); | ||
document.execCommand('styleWithCSS', false); | ||
setOpened(false); | ||
} | ||
return ( | ||
<div className="color-picker"> | ||
<button | ||
type="button" | ||
className="color-picker__button" | ||
onClick={() => setOpened(!opened)} | ||
> | ||
<FaPalette color={color}/> | ||
</button> | ||
{opened && ( | ||
<div className="color-picker__content" onMouseDown={e => e.preventDefault()}> | ||
<GithubPicker onChangeComplete={onColorSelected}/> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
export default ColorPicker; |
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,5 @@ | ||
import ColorPicker from './ColorPicker'; | ||
|
||
export const COLOR_PICKER_PLUGIN_CONFIG = [{ | ||
component: ColorPicker | ||
}]; |
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,9 +1,11 @@ | ||
import { SIMPLE_ACTION_PLUGIN_CONFIG } from './simple-action'; | ||
import { SYNONYMS_ACTION_PLUGIN_CONFIG } from './synonyms'; | ||
import { COLOR_PICKER_PLUGIN_CONFIG } from './color-picker'; | ||
|
||
const PLUGINS = [ | ||
...SIMPLE_ACTION_PLUGIN_CONFIG, | ||
...SYNONYMS_ACTION_PLUGIN_CONFIG | ||
...SYNONYMS_ACTION_PLUGIN_CONFIG, | ||
...COLOR_PICKER_PLUGIN_CONFIG | ||
]; | ||
|
||
export default PLUGINS; |