72 lines
1.8 KiB
JavaScript
72 lines
1.8 KiB
JavaScript
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>
|
||
)
|
||
}
|