add the character count extension

This commit is contained in:
Hans Pagel
2021-01-25 23:40:15 +01:00
parent 2f192700b2
commit 26fb84fe27
10 changed files with 95 additions and 27 deletions

View File

@@ -0,0 +1,41 @@
import { Extension } from '@tiptap/core'
import { Plugin, PluginKey } from 'prosemirror-state'
export const pluginKey = new PluginKey('characterLimit')
export interface CharacterCountOptions {
limit?: number,
}
export const CharacterCount = Extension.create({
name: 'characterCount',
defaultOptions: <CharacterCountOptions>{
limit: 0,
},
addProseMirrorPlugins() {
const { options } = this
return [
new Plugin({
key: pluginKey,
appendTransaction: (transactions, oldState, newState) => {
const length = newState.doc.content.size
if (options.limit && length > options.limit) {
return newState.tr.insertText('', options.limit + 1, length)
}
},
}),
]
},
})
declare module '@tiptap/core' {
interface AllExtensions {
CharacterCount: typeof CharacterCount,
}
}

View File

@@ -0,0 +1,5 @@
import { CharacterCount } from './character-count'
export * from './character-count'
export default CharacterCount