🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
feat: Limit title length
Rene Wangdcd54f50f18d
diff --git a/src/features/PageEditor/Body/Title.tsx b/src/features/PageEditor/Body/Title.tsxindex 04930b8c7e7..0ba9a5ce0f1 100644--- a/src/features/PageEditor/Body/Title.tsx+++ b/src/features/PageEditor/Body/Title.tsx@@ -9,6 +9,7 @@ import { useTranslation } from 'react-i18next';import EmojiPicker from '@/components/EmojiPicker';import { useGlobalStore } from '@/store/global';import { globalGeneralSelectors } from '@/store/global/selectors';+import { truncateByWeightedLength } from '@/utils/textLength';import { usePageEditorStore } from '../store';@@ -87,7 +88,8 @@ const Title = memo(() => {<TextAreaautoSize={{ minRows: 1 }}onChange={(e) => {- setCurrentTitle(e.target.value);+ const truncated = truncateByWeightedLength(e.target.value, 100);+ setCurrentTitle(truncated);}}onKeyDown={(e) => {if (e.key === 'Enter') {diff --git a/src/utils/textLength.test.ts b/src/utils/textLength.test.tsnew file mode 100644index 00000000000..7c7b890e282--- /dev/null+++ b/src/utils/textLength.test.ts@@ -0,0 +1,77 @@+import { describe, expect, it } from 'vitest';++import { calculateWeightedLength, truncateByWeightedLength } from './textLength';++describe('textLength utilities', () => {+ describe('calculateWeightedLength', () => {+ it('should calculate length for pure English text', () => {+ expect(calculateWeightedLength('Hello World')).toBe(11);+ });++ it('should calculate weighted length for pure CJK text', () => {+ // Chinese characters with default weight of 2+ expect(calculateWeightedLength('你好世界')).toBe(8);+ });++ it('should calculate weighted length for Japanese text', () => {+ // Hiragana and Katakana+ expect(calculateWeightedLength('こんにちは')).toBe(10);+ expect(calculateWeightedLength('カタカナ')).toBe(8);+ });++ it('should calculate weighted length for Korean text', () => {+ expect(calculateWeightedLength('안녕하세요')).toBe(10);+ });++ it('should calculate weighted length for mixed text', () => {+ // "Hello 世界" = 5 (Hello) + 1 (space) + 4 (世界) = 10+ expect(calculateWeightedLength('Hello 世界')).toBe(10);+ });++ it('should handle custom CJK weight', () => {+ expect(calculateWeightedLength('你好', 3)).toBe(6);+ });++ it('should handle empty string', () => {+ expect(calculateWeightedLength('')).toBe(0);+ });+ });++ describe('truncateByWeightedLength', () => {+ it('should not truncate text within limit', () => {+ expect(truncateByWeightedLength('Hello', 10)).toBe('Hello');+ });++ it('should truncate English text exceeding limit', () => {+ expect(truncateByWeightedLength('Hello World', 8)).toBe('Hello Wo');+ });++ it('should truncate CJK text at character boundary', () => {+ // "你好世界" weighted length = 8, limit = 6 should keep "你好世" (weight 6)+ expect(truncateByWeightedLength('你好世界', 6)).toBe('你好世');+ });++ it('should truncate mixed text correctly', () => {+ // "Hello你好" = 5 (Hello) + 4 (你好) = 9+ // With limit 7, should keep "Hello你" (5 + 2 = 7)+ expect(truncateByWeightedLength('Hello你好', 7)).toBe('Hello你');+ });++ it('should handle text exactly at limit', () => {+ expect(truncateByWeightedLength('你好', 4)).toBe('你好');+ });++ it('should return empty string when limit is 0', () => {+ expect(truncateByWeightedLength('Hello', 0)).toBe('');+ });++ it('should handle empty string', () => {+ expect(truncateByWeightedLength('', 10)).toBe('');+ });++ it('should work with custom CJK weight', () => {+ // "你好" with weight 3 = 6, limit 5 should keep "你" (weight 3)+ expect(truncateByWeightedLength('你好', 5, 3)).toBe('你');+ });+ });+});diff --git a/src/utils/textLength.ts b/src/utils/textLength.tsnew file mode 100644index 00000000000..2775a73bbd7--- /dev/null+++ b/src/utils/textLength.ts@@ -0,0 +1,67 @@+/**+ * Check if a character is CJK (Chinese, Japanese, Korean)+ */+const isCJKChar = (char: string): boolean => {+ const code = char.codePointAt(0);+ if (!code) return false;++ return (+ // CJK Unified Ideographs+ (code >= 0x4e00 && code <= 0x9fff) ||+ // CJK Unified Ideographs Extension A+ (code >= 0x3400 && code <= 0x4dbf) ||+ // CJK Compatibility Ideographs+ (code >= 0xf900 && code <= 0xfaff) ||+ // Hiragana+ (code >= 0x3040 && code <= 0x309f) ||+ // Katakana+ (code >= 0x30a0 && code <= 0x30ff) ||+ // Hangul Syllables+ (code >= 0xac00 && code <= 0xd7af) ||+ // Hangul Jamo+ (code >= 0x1100 && code <= 0x11ff) ||+ // CJK Unified Ideographs Extension B-F+ (code >= 0x20000 && code <= 0x2ebef)+ );+};++/**+ * Calculate weighted length of text where CJK characters count more+ * @param text - The text to measure+ * @param cjkWeight - Weight for CJK characters (default: 2)+ * @returns Weighted length+ */+export const calculateWeightedLength = (text: string, cjkWeight = 2): number => {+ let length = 0;+ for (const char of text) {+ length += isCJKChar(char) ? cjkWeight : 1;+ }+ return length;+};++/**+ * Truncate text to a maximum weighted length, handling CJK characters+ * @param text - The text to truncate+ * @param maxWeightedLength - Maximum weighted length allowed+ * @param cjkWeight - Weight for CJK characters (default: 2)+ * @returns Truncated text+ */+export const truncateByWeightedLength = (+ text: string,+ maxWeightedLength: number,+ cjkWeight = 2,+): string => {+ let currentWeight = 0;+ let result = '';++ for (const char of text) {+ const charWeight = isCJKChar(char) ? cjkWeight : 1;+ if (currentWeight + charWeight > maxWeightedLength) {+ break;+ }+ currentWeight += charWeight;+ result += char;+ }++ return result;+};