add character limit experiment

This commit is contained in:
Hans Pagel 2021-01-19 18:01:25 +01:00
parent c1e39abae6
commit a6008d8c9d
7 changed files with 151 additions and 4 deletions

View File

@ -7,8 +7,8 @@ export const AnnotationPlugin = (options: any) => new Plugin({
key: AnnotationPluginKey,
state: {
init: AnnotationState.init,
apply(transaction, prevState) {
return prevState.apply(transaction)
apply(transaction, oldState) {
return oldState.apply(transaction)
},
},
props: {

View File

@ -0,0 +1,64 @@
// @ts-nocheck
import { Extension } from '@tiptap/core'
import {
Plugin, PluginKey, EditorState, Transaction,
} from 'prosemirror-state'
export interface CharacterLimitOptions {
limit: number,
}
export const CharacterLimit = Extension.create({
name: 'characterLimit',
defaultOptions: <CharacterLimitOptions>{
limit: 100,
},
addProseMirrorPlugins() {
const { options } = this
return [
new Plugin({
key: new PluginKey('characterLimit'),
// state: {
// init(_, config) {
// // console.log(_, config)
// // const length = config.doc.content.size
// // if (length > options.limit) {
// // console.log('too long', options.limit, config)
// // const transaction = config.tr.insertText('', options.limit + 1, length)
// // return config.apply(transaction)
// // }
// },
// apply() {
// //
// },
// },
appendTransaction: (transactions, oldState, newState) => {
const oldLength = oldState.doc.content.size
const newLength = newState.doc.content.size
if (newLength > options.limit && newLength > oldLength) {
const newTr = newState.tr
newTr.insertText('', options.limit + 1, newLength)
return newTr
}
},
}),
]
},
})
declare module '@tiptap/core' {
interface AllExtensions {
CharacterLimit: typeof CharacterLimit,
}
}

View File

@ -0,0 +1,4 @@
import { CharacterLimit } from './CharacterLimit'
export * from './CharacterLimit'
export default CharacterLimit

View File

@ -0,0 +1,73 @@
<template>
<div>
<editor-content :editor="editor" />
<div>
{{ characters }}/{{ limit }}
</div>
</div>
</template>
<script>
import { Editor, EditorContent } from '@tiptap/vue-starter-kit'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import CharacterLimit from './extension'
export default {
components: {
EditorContent,
},
data() {
return {
editor: null,
limit: 10,
}
},
mounted() {
this.editor = new Editor({
extensions: [
Document,
Paragraph,
Text,
CharacterLimit.configure({
limit: this.limit,
}),
],
content: `
<p>
This is a radically reduced version of tiptap. It has only support for a document, paragraphs and text. Thats it. Its probably too much for real minimalists though.
</p>
<p>
The paragraph extension is not really required, but you need at least one node. Sure, that node can be something different. Youll mostly likely want to add a paragraph though.
</p>
`,
})
},
computed: {
characters() {
if (this.editor) {
return this.editor.state.doc.content.size - 2
}
return null
},
},
beforeDestroy() {
this.editor.destroy()
},
}
</script>
<style lang="scss">
/* Basic editor styles */
.ProseMirror {
> * + * {
margin-top: 0.75em;
}
}
</style>

View File

@ -51,10 +51,10 @@ export const Linter = Extension.create({
init(_, { doc }) {
return runAllLinterPlugins(doc, plugins)
},
apply(transaction, prevState) {
apply(transaction, oldState) {
return transaction.docChanged
? runAllLinterPlugins(transaction.doc, plugins)
: prevState
: oldState
},
},
props: {

View File

@ -4,3 +4,4 @@ Congratulations! Youve found our secret playground with a list of experiments
* [Linter](/experiments/linter)
* [Annotation](/experiments/annotation)
* [Comments](/experiments/comments)
* [CharacterLimit](/experiments/character-limit)

View File

@ -0,0 +1,5 @@
# CharacterLimit
⚠️ Experiment
<demo name="Experiments/CharacterLimit" />