From ba427fafaae91211a6a5cc0cecbb9990a6c11f4b Mon Sep 17 00:00:00 2001 From: Hans Pagel Date: Mon, 25 Jan 2021 11:44:22 +0100 Subject: [PATCH] improve the character limit experiment --- .../extension/CharacterLimit.ts | 32 ++++--------------- .../Experiments/CharacterLimit/index.vue | 14 ++++---- docs/src/docPages/experiments.md | 5 ++- 3 files changed, 18 insertions(+), 33 deletions(-) diff --git a/docs/src/demos/Experiments/CharacterLimit/extension/CharacterLimit.ts b/docs/src/demos/Experiments/CharacterLimit/extension/CharacterLimit.ts index 6892f647..7a8a2ea6 100644 --- a/docs/src/demos/Experiments/CharacterLimit/extension/CharacterLimit.ts +++ b/docs/src/demos/Experiments/CharacterLimit/extension/CharacterLimit.ts @@ -4,6 +4,8 @@ import { Plugin, PluginKey, } from 'prosemirror-state' +export const pluginKey = new PluginKey('characterLimit') + export interface CharacterLimitOptions { limit: number, } @@ -21,35 +23,13 @@ export const CharacterLimit = Extension.create({ 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() { - // // - // }, - // }, + key: pluginKey, appendTransaction: (transactions, oldState, newState) => { - const oldLength = oldState.doc.content.size - const newLength = newState.doc.content.size + const length = newState.doc.content.size - if (newLength > options.limit && newLength > oldLength) { - const newTr = newState.tr - newTr.insertText('', options.limit + 1, newLength) - - return newTr + if (length > options.limit) { + return newState.tr.insertText('', options.limit + 1, length) } }, }), diff --git a/docs/src/demos/Experiments/CharacterLimit/index.vue b/docs/src/demos/Experiments/CharacterLimit/index.vue index 16a4822c..fa5d9cdb 100644 --- a/docs/src/demos/Experiments/CharacterLimit/index.vue +++ b/docs/src/demos/Experiments/CharacterLimit/index.vue @@ -1,7 +1,7 @@