Skip to content

Commit f4bdba3

Browse files
authored
Merge pull request #7 from penicillin0/feature-5/indent-color
インデントをわかりやすくする機能
2 parents fd028d4 + a15719a commit f4bdba3

File tree

4 files changed

+122
-6
lines changed

4 files changed

+122
-6
lines changed

js/scrapboxCssScript.js

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,69 @@ const insertIndentCSSRule = (scrapboxIndentOptions) => {
6464
});
6565
};
6666

67-
const storageAttachment = () => {
67+
const indentColorCSS = [
68+
`.app:not(.presentation) .indent-mark .char-index:not(:nth-last-child(1)):not(:nth-last-child(2)) { position: relative; }`,
69+
`.app:not(.presentation) .indent-mark .char-index:not(:nth-last-child(1)):not(:nth-last-child(2))::before {
70+
content: " ";
71+
position: absolute;
72+
left: 44%;
73+
top: -24%;
74+
border-left: 0.2rem solid #dcdcdc;
75+
}`,
76+
`.app:not(.presentation) .indent-mark .char-index:not(:nth-last-child(1)):not(:nth-last-child(2)):nth-child(n) { background-color: #f5f5f5 }`,
77+
];
78+
79+
const insertIndentColorCSSRule = (isColoring) => {
80+
// delete existing rules
81+
const cssRules = document.styleSheets[0].cssRules;
82+
const cssRulesNum = cssRules.length;
83+
for (let i = cssRulesNum - 1; i >= 0; i--) {
84+
const cssRule = cssRules[i];
85+
const cssSelector = cssRule.selectorText;
86+
87+
if (cssSelector === undefined) continue;
88+
89+
if (
90+
cssSelector.match(
91+
/^\.app:not\(\.presentation\) .indent-mark .char-index:not\(:nth-last-child\(1\)\):not\(:nth-last-child\(2\)\)*/
92+
)
93+
) {
94+
document.styleSheets[0].deleteRule(i);
95+
}
96+
}
97+
98+
// insert new rules
99+
if (isColoring) {
100+
indentColorCSS.map((css) => {
101+
document.styleSheets[0].insertRule(css);
102+
});
103+
}
104+
};
105+
106+
const makerAttachment = () => {
68107
chrome.storage.local.get('scrapboxIndentOption', (result) => {
69108
const scrapboxIndentOptions = result.scrapboxIndentOption;
70109
insertIndentCSSRule(scrapboxIndentOptions);
71110
});
72111
};
73112

113+
const coloringAttachment = () => {
114+
chrome.storage.local.get('scrapboxIndentColoring', (result) => {
115+
const isColoring = result.scrapboxIndentColoring;
116+
insertIndentColorCSSRule(isColoring);
117+
});
118+
};
119+
74120
// update
75121
chrome.runtime.onMessage.addListener((request) => {
76122
if (request === 'scrapbox_list_maker') {
77-
storageAttachment();
123+
makerAttachment();
124+
}
125+
if (request === 'scrapbox_indent_coloring') {
126+
coloringAttachment();
78127
}
79128
});
80129

81130
// initialize
82-
storageAttachment();
131+
makerAttachment();
132+
coloringAttachment();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"react-icons": "^4.3.1",
3434
"react-scripts": "4.0.3",
3535
"react-select": "^5.2.1",
36+
"react-switch": "^6.0.0",
3637
"styled-components": "^5.3.3",
3738
"web-vitals": "^1.0.1"
3839
},

src/components/Console.tsx

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useEffect, useState } from 'react';
22
import Select, { ActionMeta, SingleValue } from 'react-select';
3+
import Switch from 'react-switch';
34
import styled from 'styled-components';
45

