Created
October 18, 2022 08:35
-
-
Save sergeysova/0f23509412c1e94adc360b4a5ef6a2ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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