Uh oh!
There was an error while loading. Please reload this page.
- 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
base:develop
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c2b898d48eeb40d41b8dcbcf08d73992438d980816df04fa4ecc279f53e8d55a9cb0dce003dd00ff86d940c654e1070b87d5cb6517e5d3815941c10File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| 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'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './markdown-editor.component'; |
| 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?.(); | ||
| } | ||
| }} | ||
| /> | ||
| ); | ||
| }; |
| 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; | ||
| `; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './markdown-view'; |
| 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> | ||
| ); | ||
| }; |
| 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" | ||
Member 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 Member 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> | ||
| ); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.