Merge branch 'main' of github.com:ueberdosis/tiptap-next into main
This commit is contained in:
5
docs/src/demos/Marks/TextStyle/index.spec.js
Normal file
5
docs/src/demos/Marks/TextStyle/index.spec.js
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
context('/api/marks/text-style', () => {
|
||||||
|
before(() => {
|
||||||
|
cy.visit('/api/marks/text-style')
|
||||||
|
})
|
||||||
|
})
|
||||||
81
docs/src/demos/Marks/TextStyle/index.vue
Normal file
81
docs/src/demos/Marks/TextStyle/index.vue
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
<template>
|
||||||
|
<div v-if="editor">
|
||||||
|
<button
|
||||||
|
@click="editor.chain().focus().fontFamily('Inter').run()"
|
||||||
|
:class="{ 'is-active': editor.isActive('textStyle', { fontFamily: 'Inter' }) }"
|
||||||
|
>
|
||||||
|
Inter
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
@click="editor.chain().focus().fontFamily('Comic Sans MS, Comic Sans').run()"
|
||||||
|
:class="{ 'is-active': editor.isActive('textStyle', { fontFamily: 'Comic Sans MS, Comic Sans' }) }"
|
||||||
|
>
|
||||||
|
Comic Sans
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
@click="editor.chain().focus().fontFamily('serif').run()"
|
||||||
|
:class="{ 'is-active': editor.isActive('textStyle', { fontFamily: 'serif' }) }"
|
||||||
|
>
|
||||||
|
serif
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
@click="editor.chain().focus().fontFamily('monospace').run()"
|
||||||
|
:class="{ 'is-active': editor.isActive('textStyle', { fontFamily: 'monospace' }) }"
|
||||||
|
>
|
||||||
|
monospace
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
@click="editor.chain().focus().fontFamily('cursive').run()"
|
||||||
|
:class="{ 'is-active': editor.isActive('textStyle', { fontFamily: 'cursive' }) }"
|
||||||
|
>
|
||||||
|
cursive
|
||||||
|
</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 TextStyle from '@tiptap/extension-text-style'
|
||||||
|
import FontFamily from '@tiptap/extension-font-family'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
components: {
|
||||||
|
EditorContent,
|
||||||
|
},
|
||||||
|
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
editor: null,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
mounted() {
|
||||||
|
this.editor = new Editor({
|
||||||
|
extensions: [
|
||||||
|
Document(),
|
||||||
|
Paragraph(),
|
||||||
|
Text(),
|
||||||
|
TextStyle(),
|
||||||
|
FontFamily(),
|
||||||
|
],
|
||||||
|
content: `
|
||||||
|
<p><span style="font-family: Inter">Inter</span></p>
|
||||||
|
<p><span style="font-family: Comic Sans MS, Comic Sans">Comic Sans</span></p>
|
||||||
|
<p><span style="font-family: serif">serif</span></p>
|
||||||
|
<p><span style="font-family: monospace">monospace</span></p>
|
||||||
|
<p><span style="font-family: cursive">cursive</span></p>
|
||||||
|
`,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
beforeDestroy() {
|
||||||
|
this.editor.destroy()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
</script>
|
||||||
26
docs/src/docPages/api/extensions/font-family.md
Normal file
26
docs/src/docPages/api/extensions/font-family.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# FontFamily
|
||||||
|
This extension enables you to set the font family in the editor.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
::: warning Use with TextStyle
|
||||||
|
This extension requires the [`TextStyle`](/api/marks/text-style) mark.
|
||||||
|
:::
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# with npm
|
||||||
|
npm install @tiptap/extension-text-style @tiptap/extension-font-family
|
||||||
|
|
||||||
|
# with Yarn
|
||||||
|
yarn add @tiptap/extension-text-style @tiptap/extension-font-family
|
||||||
|
```
|
||||||
|
|
||||||
|
## Settings
|
||||||
|
| Option | Type | Default | Description |
|
||||||
|
| ------ | ----- | ------------- | --------------------------------------------------------------------- |
|
||||||
|
| types | array | ['textStyle'] | A list of marks where the font family attribute should be applied to. |
|
||||||
|
|
||||||
|
## Source code
|
||||||
|
[packages/extension-font-family/](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-font-family/)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
<demo name="Extensions/FontFamily" highlight="" />
|
||||||
17
docs/src/docPages/api/marks/text-style.md
Normal file
17
docs/src/docPages/api/marks/text-style.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Text Style
|
||||||
|
This mark renders a `<span>` HTML tag and enables you to add a list of styling related attributes, for example font-family, font-size, or font-color. The extension doesn’t add any styling attribute by default, but other extensions use it as the foundation, for example [`FontFamily`](/api/extensions/font-family).
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
```bash
|
||||||
|
# with npm
|
||||||
|
npm install @tiptap/extension-text-style
|
||||||
|
|
||||||
|
# with Yarn
|
||||||
|
yarn add @tiptap/extension-text-style
|
||||||
|
```
|
||||||
|
|
||||||
|
## Source code
|
||||||
|
[packages/extension-text-style/](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-text-style/)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
<demo name="Marks/TextStyle" />
|
||||||
@@ -5,7 +5,7 @@ Type <code>* </code>, <code>- </code> or <code>+ </code> at the b
|
|||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
::: warning Use with ListItem
|
::: warning Use with ListItem
|
||||||
This extension requires the [`ListItem`](/api/nodes/list-item) extension.
|
This extension requires the [`ListItem`](/api/nodes/list-item) node.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ The ListItem extension adds support for the `<li>` HTML tag. It’s used for bul
|
|||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
::: warning Use with BulletList and/or OrderedList
|
::: warning Use with BulletList and/or OrderedList
|
||||||
This extension requires the [`BulletList`](/api/nodes/bullet-list) or [`OrderedList`](/api/nodes/ordered-list) extension.
|
This extension requires the [`BulletList`](/api/nodes/bullet-list) or [`OrderedList`](/api/nodes/ordered-list) node.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Type <code>1. </code> (or any other number followed by a dot) at the beginn
|
|||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
::: warning Use with ListItem
|
::: warning Use with ListItem
|
||||||
This extension requires the [`ListItem`](/api/nodes/list-item) extension.
|
This extension requires the [`ListItem`](/api/nodes/list-item) node.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ This extension doesn’t require any JavaScript framework, it’s based on plain
|
|||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
::: warning Use with TaskList
|
::: warning Use with TaskList
|
||||||
This extension requires the [`TaskList`](/api/nodes/task-list) extension.
|
This extension requires the [`TaskList`](/api/nodes/task-list) node.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -112,6 +112,9 @@
|
|||||||
link: /api/marks/link
|
link: /api/marks/link
|
||||||
- title: Strike
|
- title: Strike
|
||||||
link: /api/marks/strike
|
link: /api/marks/strike
|
||||||
|
- title: Text Style
|
||||||
|
link: /api/marks/text-style
|
||||||
|
draft: true
|
||||||
- title: Underline
|
- title: Underline
|
||||||
link: /api/marks/underline
|
link: /api/marks/underline
|
||||||
- title: Extensions
|
- title: Extensions
|
||||||
@@ -127,6 +130,9 @@
|
|||||||
link: /api/extensions/dropcursor
|
link: /api/extensions/dropcursor
|
||||||
- title: Focus
|
- title: Focus
|
||||||
link: /api/extensions/focus
|
link: /api/extensions/focus
|
||||||
|
- title: FontFamily
|
||||||
|
link: /api/extensions/font-family
|
||||||
|
draft: true
|
||||||
- title: Gapcursor
|
- title: Gapcursor
|
||||||
link: /api/extensions/gapcursor
|
link: /api/extensions/gapcursor
|
||||||
- title: History
|
- title: History
|
||||||
|
|||||||
55
packages/extension-font-family/index.ts
Normal file
55
packages/extension-font-family/index.ts
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import { Command, createExtension } from '@tiptap/core'
|
||||||
|
|
||||||
|
type FontFamilyOptions = {
|
||||||
|
types: string[],
|
||||||
|
}
|
||||||
|
|
||||||
|
const FontFamily = createExtension({
|
||||||
|
defaultOptions: <FontFamilyOptions>{
|
||||||
|
types: ['textStyle'],
|
||||||
|
},
|
||||||
|
|
||||||
|
addGlobalAttributes() {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
types: this.options.types,
|
||||||
|
attributes: {
|
||||||
|
fontFamily: {
|
||||||
|
default: null,
|
||||||
|
renderHTML: attributes => {
|
||||||
|
if (!attributes.fontFamily) {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
style: `font-family: ${attributes.fontFamily}`,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
parseHTML: element => ({
|
||||||
|
fontFamily: element.style.fontFamily.replace(/['"]+/g, ''),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
addCommands() {
|
||||||
|
return {
|
||||||
|
fontFamily: (fontFamily: string | null = null): Command => ({ chain }) => {
|
||||||
|
return chain()
|
||||||
|
.updateMarkAttributes('textStyle', { fontFamily })
|
||||||
|
.removeEmptyTextStyle()
|
||||||
|
.run()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
export default FontFamily
|
||||||
|
|
||||||
|
declare module '@tiptap/core/src/Editor' {
|
||||||
|
interface AllExtensions {
|
||||||
|
FontFamily: typeof FontFamily,
|
||||||
|
}
|
||||||
|
}
|
||||||
17
packages/extension-font-family/package.json
Normal file
17
packages/extension-font-family/package.json
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"name": "@tiptap/extension-font-family",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"source": "index.ts",
|
||||||
|
"main": "dist/tiptap-extension-font-family.js",
|
||||||
|
"umd:main": "dist/tiptap-extension-font-family.umd.js",
|
||||||
|
"module": "dist/tiptap-extension-font-family.mjs",
|
||||||
|
"unpkg": "dist/tiptap-extension-font-family.js",
|
||||||
|
"jsdelivr": "dist/tiptap-extension-font-family.js",
|
||||||
|
"files": [
|
||||||
|
"src",
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"peerDependencies": {
|
||||||
|
"@tiptap/core": "2.x"
|
||||||
|
}
|
||||||
|
}
|
||||||
50
packages/extension-text-style/index.ts
Normal file
50
packages/extension-text-style/index.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { Command, createMark, getMarkAttrs } from '@tiptap/core'
|
||||||
|
|
||||||
|
const TextStyle = createMark({
|
||||||
|
name: 'textStyle',
|
||||||
|
|
||||||
|
parseHTML() {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
tag: 'span',
|
||||||
|
getAttrs: element => {
|
||||||
|
const hasStyles = (element as HTMLElement).hasAttribute('style')
|
||||||
|
|
||||||
|
if (!hasStyles) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
renderHTML({ attributes }) {
|
||||||
|
return ['span', attributes, 0]
|
||||||
|
},
|
||||||
|
|
||||||
|
addCommands() {
|
||||||
|
return {
|
||||||
|
removeEmptyTextStyle: (): Command => ({ state, commands }) => {
|
||||||
|
const attributes = getMarkAttrs(state, this.type)
|
||||||
|
const hasStyles = Object.entries(attributes).every(([, value]) => !!value)
|
||||||
|
|
||||||
|
if (hasStyles) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return commands.removeMark('textStyle')
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
export default TextStyle
|
||||||
|
|
||||||
|
declare module '@tiptap/core/src/Editor' {
|
||||||
|
interface AllExtensions {
|
||||||
|
TextStyle: typeof TextStyle,
|
||||||
|
}
|
||||||
|
}
|
||||||
17
packages/extension-text-style/package.json
Normal file
17
packages/extension-text-style/package.json
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"name": "@tiptap/extension-text-style",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"source": "index.ts",
|
||||||
|
"main": "dist/tiptap-extension-text-style.js",
|
||||||
|
"umd:main": "dist/tiptap-extension-text-style.umd.js",
|
||||||
|
"module": "dist/tiptap-extension-text-style.mjs",
|
||||||
|
"unpkg": "dist/tiptap-extension-text-style.js",
|
||||||
|
"jsdelivr": "dist/tiptap-extension-text-style.js",
|
||||||
|
"files": [
|
||||||
|
"src",
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"peerDependencies": {
|
||||||
|
"@tiptap/core": "2.x"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user