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
4 changes: 4 additions & 0 deletions frontend/src/cse-ui-kit/assets/code-button.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions frontend/src/cse-ui-kit/small_buttons/CodeButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, { MouseEventHandler } from 'react';
import { StyledButton, buttonProps, scaleRate } from './small_buttons-Styled';
import { ReactComponent as Code } from 'src/cse-ui-kit/assets/code-button.svg';

type Props = {
onClick?: MouseEventHandler<HTMLDivElement>;
onMouseDown?: MouseEventHandler<HTMLDivElement>;
} & buttonProps;

export default function CodeButton({
onClick,
onMouseDown,
...styleProps
}: Props) {
return (
<StyledButton onClick={onClick} onMouseDown={onMouseDown} {...styleProps}>
<Code
height={styleProps.size * scaleRate}
width={styleProps.size * scaleRate}
/>
</StyledButton>
);
}
12 changes: 9 additions & 3 deletions frontend/src/packages/editor/components/EditorBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import EditorCenterAlignButton from './buttons/EditorCenterAlignButton';
import EditorLeftAlignButton from './buttons/EditorLeftAlignButton';
import EditorRightAlignButton from './buttons/EditorRightAlignButton';

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

const defaultTextSize = 16;

Expand All @@ -30,6 +31,7 @@ const Text = styled.span<{
bold: boolean;
italic: boolean;
underline: boolean;
code: boolean | string;
quote: boolean;
textSize: number;
align: string;
Expand All @@ -38,8 +40,10 @@ const Text = styled.span<{
font-style: ${(props) => (props.italic || props.quote ? 'italic' : 'normal')};
color: ${(props) => (props.quote ? '#9e9e9e' : 'black')};
font-size: ${(props) => props.textSize}px;
text-decoration-line: ${(props) => (props.underline ? 'underline' : 'none')};
font-family: ${(props) => props.code ? "monospace" : "inherit"};
text-decoration-line: ${(props) => (props.underline ? "underline" : "none")};
text-align: ${(props) => props.align};
background-color: ${(props) => props.code ? "#eee" : "#fff"};
`;

const Quote = styled.blockquote`
Expand All @@ -66,6 +70,7 @@ const EditorBlock: FC<CMSBlockProps> = ({
italic: leaf.italic ?? false,
underline: leaf.underline ?? false,
quote: leaf.quote ?? false,
code: leaf.code ?? false,
align: leaf.align ?? 'left',
textSize: leaf.textSize ?? defaultTextSize,
...attributes,
Expand Down Expand Up @@ -93,6 +98,7 @@ const EditorBlock: FC<CMSBlockProps> = ({
<EditorBoldButton />
<EditorItalicButton />
<EditorUnderlineButton />
<EditorCodeButton />
<EditorQuoteButton />
<EditorSelectFont />
<EditorLeftAlignButton />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, { FC } from "react";
import { useSlate } from "slate-react";
import CodeButton from "src/cse-ui-kit/small_buttons/CodeButton";
import { toggleMark } from "./buttonHelpers";

const EditorCodeButton: FC = () => {
const editor = useSlate();
return (
<CodeButton
size={30}
onMouseDown={(event) => {
event.preventDefault();
toggleMark(editor, "code");
}}
/>
);
};

export default EditorCodeButton;
12 changes: 9 additions & 3 deletions frontend/src/packages/editor/components/buttons/buttonHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ReactEditor } from 'slate-react';
*/
const toggleMark = (
editor: BaseEditor & ReactEditor,
format: 'bold' | 'italic' | 'underline' | 'quote'
format: 'bold' | 'italic' | 'underline' | 'quote' | 'code'
): void => {
const isActive = isMarkActive(editor, format);

Expand All @@ -27,7 +27,7 @@ const toggleMark = (
*/
const isMarkActive = (
editor: BaseEditor & ReactEditor,
format: 'bold' | 'italic' | 'underline' | 'quote'
format: 'bold' | 'italic' | 'underline' | 'quote' | 'code'
): boolean => {
// https://docs.slatejs.org/concepts/07-editor
// Editor object exposes properties of the current editor
Expand Down Expand Up @@ -69,6 +69,12 @@ const handleKey = (
toggleMark(editor, 'quote');
}
}
};
switch (event.key) {
case '`': {
event.preventDefault();
toggleMark(editor, "code");
}
}
}

export { toggleMark, isMarkActive, handleKey };
1 change: 1 addition & 0 deletions frontend/src/packages/editor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type CustomText = {
quote?: boolean;
type?: string;
align?: string;
code?: string;
};

export interface CMSBlockProps {
Expand Down