improve the character limit experiment

This commit is contained in:
Hans Pagel
2021-01-25 11:44:22 +01:00
parent 52af0b83c4
commit ba427fafaa
3 changed files with 18 additions and 33 deletions

View File

@@ -4,6 +4,8 @@ import {
Plugin, PluginKey, Plugin, PluginKey,
} from 'prosemirror-state' } from 'prosemirror-state'
export const pluginKey = new PluginKey('characterLimit')
export interface CharacterLimitOptions { export interface CharacterLimitOptions {
limit: number, limit: number,
} }
@@ -21,35 +23,13 @@ export const CharacterLimit = Extension.create({
return [ return [
new Plugin({ new Plugin({
key: new PluginKey('characterLimit'), key: pluginKey,
// 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) => { appendTransaction: (transactions, oldState, newState) => {
const oldLength = oldState.doc.content.size const length = newState.doc.content.size
const newLength = newState.doc.content.size
if (newLength > options.limit && newLength > oldLength) { if (length > options.limit) {
const newTr = newState.tr return newState.tr.insertText('', options.limit + 1, length)
newTr.insertText('', options.limit + 1, newLength)
return newTr
} }
}, },
}), }),

View File

@@ -1,7 +1,7 @@
<template> <template>
<div> <div>
<editor-content :editor="editor" /> <editor-content :editor="editor" />
<div> <div class="character-limit">
{{ characters }}/{{ limit }} {{ characters }}/{{ limit }}
</div> </div>
</div> </div>
@@ -22,7 +22,7 @@ export default {
data() { data() {
return { return {
editor: null, editor: null,
limit: 10, limit: 280,
} }
}, },
@@ -38,10 +38,7 @@ export default {
], ],
content: ` content: `
<p> <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. Lets make sure people cant write more than 280 characters. I bet you could build one of the biggest social networks on that idea.
</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> </p>
`, `,
}) })
@@ -70,4 +67,9 @@ export default {
margin-top: 0.75em; margin-top: 0.75em;
} }
} }
.character-limit {
margin-top: 1rem;
color: #adb5bd;
}
</style> </style>

View File

@@ -1,9 +1,12 @@
# Experiments # Experiments
Congratulations! Youve found our secret playground with a list of experiments. Be aware, that nothing here is ready to use. Feel free to play around, but please, dont open an issue for a bug youve found here or send pull requests. :-) Congratulations! Youve found our secret playground with a list of experiments. Be aware, that nothing here is ready to use. Feel free to play around, but please, dont open an issue for a bug youve found here or send pull requests. :-)
## New
* [Linter](/experiments/linter) * [Linter](/experiments/linter)
* [Annotation](/experiments/annotation) * [Annotation](/experiments/annotation)
* [Comments](/experiments/comments) * [Comments](/experiments/comments)
* [CharacterLimit](/experiments/character-limit)
* [Color](/experiments/color) * [Color](/experiments/color)
* [Commands](/experiments/commands) * [Commands](/experiments/commands)
## Waiting for approval
* [CharacterLimit](/experiments/character-limit)