2021-01-20 01:01:25 +08:00
|
|
|
// @ts-nocheck
|
|
|
|
import { Extension } from '@tiptap/core'
|
|
|
|
import {
|
2021-01-20 02:42:44 +08:00
|
|
|
Plugin, PluginKey,
|
2021-01-20 01:01:25 +08:00
|
|
|
} from 'prosemirror-state'
|
|
|
|
|
2021-01-25 18:44:22 +08:00
|
|
|
export const pluginKey = new PluginKey('characterLimit')
|
|
|
|
|
2021-01-20 01:01:25 +08:00
|
|
|
export interface CharacterLimitOptions {
|
|
|
|
limit: number,
|
|
|
|
}
|
|
|
|
|
|
|
|
export const CharacterLimit = Extension.create({
|
|
|
|
name: 'characterLimit',
|
|
|
|
|
|
|
|
defaultOptions: <CharacterLimitOptions>{
|
|
|
|
limit: 100,
|
|
|
|
},
|
|
|
|
|
|
|
|
addProseMirrorPlugins() {
|
|
|
|
const { options } = this
|
|
|
|
|
|
|
|
return [
|
|
|
|
new Plugin({
|
|
|
|
|
2021-01-25 18:44:22 +08:00
|
|
|
key: pluginKey,
|
2021-01-20 01:01:25 +08:00
|
|
|
|
|
|
|
appendTransaction: (transactions, oldState, newState) => {
|
2021-01-25 18:44:22 +08:00
|
|
|
const length = newState.doc.content.size
|
2021-01-20 01:01:25 +08:00
|
|
|
|
2021-01-25 18:44:22 +08:00
|
|
|
if (length > options.limit) {
|
|
|
|
return newState.tr.insertText('', options.limit + 1, length)
|
2021-01-20 01:01:25 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
declare module '@tiptap/core' {
|
|
|
|
interface AllExtensions {
|
|
|
|
CharacterLimit: typeof CharacterLimit,
|
|
|
|
}
|
|
|
|
}
|