-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
||
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> | ||
); | ||
} |
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'; | ||
|
@@ -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; | ||
|
||
|
@@ -46,6 +48,51 @@ const Text = styled.span<{ | |
background-color: ${(props) => props.code ? "#eee" : "#fff"}; | ||
`; | ||
|
||
|
||
const CheckListItemElement = ({children, attributes, leaf}: any) => { | ||
const readOnly = useReadOnly() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
// 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; | ||
|
@@ -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 }) => { | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? ( | ||
|
@@ -100,6 +155,7 @@ const EditorBlock: FC<CMSBlockProps> = ({ | |
<EditorUnderlineButton /> | ||
<EditorCodeButton /> | ||
<EditorQuoteButton /> | ||
<EditorChecklistButton/> | ||
<EditorSelectFont /> | ||
<EditorLeftAlignButton /> | ||
<EditorCenterAlignButton /> | ||
|
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; |
There was a problem hiding this comment.
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