56
const options = [
@@ -25,6 +26,7 @@ export const Console: React.FC<Props> = () => {
2526
label: string;
2627
}[]
2728
>(initialState);
29+
const [indentColoring, setIndentColoring] = useState<boolean>(false);
2830

2931
useEffect(() => {
3032
chrome.storage.local.get('scrapboxIndentOption', (result) => {
@@ -37,8 +39,34 @@ export const Console: React.FC<Props> = () => {
3739
setIndentOptions(scrapboxIndentOption);
3840
}
3941
});
42+
43+
chrome.storage.local.get('scrapboxIndentColoring', (result) => {
44+
const scrapboxIndentColoring = result.scrapboxIndentColoring;
45+
46+
if (!scrapboxIndentColoring) {
47+
chrome.storage.local.set({ scrapboxIndentColoring: false });
48+
setIndentColoring(false);
49+
} else {
50+
setIndentColoring(scrapboxIndentColoring);
51+
}
52+
});
4053
}, []);
4154

55+
const handleIndentColoringChange = (checked: boolean) => {
56+
chrome.storage.local.set({ scrapboxIndentColoring: checked });
57+
setIndentColoring(checked);
58+
59+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
60+
tabs.forEach((tab) => {
61+
if (tab.url === undefined || tab.id === undefined) return;
62+
const url = new URL(tab.url);
63+
if (url.hostname === 'scrapbox.io') {
64+
chrome.tabs.sendMessage(tab.id, 'scrapbox_indent_coloring');
65+
}
66+
});
67+
});
68+
};
69+
4270
const handleOnChange = (
4371
newValue: SingleValue<{
4472
value: string;
@@ -67,7 +95,6 @@ export const Console: React.FC<Props> = () => {
6795
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
6896
tabs.forEach((tab) => {
6997
if (tab.url === undefined || tab.id === undefined) return;
70-
7198
const url = new URL(tab.url);
7299
if (url.hostname === 'scrapbox.io') {
73100
chrome.tabs.sendMessage(tab.id, 'scrapbox_list_maker');
@@ -78,7 +105,24 @@ export const Console: React.FC<Props> = () => {
78105

79106
return (
80107
<MainContainer>
81-
<Title>Select Favorite List Maker</Title>
108+
<TitleWrapper>
109+
<Title>Select Favorite List Maker</Title>
110+
<SwitchLabel>
111+
<Switch
112+
checked={indentColoring}
113+
onChange={handleIndentColoringChange}
114+
onColor="#00b428"
115+
boxShadow="0px 1px 5px rgba(0, 0, 0, 0.6)"
116+
height={16}
117+
width={30}
118+
handleDiameter={18}
119+
checkedIcon={false}
120+
uncheckedIcon={false}
121+
/>
122+
<Spacer width={6} />
123+
<span>Indent Visible Line</span>
124+
</SwitchLabel>
125+
</TitleWrapper>
82126
<IndentContainer>
83127
{indentOptions.map((indentOption) => {
84128
const spaceNum = +indentOption.label.replace(/[^0-9]/g, '') - 1;
@@ -102,7 +146,6 @@ export const Console: React.FC<Props> = () => {
102146
);
103147
})}
104148
</IndentContainer>
105-
106149
<DemonstrationContainer>
107150
<Demonstration>
108151
<Title>Demonstration</Title>
@@ -134,12 +177,27 @@ const MainContainer = styled.div`
134177
padding-bottom: 8px;
135178
`;
136179

180+
const Spacer = styled.div<{ width: number }>`
181+
width: ${(props) => props.width}px;
182+
`;
183+
184+
const TitleWrapper = styled.div`
185+
display: flex;
186+
align-items: center;
187+
`;
188+
137189
const Title = styled.div`
138190
font-size: 16px;
139191
text-align: left;
140192
margin: 12px 10px;
141193
`;
142194

195+
const SwitchLabel = styled.label`
196+
margin-left: 8px;
197+
display: flex;
198+
align-items: center;
199+
`;
200+
143201
const Label = styled.div`
144202
font-size: 14px;
145203
margin-left: 12px;

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9543,6 +9543,13 @@ react-select@*, react-select@^5.2.1:
95439543
prop-types "^15.6.0"
95449544
react-transition-group "^4.3.0"
95459545

9546+
react-switch@^6.0.0:
9547+
version "6.0.0"
9548+
resolved "https://registry.yarnpkg.com/react-switch/-/react-switch-6.0.0.tgz#bd4a2dea08f211b8a32e55e8314fd44bc1ec947e"
9549+
integrity sha512-QV3/6eRK5/5epdQzIqvDAHRoGLbCv/wDpHUi6yBMXY1Xco5XGuIZxvB49PHoV1v/SpEgOCJLD/Zo43iic+aEIw==
9550+
dependencies:
9551+
prop-types "^15.7.2"
9552+
95469553
react-transition-group@^4.3.0:
95479554
version "4.4.2"
95489555
resolved "https://registry.yarnpkg.com/react-transition-group/-/react-transition-group-4.4.2.tgz#8b59a56f09ced7b55cbd53c36768b922890d5470"

0 commit comments

Comments
 (0)