Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 232 additions & 5 deletions src/components/TextWidget/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,68 @@ type Props = {
const FormGroup = styled.div`
display: flex;
margin-bottom: 1rem;
width: 480px;
& > label {
width: 20%;
}
& > input {
min-width: 480px;
& > div {
flex-grow: 1;
margin-left: 0.25rem;
}
`;

class Color {
public r: number;
public g: number;
public b: number;
public a: number;

constructor(r: number, g: number, b: number, a: number) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}

// from #rrggbb
static fromRGBCode(rgb: string): Color {
return new Color(
parseInt(rgb.substr(1,2), 16),
parseInt(rgb.substr(3,2), 16),
parseInt(rgb.substr(5,2), 16),
1,
);
}

// from rgba(r,g,b,a)
static fromRGBA(rgba: string): Color {
const match = rgba.match(/rgba\((\d+),(\d+),(\d+),(\d(\.\d+)?)\)/);
return new Color(
parseInt(match[1]),
parseInt(match[2]),
parseInt(match[3]),
parseFloat(match[4])
);
}

toRGBA(): string {
const rgba = [this.r, this.g, this.b, this.a];
return `rgba(${rgba.join(',')})`;
}

toRGBCode(): string {
function toHex(num): string {
let res = num.toString(16);
if (res.length === 1) {
res = `0${res}`;
}
return res;
}
const r = toHex(this.r);
const g = toHex(this.g);
const b = toHex(this.b);

return `#${r}${g}${b}`;
}
}

