Skip to content

Commit 4e9e7f3

Browse files
committed
demo indent line color change
1 parent c7566ed commit 4e9e7f3

File tree

5 files changed

+222
-13
lines changed

5 files changed

+222
-13
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
},
3030
"dependencies": {
3131
"react": "^17.0.2",
32+
"react-color": "^2.19.3",
3233
"react-dom": "^17.0.2",
3334
"react-icons": "^4.3.1",
35+
"react-outside-click-handler": "^1.3.0",
3436
"react-scripts": "4.0.3",
3537
"react-select": "^5.2.1",
3638
"react-switch": "^6.0.0",
@@ -45,7 +47,9 @@
4547
"@types/jest": "^26.0.15",
4648
"@types/node": "^12.0.0",
4749
"@types/react": "^17.0.0",
50+
"@types/react-color": "^3.0.6",
4851
"@types/react-dom": "^17.0.0",
52+
"@types/react-outside-click-handler": "^1.3.0",
4953
"@types/react-select": "^5.0.1",
5054
"@types/styled-components": "^5.1.15",
5155
"@typescript-eslint/eslint-plugin": "^5.3.1",

src/components/Console.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const Console: React.FC<Props> = () => {
1717
const [indentOptions, setIndentOptions] =
1818
useState<IndentOptionsType>(initialState);
1919
const [indentLining, setIndentLining] = useState<boolean>(false);
20+
const [indentLiningColor, setIndentLiningColor] = useState<string>('#dcdcdc');
2021

2122
useEffect(() => {
2223
chrome.storage.local.get('scrapboxIndentOption', (result) => {
@@ -40,6 +41,17 @@ export const Console: React.FC<Props> = () => {
4041
setIndentLining(scrapboxIndentLining);
4142
}
4243
});
44+
45+
chrome.storage.local.get('scrapboxIndentLiningColor', (result) => {
46+
const scrapboxIndentLiningColor = result.scrapboxIndentLiningColor;
47+
48+
if (!scrapboxIndentLiningColor) {
49+
chrome.storage.local.set({ scrapboxIndentLiningColor: '#dcdcdc' });
50+
setIndentLiningColor('#dcdcdc');
51+
} else {
52+
setIndentLiningColor(scrapboxIndentLiningColor);
53+
}
54+
});
4355
}, []);
4456

4557
const handleIndentLiningChange = (checked: boolean) => {
@@ -64,8 +76,13 @@ export const Console: React.FC<Props> = () => {
6476
setIndentOptions={setIndentOptions}
6577
indentLining={indentLining}
6678
handleIndentLiningChange={handleIndentLiningChange}
79+
setIndentLiningColor={setIndentLiningColor}
80+
/>
81+
<Demonstration
82+
hasLine={indentLining}
83+
indentOptions={indentOptions}
84+
indentLiningColor={indentLiningColor}
6785
/>
68-
<Demonstration hasLine={indentLining} indentOptions={indentOptions} />
6986
</MainContainer>
7087
);
7188
};

src/components/Demonstration.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { IndentOptionsType } from '../utils/types';
55
type Props = {
66
hasLine: boolean;
77
indentOptions: IndentOptionsType;
8+
indentLiningColor: string;
89
};
910

1011
export const Demonstration: React.FC<Props> = (props) => {
@@ -21,8 +22,12 @@ export const Demonstration: React.FC<Props> = (props) => {
2122
{[...Array(spaceNum)].map((j) => {
2223
return (
2324
<VerticalLineIndentContainer key={j}>
24-
{props.hasLine && <VerticalLine />}
25-
<Indent hasLine={props.hasLine} />
25+
{props.hasLine && (
26+
<VerticalLine
27+
indentLiningColor={props.indentLiningColor}
28+
/>
29+
)}
30+
<Indent />
2631
</VerticalLineIndentContainer>
2732
);
2833
})}
@@ -74,16 +79,16 @@ const VerticalLineIndentContainer = styled.div`
7479
position: relative;
7580
`;
7681

77-
const VerticalLine = styled.div`
82+
const VerticalLine = styled.div<{ indentLiningColor: string }>`
7883
width: 1px;
7984
height: 23px;
8085
position: absolute;
8186
top: 0;
8287
left: 45%;
83-
background-color: #dcdcdc;
88+
background-color: ${(props) => props.indentLiningColor};
8489
`;
8590

86-
const Indent = styled.div<{ hasLine: boolean }>`
91+
const Indent = styled.div`
8792
width: 24px;
8893
height: 23px;
8994
`;

src/components/MakerConsole.tsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from 'react';
2+
import { ColorResult, TwitterPicker } from 'react-color';
23
import { IconContext } from 'react-icons';
34
import { MdOutlineColorLens } from 'react-icons/md';
5+
import OutsideClickHandler from 'react-outside-click-handler';
46
import Select, { ActionMeta, SingleValue } from 'react-select';
57
import Switch from 'react-switch';
68
import styled from 'styled-components';
@@ -18,9 +20,11 @@ type Props = {
1820
setIndentOptions: React.Dispatch<React.SetStateAction<IndentOptionsType>>;
1921
indentLining: boolean;
2022
handleIndentLiningChange: (checked: boolean) => void;
23+
setIndentLiningColor: (color: string) => void;
2124
};
2225

2326
export const MakerConsole: React.FC<Props> = (props) => {
27+
const [pickerOpen, setPickerOpen] = React.useState(false);
2428
const handleOnChange = (
2529
newValue: SingleValue<{
2630
value: string;
@@ -57,6 +61,26 @@ export const MakerConsole: React.FC<Props> = (props) => {
5761
});
5862
};
5963

64+
const handlePickerOpen = () => {
65+
setPickerOpen(true);
66+
};
67+
68+
const handleColorChange = ({ hex }: ColorResult) => {
69+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
70+
chrome.storage.local.set({ scrapboxIndentLineColor: hex });
71+
72+
tabs.forEach((tab) => {
73+
if (tab.url === undefined || tab.id === undefined) return;
74+
const url = new URL(tab.url);
75+
if (url.hostname === 'scrapbox.io') {
76+
chrome.tabs.sendMessage(tab.id, 'scrapbox_indent_lining_color');
77+
}
78+
});
79+
});
80+
81+
props.setIndentLiningColor(hex);
82+
};
83+
6084
return (
6185
<>
6286
<TitleWrapper>
@@ -79,9 +103,23 @@ export const MakerConsole: React.FC<Props> = (props) => {
79103
<IconContext.Provider
80104
value={{ size: '20px', style: { padding: '2px' } }}
81105
>
82-
<ColorLens />
106+
<ColorLens onClick={handlePickerOpen} />
83107
</IconContext.Provider>
84108
</TitleWrapper>
109+
{pickerOpen && (
110+
<OutsideClickHandler
111+
onOutsideClick={() => {
112+
setPickerOpen(false);
113+
}}
114+
>
115+
<TwitterPickerWrapper>
116+
<TwitterPicker
117+
triangle="top-right"
118+
onChangeComplete={handleColorChange}
119+
/>
120+
</TwitterPickerWrapper>
121+
</OutsideClickHandler>
122+
)}
85123
<IndentContainer>
86124
{props.indentOptions.map((indentOption) => {
87125
const spaceNum = +indentOption.label.replace(/[^0-9]/g, '') - 1;
@@ -109,6 +147,13 @@ export const MakerConsole: React.FC<Props> = (props) => {
109147
);
110148
};
111149

150+
const TwitterPickerWrapper = styled.div`
151+
position: absolute;
152+
top: 114px;
153+
right: 10px;
154+
z-index: 1;
155+
`;
156+
112157
const TitleWrapper = styled.div`
113158
display: flex;
114159
align-items: center;

0 commit comments

Comments
 (0)