add the character count extension
This commit is contained in:
41
packages/extension-character-count/src/character-count.ts
Normal file
41
packages/extension-character-count/src/character-count.ts
Normal 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,
|
||||
}
|
||||
}
|
||||
5
packages/extension-character-count/src/index.ts
Normal file
5
packages/extension-character-count/src/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { CharacterCount } from './character-count'
|
||||
|
||||
export * from './character-count'
|
||||
|
||||
export default CharacterCount
|
||||
Reference in New Issue
Block a user