From 304d187373fcb13d32e0d07a8be31582c692e97a Mon Sep 17 00:00:00 2001 From: HariHaran Subramanian Date: Thu, 21 Nov 2024 17:34:02 +0530 Subject: [PATCH] fix: [Bug]: While setting `content` directly while using CharacterCount with limit is not obeyed #5851 --- .../src/character-count.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/packages/extension-character-count/src/character-count.ts b/packages/extension-character-count/src/character-count.ts index 16dcec0da..0b1be3bc9 100644 --- a/packages/extension-character-count/src/character-count.ts +++ b/packages/extension-character-count/src/character-count.ts @@ -28,6 +28,11 @@ export interface CharacterCountOptions { * @example (text) => text.split(/\s+/).filter(word => word !== '').length */ wordCounter: (text: string) => number + /** + * Whether to trim initial content that exceeds the limit + * @default true + */ + trimInitialContent: boolean } export interface CharacterCountStorage { @@ -60,6 +65,7 @@ export const CharacterCount = Extension.create text.length, wordCounter: text => text.split(' ').filter(word => word !== '').length, + trimInitialContent: true, } }, @@ -92,6 +98,32 @@ export const CharacterCount = Extension.create limit) { + const text = doc.textBetween(0, doc.content.size, undefined, ' ') + const trimmedContent = text.slice(0, limit) + + // Replace the entire document content + editor.commands.setContent(trimmedContent) + + // Add a console warning + console.warn(`[CharacterCount] Initial content exceeded limit of ${limit} characters. Content was automatically trimmed.`) + } + }, + addProseMirrorPlugins() { return [ new Plugin({