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 <info@svenadlung.de>
Co-authored-by: Philipp Kühn <kontakt@philipp-kuehn.com>
This commit is contained in:
Hans Pagel
2021-07-28 23:42:11 +02:00
committed by GitHub
parent e76f7966b5
commit 47d1d3425c
8 changed files with 188 additions and 2 deletions

View File

@@ -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>
)

View File

@@ -0,0 +1,9 @@
.code-block {
position: relative;
select {
position: absolute;
right: 0.5rem;
top: 0.5rem;
}
}

View File

@@ -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>
Thats 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>
)
}

View File

@@ -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;
}
}
}

View File

@@ -1,3 +1,6 @@
# Syntax highlighting # Syntax highlighting
<demo name="Examples/CodeBlockLanguage" /> <demos :items="{
Vue: 'Examples/CodeBlockLanguage/Vue',
React: 'Examples/CodeBlockLanguage/React',
}" />

View File

@@ -73,7 +73,13 @@ export const focus: RawCommands['focus'] = (position = null) => ({
tr.setStoredMarks(storedMarks) tr.setStoredMarks(storedMarks)
} }
view.focus() // focus async because in some situations weird things happen
// see: https://github.com/ueberdosis/tiptap/issues/1520
setTimeout(() => {
if (!editor.isDestroyed) {
view.focus()
}
}, 0)
} }
return true return true