Add Code demo for React
This commit is contained in:
15
demos/src/Marks/Code/React/index.html
Normal file
15
demos/src/Marks/Code/React/index.html
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app"></div>
|
||||||
|
<script type="module">
|
||||||
|
import setup from "../../../../setup/react.ts";
|
||||||
|
import source from "@source";
|
||||||
|
setup("Marks/Code", source);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
46
demos/src/Marks/Code/React/index.jsx
Normal file
46
demos/src/Marks/Code/React/index.jsx
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import { useEditor, EditorContent } from '@tiptap/react'
|
||||||
|
import Document from '@tiptap/extension-document'
|
||||||
|
import Paragraph from '@tiptap/extension-paragraph'
|
||||||
|
import Text from '@tiptap/extension-text'
|
||||||
|
import Code from '@tiptap/extension-code'
|
||||||
|
import './styles.scss'
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
const editor = useEditor({
|
||||||
|
extensions: [Document, Paragraph, Text, Code],
|
||||||
|
content: `
|
||||||
|
<p>This isn’t code.</p>
|
||||||
|
<p><code>This is code.</code></p>
|
||||||
|
`,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!editor) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<button
|
||||||
|
onClick={() => editor.chain().focus().toggleCode().run()}
|
||||||
|
className={editor.isActive('code') ? 'is-active' : ''}
|
||||||
|
>
|
||||||
|
toggleCode
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => editor.chain().focus().setCode().run()}
|
||||||
|
disabled={editor.isActive('code')}
|
||||||
|
>
|
||||||
|
setCode
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => editor.chain().focus().unsetCode().run()}
|
||||||
|
disabled={!editor.isActive('code')}
|
||||||
|
>
|
||||||
|
unsetCode
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<EditorContent editor={editor} />
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
60
demos/src/Marks/Code/React/index.spec.js
Normal file
60
demos/src/Marks/Code/React/index.spec.js
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
context('/src/Marks/Code/React/', () => {
|
||||||
|
before(() => {
|
||||||
|
cy.visit('/src/Marks/Code/React/')
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||||
|
editor.commands.setContent('<p>Example Text</p>')
|
||||||
|
cy.get('.ProseMirror').type('{selectall}')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should parse code tags correctly', () => {
|
||||||
|
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||||
|
editor.commands.setContent('<p><code>Example Text</code></p>')
|
||||||
|
expect(editor.getHTML()).to.eq('<p><code>Example Text</code></p>')
|
||||||
|
|
||||||
|
editor.commands.setContent('<code>Example Text</code>')
|
||||||
|
expect(editor.getHTML()).to.eq('<p><code>Example Text</code></p>')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should mark the selected text as inline code', () => {
|
||||||
|
cy.get('button:first').click()
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').find('code').should('contain', 'Example Text')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should toggle the selected text as inline code', () => {
|
||||||
|
cy.get('button:first').click()
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').type('{selectall}')
|
||||||
|
|
||||||
|
cy.get('button:first').click()
|
||||||
|
|
||||||
|
cy.get('.ProseMirror code').should('not.exist')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should make the selected text bold when the keyboard shortcut is pressed', () => {
|
||||||
|
cy.get('.ProseMirror')
|
||||||
|
.trigger('keydown', { modKey: true, key: 'e' })
|
||||||
|
.find('code')
|
||||||
|
.should('contain', 'Example Text')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should toggle the selected text bold when the keyboard shortcut is pressed', () => {
|
||||||
|
cy.get('.ProseMirror')
|
||||||
|
.trigger('keydown', { modKey: true, key: 'e' })
|
||||||
|
.find('code')
|
||||||
|
.should('contain', 'Example Text')
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').trigger('keydown', { modKey: true, key: 'e' })
|
||||||
|
|
||||||
|
cy.get('.ProseMirror code').should('not.exist')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should make inline code from the markdown shortcut', () => {
|
||||||
|
cy.get('.ProseMirror').type('`Example`').find('code').should('contain', 'Example')
|
||||||
|
})
|
||||||
|
})
|
||||||
15
demos/src/Marks/Code/React/styles.scss
Normal file
15
demos/src/Marks/Code/React/styles.scss
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
/* Basic editor styles */
|
||||||
|
.ProseMirror {
|
||||||
|
> * + * {
|
||||||
|
margin-top: 0.75em;
|
||||||
|
}
|
||||||
|
|
||||||
|
code {
|
||||||
|
background-color: rgba(#616161, 0.1);
|
||||||
|
border-radius: 0.25em;
|
||||||
|
box-decoration-break: clone;
|
||||||
|
color: #616161;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
padding: 0.25em;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user