feat: 完善官网配置、预约管理与文章编辑
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
import { decodeHTML } from 'entities';
|
||||
|
||||
export const ARTICLE_BODY_LIMIT = 20000;
|
||||
|
||||
export const countRichText = (text) =>
|
||||
Array.from(text.replace(/\s/g, '')).length;
|
||||
|
||||
export const countArticleHtml = (html) =>
|
||||
countRichText(decodeHTML(String(html || '').replace(/<[^>]*>/g, '')));
|
||||
|
||||
const countDelta = (delta) =>
|
||||
delta.ops.reduce(
|
||||
(total, op) =>
|
||||
total + (typeof op.insert === 'string' ? countRichText(op.insert) : 0),
|
||||
0,
|
||||
);
|
||||
|
||||
// Budget only newly inserted text, keeping the original text after the caret intact.
|
||||
export function limitRichTextChange(old, change, limit) {
|
||||
let oldIndex = 0;
|
||||
let remaining = limit - countDelta(old);
|
||||
for (const op of change.ops) {
|
||||
if (op.delete)
|
||||
remaining += countDelta(old.slice(oldIndex, oldIndex + op.delete));
|
||||
oldIndex += op.retain || op.delete || 0;
|
||||
}
|
||||
remaining = Math.max(0, remaining);
|
||||
const ops = change.ops.flatMap((op) => {
|
||||
if (typeof op.insert !== 'string') return [op];
|
||||
const text = Array.from(op.insert)
|
||||
.filter((char) => {
|
||||
if (/\s/.test(char)) return true;
|
||||
if (!remaining) return false;
|
||||
remaining--;
|
||||
return true;
|
||||
})
|
||||
.join('');
|
||||
return text ? [{ ...op, insert: text }] : [];
|
||||
});
|
||||
return ops;
|
||||
}
|
||||
Reference in New Issue
Block a user