add code extension demo with tests

This commit is contained in:
Hans Pagel
2020-09-02 15:26:46 +02:00
parent bdbf6e7f61
commit 819052912d
7 changed files with 82 additions and 51 deletions

View File

@@ -13,9 +13,6 @@ context('/api/extensions/bold', () => {
it('should make the selected text bold', () => { it('should make the selected text bold', () => {
cy.get('.demo__preview button:first').click({ force: true }) cy.get('.demo__preview button:first').click({ force: true })
cy.get('.ProseMirror').contains('strong', 'Example Text') cy.get('.ProseMirror').contains('strong', 'Example Text')
cy.get('.demo__preview button:first').click({ force: true })
cy.get('.ProseMirror strong').should('not.exist')
}) })
it('should toggle the selected text bold', () => { it('should toggle the selected text bold', () => {

View File

@@ -0,0 +1,23 @@
context('/api/extensions/code', () => {
beforeEach(() => {
cy.visit('/api/extensions/code')
cy.get('.ProseMirror').window().then(window => {
const { editor } = window
editor.setContent('<p>Example Text</p>')
editor.focus().selectAll()
})
})
describe('code', () => {
it('should mark the selected text as inline code', () => {
cy.get('.demo__preview button:first').click({ force: true })
cy.get('.ProseMirror').contains('code', 'Example Text')
})
it('should toggle the selected text as inline code', () => {
cy.get('.demo__preview button:first').dblclick({ force: true })
cy.get('.ProseMirror code').should('not.exist')
})
})
})

View File

@@ -0,0 +1,51 @@
<template>
<div v-if="editor">
<button @click="editor.focus().code()" :class="{ 'is-active': editor.isActive('code') }">
code
</button>
<editor-content :editor="editor" />
</div>
</template>
<script>
import { Editor } from '@tiptap/core'
import { EditorContent } from '@tiptap/vue'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import Code from '@tiptap/extension-code'
export default {
components: {
EditorContent,
},
data() {
return {
editor: null,
}
},
mounted() {
this.editor = new Editor({
extensions: [
new Document(),
new Paragraph(),
new Text(),
new Code(),
],
content: `
<p>This isnt code.</p>
<p><code>This is code.</code></p>
`,
})
window.editor = this.editor
},
beforeDestroy() {
this.editor.destroy()
}
}
</script>

View File

@@ -13,9 +13,6 @@ context('/api/extensions/italic', () => {
it('should make the selected text italic', () => { it('should make the selected text italic', () => {
cy.get('.demo__preview button:first').click({ force: true }) cy.get('.demo__preview button:first').click({ force: true })
cy.get('.ProseMirror').contains('em', 'Example Text') cy.get('.ProseMirror').contains('em', 'Example Text')
cy.get('.demo__preview button:first').click({ force: true })
cy.get('.ProseMirror em').should('not.exist')
}) })
it('should toggle the selected text italic', () => { it('should toggle the selected text italic', () => {

View File

@@ -1,55 +1,19 @@
# Code # Code
The Code extensions enables you to use the `<code>` HTML tag in the editor. The Code extensions enables you to use the `<code>` HTML tag in the editor. If you paste in text with `<code>` tags it will rendered accordingly.
## Options ## Options
*None* *None*
## Commands ## Commands
| Command | Options | Description | | Command | Options | Description |
| ------ | ---- | ---------------- | | ------- | ------- | ----------- |
| code | — | Mark text as code. | | code | — | Mark text as inline code. |
## Keybindings ## Keybindings
* `Alt` + ` * `Alt` + `
## Source Code
[packages/extension-code/](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-code/)
## Usage ## Usage
```markup <demo name="Extensions/Code" highlight="3-5,17,36" />
<template>
<div>
<editor-menu-bar :editor="editor" v-slot="{ commands, isActive }">
<button type="button" :class="{ 'is-active': isActive.code() }" @click="commands.code">
Code
</button>
</editor-menu-bar>
<editor-content :editor="editor" />
</div>
</template>
<script>
import { Editor, EditorContent, EditorMenuBar } from 'tiptap'
import { Code } from 'tiptap-extensions'
export default {
components: {
EditorMenuBar,
EditorContent,
},
data() {
return {
editor: new Editor({
extensions: [
new Code(),
],
content: `
<p>This is some <code>inline code.</code></p>
`,
}),
}
},
beforeDestroy() {
this.editor.destroy()
}
}
</script>
```

View File

@@ -10,7 +10,7 @@ The extension will generate the corresponding `<em>` HTML tags when reading cont
## Commands ## Commands
| Command | Options | Description | | Command | Options | Description |
| ------ | ---- | ---------------- | | ------- | ------- | ----------- |
| italic | — | Mark text italic. | | italic | — | Mark text italic. |
## Keybindings ## Keybindings

View File

@@ -117,7 +117,6 @@
draft: true draft: true
- title: Code - title: Code
link: /api/extensions/code link: /api/extensions/code
draft: true
- title: CodeBlock - title: CodeBlock
link: /api/extensions/code-block link: /api/extensions/code-block
draft: true draft: true