Skip to content

Commit

Permalink
feat: add layer icon
Browse files Browse the repository at this point in the history
  • Loading branch information
F-star committed Nov 17, 2024
1 parent 896f3ad commit 4c766e0
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/graphics/ellipse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,8 @@ export class SuikaEllipse extends SuikaGraphics<EllipseAttrs> {
)})"`;
}
}

override getLayerIconPath() {
return 'M11 6C11 8.76142 8.76142 11 6 11C3.23858 11 1 8.76142 1 6C1 3.23858 3.23858 1 6 1C8.76142 1 11 3.23858 11 6Z';
}
}
4 changes: 4 additions & 0 deletions packages/core/src/graphics/graphics/graphics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,10 @@ export class SuikaGraphics<ATTRS extends GraphicsAttrs = GraphicsAttrs> {
};
}

getLayerIconPath() {
return 'M1 1H11V11H1V1Z';
}

getWorldTransform(): IMatrixArr {
const parent = this.getParent();
if (parent) {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/graphics/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,8 @@ export class SuikaLine extends SuikaGraphics<LineAttrs> {
strokePaints: this.attrs.stroke ?? [],
};
}

override getLayerIconPath() {
return 'M0.5 0.5 L 10.5 10.5';
}
}
4 changes: 4 additions & 0 deletions packages/core/src/graphics/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,8 @@ export class SuikaRect extends SuikaGraphics<RectAttrs> {
this.attrs.height
}" transform="matrix(${tf.join(' ')})"${cornerRadiusStr}`;
}

override getLayerIconPath() {
return 'M1 1H11V11H1V1Z';
}
}
4 changes: 4 additions & 0 deletions packages/core/src/graphics/regular_polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,8 @@ export class SuikaRegularPolygon extends SuikaGraphics<RegularPolygonAttrs> {
.map((p) => `${p.x},${p.y}`)
.join(' ')}" transform="matrix(${tf.join(' ')})"`;
}

override getLayerIconPath() {
return 'M6 2L11.1962 11H0.803848L6 2Z';
}
}
4 changes: 4 additions & 0 deletions packages/core/src/graphics/star.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,8 @@ export class SuikaStar extends SuikaGraphics<StarAttrs> {
.map((p) => `${p.x},${p.y}`)
.join(' ')}" transform="matrix(${tf.join(' ')})"`;
}

override getLayerIconPath() {
return 'M6.5 2L7.51031 5.10942H10.7798L8.13472 7.03115L9.14503 10.1406L6.5 8.21885L3.85497 10.1406L4.86528 7.03115L2.22025 5.10942H5.48969L6.5 2Z';
}
}
4 changes: 4 additions & 0 deletions packages/core/src/graphics/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export class SuikaText extends SuikaGraphics<TextAttrs> {
return `>${content}</text>`;
}

override getLayerIconPath() {
return 'M0 0H11V3H10V1H6V9H7.5V10H3.5V9H5V1H1V3H0V0Z';
}

