Files
claudia-docs-web/node_modules/@tiptap/extensions/dist/character-count/index.cjs

129 lines
4.5 KiB
JavaScript

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/character-count/index.ts
var index_exports = {};
__export(index_exports, {
CharacterCount: () => CharacterCount
});
module.exports = __toCommonJS(index_exports);
// src/character-count/character-count.ts
var import_core = require("@tiptap/core");
var import_state = require("@tiptap/pm/state");
var CharacterCount = import_core.Extension.create({
name: "characterCount",
addOptions() {
return {
limit: null,
mode: "textSize",
textCounter: (text) => text.length,
wordCounter: (text) => text.split(" ").filter((word) => word !== "").length
};
},
addStorage() {
return {
characters: () => 0,
words: () => 0
};
},
onBeforeCreate() {
this.storage.characters = (options) => {
const node = (options == null ? void 0 : options.node) || this.editor.state.doc;
const mode = (options == null ? void 0 : options.mode) || this.options.mode;
if (mode === "textSize") {
const text = node.textBetween(0, node.content.size, void 0, " ");
return this.options.textCounter(text);
}
return node.nodeSize;
};
this.storage.words = (options) => {
const node = (options == null ? void 0 : options.node) || this.editor.state.doc;
const text = node.textBetween(0, node.content.size, " ", " ");
return this.options.wordCounter(text);
};
},
addProseMirrorPlugins() {
let initialEvaluationDone = false;
return [
new import_state.Plugin({
key: new import_state.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) => {
const limit = this.options.limit;
if (!transaction.docChanged || limit === 0 || limit === null || limit === void 0) {
return true;
}
const oldSize = this.storage.characters({ node: state.doc });
const newSize = this.storage.characters({ node: transaction.doc });
if (newSize <= limit) {
return true;
}
if (oldSize > limit && newSize > limit && newSize <= oldSize) {
return true;
}
if (oldSize > limit && newSize > limit && newSize > oldSize) {
return false;
}
const isPaste = transaction.getMeta("paste");
if (!isPaste) {
return false;
}
const pos = transaction.selection.$head.pos;
const over = newSize - limit;
const from = pos - over;
const to = pos;
transaction.deleteRange(from, to);
const updatedSize = this.storage.characters({ node: transaction.doc });
if (updatedSize > limit) {
return false;
}
return true;
}
})
];
}
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CharacterCount
});
//# sourceMappingURL=index.cjs.map