-
Notifications
You must be signed in to change notification settings - Fork 18
#158 add codemirror in new text section #164
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
Open
Franlop7
wants to merge
17
commits into
Lemoncode:develop
Choose a base branch
from
Franlop7:#158-Add-codemirror-in-new-text-section
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c2b898d
first component
Franlop7 48eeb40
update Select component
Franlop7 d41b8dc
Co-authored-by: Manolo Dorado <manudous@users.noreply.github.com>
Franlop7 bcf08d7
delete error EditorView theme
Franlop7 3992438
update value MarkdownEditor
Franlop7 d980816
update makdown and new text
Franlop7 df04fa4
Update styles
Franlop7 ecc279f
delete textarea componet
Franlop7 53e8d55
add markdowneditor
Franlop7 a9cb0dc
add react markdown component view and styles
Franlop7 e003dd0
edit styles
Franlop7 0ff86d9
update component
Franlop7 40c654e
update editor
Franlop7 1070b87
Updated components
Franlop7 d5cb651
updated prettier
Franlop7 7e5d381
delete model .css dont need
Franlop7 5941c10
change role for div component
Franlop7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './hooks'; | ||
export * from './global-window'; | ||
export * from './markdown-editor'; | ||
export * from './markdown-view'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './markdown-editor.component'; |
93 changes: 93 additions & 0 deletions
93
front/src/common/markdown-editor/markdown-editor.component.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import React from 'react'; | ||
import { basicSetup, EditorView } from 'codemirror'; | ||
import { EditorState } from '@codemirror/state'; | ||
import { markdown } from '@codemirror/lang-markdown'; | ||
import { languages } from '@codemirror/language-data'; | ||
import { tags } from '@lezer/highlight'; | ||
import { syntaxHighlighting, HighlightStyle } from '@codemirror/language'; | ||
import * as classes from './markdown-editor.styles'; | ||
|
||
const editorStyles = HighlightStyle.define([ | ||
{ tag: tags.heading1, class: classes.headerH1 }, | ||
{ tag: tags.heading2, class: classes.headerH2 }, | ||
{ tag: tags.heading3, class: classes.headerH3 }, | ||
]); | ||
|
||
interface Props { | ||
value: string; | ||
onChange?: (value: string) => void; | ||
className?: string; | ||
onAppendTrainerTextInternal?: () => void; | ||
} | ||
|
||
export const MarkdownEditor: React.FC<Props> = (props) => { | ||
const { value, onChange, className, onAppendTrainerTextInternal } = props; | ||
|
||
const refContainer = React.useRef(null); | ||
const editorView = React.useRef<EditorView>(); | ||
|
||
React.useEffect(() => { | ||
if (!refContainer.current) return; | ||
|
||
editorView.current = new EditorView({ | ||
state: EditorState.create({ | ||
doc: value, | ||
extensions: [ | ||
basicSetup, | ||
markdown({ | ||
codeLanguages: languages, | ||
}), | ||
syntaxHighlighting(editorStyles), | ||
EditorView.lineWrapping, | ||
EditorView.updateListener.of((update) => { | ||
onChange(update.state.doc.toString()); | ||
}), | ||
EditorView.theme({ | ||
'&': { | ||
border: '2px solid #070707', | ||
height: '300px', | ||
}, | ||
}), | ||
], | ||
}), | ||
parent: refContainer.current, | ||
}); | ||
|
||
return () => editorView.current?.destroy(); | ||
}, []); | ||
|
||
const scrollToEnd = () => { | ||
if (!refContainer.current) return; | ||
|
||
const scrollHeight = refContainer.current.scrollHeight; | ||
const clientHeight = refContainer.current.clientHeight; | ||
editorView.current.scrollDOM.scrollTop = scrollHeight - clientHeight; | ||
}; | ||
|
||
React.useEffect(() => { | ||
const state = editorView.current?.state; | ||
const currentValue = state?.doc.toString(); | ||
if (state && currentValue !== value) { | ||
const update = state.update({ | ||
changes: { from: 0, to: state.doc.length, insert: value }, | ||
}); | ||
editorView.current?.update([update]); | ||
scrollToEnd(); | ||
} | ||
}, [value]); | ||
// TODO HAY QUE ARREGLARLO PARA EL SESION | ||
// ?. si existe llama a la función, de lo contrario, se ignora | ||
return ( | ||
<div | ||
role="log" | ||
id="session" | ||
ref={refContainer} | ||
className={className} | ||
onKeyDown={(event) => { | ||
if (event.key === 'Enter' && event.ctrlKey) { | ||
onAppendTrainerTextInternal?.(); | ||
} | ||
}} | ||
/> | ||
); | ||
}; |
13 changes: 13 additions & 0 deletions
13
front/src/common/markdown-editor/markdown-editor.styles.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { css } from '@emotion/css'; | ||
|
||
export const headerH1 = css` | ||
font-size: 2.3rem; | ||
`; | ||
|
||
export const headerH2 = css` | ||
font-size: 1.8rem; | ||
`; | ||
|
||
export const headerH3 = css` | ||
font-size: 1.3rem; | ||
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './markdown-view'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
import ReactMarkdown from 'react-markdown'; | ||
import remarkGfm from 'remark-gfm'; | ||
|
||
interface Props { | ||
value: string; | ||
className?: string; | ||
} | ||
|
||
export const MarkdownViewer: React.FC<Props> = (props) => { | ||
return ( | ||
<div role="log" className={props.className}> | ||
<ReactMarkdown children={props.value} remarkPlugins={[remarkGfm]} /> | ||
</div> | ||
); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react'; | ||
import { FormControl, InputLabel, Select, MenuItem } from '@mui/material'; | ||
import { languageFormat } from '../trainer.constants'; | ||
|
||
interface Props { | ||
onChange: (value: string) => void; | ||
value: string; | ||
} | ||
|
||
export const SelectComponent: React.FC<Props> = (props) => { | ||
const { onChange, value } = props; | ||
|
||
const handleSelectChange = (event) => { | ||
onChange(event.target.value as string); | ||
}; | ||
|
||
return ( | ||
<FormControl variant="outlined"> | ||
<InputLabel htmlFor="select">Language</InputLabel> | ||
<Select | ||
id="select" | ||
label="Language" | ||
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. remove htmlFor 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. or try add id="select" and change htmlFor="select" |
||
value={value} | ||
onChange={handleSelectChange} | ||
> | ||
{languageFormat.map((language) => ( | ||
<MenuItem key={language.id} value={language.id}> | ||
{language.label} | ||
</MenuItem> | ||
))} | ||
</Select> | ||
</FormControl> | ||
); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.