getGlyphs() {
if (this._glyphs) return this._glyphs;
this._glyphs = calcGlyphInfos(this.attrs.content, {
Expand Down
12 changes: 10 additions & 2 deletions packages/suika/src/components/LayerPanel/LayerPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type IObject, MutateGraphsAndRecord } from '@suika/core';
import { type FC, useContext, useEffect, useState } from 'react';

import { EditorContext } from '../../context';
import { Tree } from './LayerItem/tree';
import { LayerTree } from './LayerTree/LayerTree';

export const LayerPanel: FC = () => {
const editor = useContext(EditorContext);
Expand Down Expand Up @@ -44,6 +44,13 @@ export const LayerPanel: FC = () => {
editor.render();
};

const getLayerIcon = (id: string) => {
if (!editor) return '';

const graphics = editor.doc.getGraphicsById(id);
return graphics ? graphics.getLayerIconPath() : '';
};

const setEditorHlId = (id: string) => {
if (editor) {
const graphics = editor.doc.getGraphicsById(id) ?? null;
Expand Down Expand Up @@ -85,7 +92,7 @@ export const LayerPanel: FC = () => {

return (
<div className="layer-panel">
<Tree
<LayerTree
treeData={objects}
activeIds={Array.from(selectedIds)}
hlId={hlId}
Expand All @@ -94,6 +101,7 @@ export const LayerPanel: FC = () => {
setHlId={setEditorHlId}
setName={setName}
setSelectedGraph={setSelectedGraph}
getLayerIcon={getLayerIcon}
/>
</div>
);
Expand Down
27 changes: 27 additions & 0 deletions packages/suika/src/components/LayerPanel/LayerTree/LayerIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';

interface LayerIconProps {
content: string;
enableStroke?: boolean;
enableFill?: boolean;
}

export const LayerIcon = React.memo(
({ content, enableStroke = true, enableFill }: LayerIconProps) => {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
xmlnsXlink="http://www.w3.org/1999/xlink"
width="12"
height="12"
>
<path
d={content}
fill={enableFill ? 'currentColor' : 'none'}
strokeWidth="1"
stroke={enableStroke ? 'currentColor' : 'none'}
/>
</svg>
);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
flex-shrink: 0;
}

.sk-layer-icon {
width: 18px;
display: flex;
justify-content: center;
align-items: center;
flex-shrink: 0;
color: #0000004d;
}

.sk-layout-name {
padding: 0 4px;
border: 1px solid transparent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import {
import classNames from 'classnames';
import { type FC, useEffect, useRef, useState } from 'react';

import { LayerIcon } from './LayerIcon';
import { type IBaseEvents } from './type';

interface IProps extends IBaseEvents {
id: string;
type: string;
name: string;
children?: IObject[];
active?: boolean;
Expand All @@ -35,6 +37,7 @@ const LayerItem: FC<IProps> = ({
active = false,
activeSecond = false,
id,
type,
activeIds = [],
level = 0,
hlId,
Expand All @@ -47,6 +50,7 @@ const LayerItem: FC<IProps> = ({
setHlId,
setName,
setSelectedGraph,
getLayerIcon,
}) => {
const indentWidth = level * 16;
const [isEditing, setIsEditing] = useState(false);
Expand Down Expand Up @@ -116,6 +120,13 @@ const LayerItem: FC<IProps> = ({
<div className="sk-group-collapse-btn">
{children?.length ? <SmallCaretDownSolid /> : undefined}
</div>
<div className="sk-layer-icon">
<LayerIcon
content={getLayerIcon(id)}
enableFill={type === 'Text'}
enableStroke={type !== 'Text'}
/>
</div>
{!isEditing && (
<span key={'span'} className="sk-layout-name">
{layoutName}
Expand Down Expand Up @@ -187,6 +198,7 @@ const LayerItem: FC<IProps> = ({
<LayerItem
key={item.id}
id={item.id}
type={item.type}
name={item.name}
active={activeIds.includes(item.id)}
activeSecond={activeSecond || activeIds.includes(item.id)}
Expand All @@ -203,6 +215,7 @@ const LayerItem: FC<IProps> = ({
toggleLock={toggleLock}
setHlId={setHlId}
setSelectedGraph={setSelectedGraph}
getLayerIcon={getLayerIcon}
/>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ interface IProps extends IBaseEvents {
treeData: IObject[];
activeIds: string[];
hlId: string;
getLayerIcon: (id: string) => string;
}

export const Tree: FC<IProps> = ({
export const LayerTree: FC<IProps> = ({
treeData,
activeIds = [],
hlId: hoverId,
Expand All @@ -19,11 +20,13 @@ export const Tree: FC<IProps> = ({
setHlId: setHoverId,
setName,
setSelectedGraph,
getLayerIcon,
}) => {
return (
<div>
{[...treeData].reverse().map((item) => (
<LayerItem
type={item.type}
active={activeIds.includes(item.id)}
activeSecond={activeIds.includes(item.id)}
key={item.id}
Expand All @@ -39,6 +42,7 @@ export const Tree: FC<IProps> = ({
setHlId={setHoverId}
setName={setName}
setSelectedGraph={setSelectedGraph}
getLayerIcon={getLayerIcon}
/>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface IBaseEvents {
objId: string,
event: React.MouseEvent<Element, MouseEvent>,
) => void;
getLayerIcon: (id: string) => string;
}

0 comments on commit 4c766e0

Please sign in to comment.