Skip to content

Instantly share code, notes, and snippets.

@sergeysova
Created October 18, 2022 08:35
Show Gist options
  • Save sergeysova/0f23509412c1e94adc360b4a5ef6a2ef to your computer and use it in GitHub Desktop.
Save sergeysova/0f23509412c1e94adc360b4a5ef6a2ef to your computer and use it in GitHub Desktop.
import { Plugin } from 'prosemirror-state'
import { Node } from 'prosemirror-model'
import { Decoration, DecorationSet } from 'prosemirror-view'
function checkLength(doc: Node, maxLength: number) {
if (doc.nodeSize > maxLength) {
const elem = document.createElement('span')
return DecorationSet.create(doc, [
Decoration.inline(maxLength, doc.nodeSize, { class: 'bg-red-300' }),
])
}
}
const maxSymbols = ({ maxLength }: { maxLength: number }) =>
new Plugin({
state: {
init(_, { doc }) {
return checkLength(doc, maxLength)
},
apply(tr, old) {
return tr.docChanged ? checkLength(tr.doc, maxLength) : old
},
},
props: {
decorations(state) {
return (this as any).getState(state)
},
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment