Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WEB-72] Implement checklists #382

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"redux-persist": "^6.0.0",
"redux-saga": "1.1.3",
"slate": "0.78.0",
"slate-history": "^0.93.0",
"slate-react": "0.79.0",
"socket.io": "^4.5.3",
"socket.io-client": "^4.5.3",
Expand Down
44 changes: 44 additions & 0 deletions frontend/src/cse-ui-kit/assets/checklist-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/ChecklistButton.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/checklist-button.svg';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename the component as Checklist instead


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>
);
}
64 changes: 60 additions & 4 deletions frontend/src/packages/editor/components/EditorBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import styled from 'styled-components';
import { createEditor } from 'slate';
import React, { FC, useMemo, useCallback } from 'react';
import { Slate, Editable, withReact, RenderLeafProps } from 'slate-react';
import { Slate, Editable, withReact, RenderLeafProps, useReadOnly } from 'slate-react';
import { createEditor } from 'slate'

import { CMSBlockProps } from '../types';
import EditorBoldButton from './buttons/EditorBoldButton';
Expand All @@ -12,10 +12,12 @@ import EditorSelectFont from './buttons/EditorSelectFont';
import EditorCenterAlignButton from './buttons/EditorCenterAlignButton';
import EditorLeftAlignButton from './buttons/EditorLeftAlignButton';
import EditorRightAlignButton from './buttons/EditorRightAlignButton';
import EditorCodeButton from "./buttons/EditorCodeButton";
import EditorChecklistButton from "./buttons/EditorChecklistButton";

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

const defaultTextSize = 16;

Expand Down Expand Up @@ -46,6 +48,51 @@ const Text = styled.span<{
background-color: ${(props) => props.code ? "#eee" : "#fff"};
`;


const CheckListItemElement = ({children, attributes, leaf}: any) => {
const readOnly = useReadOnly()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the use of this hook as it is unlikely for us to set the editor to readonly. We can always add it back in when needed though :D

const [checked, setChecked] = React.useState(false)
return (
<div {...attributes} style={{display: 'flex', alignItems: 'left'}}>
<div
style={{ userSelect: "none" }}
contentEditable={false}
>
Comment on lines +57 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This div doesn't appear to do much so you can remove it

<span
contentEditable={false}
style={{
marginRight: '0.75em'
}}
>
<input
type="checkbox"
checked={checked}
onChange={event => {
// Figure out why ReactEditor.findPath does not work
// const path = ReactEditor.findPath(editor as ReactEditor, leaf);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your current implementation works so you can just remove these comments but if you wanted to know why the ReactEditor.findPath() doesn't work, i think these could possibly be the reasons:

  1. the editor needs to be passed in from the EditorBlock component
  2. this findPath function is specifically finding a node (https://docs.slatejs.org/libraries/slate-react/react-editor#reacteditor.findpath-editor-reacteditor-node-node-path) but here you are rendering the checklists as leaves. This means that the function will never return the current leaf. You can resolve this by changing the checklists into nodes. NOTE: you can think of a node as sort of a big wrapper/container whilst leaves are sort of the children with decorations. However, I do have some doubts about this cause in that case it should in theory find the leaf level text node surrounding this lead? Not completely sure without delving into it too deeply.
  3. This is probably the most likely reason: the structure of our editor/leaves/nodes does not follow typical slate operations and therefore any functions that involve traversing through the editor path is doomed to fail

// const newProperties = {
// checked: event.target.checked,
// }
// Transforms.setNodes(editor, newProperties, { at: path })
setChecked(event.target.checked);
}}
/>
</span>
</div>
<span
contentEditable={!readOnly}
suppressContentEditableWarning
style = {{
flex: 1,
opacity: checked ? 0.666 : 1,
textDecoration: checked ? 'line-through' : 'none',}}
>
{children}
</span>
</div>
)}


const Quote = styled.blockquote`
border-left: 3px solid #9e9e9e;
margin: 0px;
Expand All @@ -61,7 +108,7 @@ const EditorBlock: FC<CMSBlockProps> = ({
showToolBar,
onEditorClick,
}) => {
const editor = useMemo(() => withReact(createEditor()), []);
const editor = useMemo(() => withHistory(withReact(createEditor())), [])

const renderLeaf: (props: RenderLeafProps) => JSX.Element = useCallback(
({ attributes, children, leaf }) => {
Expand All @@ -73,9 +120,17 @@ const EditorBlock: FC<CMSBlockProps> = ({
code: leaf.code ?? false,
align: leaf.align ?? 'left',
textSize: leaf.textSize ?? defaultTextSize,
checklist: leaf.checklist ?? false,
checked: leaf.checklist ?? false,
Comment on lines +123 to +124
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't appear to be used elsewhere so you can remove it. Let me know if i missed its use case

...attributes,
};


if (leaf.checklist) {
const checklistProps = {attributes, children, leaf}
return <CheckListItemElement {...checklistProps}/>
}

return leaf.quote ? (
<QuoteText {...props}>{children}</QuoteText>
) : leaf.align == null ? (
Expand All @@ -100,6 +155,7 @@ const EditorBlock: FC<CMSBlockProps> = ({
<EditorUnderlineButton />
<EditorCodeButton />
<EditorQuoteButton />
<EditorChecklistButton/>
<EditorSelectFont />
<EditorLeftAlignButton />
<EditorCenterAlignButton />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { FC } from "react";
import { useSlate } from "slate-react";
import { toggleMark } from "./buttonHelpers";
import ChecklistButton from "src/cse-ui-kit/small_buttons/ChecklistButton";
import { Editor } from "slate";

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

export default EditorChecklistButton;
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' | 'code'
format: 'bold' | 'italic' | 'underline' | 'quote' | 'code' | 'checklist'
): void => {
const isActive = isMarkActive(editor, format);

Expand All @@ -27,7 +27,7 @@ const toggleMark = (
*/
const isMarkActive = (
editor: BaseEditor & ReactEditor,
format: 'bold' | 'italic' | 'underline' | 'quote' | 'code'
format: 'bold' | 'italic' | 'underline' | 'quote' | 'code' | 'checklist'
): boolean => {
// https://docs.slatejs.org/concepts/07-editor
// Editor object exposes properties of the current editor
Expand Down
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 = {
type?: string;
align?: string;
code?: string;
checklist?: boolean;
};

export type CustomElement = {
Expand Down