mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-28 15:49:23 +08:00
improve text style extension
This commit is contained in:
parent
944d5e7039
commit
1af7829924
@ -1,11 +1,14 @@
|
|||||||
<template>
|
<template>
|
||||||
<div v-if="editor">
|
<div v-if="editor">
|
||||||
<button @click="editor.chain().focus().fontFamily('Inter').run()">
|
<button
|
||||||
Inter
|
@click="editor.chain().focus().fontFamily('Comic Sans MS').run()"
|
||||||
</button>
|
:class="{ 'is-active': editor.isActive('textStyle', { fontFamily: 'Comic Sans MS' }) }"
|
||||||
<button @click="editor.chain().focus().fontFamily('Comic Sans MS').run()">
|
>
|
||||||
Comic Sans
|
Comic Sans
|
||||||
</button>
|
</button>
|
||||||
|
<button @click="editor.chain().focus().fontFamily().run()">
|
||||||
|
Remove font-family
|
||||||
|
</button>
|
||||||
|
|
||||||
<editor-content :editor="editor" />
|
<editor-content :editor="editor" />
|
||||||
</div>
|
</div>
|
||||||
@ -17,7 +20,6 @@ import { EditorContent } from '@tiptap/vue'
|
|||||||
import Document from '@tiptap/extension-document'
|
import Document from '@tiptap/extension-document'
|
||||||
import Paragraph from '@tiptap/extension-paragraph'
|
import Paragraph from '@tiptap/extension-paragraph'
|
||||||
import Text from '@tiptap/extension-text'
|
import Text from '@tiptap/extension-text'
|
||||||
import Heading from '@tiptap/extension-heading'
|
|
||||||
import TextStyle from '@tiptap/extension-text-style'
|
import TextStyle from '@tiptap/extension-text-style'
|
||||||
import FontFamily from '@tiptap/extension-font-family'
|
import FontFamily from '@tiptap/extension-font-family'
|
||||||
|
|
||||||
@ -38,13 +40,11 @@ export default {
|
|||||||
Document(),
|
Document(),
|
||||||
Paragraph(),
|
Paragraph(),
|
||||||
Text(),
|
Text(),
|
||||||
Heading(),
|
|
||||||
TextStyle(),
|
TextStyle(),
|
||||||
FontFamily(),
|
FontFamily(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<h2>Hello</h2>
|
<p><span style="font-family: Comic Sans MS">Welcome to the internet.</span></p>
|
||||||
<p>This is text.</p>
|
|
||||||
`,
|
`,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
@ -16,11 +16,17 @@ const FontFamily = createExtension({
|
|||||||
attributes: {
|
attributes: {
|
||||||
fontFamily: {
|
fontFamily: {
|
||||||
default: null,
|
default: null,
|
||||||
renderHTML: attributes => ({
|
renderHTML: attributes => {
|
||||||
style: `font-family: ${attributes.fontFamily}`,
|
if (!attributes.fontFamily) {
|
||||||
}),
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
style: `font-family: ${attributes.fontFamily}`,
|
||||||
|
}
|
||||||
|
},
|
||||||
parseHTML: element => ({
|
parseHTML: element => ({
|
||||||
fontFamily: element.style.fontFamily,
|
fontFamily: element.style.fontFamily.replace(/['"]+/g, ''),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -30,8 +36,11 @@ const FontFamily = createExtension({
|
|||||||
|
|
||||||
addCommands() {
|
addCommands() {
|
||||||
return {
|
return {
|
||||||
fontFamily: (fontFamily: string): Command => ({ commands }) => {
|
fontFamily: (fontFamily: string | null = null): Command => ({ chain }) => {
|
||||||
return commands.updateMarkAttributes('textStyle', { fontFamily })
|
return chain()
|
||||||
|
.updateMarkAttributes('textStyle', { fontFamily })
|
||||||
|
.removeEmptyTextStyle()
|
||||||
|
.run()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { createMark } from '@tiptap/core'
|
import { Command, createMark, getMarkAttrs } from '@tiptap/core'
|
||||||
|
|
||||||
const TextStyle = createMark({
|
const TextStyle = createMark({
|
||||||
name: 'textStyle',
|
name: 'textStyle',
|
||||||
@ -7,6 +7,15 @@ const TextStyle = createMark({
|
|||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
tag: 'span',
|
tag: 'span',
|
||||||
|
getAttrs: element => {
|
||||||
|
const hasStyles = (element as HTMLElement).hasAttribute('style')
|
||||||
|
|
||||||
|
if (!hasStyles) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return {}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -14,6 +23,22 @@ const TextStyle = createMark({
|
|||||||
renderHTML({ attributes }) {
|
renderHTML({ attributes }) {
|
||||||
return ['span', attributes, 0]
|
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
|
export default TextStyle
|
||||||
|
Loading…
Reference in New Issue
Block a user