@tiptap/extension-character-count
Advanced tools
Comparing version 3.0.0-next.1 to 3.0.0-next.2
@@ -18,2 +18,14 @@ import { Extension } from '@tiptap/core'; | ||
mode: 'textSize' | 'nodeSize'; | ||
/** | ||
* The text counter function to use. Defaults to a simple character count. | ||
* @default (text) => text.length | ||
* @example (text) => [...new Intl.Segmenter().segment(text)].length | ||
*/ | ||
textCounter: (text: string) => number; | ||
/** | ||
* The word counter function to use. Defaults to a simple word count. | ||
* @default (text) => text.split(' ').filter(word => word !== '').length | ||
* @example (text) => text.split(/\s+/).filter(word => word !== '').length | ||
*/ | ||
wordCounter: (text: string) => number; | ||
} | ||
@@ -20,0 +32,0 @@ interface CharacterCountStorage { |
@@ -9,3 +9,5 @@ // src/character-count.ts | ||
limit: null, | ||
mode: "textSize" | ||
mode: "textSize", | ||
textCounter: (text) => text.length, | ||
wordCounter: (text) => text.split(" ").filter((word) => word !== "").length | ||
}; | ||
@@ -25,3 +27,3 @@ }, | ||
const text = node.textBetween(0, node.content.size, void 0, " "); | ||
return text.length; | ||
return this.options.textCounter(text); | ||
} | ||
@@ -33,10 +35,31 @@ return node.nodeSize; | ||
const text = node.textBetween(0, node.content.size, " ", " "); | ||
const words = text.split(" ").filter((word) => word !== ""); | ||
return words.length; | ||
return this.options.wordCounter(text); | ||
}; | ||
}, | ||
addProseMirrorPlugins() { | ||
let initialEvaluationDone = false; | ||
return [ | ||
new Plugin({ | ||
key: new PluginKey("characterCount"), | ||
appendTransaction: (transactions, oldState, newState) => { | ||
if (initialEvaluationDone) { | ||
return; | ||
} | ||
const limit = this.options.limit; | ||
if (limit === null || limit === void 0 || limit === 0) { | ||
initialEvaluationDone = true; | ||
return; | ||
} | ||
const initialContentSize = this.storage.characters({ node: newState.doc }); | ||
if (initialContentSize > limit) { | ||
const over = initialContentSize - limit; | ||
const from = 0; | ||
const to = over; | ||
console.warn(`[CharacterCount] Initial content exceeded limit of ${limit} characters. Content was automatically trimmed.`); | ||
const tr = newState.tr.deleteRange(from, to); | ||
initialEvaluationDone = true; | ||
return tr; | ||
} | ||
initialEvaluationDone = true; | ||
}, | ||
filterTransaction: (transaction, state) => { | ||
@@ -43,0 +66,0 @@ const limit = this.options.limit; |
{ | ||
"name": "@tiptap/extension-character-count", | ||
"description": "font family extension for tiptap", | ||
"version": "3.0.0-next.1", | ||
"version": "3.0.0-next.2", | ||
"homepage": "https://tiptap.dev", | ||
@@ -31,4 +31,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"@tiptap/core": "^3.0.0-next.1", | ||
"@tiptap/pm": "^3.0.0-next.1" | ||
"@tiptap/core": "^3.0.0-next.2", | ||
"@tiptap/pm": "^3.0.0-next.2" | ||
}, | ||
@@ -35,0 +35,0 @@ "peerDependencies": { |
@@ -19,2 +19,14 @@ import { Extension } from '@tiptap/core' | ||
mode: 'textSize' | 'nodeSize' | ||
/** | ||
* The text counter function to use. Defaults to a simple character count. | ||
* @default (text) => text.length | ||
* @example (text) => [...new Intl.Segmenter().segment(text)].length | ||
*/ | ||
textCounter: (text: string) => number | ||
/** | ||
* The word counter function to use. Defaults to a simple word count. | ||
* @default (text) => text.split(' ').filter(word => word !== '').length | ||
* @example (text) => text.split(/\s+/).filter(word => word !== '').length | ||
*/ | ||
wordCounter: (text: string) => number | ||
} | ||
@@ -50,2 +62,4 @@ | ||
mode: 'textSize', | ||
textCounter: text => text.length, | ||
wordCounter: text => text.split(' ').filter(word => word !== '').length, | ||
} | ||
@@ -69,3 +83,3 @@ }, | ||
return text.length | ||
return this.options.textCounter(text) | ||
} | ||
@@ -79,5 +93,4 @@ | ||
const text = node.textBetween(0, node.content.size, ' ', ' ') | ||
const words = text.split(' ').filter(word => word !== '') | ||
return words.length | ||
return this.options.wordCounter(text) | ||
} | ||
@@ -87,5 +100,35 @@ }, | ||
addProseMirrorPlugins() { | ||
let initialEvaluationDone = false | ||
return [ | ||
new Plugin({ | ||
key: new PluginKey('characterCount'), | ||
appendTransaction: (transactions, oldState, newState) => { | ||
if (initialEvaluationDone) { | ||
return | ||
} | ||
const limit = this.options.limit | ||
if (limit === null || limit === undefined || limit === 0) { | ||
initialEvaluationDone = true | ||
return | ||
} | ||
const initialContentSize = this.storage.characters({ node: newState.doc }) | ||
if (initialContentSize > limit) { | ||
const over = initialContentSize - limit | ||
const from = 0 | ||
const to = over | ||
console.warn(`[CharacterCount] Initial content exceeded limit of ${limit} characters. Content was automatically trimmed.`) | ||
const tr = newState.tr.deleteRange(from, to) | ||
initialEvaluationDone = true | ||
return tr | ||
} | ||
initialEvaluationDone = true | ||
}, | ||
filterTransaction: (transaction, state) => { | ||
@@ -92,0 +135,0 @@ const limit = this.options.limit |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
38576
441
0