Skip to content

Commit

Permalink
Examples: Syntax highlighting for React (#1583)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Jul 28, 2021
1 parent e76f796 commit 47d1d34
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 2 deletions.
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>
)
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;
}
}
71 changes: 71 additions & 0 deletions docs/src/demos/Examples/CodeBlockLanguage/React/index.jsx
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 docs/src/demos/Examples/CodeBlockLanguage/React/styles.scss
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;
}
}
}
5 changes: 4 additions & 1 deletion docs/src/docPages/examples/syntax-highlighting.md
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',
}" />
8 changes: 7 additions & 1 deletion packages/core/src/commands/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ export const focus: RawCommands['focus'] = (position = null) => ({
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
Expand Down

0 comments on commit 47d1d34

Please sign in to comment.