class TextWidgetEditor extends Component<Props, TextWidgetProps> {
constructor(props) {
super(props);
Expand All @@ -46,6 +99,180 @@ class TextWidgetEditor extends Component<Props, TextWidgetProps> {
value={this.state.text}
/>
</FormGroup>
<FormGroup>
<TextField
type="text"
label="text size"
fullWidth
variant="outlined"
onChange={(e) => {
this.setState({ ...this.state, fontSize: e.target.value });
}}
value={this.state.fontSize}
/>
<TextField
type="color"
label="text color"
fullWidth
variant="outlined"
onChange={(e) => {
this.setState({ ...this.state, textColor: e.target.value });
}}
value={this.state.textColor}
/>
<TextField
type="number"
label="text edge weight"
fullWidth
variant="outlined"
onChange={(e) => {
this.setState({ ...this.state, edgeWeight: parseFloat(e.target.value) });
}}
value={this.state.edgeWeight}
/>
<TextField
type="color"
label="text edge color"
fullWidth
variant="outlined"
onChange={(e) => {
this.setState({ ...this.state, edgeColor: e.target.value });
}}
value={this.state.edgeColor}
/>
<TextField
type="color"
label="background color"
fullWidth
variant="outlined"
onChange={(e) => {
const color = Color.fromRGBA(this.state.backgroundColor);
const newColor = Color.fromRGBCode(e.target.value);
newColor.a = color.a;
this.setState({ ...this.state, backgroundColor: newColor.toRGBA() });
}}
value={Color.fromRGBA(this.state.backgroundColor).toRGBCode()}
/>
<TextField
type="number"
label="background opacity"
fullWidth
variant="outlined"
onChange={(e) => {
const color = Color.fromRGBA(this.state.backgroundColor)
color.a = parseFloat(e.target.value);
this.setState({ ...this.state, backgroundColor: color.toRGBA() });
}}
value={Color.fromRGBA(this.state.backgroundColor).a}
/>
</FormGroup>
<FormGroup>
<TextField
type="number"
label="width"
fullWidth
variant="outlined"
onChange={(e) => {
this.setState({ ...this.state, width: parseFloat(e.target.value) });
}}
value={this.state.width}
/>
<TextField
type="number"
label="height"
fullWidth
variant="outlined"
onChange={(e) => {
this.setState({ ...this.state, height: parseFloat(e.target.value) });
}}
value={this.state.height}
/>

<TextField
type="number"
label="position top"
fullWidth
variant="outlined"
onChange={(e) => {
const pos = this.state.position || {};
if (e.target.value !== "") {
const v = parseInt(e.target.value);
if (isNaN(v)) {
delete pos.top;
} else {
pos.top = v;
}
} else {
delete pos.top;
}
this.setState({ ...this.state, position: pos });
}}
value={this.state?.position?.top}
/>
<TextField
type="number"
label="position right"
fullWidth
variant="outlined"
onChange={(e) => {
const pos = this.state.position || {};
if (e.target.value !== "") {
const v = parseInt(e.target.value);
if (isNaN(v)) {
delete pos.right;
} else {
pos.right = v;
}
} else {
delete pos.right;
}
this.setState({ ...this.state, position: pos });
}}
value={this.state.position?.right}
/>
<TextField
type="number"
label="position bottom"
fullWidth
variant="outlined"
onChange={(e) => {
const pos = this.state.position || {};
if (e.target.value !== "") {
const v = parseInt(e.target.value);
if (isNaN(v)) {
delete pos.bottom;
} else {
pos.bottom = v;
}
} else {
delete pos.bottom;
}
this.setState({ ...this.state, position: pos });
}}
value={this.state.position?.bottom}
/>
<TextField
type="number"
label="position left"
fullWidth
variant="outlined"
onChange={(e) => {
const pos = this.state.position || {};
if (e.target.value !== "") {
const v = parseInt(e.target.value);
if (isNaN(v)) {
delete pos.left;
} else {
pos.left = v;
}
} else {
delete pos.left;
}
this.setState({ ...this.state, position: pos });
}}
value={this.state.position?.left}
/>
</FormGroup>

<FirebaseDatabaseMutation
type="set"
Expand Down
1 change: 1 addition & 0 deletions src/components/TextWidget/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Position = {
type TextWidgetProps = {
text: string;
textColor?: string;
fontSize?: string;
backgroundColor?: string;
edgeWeight?: number; // px
edgeColor?: string;
Expand Down
32 changes: 15 additions & 17 deletions src/components/TextWidget/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ const defaultStyle: CSSProperties = {
boxSizing: 'border-box',
whiteSpace: 'pre-wrap',
overflow: 'hidden',
color: 'white',
backgroundColor: 'rgba(0, 0, 0 0.1)',
textShadow: calcTextShadow(1, 'black'),
color: '#ffffff',
backgroundColor: 'rgba(0,0,0,0.1)',
textShadow: calcTextShadow(1, '#000000'),
width: 320,
height: 540,
padding: '4px 8px',
padding: '0.25rem 0.5rem',
position: 'absolute',
};

const TextWidget: VFC<TextWidgetProps> = ({
text,
textColor,
fontSize,
backgroundColor,
edgeWeight,
edgeColor,
Expand All @@ -39,26 +40,23 @@ const TextWidget: VFC<TextWidgetProps> = ({
padding,
position,
}) => {
const edge = calcTextShadow(edgeWeight || 1, edgeColor || 'black');
const edge = calcTextShadow(edgeWeight || 1, edgeColor || '#000000');

const style: CSSProperties = {
...defaultStyle,
width: `${width || 320}px`,
height: `${height | 540}px`,
padding: padding || '4px 8px',
color: textColor || 'white',
height: `${height || 540}px`,
padding: padding || '0.25rem 0.5rem',
color: textColor || '#ffffff',
fontSize: fontSize || '1rem',
textShadow: edge,
backgroundColor: backgroundColor || 'rgba(0, 0, 0, 0.1)',
backgroundColor: backgroundColor || 'rgba(0,0,0,0.1)',
};

if (position?.top || position?.right || position?.bottom || position?.left) {
if (position?.top) style.top = position.top;
if (position?.right) style.right = position.right;
if (position?.bottom) style.bottom = position.bottom;
if (position?.left) style.left = position.left;
} else {
style.right = 0;
}
if (position?.top !== undefined) style.top = position.top;
if (position?.right !== undefined) style.right = position.right;
if (position?.bottom !== undefined) style.bottom = position.bottom;
if (position?.left !== undefined) style.left = position.left;

console.log(style);

Expand Down