mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-24 17:47:50 +08:00
36 lines
767 B
TypeScript
36 lines
767 B
TypeScript
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)
|
|
}
|
|
},
|
|
}),
|
|
]
|
|
},
|
|
})
|