add basic text align extension

This commit is contained in:
Philipp Kühn
2020-10-23 15:23:40 +02:00
parent ecadf7ea0a
commit 5976d6083c
7 changed files with 134 additions and 30 deletions

View File

@@ -0,0 +1,18 @@
context('/api/extensions/text', () => {
before(() => {
cy.visit('/api/extensions/text')
})
beforeEach(() => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.clearContent()
})
})
it('text should be wrapped in a paragraph by default', () => {
cy.get('.ProseMirror')
.type('Example Text')
.find('p')
.should('contain', 'Example Text')
})
})

View File

@@ -0,0 +1,54 @@
<template>
<div v-if="editor">
<button @click="editor.chain().focus().setAttributes({ align: 'left' }).run()">
left
</button>
<button @click="editor.chain().focus().setAttributes({ align: 'center' }).run()">
center
</button>
<button @click="editor.chain().focus().setAttributes({ align: 'right' }).run()">
right
</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 TextAlign from '@tiptap/extension-text-align'
export default {
components: {
EditorContent,
},
data() {
return {
editor: null,
}
},
mounted() {
this.editor = new Editor({
extensions: [
Document(),
Paragraph(),
Text(),
TextAlign(),
],
content: `
<p>first paragraph</p>
<p>second paragraph</p>
`,
})
},
beforeDestroy() {
this.editor.destroy()
},
}
</script>

View File

@@ -0,0 +1,16 @@
# Text Align
## Installation
```bash
# With npm
npm install @tiptap/extension-text-align
# Or: With Yarn
yarn add @tiptap/extension-text-align
```
## Source code
[packages/extension-text-align/](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-text-align/)
## Usage
<demo name="Extensions/TextAlign" highlight="12,30" />

View File

@@ -161,6 +161,8 @@
# draft: true # draft: true
- title: Text - title: Text
link: /api/extensions/text link: /api/extensions/text
- title: Text Align
link: /api/extensions/text-align
# - title: TodoItem # - title: TodoItem
# link: /api/extensions/todo-item # link: /api/extensions/todo-item
# draft: true # draft: true

View File

@@ -8,36 +8,6 @@ const Paragraph = createNode({
content: 'inline*', content: 'inline*',
// addGlobalAttributes() {
// return [
// {
// types: ['paragraph'],
// attributes: {
// align: {
// default: 'right',
// renderHTML: attributes => ({
// class: 'foo',
// style: `text-align: ${attributes.align}`,
// }),
// },
// },
// },
// ]
// },
// addAttributes() {
// return {
// id: {
// default: '123',
// rendered: true,
// renderHTML: attributes => ({
// class: 'bar',
// style: 'color: red',
// }),
// },
// }
// },
parseHTML() { parseHTML() {
return [ return [
{ tag: 'p' }, { tag: 'p' },

View File

@@ -0,0 +1,27 @@
import { createExtension } from '@tiptap/core'
const TextAlign = createExtension({
addGlobalAttributes() {
return [
{
types: ['paragraph'],
attributes: {
align: {
default: 'left',
renderHTML: attributes => ({
style: `text-align: ${attributes.align}`,
}),
},
},
},
]
},
})
export default TextAlign
declare module '@tiptap/core/src/Editor' {
interface AllExtensions {
TextAlign: typeof TextAlign,
}
}

View File

@@ -0,0 +1,17 @@
{
"name": "@tiptap/extension-text-align",
"version": "1.0.0",
"source": "index.ts",
"main": "dist/tiptap-extension-text-align.js",
"umd:main": "dist/tiptap-extension-text-align.umd.js",
"module": "dist/tiptap-extension-text-align.mjs",
"unpkg": "dist/tiptap-extension-text-align.js",
"jsdelivr": "dist/tiptap-extension-text-align.js",
"files": [
"src",
"dist"
],
"peerDependencies": {
"@tiptap/core": "2.x"
}
}