diff --git a/demos/src/Nodes/Heading/React/index.html b/demos/src/Nodes/Heading/React/index.html new file mode 100644 index 00000000..b8d54fbb --- /dev/null +++ b/demos/src/Nodes/Heading/React/index.html @@ -0,0 +1,15 @@ + + + + + + + +
+ + + diff --git a/demos/src/Nodes/Heading/React/index.jsx b/demos/src/Nodes/Heading/React/index.jsx new file mode 100644 index 00000000..81baf9e4 --- /dev/null +++ b/demos/src/Nodes/Heading/React/index.jsx @@ -0,0 +1,54 @@ +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 Heading from '@tiptap/extension-heading' + +export default () => { + const editor = useEditor({ + extensions: [ + Document, + Paragraph, + Text, + Heading.configure({ + levels: [1, 2, 3], + }), + ], + content: ` +

This is a 1st level heading

+

This is a 2nd level heading

+

This is a 3rd level heading

+

This 4th level heading will be converted to a paragraph, because levels are configured to be only 1, 2 or 3.

+ `, + }) + + if (!editor) { + return null + } + + return ( + <> + + + + + + + ) +} diff --git a/demos/src/Nodes/Heading/React/index.spec.js b/demos/src/Nodes/Heading/React/index.spec.js new file mode 100644 index 00000000..34112084 --- /dev/null +++ b/demos/src/Nodes/Heading/React/index.spec.js @@ -0,0 +1,111 @@ +context('/src/Nodes/Heading/React/', () => { + before(() => { + cy.visit('/src/Nodes/Heading/React/') + }) + + beforeEach(() => { + cy.get('.ProseMirror').then(([{ editor }]) => { + editor.commands.setContent('

Example Text

') + cy.get('.ProseMirror').type('{selectall}') + }) + }) + + const headings = ['

Example Text

', '

Example Text

', '

Example Text

'] + + headings.forEach(html => { + it(`should parse headings correctly (${html})`, () => { + cy.get('.ProseMirror').then(([{ editor }]) => { + editor.commands.setContent(html) + expect(editor.getHTML()).to.eq(html) + }) + }) + }) + + it('should omit disabled heading levels', () => { + cy.get('.ProseMirror').then(([{ editor }]) => { + editor.commands.setContent('

Example Text

') + expect(editor.getHTML()).to.eq('

Example Text

') + }) + }) + + it('the button should make the selected line a h1', () => { + cy.get('.ProseMirror h1').should('not.exist') + + cy.get('button:nth-child(1)').click() + + cy.get('.ProseMirror').find('h1').should('contain', 'Example Text') + }) + + it('the button should make the selected line a h2', () => { + cy.get('.ProseMirror h2').should('not.exist') + + cy.get('button:nth-child(2)').click() + + cy.get('.ProseMirror').find('h2').should('contain', 'Example Text') + }) + + it('the button should make the selected line a h3', () => { + cy.get('.ProseMirror h3').should('not.exist') + + cy.get('button:nth-child(3)').click() + + cy.get('.ProseMirror').find('h3').should('contain', 'Example Text') + }) + + it('the button should toggle the heading', () => { + cy.get('.ProseMirror h1').should('not.exist') + + cy.get('button:nth-child(1)').click() + + cy.get('.ProseMirror').find('h1').should('contain', 'Example Text') + + cy.get('button:nth-child(1)').click() + + cy.get('.ProseMirror h1').should('not.exist') + }) + + it('should make the paragraph a h1 keyboard shortcut is pressed', () => { + cy.get('.ProseMirror') + .trigger('keydown', { modKey: true, altKey: true, key: '1' }) + .find('h1') + .should('contain', 'Example Text') + }) + + it('should make the paragraph a h2 keyboard shortcut is pressed', () => { + cy.get('.ProseMirror') + .trigger('keydown', { modKey: true, altKey: true, key: '2' }) + .find('h2') + .should('contain', 'Example Text') + }) + + it('should make the paragraph a h3 keyboard shortcut is pressed', () => { + cy.get('.ProseMirror') + .trigger('keydown', { modKey: true, altKey: true, key: '3' }) + .find('h3') + .should('contain', 'Example Text') + }) + + it('should make a h1 from the default markdown shortcut', () => { + cy.get('.ProseMirror').then(([{ editor }]) => { + editor.commands.clearContent() + }) + + cy.get('.ProseMirror').type('# Headline').find('h1').should('contain', 'Headline') + }) + + it('should make a h2 from the default markdown shortcut', () => { + cy.get('.ProseMirror').then(([{ editor }]) => { + editor.commands.clearContent() + }) + + cy.get('.ProseMirror').type('## Headline').find('h2').should('contain', 'Headline') + }) + + it('should make a h3 from the default markdown shortcut', () => { + cy.get('.ProseMirror').then(([{ editor }]) => { + editor.commands.clearContent() + }) + + cy.get('.ProseMirror').type('### Headline').find('h3').should('contain', 'Headline') + }) +})