Commit 69ee823b authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[CodeEditor] postprocessing in html preview, styling fixes

Syntax highlighting of JSON in the preview.
parent c670fc65
...@@ -90,11 +90,15 @@ ...@@ -90,11 +90,15 @@
margin-left: 25px; margin-left: 25px;
padding-left: 25px; padding-left: 25px;
} }
.code-editor .editor .html ul li { .code-editor .editor .html.language-md ul li {
list-style: disc !important; list-style: disc !important;
} }
.code-editor .editor .html ol li { .code-editor .editor .html.language-md ol li {
list-style: decimal !important; list-style: decimal !important;
} }
.code-editor .editor .html.language-json {
font-family: Fira code, Fira Mono, Consolas, Menlo, Courier, monospace;
white-space: pre;
}
/*# sourceMappingURL=CodeEditor.css.map */ /*# sourceMappingURL=CodeEditor.css.map */
...@@ -74,9 +74,13 @@ ...@@ -74,9 +74,13 @@
flex-grow: 2 flex-grow: 2
margin-left: 25px margin-left: 25px
padding-left: 25px padding-left: 25px
ul &.language-md
li ul
list-style: disc !important li
ol list-style: disc !important
li ol
list-style: decimal !important li
list-style: decimal !important
&.language-json
font-family: Fira code,Fira Mono,Consolas,Menlo,Courier,monospace
white-space: pre
...@@ -51,9 +51,8 @@ type Props = ...@@ -51,9 +51,8 @@ type Props =
-- Fixes newlines in code -- Fixes newlines in code
-- This is useful eg for proper rendering of the textarea overlay -- This is useful eg for proper rendering of the textarea overlay
codeNlFix :: CodeType -> Code -> Code codeNlFix :: CodeType -> Code -> Code
codeNlFix Markdown "" = " " codeNlFix _ "" = " "
codeNlFix Markdown c = if endsWith "\n" c then (c <> " ") else c codeNlFix _ c = if endsWith "\n" c then (c <> " ") else c
codeNlFix _ c = c
compile :: CodeType -> Code -> Either Error Html compile :: CodeType -> Code -> Either Error Html
compile JSON code = result compile JSON code = result
...@@ -64,6 +63,11 @@ compile JSON code = result ...@@ -64,6 +63,11 @@ compile JSON code = result
Right parsed -> Right $ R2.stringify parsed 2 Right parsed -> Right $ R2.stringify parsed 2
compile Markdown code = Right $ compileMd $ codeNlFix Markdown code compile Markdown code = Right $ compileMd $ codeNlFix Markdown code
previewPostProcess :: CodeType -> Element -> Effect Unit
previewPostProcess Markdown _ = pure unit
previewPostProcess JSON htmlEl = do
HLJS.highlightBlock htmlEl
-- TODO Replace with markdown-it? -- TODO Replace with markdown-it?
-- https://pursuit.purescript.org/packages/purescript-markdown-it -- https://pursuit.purescript.org/packages/purescript-markdown-it
compileMd' :: forall e. MD.ToMarkupOptions e -> String -> String compileMd' :: forall e. MD.ToMarkupOptions e -> String -> String
...@@ -113,7 +117,7 @@ codeEditorCpt = R.hooksComponent "G.C.CE.CodeEditor" cpt ...@@ -113,7 +117,7 @@ codeEditorCpt = R.hooksComponent "G.C.CE.CodeEditor" cpt
, on: { change: onEditChange controls } , on: { change: onEditChange controls }
, placeholder: "Type some code..." , placeholder: "Type some code..."
, ref: controls.codeElRef } [ ] , ref: controls.codeElRef } [ ]
, H.pre { className: "" , H.pre { className: (langClass $ fst controls.codeType)
-- , contentEditable: "true" -- , contentEditable: "true"
, ref: controls.codeOverlayElRef , ref: controls.codeOverlayElRef
, rows: 30 , rows: 30
...@@ -122,7 +126,7 @@ codeEditorCpt = R.hooksComponent "G.C.CE.CodeEditor" cpt ...@@ -122,7 +126,7 @@ codeEditorCpt = R.hooksComponent "G.C.CE.CodeEditor" cpt
] ]
] ]
, H.div { className: "v-divider " <> (dividerHidden $ fst controls.viewType) } [ H.text " " ] , H.div { className: "v-divider " <> (dividerHidden $ fst controls.viewType) } [ H.text " " ]
, H.div { className: "html " <> (previewHidden $ fst controls.viewType) , H.div { className: "html " <> (langClass $ fst controls.codeType) <> (previewHidden $ fst controls.viewType)
, ref: controls.htmlElRef , ref: controls.htmlElRef
} [] } []
] ]
...@@ -137,6 +141,10 @@ codeEditorCpt = R.hooksComponent "G.C.CE.CodeEditor" cpt ...@@ -137,6 +141,10 @@ codeEditorCpt = R.hooksComponent "G.C.CE.CodeEditor" cpt
dividerHidden Both = "" dividerHidden Both = ""
dividerHidden _ = "hidden" dividerHidden _ = "hidden"
langClass :: CodeType -> String
langClass JSON = "language-json"
langClass Markdown = "language-md"
previewHidden :: ViewType -> String previewHidden :: ViewType -> String
previewHidden Preview = "" previewHidden Preview = ""
previewHidden Both = "" previewHidden Both = ""
...@@ -171,6 +179,7 @@ renderHtml code {codeType: (codeType /\ _), htmlElRef, error: (_ /\ setError)} = ...@@ -171,6 +179,7 @@ renderHtml code {codeType: (codeType /\ _), htmlElRef, error: (_ /\ setError)} =
Right compiled -> do Right compiled -> do
setError $ const Nothing setError $ const Nothing
_ <- pure $ (htmlEl .= "innerHTML") compiled _ <- pure $ (htmlEl .= "innerHTML") compiled
previewPostProcess codeType htmlEl
pure unit pure unit
toolbar :: Record Controls -> R.Element toolbar :: Record Controls -> R.Element
...@@ -192,8 +201,8 @@ toolbarCpt = R.hooksComponent "G.C.CE.toolbar" cpt ...@@ -192,8 +201,8 @@ toolbarCpt = R.hooksComponent "G.C.CE.toolbar" cpt
-- Handle rerendering of preview when viewType changed -- Handle rerendering of preview when viewType changed
onChangeCodeType :: forall e. Record Controls -> e -> Effect Unit onChangeCodeType :: forall e. Record Controls -> e -> Effect Unit
onChangeCodeType controls _ = do onChangeCodeType controls _ = do
_ <- renderHtml (R.readRef controls.editorCodeRef) controls setCodeOverlay controls (R.readRef controls.editorCodeRef)
pure unit renderHtml (R.readRef controls.editorCodeRef) controls
type ErrorComponentProps = type ErrorComponentProps =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment