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

Bug: solve code and quote break/return #244

Merged
merged 3 commits into from
Jul 24, 2023
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
2 changes: 2 additions & 0 deletions packages/app/src/components/commons/Editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import EditorImagePicker from "./EditorComponents/EditorImagePicker"
import EditorLink from "./EditorComponents/EditorLink"
import EditorShowImage from "./EditorComponents/EditoShowImage"
import { palette, typography } from "../../../theme"
import useHandlePastedText from "./hooks/useHandlePasteText"

const { hasCommandModifier } = KeyBindingUtil
type Config = IConvertToHTMLConfig<DraftInlineStyleType, string, RawDraftEntity>
Expand Down Expand Up @@ -351,6 +352,7 @@ const Editor: React.FC = () => {
handleReturn={handleReturn}
customStyleMap={styleMap}
spellCheck={true}
handlePastedText={useHandlePastedText(editorState, setEditorState)}
/>
<EditorInlineText
showCommand={showInlinePopup}
Expand Down
5 changes: 3 additions & 2 deletions packages/app/src/components/commons/Editor/EditorBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const EditorBlockItem: React.FC<EditorBlockItemProps> = (props) => {
sx={{
position: "relative",
cursor: "text",
mt: 1,
mt: type.includes("code-block") ? 0 : 1,

"&:hover .rich-text": {
opacity: 1,
},
Expand All @@ -62,7 +63,7 @@ const EditorBlockItem: React.FC<EditorBlockItemProps> = (props) => {
position: "absolute",
top: handleTop,
left: type.includes("ordered-list-item" || "unordered-list-item") ? 30 : 0,
color: palette.grays[600],
color: type.includes("code-block") ? palette.whites[600] : palette.grays[600],
}}
>
Type '/' for commands...
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EditorState, DraftHandleValue, Modifier } from "draft-js"

const useHandlePastedText = (
editorState: EditorState,
setEditorState: React.Dispatch<React.SetStateAction<EditorState>>,
) => {
return (text: string, html: string | undefined, editorState: EditorState): DraftHandleValue => {
// We obtain the current content and selection;
const contentState = editorState.getCurrentContent()
const selectionState = editorState.getSelection()

// Replace the text selected with the copied text and we use it as one block
const newContentState = Modifier.replaceText(contentState, selectionState, text)
const newEditorState = EditorState.push(editorState, newContentState, "insert-fragment")

setEditorState(newEditorState)
return "handled"
}
}

export default useHandlePastedText
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { EditorState, DraftHandleValue, RichUtils } from "draft-js"
import { EditorState, DraftHandleValue, Modifier, RichUtils } from "draft-js"

const useHandleReturn = (
editorState: EditorState,
setEditorState: React.Dispatch<React.SetStateAction<EditorState>>,
hasCommandModifier: (e: React.KeyboardEvent) => boolean,
) => {
return (e: React.KeyboardEvent): DraftHandleValue => {
if (e.altKey || !hasCommandModifier(e)) {
return "not-handled"
}
if (e.shiftKey) {
// If we press shift + return, add new line
setEditorState(RichUtils.insertSoftNewline(editorState))
return "handled"
} else {
// If we press return without shift, we create a new block
const contentState = editorState.getCurrentContent()
const selectionState = editorState.getSelection()

let newContentState = Modifier.splitBlock(contentState, selectionState)
newContentState = Modifier.setBlockType(newContentState, newContentState.getSelectionAfter(), "unstyled")
const newEditorState = EditorState.push(editorState, newContentState, "split-block")

setEditorState(RichUtils.insertSoftNewline(editorState))
return "handled"
setEditorState(newEditorState)
return "handled"
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ let theme = createTheme({
font-family: ${typography.fontFamilies.monospace};
margin-bottom: 1rem;
overflow: auto;
padding: 1rem;
padding: 0.5rem;
font-size: 13px;
}
.rich-editor-placeholder{
font-family: ${fontFamilies.serif} !important;
Expand Down
Loading