From 505c0016d9b498ba51d5ba43aef28ecc1ba4583a Mon Sep 17 00:00:00 2001 From: Sven Adlung Date: Tue, 16 Nov 2021 13:20:26 +0100 Subject: [PATCH] Add History demo for React --- demos/src/Extensions/History/React/index.jsx | 37 ++++++++ .../Extensions/History/React/index.spec.js | 90 +++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 demos/src/Extensions/History/React/index.jsx create mode 100644 demos/src/Extensions/History/React/index.spec.js diff --git a/demos/src/Extensions/History/React/index.jsx b/demos/src/Extensions/History/React/index.jsx new file mode 100644 index 00000000..23ab1796 --- /dev/null +++ b/demos/src/Extensions/History/React/index.jsx @@ -0,0 +1,37 @@ +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 History from '@tiptap/extension-history' + +export default () => { + const editor = useEditor({ + extensions: [Document, Paragraph, Text, History], + content: ` +

+ With the History extension the Editor will keep track of your changes. And if you think you made a mistake, you can redo your changes. Try it out, change the content and hit the undo button! +

+

+ And yes, you can also use a keyboard shortcut to undo changes (Control/Cmd Z) or redo changes (Control/Cmd Shift Z). +

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

Mistake

') + }) + }) + + it('should make the last change undone', () => { + cy.get('.ProseMirror').should('contain', 'Mistake') + + cy.get('button:first').should('not.have.attr', 'disabled') + + cy.get('button:first').click() + + cy.get('.ProseMirror').should('not.contain', 'Mistake') + }) + + it('should make the last change undone with the keyboard shortcut', () => { + cy.get('.ProseMirror').trigger('keydown', { modKey: true, key: 'z' }) + + cy.get('.ProseMirror').should('not.contain', 'Mistake') + }) + + it('should make the last change undone with the keyboard shortcut (russian)', () => { + cy.get('.ProseMirror').should('contain', 'Mistake') + + cy.get('.ProseMirror').trigger('keydown', { modKey: true, key: 'я' }) + + cy.get('.ProseMirror').should('not.contain', 'Mistake') + }) + + it('should apply the last undone change again with the keyboard shortcut', () => { + cy.get('.ProseMirror').trigger('keydown', { modKey: true, key: 'z' }) + + cy.get('.ProseMirror').should('not.contain', 'Mistake') + + cy.get('.ProseMirror').trigger('keydown', { modKey: true, shiftKey: true, key: 'z' }) + + cy.get('.ProseMirror').should('contain', 'Mistake') + }) + + it('should apply the last undone change again with the keyboard shortcut (russian)', () => { + cy.get('.ProseMirror').trigger('keydown', { modKey: true, key: 'я' }) + + cy.get('.ProseMirror').should('not.contain', 'Mistake') + + cy.get('.ProseMirror').trigger('keydown', { modKey: true, shiftKey: true, key: 'я' }) + + cy.get('.ProseMirror').should('contain', 'Mistake') + }) + + it('should apply the last undone change again', () => { + cy.get('.ProseMirror').should('contain', 'Mistake') + + cy.get('button:first').should('not.have.attr', 'disabled') + + cy.get('button:first').click() + + cy.get('.ProseMirror').should('not.contain', 'Mistake') + + cy.get('button:first').should('have.attr', 'disabled') + + cy.get('button:nth-child(2)').click() + + cy.get('.ProseMirror').should('contain', 'Mistake') + }) + + it('should disable undo button when there are no more changes to undo', () => { + cy.get('.ProseMirror').should('contain', 'Mistake') + + cy.get('button:first').should('not.have.attr', 'disabled') + + cy.get('button:first').click() + + cy.get('button:first').should('have.attr', 'disabled') + }) + + it('should disable redo button when there are no more changes to redo', () => { + cy.get('.ProseMirror').should('contain', 'Mistake') + + cy.get('button:nth-child(2)').should('have.attr', 'disabled') + + cy.get('button:first').should('not.have.attr', 'disabled') + + cy.get('button:first').click() + + cy.get('button:nth-child(2)').should('not.have.attr', 'disabled') + }) +})