add character limit experiment
This commit is contained in:
@@ -7,8 +7,8 @@ export const AnnotationPlugin = (options: any) => new Plugin({
|
|||||||
key: AnnotationPluginKey,
|
key: AnnotationPluginKey,
|
||||||
state: {
|
state: {
|
||||||
init: AnnotationState.init,
|
init: AnnotationState.init,
|
||||||
apply(transaction, prevState) {
|
apply(transaction, oldState) {
|
||||||
return prevState.apply(transaction)
|
return oldState.apply(transaction)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
props: {
|
props: {
|
||||||
|
|||||||
@@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import { CharacterLimit } from './CharacterLimit'
|
||||||
|
|
||||||
|
export * from './CharacterLimit'
|
||||||
|
export default CharacterLimit
|
||||||
73
docs/src/demos/Experiments/CharacterLimit/index.vue
Normal file
73
docs/src/demos/Experiments/CharacterLimit/index.vue
Normal 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. That’s it. It’s 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. You’ll 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>
|
||||||
@@ -51,10 +51,10 @@ export const Linter = Extension.create({
|
|||||||
init(_, { doc }) {
|
init(_, { doc }) {
|
||||||
return runAllLinterPlugins(doc, plugins)
|
return runAllLinterPlugins(doc, plugins)
|
||||||
},
|
},
|
||||||
apply(transaction, prevState) {
|
apply(transaction, oldState) {
|
||||||
return transaction.docChanged
|
return transaction.docChanged
|
||||||
? runAllLinterPlugins(transaction.doc, plugins)
|
? runAllLinterPlugins(transaction.doc, plugins)
|
||||||
: prevState
|
: oldState
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
props: {
|
props: {
|
||||||
|
|||||||
@@ -4,3 +4,4 @@ Congratulations! You’ve found our secret playground with a list of experiments
|
|||||||
* [Linter](/experiments/linter)
|
* [Linter](/experiments/linter)
|
||||||
* [Annotation](/experiments/annotation)
|
* [Annotation](/experiments/annotation)
|
||||||
* [Comments](/experiments/comments)
|
* [Comments](/experiments/comments)
|
||||||
|
* [CharacterLimit](/experiments/character-limit)
|
||||||
|
|||||||
5
docs/src/docPages/experiments/character-limit.md
Normal file
5
docs/src/docPages/experiments/character-limit.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# CharacterLimit
|
||||||
|
|
||||||
|
⚠️ Experiment
|
||||||
|
|
||||||
|
<demo name="Experiments/CharacterLimit" />
|
||||||
Reference in New Issue
Block a user