-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Examples: Syntax highlighting for React (#1583)
* Docs: Syntax highlighting - add react example * Docs: Clean up syntax highlighting demo, make use of functional component * fix: focus view asynchronously, fix #1520 Co-authored-by: Sven Adlung <[email protected]> Co-authored-by: Philipp Kühn <[email protected]>
- Loading branch information
1 parent
e76f796
commit 47d1d34
Showing
8 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
docs/src/demos/Examples/CodeBlockLanguage/React/CodeBlockComponent.jsx
This file contains 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,24 @@ | ||
import React from 'react' | ||
import { NodeViewWrapper, NodeViewContent } from '@tiptap/react' | ||
import './CodeBlockComponent.scss' | ||
|
||
export default ({ node: { attrs: { language: defaultLanguage } }, updateAttributes, extension }) => ( | ||
<NodeViewWrapper className="code-block"> | ||
<select contentEditable={false} defaultValue={defaultLanguage} onChange={event => updateAttributes({ language: event.target.value })}> | ||
<option value="null"> | ||
auto | ||
</option> | ||
<option disabled> | ||
— | ||
</option> | ||
{extension.options.lowlight.listLanguages().map((lang, index) => ( | ||
<option key={index} value={lang}> | ||
{lang} | ||
</option> | ||
))} | ||
</select> | ||
<pre> | ||
<NodeViewContent as="code" /> | ||
</pre> | ||
</NodeViewWrapper> | ||
) |
9 changes: 9 additions & 0 deletions
9
docs/src/demos/Examples/CodeBlockLanguage/React/CodeBlockComponent.scss
This file contains 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,9 @@ | ||
.code-block { | ||
position: relative; | ||
|
||
select { | ||
position: absolute; | ||
right: 0.5rem; | ||
top: 0.5rem; | ||
} | ||
} |
This file contains 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,71 @@ | ||
import React from 'react' | ||
import { useEditor, EditorContent, ReactNodeViewRenderer } from '@tiptap/react' | ||
import Document from '@tiptap/extension-document' | ||
import Paragraph from '@tiptap/extension-paragraph' | ||
import Text from '@tiptap/extension-text' | ||
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight' | ||
import CodeBlockComponent from './CodeBlockComponent' | ||
|
||
// load all highlight.js languages | ||
import lowlight from 'lowlight' | ||
|
||
// load specific languages only | ||
// import lowlight from 'lowlight/lib/core' | ||
// import javascript from 'highlight.js/lib/languages/javascript' | ||
// lowlight.registerLanguage('javascript', javascript) | ||
import './styles.scss' | ||
|
||
const MenuBar = ({ editor }) => { | ||
if (!editor) { | ||
return null | ||
} | ||
|
||
return ( | ||
<button onClick={() => editor.chain().focus().toggleCodeBlock().run()} className={editor.isActive('codeBlock') ? 'is-active' : ''}> | ||
code block | ||
</button> | ||
) | ||
} | ||
|
||
export default () => { | ||
const editor = useEditor({ | ||
extensions: [ | ||
Document, | ||
Paragraph, | ||
Text, | ||
CodeBlockLowlight | ||
.extend({ | ||
addNodeView() { | ||
return ReactNodeViewRenderer(CodeBlockComponent) | ||
}, | ||
}) | ||
.configure({ lowlight }), | ||
], | ||
content: ` | ||
<p> | ||
That’s a boring paragraph followed by a fenced code block: | ||
</p> | ||
<pre><code class="language-javascript">for (var i=1; i <= 20; i++) | ||
{ | ||
if (i % 15 == 0) | ||
console.log("FizzBuzz"); | ||
else if (i % 3 == 0) | ||
console.log("Fizz"); | ||
else if (i % 5 == 0) | ||
console.log("Buzz"); | ||
else | ||
console.log(i); | ||
}</code></pre> | ||
<p> | ||
Press Command/Ctrl + Enter to leave the fenced code block and continue typing in boring paragraphs. | ||
</p> | ||
`, | ||
}) | ||
|
||
return ( | ||
<div> | ||
<MenuBar editor={editor} /> | ||
<EditorContent editor={editor} /> | ||
</div> | ||
) | ||
} |
73 changes: 73 additions & 0 deletions
73
docs/src/demos/Examples/CodeBlockLanguage/React/styles.scss
This file contains 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,73 @@ | ||
/* Basic editor styles */ | ||
.ProseMirror { | ||
> * + * { | ||
margin-top: 0.75em; | ||
} | ||
|
||
pre { | ||
background: #0d0d0d; | ||
color: #fff; | ||
font-family: "JetBrainsMono", monospace; | ||
padding: 0.75rem 1rem; | ||
border-radius: 0.5rem; | ||
|
||
code { | ||
color: inherit; | ||
padding: 0; | ||
background: none; | ||
font-size: 0.8rem; | ||
} | ||
|
||
.hljs-comment, | ||
.hljs-quote { | ||
color: #616161; | ||
} | ||
|
||
.hljs-variable, | ||
.hljs-template-variable, | ||
.hljs-attribute, | ||
.hljs-tag, | ||
.hljs-name, | ||
.hljs-regexp, | ||
.hljs-link, | ||
.hljs-name, | ||
.hljs-selector-id, | ||
.hljs-selector-class { | ||
color: #f98181; | ||
} | ||
|
||
.hljs-number, | ||
.hljs-meta, | ||
.hljs-built_in, | ||
.hljs-builtin-name, | ||
.hljs-literal, | ||
.hljs-type, | ||
.hljs-params { | ||
color: #fbbc88; | ||
} | ||
|
||
.hljs-string, | ||
.hljs-symbol, | ||
.hljs-bullet { | ||
color: #b9f18d; | ||
} | ||
|
||
.hljs-title, | ||
.hljs-section { | ||
color: #faf594; | ||
} | ||
|
||
.hljs-keyword, | ||
.hljs-selector-tag { | ||
color: #70cff8; | ||
} | ||
|
||
.hljs-emphasis { | ||
font-style: italic; | ||
} | ||
|
||
.hljs-strong { | ||
font-weight: 700; | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains 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,3 +1,6 @@ | ||
# Syntax highlighting | ||
|
||
<demo name="Examples/CodeBlockLanguage" /> | ||
<demos :items="{ | ||
Vue: 'Examples/CodeBlockLanguage/Vue', | ||
React: 'Examples/CodeBlockLanguage/React', | ||
}" /> |
This file contains 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