Skip to content

Commit 4a7f9a2

Browse files
josh-ramos-22Josh Ramos
andauthored
[WEB-28] [EDITOR] implement code blocks (#346)
* stubbed code format btn * imported code icon * Stubbed btn appearing in editor (tho only half of the icon rip * initial imp * fixed bg color of code blocks * fixed code icon * found icon closer to the spec * reverted style in ternary operator * Removed TODO comment * fixed merge conflict --------- Co-authored-by: Josh Ramos <josh.ramos@student.unsw.edu.au>
1 parent 8c089e5 commit 4a7f9a2

File tree

6 files changed

+65
-6
lines changed

6 files changed

+65
-6
lines changed
Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, { MouseEventHandler } from 'react';
2+
import { StyledButton, buttonProps, scaleRate } from './small_buttons-Styled';
3+
import { ReactComponent as Code } from 'src/cse-ui-kit/assets/code-button.svg';
4+
5+
type Props = {
6+
onClick?: MouseEventHandler<HTMLDivElement>;
7+
onMouseDown?: MouseEventHandler<HTMLDivElement>;
8+
} & buttonProps;
9+
10+
export default function CodeButton({
11+
onClick,
12+
onMouseDown,
13+
...styleProps
14+
}: Props) {
15+
return (
16+
<StyledButton onClick={onClick} onMouseDown={onMouseDown} {...styleProps}>
17+
<Code
18+
height={styleProps.size * scaleRate}
19+
width={styleProps.size * scaleRate}
20+
/>
21+
</StyledButton>
22+
);
23+
}

frontend/src/packages/editor/components/EditorBlock.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import EditorCenterAlignButton from './buttons/EditorCenterAlignButton';
1313
import EditorLeftAlignButton from './buttons/EditorLeftAlignButton';
1414
import EditorRightAlignButton from './buttons/EditorRightAlignButton';
1515

16-
import ContentBlock from '../../../cse-ui-kit/contentblock/contentblock-wrapper';
17-
import { handleKey } from './buttons/buttonHelpers';
16+
import ContentBlock from "../../../cse-ui-kit/contentblock/contentblock-wrapper";
17+
import { handleKey } from "./buttons/buttonHelpers";
18+
import EditorCodeButton from "./buttons/EditorCodeButton";
1819

1920
const defaultTextSize = 16;
2021

@@ -30,6 +31,7 @@ const Text = styled.span<{
3031
bold: boolean;
3132
italic: boolean;
3233
underline: boolean;
34+
code: boolean | string;
3335
quote: boolean;
3436
textSize: number;
3537
align: string;
@@ -38,8 +40,10 @@ const Text = styled.span<{
3840
font-style: ${(props) => (props.italic || props.quote ? 'italic' : 'normal')};
3941
color: ${(props) => (props.quote ? '#9e9e9e' : 'black')};
4042
font-size: ${(props) => props.textSize}px;
41-
text-decoration-line: ${(props) => (props.underline ? 'underline' : 'none')};
43+
font-family: ${(props) => props.code ? "monospace" : "inherit"};
44+
text-decoration-line: ${(props) => (props.underline ? "underline" : "none")};
4245
text-align: ${(props) => props.align};
46+
background-color: ${(props) => props.code ? "#eee" : "#fff"};
4347
`;
4448

4549
const Quote = styled.blockquote`
@@ -66,6 +70,7 @@ const EditorBlock: FC<CMSBlockProps> = ({
6670
italic: leaf.italic ?? false,
6771
underline: leaf.underline ?? false,
6872
quote: leaf.quote ?? false,
73+
code: leaf.code ?? false,
6974
align: leaf.align ?? 'left',
7075
textSize: leaf.textSize ?? defaultTextSize,
7176
...attributes,
@@ -93,6 +98,7 @@ const EditorBlock: FC<CMSBlockProps> = ({
9398
<EditorBoldButton />
9499
<EditorItalicButton />
95100
<EditorUnderlineButton />
101+
<EditorCodeButton />
96102
<EditorQuoteButton />
97103
<EditorSelectFont />
98104
<EditorLeftAlignButton />
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React, { FC } from "react";
2+
import { useSlate } from "slate-react";
3+
import CodeButton from "src/cse-ui-kit/small_buttons/CodeButton";
4+
import { toggleMark } from "./buttonHelpers";
5+
6+
const EditorCodeButton: FC = () => {
7+
const editor = useSlate();
8+
return (
9+
<CodeButton
10+
size={30}
11+
onMouseDown={(event) => {
12+
event.preventDefault();
13+
toggleMark(editor, "code");
14+
}}
15+
/>
16+
);
17+
};
18+
19+
export default EditorCodeButton;

frontend/src/packages/editor/components/buttons/buttonHelpers.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ReactEditor } from 'slate-react';
88
*/
99
const toggleMark = (
1010
editor: BaseEditor & ReactEditor,
11-
format: 'bold' | 'italic' | 'underline' | 'quote'
11+
format: 'bold' | 'italic' | 'underline' | 'quote' | 'code'
1212
): void => {
1313
const isActive = isMarkActive(editor, format);
1414

@@ -27,7 +27,7 @@ const toggleMark = (
2727
*/
2828
const isMarkActive = (
2929
editor: BaseEditor & ReactEditor,
30-
format: 'bold' | 'italic' | 'underline' | 'quote'
30+
format: 'bold' | 'italic' | 'underline' | 'quote' | 'code'
3131
): boolean => {
3232
// https://docs.slatejs.org/concepts/07-editor
3333
// Editor object exposes properties of the current editor
@@ -69,6 +69,12 @@ const handleKey = (
6969
toggleMark(editor, 'quote');
7070
}
7171
}
72-
};
72+
switch (event.key) {
73+
case '`': {
74+
event.preventDefault();
75+
toggleMark(editor, "code");
76+
}
77+
}
78+
}
7379

7480
export { toggleMark, isMarkActive, handleKey };

frontend/src/packages/editor/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type CustomText = {
1616
quote?: boolean;
1717
type?: string;
1818
align?: string;
19+
code?: string;
1920
};
2021

2122
export interface CMSBlockProps {

0 commit comments

Comments
 (0)