Skip to content

Commit

Permalink
Merge pull request #190 from tsukuba-cojt/feature/182_refactoring
Browse files Browse the repository at this point in the history
Feature/182 refactoring
  • Loading branch information
itsu-dev authored Dec 6, 2023
2 parents 12d23df + 15d532d commit 3dea317
Show file tree
Hide file tree
Showing 30 changed files with 1,512 additions and 1,414 deletions.
44 changes: 17 additions & 27 deletions src/components/Base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import {
useElementSelectionContext,
} from '../contexts/ElementSelectionContext';
import ElementSelector from './utils/ElementSelector';
import { setFormatsAndPushToAry } from '../features/formatter';
import { getAbsoluteCSSSelector } from '../utils/CSSUtils';
import TemplateCustomizer from './tabitems/template/TemplateCustomizer';
import Scrollable from './tabitems/common/Scrollable';
import LayerCustomizer from './tabitems/layer/LayerCustomizer';
import { UIUpdaterContext, useUIUpdater } from '../contexts/UIUpdater';
import t from '../features/translator';
import usePropsContext, { PropsContext } from '../contexts/PropsContext';
import Controller from './Controller';

const Wrapper = styled.div`
width: 100%;
Expand Down Expand Up @@ -69,27 +68,13 @@ const Base = ({}: /* categoryMap */ BaseProps) => {
const elementSelection = useElementSelectionContext();
const updater = useUIUpdater();
const props = usePropsContext();
const [fontPermissionGranted, setFontPermissionGranted] = useState<boolean>(true);

const onChange = (key: string, value: string, id: number | string) => {
if (elementSelection.selectedElement) {
setFormatsAndPushToAry([
{
id,
cssSelector:
getAbsoluteCSSSelector(elementSelection.selectedElement) +
elementSelection.selectedPseudoClass,
values: [{ key, value }],
},
]);
updater.formatChanged();
}
};
const [fontPermissionGranted, setFontPermissionGranted] =
useState<boolean>(true);

const tabs = {
//templates.json: <ChangeStyleTabItem categoryMap={categoryMap} />,
fonts: <FontCustomizer onChange={onChange} />,
blocks: <BlockCustomizer onChange={onChange} />,
fonts: <FontCustomizer />,
blocks: <BlockCustomizer />,
templates: <TemplateCustomizer />,
layers: <LayerCustomizer />,
settings: <PageSettingTabItem />,
Expand Down Expand Up @@ -121,7 +106,7 @@ const Base = ({}: /* categoryMap */ BaseProps) => {
} catch {
setFontPermissionGranted(false);
}
}
};

useEffect(() => {
(async () => {
Expand All @@ -134,20 +119,25 @@ const Base = ({}: /* categoryMap */ BaseProps) => {
<PropsContext.Provider value={props}>
<ElementSelectionContext.Provider value={elementSelection}>
<UIUpdaterContext.Provider value={updater}>
<Controller />
<ToolBar />
{ fontPermissionGranted &&
{fontPermissionGranted && (
<TabWrapper>
<Scrollable>
<Tabs items={items} tabBarGutter={0} />
</Scrollable>
</TabWrapper>
}
{ !fontPermissionGranted &&
)}
{!fontPermissionGranted && (
<>
<Description>{t('need_to_grant_for_access_local_font')}</Description>
<Button block type={'primary'} onClick={checkFontPermission}>{t('grant_for_access')}</Button>
<Description>
{t('need_to_grant_for_access_local_font')}
</Description>
<Button block type={'primary'} onClick={checkFontPermission}>
{t('grant_for_access')}
</Button>
</>
}
)}
<ElementSelector />
</UIUpdaterContext.Provider>
</ElementSelectionContext.Provider>
Expand Down
277 changes: 140 additions & 137 deletions src/components/ChangeStyleCategory.tsx
Original file line number Diff line number Diff line change
@@ -1,137 +1,140 @@
import styled from 'styled-components';
import Title from 'antd/lib/typography/Title';
import React, { useContext } from 'react';
import { ChangeStyleElement, LayoutPart } from '../types/ChangeStyleElement';
import { Collapse, Input, InputNumber } from 'antd';
import { CSSParseResultElementType } from '../types/RestaSetting';
import t from '../features/translator';
import { setFormatAndPushToAry } from '../features/formatter';
import InputNumberWithUnit from './controls/InputNumberWithUnit';
import Select from './controls/Select';
import { getAbsoluteCSSSelector } from '../utils/CSSUtils';
import { ElementSelectionContext } from '../contexts/ElementSelectionContext';

const Wrapper = styled.div``;

const CollapseWrapper = styled.div`
background-color: white;
border-radius: 8px;
`;

const Description = styled.p`
font-size: 10pt;
padding-bottom: 12px;
`;

const { Panel } = Collapse;

interface CategoryProps {
searchText: string;
title: string;
elements: ChangeStyleElement[];
}

const ChangeStyleCategory = ({
searchText,
title,
elements,
}: CategoryProps) => {
const elementSelection = useContext(ElementSelectionContext);

const onChange = (key: string, value: string, id: number | string) => {
if (elementSelection.selectedElement) {
setFormatAndPushToAry(
getAbsoluteCSSSelector(elementSelection.selectedElement),
key,
value,
id,
);
}
};

const filter = (element: ChangeStyleElement) => {
if (searchText.length == 0) {
return true;
}
return (
t(element.name).includes(searchText) || element.key.includes(searchText)
);
};

const isNumberWithUnit = (parts: LayoutPart[]) => {
return (
parts.length == 2 &&
parts[0].type === CSSParseResultElementType.NUMBER &&
parts[1].type === CSSParseResultElementType.SELECT
);
};

return (
<Wrapper>
<Title level={5} style={{ margin: '16px 0 8px' }}>
{t(title)}
</Title>
<CollapseWrapper>
<Collapse size="small">
{elements.filter(filter).map((element, index) => (
<Panel key={index} header={t(element.name)}>
<Description>{t(element.description)}</Description>
{isNumberWithUnit(element.parts) && (
<InputNumberWithUnit
key={index}
cssKey={element.key}
id={element.id}
options={element.parts[1].options!}
onChange={onChange}
/>
)}
{!isNumberWithUnit(element.parts) &&
element.parts.map((part, index) => (
<>
{part.type === CSSParseResultElementType.SELECT && (
<Select
key={index}
cssKey={element.key}
id={element.id}
options={{}}
onChange={onChange}
/>
)}
{part.type === CSSParseResultElementType.STRING && (
<Input
key={index}
onChange={(value) =>
onChange(
element.key,
value.currentTarget.value,
element.id,
)
}
/>
)}
{part.type === CSSParseResultElementType.NUMBER && (
<InputNumber
key={index}
onChange={(value) =>
onChange(
element.key,
value?.toString() ?? '',
element.id,
)
}
/>
)}
{part.type === CSSParseResultElementType.RAWTEXT && (
<span key={index}>{part.text}</span>
)}
</>
))}
</Panel>
))}
</Collapse>
</CollapseWrapper>
</Wrapper>
);
};

export default ChangeStyleCategory;
import styled from 'styled-components';
import Title from 'antd/lib/typography/Title';
import React, { useContext } from 'react';
import { ChangeStyleElement, LayoutPart } from '../types/ChangeStyleElement';
import { Collapse, Input, InputNumber } from 'antd';
import { CSSParseResultElementType } from '../types/RestaSetting';
import t from '../features/translator';
import { setFormatAndPushToAry } from '../features/formatter';
import InputNumberWithUnit from './controls/InputNumberWithUnit';
import Select from './controls/Select';
import { getAbsoluteCSSSelector } from '../utils/CSSUtils';
import { ElementSelectionContext } from '../contexts/ElementSelectionContext';
import { PropsContext } from '../contexts/PropsContext';

const Wrapper = styled.div``;

const CollapseWrapper = styled.div`
background-color: white;
border-radius: 8px;
`;

const Description = styled.p`
font-size: 10pt;
padding-bottom: 12px;
`;

const { Panel } = Collapse;

interface CategoryProps {
searchText: string;
title: string;
elements: ChangeStyleElement[];
}

const ChangeStyleCategory = ({
searchText,
title,
elements,
}: CategoryProps) => {
const elementSelection = useContext(ElementSelectionContext);
const prop = useContext(PropsContext);

const onChange = (key: string, value: string, id: number | string) => {
if (elementSelection.selectedElement) {
setFormatAndPushToAry(
getAbsoluteCSSSelector(elementSelection.selectedElement),
key,
value,
id,
prop,
);
}
};

const filter = (element: ChangeStyleElement) => {
if (searchText.length == 0) {
return true;
}
return (
t(element.name).includes(searchText) || element.key.includes(searchText)
);
};

const isNumberWithUnit = (parts: LayoutPart[]) => {
return (
parts.length == 2 &&
parts[0].type === CSSParseResultElementType.NUMBER &&
parts[1].type === CSSParseResultElementType.SELECT
);
};

return (
<Wrapper>
<Title level={5} style={{ margin: '16px 0 8px' }}>
{t(title)}
</Title>
<CollapseWrapper>
<Collapse size="small">
{elements.filter(filter).map((element, index) => (
<Panel key={index} header={t(element.name)}>
<Description>{t(element.description)}</Description>
{isNumberWithUnit(element.parts) && (
<InputNumberWithUnit
key={index}
cssKey={element.key}
id={element.id}
options={element.parts[1].options!}
onChange={onChange}
/>
)}
{!isNumberWithUnit(element.parts) &&
element.parts.map((part, index) => (
<>
{part.type === CSSParseResultElementType.SELECT && (
<Select
key={index}
cssKey={element.key}
id={element.id}
options={{}}
onChange={onChange}
/>
)}
{part.type === CSSParseResultElementType.STRING && (
<Input
key={index}
onChange={(value) =>
onChange(
element.key,
value.currentTarget.value,
element.id,
)
}
/>
)}
{part.type === CSSParseResultElementType.NUMBER && (
<InputNumber
key={index}
onChange={(value) =>
onChange(
element.key,
value?.toString() ?? '',
element.id,
)
}
/>
)}
{part.type === CSSParseResultElementType.RAWTEXT && (
<span key={index}>{part.text}</span>
)}
</>
))}
</Panel>
))}
</Collapse>
</CollapseWrapper>
</Wrapper>
);
};

export default ChangeStyleCategory;
18 changes: 18 additions & 0 deletions src/components/Controller.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useContext, useEffect } from 'react';
import { PropsContext } from '../contexts/PropsContext';
import { loadFormat, loadImportedStyle } from '../features/format_manager';

export default function Controller() {
const prop = useContext(PropsContext);

prop.setCurrentUrl(window.location.href);
prop.setEditedUrl(window.location.href);

useEffect(() => {
// do something
loadFormat(prop);
loadImportedStyle(prop);
}, []);

return null;
}
Loading

0 comments on commit 3dea317

Please sign in to comment.