From 78448a08117fd41265ec6b23a1d863e2d67e4cec Mon Sep 17 00:00:00 2001 From: Douglas Henri Date: Wed, 28 Aug 2024 10:04:56 -0300 Subject: [PATCH] AI Assistant: Remove dead Breve code (#39089) * remove old files and use typescript on all files * changelog --- .../changelog/update-jetpack-ai-breve-clean | 4 + .../breve/{controls.js => controls.tsx} | 10 +- .../breve/dictionaries/adjectives.js | 461 ---------------- .../components/breve/dictionaries/adverbs.js | 504 ------------------ .../breve/dictionaries/dictionaries-config.js | 51 -- ...hKincaidUtils.js => FleschKincaidUtils.ts} | 12 +- .../{escapeRegExp.js => escapeRegExp.ts} | 2 +- .../components/breve/utils/getClientId.js | 13 - .../breve/utils/getHighlightRects.js | 96 ---- .../components/breve/utils/handleMessage.js | 44 -- .../breve/utils/requestAnimationFrame.js | 1 - .../breve/utils/textEditingHelpers.js | 106 ---- 12 files changed, 18 insertions(+), 1286 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/update-jetpack-ai-breve-clean rename projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/{controls.js => controls.tsx} (94%) delete mode 100644 projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/adjectives.js delete mode 100644 projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/adverbs.js delete mode 100644 projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/dictionaries-config.js rename projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/{FleschKincaidUtils.js => FleschKincaidUtils.ts} (73%) rename projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/{escapeRegExp.js => escapeRegExp.ts} (53%) delete mode 100644 projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/getClientId.js delete mode 100644 projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/getHighlightRects.js delete mode 100644 projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/handleMessage.js delete mode 100644 projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/requestAnimationFrame.js delete mode 100644 projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/textEditingHelpers.js diff --git a/projects/plugins/jetpack/changelog/update-jetpack-ai-breve-clean b/projects/plugins/jetpack/changelog/update-jetpack-ai-breve-clean new file mode 100644 index 0000000000000..a0a449db0a03f --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-jetpack-ai-breve-clean @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +AI Assistant: Remove dead Breve code diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/controls.js b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/controls.tsx similarity index 94% rename from projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/controls.js rename to projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/controls.tsx index 1920433d3f8d0..834570cdc440f 100644 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/controls.js +++ b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/controls.tsx @@ -23,6 +23,10 @@ import features from './features'; import calculateFleschKincaid from './utils/FleschKincaidUtils'; import { getPostText } from './utils/getPostText'; import './breve.scss'; +/** + * Types + */ +import type { BreveSelect } from './types'; export const useInit = init => { const [ initialized, setInitialized ] = useState( false ); @@ -34,13 +38,13 @@ export const useInit = init => { }; const Controls = ( { blocks, disabledFeatures } ) => { - const [ gradeLevel, setGradeLevel ] = useState( null ); + const [ gradeLevel, setGradeLevel ] = useState< string | null >( null ); const { toggleFeature, toggleProofread, setPopoverHover, setHighlightHover, setPopoverAnchor } = useDispatch( 'jetpack/ai-breve' ); const { tracks } = useAnalytics(); const isProofreadEnabled = useSelect( - select => select( 'jetpack/ai-breve' ).isProofreadEnabled(), + select => ( select( 'jetpack/ai-breve' ) as BreveSelect ).isProofreadEnabled(), [] ); @@ -64,7 +68,7 @@ const Controls = ( { blocks, disabledFeatures } ) => { const debouncedGradeLevelUpdate = useDebounce( updateGradeLevel, 250 ); const handleToggleFeature = useCallback( - feature => checked => { + ( feature: string ) => ( checked: boolean ) => { tracks.recordEvent( 'jetpack_ai_breve_feature_toggle', { type: feature, on: checked } ); toggleFeature( feature, checked ); }, diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/adjectives.js b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/adjectives.js deleted file mode 100644 index d94a988a35541..0000000000000 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/adjectives.js +++ /dev/null @@ -1,461 +0,0 @@ -const adjectives = [ - 'amazing', - 'authentic', - 'awesome', - 'beautiful', - 'best', - 'breathtaking', - 'brilliant', - 'captivating', - 'classic', - 'compelling', - 'creative', - 'cutting-edge', - 'dazzling', - 'delightful', - 'effective', - 'efficient', - 'elegant', - 'elite', - 'empowering', - 'enchanting', - 'endless', - 'engaging', - 'epic', - 'exclusive', - 'extraordinary', - 'fabulous', - 'fantastic', - 'flawless', - 'foolproof', - 'foremost', - 'glamorous', - 'glorious', - 'gorgeous', - 'groundbreaking', - 'guaranteed', - 'hassle-free', - 'heroic', - 'incredible', - 'ingenious', - 'innovative', - 'inspirational', - 'inspiring', - 'invincible', - 'legendary', - 'life-changing', - 'luxurious', - 'magical', - 'magnificent', - 'marvelous', - 'miraculous', - 'modern', - 'must-have', - 'one-of-a-kind', - 'outstanding', - 'phenomenal', - 'premium', - 'proven', - 'remarkable', - 'revolutionary', - 'riveting', - 'sensational', - 'sleek', - 'smart', - 'sophisticated', - 'stunning', - 'superb', - 'superior', - 'supreme', - 'transformative', - 'unbelievable', - 'unique', - 'unmatched', - 'unparalleled', - 'unrivaled', - 'unstoppable', - 'untouchable', - 'visionary', - 'world-class', - 'amazing', - 'authentic', - 'awesome', - 'beautiful', - 'best', - 'breathtaking', - 'brilliant', - 'captivating', - 'classic', - 'compelling', - 'creative', - 'cutting-edge', - 'dazzling', - 'delightful', - 'effective', - 'efficient', - 'elegant', - 'elite', - 'empowering', - 'enchanting', - 'endless', - 'engaging', - 'epic', - 'exclusive', - 'extraordinary', - 'fabulous', - 'fantastic', - 'flawless', - 'foolproof', - 'foremost', - 'glamorous', - 'glorious', - 'gorgeous', - 'groundbreaking', - 'guaranteed', - 'hassle-free', - 'heroic', - 'incredible', - 'ingenious', - 'innovative', - 'inspirational', - 'inspiring', - 'invincible', - 'legendary', - 'life-changing', - 'luxurious', - 'magical', - 'magnificent', - 'marvelous', - 'miraculous', - 'modern', - 'must-have', - 'one-of-a-kind', - 'outstanding', - 'phenomenal', - 'premium', - 'proven', - 'remarkable', - 'revolutionary', - 'riveting', - 'sensational', - 'sleek', - 'smart', - 'sophisticated', - 'stunning', - 'superb', - 'superior', - 'supreme', - 'transformative', - 'unbelievable', - 'unique', - 'unmatched', - 'unparalleled', - 'unrivaled', - 'unstoppable', - 'untouchable', - 'visionary', - 'world-class', - 'affordable', - 'astounding', - 'astounding', - 'bargain', - 'best-selling', - 'bold', - 'brilliant', - 'captivating', - 'charismatic', - 'charming', - 'commendable', - 'confident', - 'convenient', - 'cool', - 'coveted', - 'creative', - 'crisp', - 'customizable', - 'cutting-edge', - 'daring', - 'dazzling', - 'dependable', - 'desirable', - 'distinctive', - 'dynamic', - 'efficient', - 'effortless', - 'elegant', - 'elite', - 'enduring', - 'enlightening', - 'enthralling', - 'enticing', - 'entrancing', - 'epic', - 'essential', - 'eternal', - 'euphoric', - 'everlasting', - 'exemplary', - 'exhilarating', - 'extraordinary', - 'eye-catching', - 'fabulous', - 'fascinating', - 'faultless', - 'fearless', - 'first-class', - 'flawless', - 'foremost', - 'formidable', - 'glamorous', - 'gleaming', - 'glorious', - 'gorgeous', - 'groundbreaking', - 'guaranteed', - 'hassle-free', - 'heroic', - 'high-quality', - 'ideal', - 'illustrious', - 'impeccable', - 'impressive', - 'incomparable', - 'incredible', - 'indispensable', - 'ingenious', - 'inspirational', - 'inspiring', - 'intuitive', - 'invincible', - 'legendary', - 'life-changing', - 'luxurious', - 'magical', - 'magnificent', - 'majestic', - 'marvelous', - 'mind-blowing', - 'modern', - 'must-have', - 'noteworthy', - 'one-of-a-kind', - 'outstanding', - 'peerless', - 'perfect', - 'phenomenal', - 'pioneering', - 'plush', - 'popular', - 'posh', - 'powerful', - 'premium', - 'prestigious', - 'priceless', - 'profound', - 'proven', - 'quintessential', - 'radiant', - 'rare', - 'reliable', - 'remarkable', - 'renowned', - 'revolutionary', - 'rich', - 'riveting', - 'sensational', - 'simple', - 'sleek', - 'smart', - 'smooth', - 'sophisticated', - 'special', - 'spectacular', - 'spellbinding', - 'stellar', - 'stunning', - 'stylish', - 'successful', - 'superb', - 'superior', - 'supreme', - 'timeless', - 'top-notch', - 'transformative', - 'trendy', - 'trustworthy', - 'unbelievable', - 'unforgettable', - 'unique', - 'unmatched', - 'unparalleled', - 'unrivaled', - 'unseen', - 'unstoppable', - 'untouchable', - 'uplifting', - 'valiant', - 'versatile', - 'vibrant', - 'visionary', - 'vital', - 'wholesome', - 'world-class', - 'youthful', - 'zesty', - 'admired', - 'adored', - 'amazing', - 'appealing', - 'astounding', - 'astounding', - 'awesome', - 'beautiful', - 'beloved', - 'breathtaking', - 'brilliant', - 'captivating', - 'classic', - 'compelling', - 'confident', - 'cool', - 'coveted', - 'creative', - 'crisp', - 'cutting-edge', - 'dazzling', - 'delightful', - 'dependable', - 'desirable', - 'distinctive', - 'dynamic', - 'effective', - 'efficient', - 'effortless', - 'elegant', - 'elite', - 'empowering', - 'enchanting', - 'endless', - 'enduring', - 'engaging', - 'enlightening', - 'enthralling', - 'enticing', - 'entrancing', - 'epic', - 'essential', - 'eternal', - 'euphoric', - 'everlasting', - 'exemplary', - 'exclusive', - 'exhilarating', - 'extraordinary', - 'eye-catching', - 'fabulous', - 'fascinating', - 'faultless', - 'fearless', - 'first-class', - 'flawless', - 'foremost', - 'formidable', - 'glamorous', - 'gleaming', - 'glorious', - 'gorgeous', - 'groundbreaking', - 'guaranteed', - 'hassle-free', - 'heroic', - 'high-quality', - 'ideal', - 'illustrious', - 'impeccable', - 'impressive', - 'incomparable', - 'incredible', - 'indispensable', - 'ingenious', - 'inspirational', - 'inspiring', - 'intuitive', - 'invincible', - 'legendary', - 'life-changing', - 'luxurious', - 'magical', - 'magnificent', - 'majestic', - 'marvelous', - 'mind-blowing', - 'modern', - 'must-have', - 'noteworthy', - 'one-of-a-kind', - 'outstanding', - 'peerless', - 'perfect', - 'phenomenal', - 'pioneering', - 'plush', - 'popular', - 'posh', - 'powerful', - 'premium', - 'prestigious', - 'priceless', - 'profound', - 'proven', - 'quintessential', - 'radiant', - 'rare', - 'reliable', - 'remarkable', - 'renowned', - 'revolutionary', - 'rich', - 'riveting', - 'sensational', - 'simple', - 'sleek', - 'smart', - 'smooth', - 'sophisticated', - 'special', - 'spectacular', - 'spellbinding', - 'stellar', - 'stunning', - 'stylish', - 'successful', - 'superb', - 'superior', - 'supreme', - 'timeless', - 'top-notch', - 'transformative', - 'trendy', - 'trustworthy', - 'unbelievable', - 'unforgettable', - 'unique', - 'unmatched', - 'unparalleled', - 'unrivaled', - 'unseen', - 'unstoppable', - 'untouchable', - 'uplifting', - 'valiant', - 'versatile', - 'vibrant', - 'visionary', - 'vital', - 'wholesome', - 'world-class', - 'youthful', - 'zesty', -]; - -export default adjectives; diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/adverbs.js b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/adverbs.js deleted file mode 100644 index f29561f391e23..0000000000000 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/adverbs.js +++ /dev/null @@ -1,504 +0,0 @@ -const adverbs = [ - 'abruptly', - 'accurately', - 'actively', - 'adaptively', - 'aesthetically', - 'asynchronously', - 'automatically', - 'autonomously', - 'broadly', - 'carefully', - 'clearly', - 'collaboratively', - 'completely', - 'comprehensively', - 'consistently', - 'constantly', - 'continuously', - 'correctly', - 'creatively', - 'critically', - 'dynamically', - 'effectively', - 'efficiently', - 'elegantly', - 'energetically', - 'enthusiastically', - 'entirely', - 'ergonomically', - 'evolutionarily', - 'exponentially', - 'extensively', - 'fault-tolerantly', - 'flexibly', - 'frequently', - 'fundamentally', - 'generally', - 'globally', - 'gracefully', - 'graphically', - 'holistically', - 'immediately', - 'independently', - 'industrially', - 'intelligently', - 'interactively', - 'intuitively', - 'invariably', - 'locally', - 'logically', - 'low-latency', - 'massively', - 'minimally', - 'modularly', - 'naturally', - 'normally', - 'objectively', - 'optimally', - 'partially', - 'periodically', - 'personally', - 'precisely', - 'predictively', - 'primarily', - 'privately', - 'proactively', - 'promptly', - 'quickly', - 'quietly', - 'rapidly', - 'readily', - 'reliably', - 'remotely', - 'repeatedly', - 'resiliently', - 'robustly', - 'scalably', - 'securely', - 'sequentially', - 'smoothly', - 'solely', - 'specifically', - 'speedily', - 'steadily', - 'strategically', - 'strongly', - 'structurally', - 'successfully', - 'synchronously', - 'systematically', - 'technically', - 'thoroughly', - 'transparently', - 'uniformly', - 'universally', - 'uniquely', - 'universally', - 'user-centrically', - 'virtually', - 'visually', - 'abundantly', - 'accurately', - 'actively', - 'adaptively', - 'aesthetically', - 'aggressively', - 'automatically', - 'autonomously', - 'broadly', - 'carefully', - 'clearly', - 'collaboratively', - 'completely', - 'comprehensively', - 'consistently', - 'constantly', - 'continuously', - 'correctly', - 'creatively', - 'critically', - 'dynamically', - 'effectively', - 'efficiently', - 'elegantly', - 'energetically', - 'enthusiastically', - 'entirely', - 'ergonomically', - 'evolutionarily', - 'exponentially', - 'extensively', - 'fault-tolerantly', - 'flexibly', - 'frequently', - 'fundamentally', - 'generally', - 'globally', - 'gracefully', - 'graphically', - 'holistically', - 'immediately', - 'independently', - 'industrially', - 'intelligently', - 'interactively', - 'intuitively', - 'invariably', - 'locally', - 'logically', - 'low-latency', - 'massively', - 'minimally', - 'modularly', - 'naturally', - 'normally', - 'objectively', - 'optimally', - 'partially', - 'periodically', - 'personally', - 'precisely', - 'predictively', - 'primarily', - 'privately', - 'proactively', - 'promptly', - 'quickly', - 'quietly', - 'rapidly', - 'readily', - 'reliably', - 'remotely', - 'repeatedly', - 'resiliently', - 'robustly', - 'scalably', - 'securely', - 'sequentially', - 'smoothly', - 'solely', - 'specifically', - 'speedily', - 'steadily', - 'strategically', - 'strongly', - 'structurally', - 'successfully', - 'synchronously', - 'systematically', - 'technically', - 'thoroughly', - 'transparently', - 'uniformly', - 'universally', - 'uniquely', - 'universally', - 'user-centrically', - 'virtually', - 'visually', - 'abnormally', - 'absentmindedly', - 'accidentally', - 'actually', - 'adventurously', - 'allegedly', - 'almost', - 'always', - 'angrily', - 'annually', - 'anxiously', - 'arrogantly', - 'awkwardly', - 'badly', - 'barely', - 'bashfully', - 'beautifully', - 'bitterly', - 'bleakly', - 'blindly', - 'blissfully', - 'boldly', - 'bravely', - 'briefly', - 'brightly', - 'briskly', - 'broadly', - 'busily', - 'calmly', - 'carelessly', - 'cautiously', - 'certainly', - 'cheerfully', - 'cleverly', - 'closely', - 'coaxingly', - 'colorfully', - 'commonly', - 'continually', - 'coolly', - 'correctly', - 'courageously', - 'crossly', - 'cruelly', - 'curiously', - 'daily', - 'deceivingly', - 'deeply', - 'defiantly', - 'deliberately', - 'delightfully', - 'diligently', - 'dimly', - 'dramatically', - 'dreamily', - 'easily', - 'eloquently', - 'endlessly', - 'equally', - 'especially', - 'even', - 'evenly', - 'eventually', - 'exactly', - 'excitedly', - 'extremely', - 'fairly', - 'faithfully', - 'famously', - 'far', - 'fast', - 'fatally', - 'ferociously', - 'fervently', - 'fiercely', - 'fondly', - 'foolishly', - 'fortunately', - 'frankly', - 'frantically', - 'freely', - 'frightfully', - 'fully', - 'generally', - 'generously', - 'gently', - 'gladly', - 'gleefully', - 'gracefully', - 'gratefully', - 'greatly', - 'greedily', - 'happily', - 'hastily', - 'healthily', - 'heavily', - 'helpfully', - 'helplessly', - 'highly', - 'honestly', - 'hopelessly', - 'hourly', - 'hungrily', - 'impatiently', - 'innocently', - 'inquisitively', - 'irritably', - 'jovially', - 'joyfully', - 'joyously', - 'jubilantly', - 'judgmentally', - 'justly', - 'keenly', - 'kindly', - 'knavishly', - 'knowingly', - 'knowledgeably', - 'lazily', - 'lightly', - 'likely', - 'limply', - 'lively', - 'loftily', - 'longingly', - 'loosely', - 'loudly', - 'lovingly', - 'loyally', - 'madly', - 'majestically', - 'meaningfully', - 'mechanically', - 'merrily', - 'miserably', - 'mockingly', - 'monthly', - 'morally', - 'mortally', - 'mysteriously', - 'naturally', - 'nearly', - 'neatly', - 'nervously', - 'never', - 'nicely', - 'noisily', - 'obediently', - 'obnoxiously', - 'oddly', - 'offensively', - 'officially', - 'often', - 'only', - 'openly', - 'optimistically', - 'overconfidently', - 'painfully', - 'partially', - 'patiently', - 'perfectly', - 'periodically', - 'permanently', - 'persistently', - 'personally', - 'physically', - 'playfully', - 'politely', - 'poorly', - 'positively', - 'potentially', - 'powerfully', - 'promptly', - 'properly', - 'punctually', - 'quaintly', - 'queasily', - 'questionably', - 'questioningly', - 'quickly', - 'quietly', - 'quintessentially', - 'quizzically', - 'randomly', - 'rapidly', - 'rarely', - 'readily', - 'really', - 'reassuringly', - 'recklessly', - 'regularly', - 'reliably', - 'reluctantly', - 'repeatedly', - 'reproachfully', - 'republically', - 'resentfully', - 'restfully', - 'righteously', - 'rightfully', - 'rigidly', - 'roughly', - 'rudely', - 'sadly', - 'safely', - 'seldom', - 'selfishly', - 'selflessly', - 'separately', - 'seriously', - 'shakily', - 'sharply', - 'sheepishly', - 'shrilly', - 'shyly', - 'sickly', - 'silently', - 'sleepily', - 'slowly', - 'smoothly', - 'softly', - 'solemnly', - 'solidly', - 'soon', - 'speedily', - 'stealthily', - 'sternly', - 'strictly', - 'stubbornly', - 'stylishly', - 'substantially', - 'successfully', - 'suddenly', - 'sufficiently', - 'supposedly', - 'surprisingly', - 'suspiciously', - 'sympathetically', - 'tenderly', - 'tensely', - 'terribly', - 'thankfully', - 'thoroughly', - 'thoughtfully', - 'tightly', - 'tomorrow', - 'tremendously', - 'triumphantly', - 'truly', - 'truthfully', - 'ultimately', - 'unabashedly', - 'unaccountably', - 'unbearably', - 'unethically', - 'unexpectedly', - 'unfortunately', - 'unimpressively', - 'unnaturally', - 'unnecessarily', - 'upbeat', - 'upliftingly', - 'uprightly', - 'upward', - 'urgently', - 'usefully', - 'uselessly', - 'usually', - 'utterly', - 'vacantly', - 'vaguely', - 'vainly', - 'valiantly', - 'vastly', - 'verbally', - 'very', - 'viciously', - 'victoriously', - 'violently', - 'vivaciously', - 'voluntarily', - 'warmly', - 'weakly', - 'wearily', - 'weekly', - 'weirdly', - 'wetly', - 'wholly', - 'wildly', - 'wisely', - 'woefully', - 'wonderfully', - 'worriedly', - 'wrongly', - 'yearly', - 'yearningly', - 'yesterday', - 'yieldingly', - 'youthfully', - 'zealously', - 'zestfully', - 'zestily', -]; - -export default adverbs; diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/dictionaries-config.js b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/dictionaries-config.js deleted file mode 100644 index 23b0a1f3e5ad2..0000000000000 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/dictionaries/dictionaries-config.js +++ /dev/null @@ -1,51 +0,0 @@ -import phrases from '../features/complex-words/phrases'; -import weaselWords from '../features/weasel-words/words'; -import { escapeRegExp } from '../utils/escapeRegExp'; -import adjectives from './adjectives'; -import adverbs from './adverbs'; - -const config = { - dictionaries: { - phrase: { - dictionary: phrases, - type: 'key-value', - tooltip: "Consider replacing with '{value}'.", - label: 'Complex words', - }, - 'long-sentence': { - type: 'function', - function: text => { - const sentenceRegex = /[^.!?]+[.!?]+/g; - const sentences = text.match( sentenceRegex ) || []; - return sentences - .filter( sentence => sentence.split( /\s+/ ).length > 20 ) - .map( sentence => ( { - sentence, - regex: new RegExp( escapeRegExp( sentence ), 'gi' ), - } ) ); - }, - tooltip: 'Break this long sentence into shorter ones.', - label: 'Long sentences', - }, - weasel: { - dictionary: weaselWords, - type: 'list', - tooltip: "Remove 'Weasel' words to add confidence.", - label: 'Weasel words', - }, - adverb: { - dictionary: adverbs, - type: 'list', - tooltip: 'Adverbs make your writing less concise.', - label: 'Adverbs', - }, - adjective: { - dictionary: adjectives, - type: 'list', - tooltip: 'Replace adjectives with data or remove them.', - label: 'Adjectives', - }, - }, -}; - -export default config; diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/FleschKincaidUtils.js b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/FleschKincaidUtils.ts similarity index 73% rename from projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/FleschKincaidUtils.js rename to projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/FleschKincaidUtils.ts index fb0de09ed02b1..d08cc101493cb 100644 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/FleschKincaidUtils.js +++ b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/FleschKincaidUtils.ts @@ -1,12 +1,12 @@ -const countWords = text => { +const countWords = ( text: string ) => { return text.split( /\s+/ ).filter( word => word.length > 0 ).length; }; -const countSentences = text => { +const countSentences = ( text: string ) => { return text.split( /[.!?]+/ ).filter( sentence => sentence.length > 0 ).length; }; -const countSyllables = word => { +const countSyllables = ( word: string ) => { if ( word.length <= 3 ) { return 1; } @@ -21,11 +21,11 @@ const countSyllables = word => { return syllables ? syllables.length : 1; }; -const countTotalSyllables = text => { +const countTotalSyllables = ( text: string ) => { return text.split( /\s+/ ).reduce( ( total, word ) => total + countSyllables( word ), 0 ); }; -const fleschKincaidGrade = ( words, sentences, syllables ) => { +const fleschKincaidGrade = ( words: number, sentences: number, syllables: number ) => { if ( words === 0 || sentences === 0 ) { return null; } @@ -33,7 +33,7 @@ const fleschKincaidGrade = ( words, sentences, syllables ) => { return 0.39 * ( words / sentences ) + 11.8 * ( syllables / words ) - 15.59; }; -const calculateFleschKincaid = text => { +const calculateFleschKincaid = ( text: string ) => { const words = countWords( text ); const sentences = countSentences( text ); const syllables = countTotalSyllables( text ); diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/escapeRegExp.js b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/escapeRegExp.ts similarity index 53% rename from projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/escapeRegExp.js rename to projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/escapeRegExp.ts index d22279c125399..733295ea3a8ed 100644 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/escapeRegExp.js +++ b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/escapeRegExp.ts @@ -1,3 +1,3 @@ -export const escapeRegExp = string => { +export const escapeRegExp = ( string: string ) => { return string.replace( /[.*+?^${}()|[\]\\/]/g, '\\$&' ); }; diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/getClientId.js b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/getClientId.js deleted file mode 100644 index 411ffaae84976..0000000000000 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/getClientId.js +++ /dev/null @@ -1,13 +0,0 @@ -export const getClientId = element => { - if ( ! element ) { - return null; - } - - const clientId = element.getAttribute( 'data-block' ); - - if ( clientId ) { - return clientId; - } - - return getClientId( element.parentElement ); -}; diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/getHighlightRects.js b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/getHighlightRects.js deleted file mode 100644 index a32f3a4749ece..0000000000000 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/getHighlightRects.js +++ /dev/null @@ -1,96 +0,0 @@ -import config from '../dictionaries/dictionaries-config'; -import { escapeRegExp } from './escapeRegExp'; - -const getWordRects = ( content, regex ) => { - const ranges = []; - const nodeIterator = document.createNodeIterator( content, window.NodeFilter.SHOW_TEXT ); - let currentNode; - while ( ( currentNode = nodeIterator.nextNode() ) ) { - let match; - while ( ( match = regex.exec( currentNode.nodeValue ) ) ) { - const range = document.createRange(); - range.setStart( currentNode, match.index ); - range.setEnd( currentNode, match.index + match[ 0 ].length ); - ranges.push( { range, text: match[ 0 ] } ); - } - } - return ranges; -}; - -export const getHighlightRects = element => { - const rects = []; - const textContent = element.innerText || element.textContent; - - const allRanges = []; - - for ( const key in config.dictionaries ) { - const dictConfig = config.dictionaries[ key ]; - if ( dictConfig.type === 'function' ) { - const ranges = dictConfig - .function( textContent ) - .map( ( { regex } ) => getWordRects( element, regex ) ) - .flat(); - allRanges.push( ...ranges ); - } else { - const dictionary = dictConfig.dictionary; - let regex; - if ( dictConfig.type === 'key-value' ) { - regex = new RegExp( - `\\b(${ Object.keys( dictionary ).map( escapeRegExp ).join( '|' ) })\\b`, - 'gi' - ); - } else if ( dictConfig.type === 'list' ) { - regex = new RegExp( `\\b(${ dictionary.map( escapeRegExp ).join( '|' ) })\\b`, 'gi' ); - } - const ranges = getWordRects( element, regex ); - allRanges.push( ...ranges ); - } - } - - allRanges.forEach( ( { range, text } ) => { - const clientRects = Array.from( range.getClientRects() ); - clientRects.forEach( ( rect, rangeIndex ) => { - let type; - let replacement = text.toLowerCase(); - - for ( const key in config.dictionaries ) { - const dictConfig = config.dictionaries[ key ]; - if ( dictConfig.type === 'key-value' && dictConfig.dictionary[ text.toLowerCase() ] ) { - type = key; - replacement = dictConfig.dictionary[ text.toLowerCase() ]; - break; - } else if ( - dictConfig.type === 'list' && - dictConfig.dictionary.includes( text.toLowerCase() ) - ) { - type = key; - break; - } else if ( dictConfig.type === 'function' && key === 'long-sentence' ) { - const longSentences = dictConfig - .function( textContent ) - .map( ( { sentence } ) => sentence ); - if ( longSentences.includes( text ) ) { - type = key; - break; - } - } - } - - if ( ! type ) { - type = 'phrase'; - } - - rects.push( { - rect, - rangeIndex, - range, - target: `${ text }`, // The original text - replacement, // It's replacement - type, - highlightId: `${ range.startOffset }${ text }${ range.endOffset }`, - } ); - } ); - } ); - - return rects; -}; diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/handleMessage.js b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/handleMessage.js deleted file mode 100644 index 18780d7294e71..0000000000000 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/handleMessage.js +++ /dev/null @@ -1,44 +0,0 @@ -/** - * WordPress dependencies - */ -import { select, dispatch } from '@wordpress/data'; - -export const findBlockRecursively = ( clientId, blocks ) => { - const block = blocks.find( currentBlock => currentBlock.clientId === clientId ); - - if ( block ) { - return block; - } - - // If the block was not found, check the inner blocks - for ( const currentBlock of blocks ) { - if ( currentBlock.innerBlocks?.length > 0 ) { - const innerBlock = findBlockRecursively( clientId, currentBlock.innerBlocks ); - - if ( innerBlock ) { - return innerBlock; - } - } - } - - return null; -}; - -// messageHandler.js -export const handleMessage = event => { - const { clientId, aiReplacementText, updateFunc } = event; - const { getBlocks } = select( 'core/block-editor' ); - const { selectBlock, updateBlockAttributes } = dispatch( 'core/block-editor' ); - - const updateBlocks = attributes => { - selectBlock( clientId ); - updateBlockAttributes( clientId, attributes ); - }; - - const block = findBlockRecursively( clientId, getBlocks() ); - - if ( block ) { - updateBlocks( { content: aiReplacementText } ); - updateFunc(); - } -}; diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/requestAnimationFrame.js b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/requestAnimationFrame.js deleted file mode 100644 index 6c9b3c5a00298..0000000000000 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/requestAnimationFrame.js +++ /dev/null @@ -1 +0,0 @@ -export const requestAnimationFrame = window.requestAnimationFrame || ( cb => cb() ); diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/textEditingHelpers.js b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/textEditingHelpers.js deleted file mode 100644 index 220a50a66deab..0000000000000 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/textEditingHelpers.js +++ /dev/null @@ -1,106 +0,0 @@ -import { requestAnimationFrame } from './requestAnimationFrame'; - -export const saveCaretPosition = ( containerEl, caretPositionRef ) => { - const selection = containerEl.ownerDocument.defaultView.getSelection(); - - if ( selection.rangeCount > 0 ) { - const range = selection.getRangeAt( 0 ); - const preCaretRange = range.cloneRange(); - preCaretRange.selectNodeContents( range.startContainer ); - preCaretRange.setEnd( range.startContainer, range.startOffset ); - const caretOffset = preCaretRange.toString().length; - - caretPositionRef.current = { - container: range.startContainer, - offset: caretOffset, - }; - } -}; - -export const restoreCaretPosition = ( containerEl, caretPositionRef ) => { - const { container, offset } = caretPositionRef.current || {}; - - if ( container && typeof offset === 'number' ) { - const range = containerEl.ownerDocument.createRange(); - const selection = containerEl.ownerDocument.defaultView.getSelection(); - range.setStart( container, offset ); - range.collapse( true ); - selection.removeAllRanges(); - selection.addRange( range ); - } -}; - -export const getChildTextNodes = element => { - const textNodes = []; - - for ( let i = 0; i < element.childNodes.length; i++ ) { - const childNode = element.childNodes[ i ]; - - if ( childNode.nodeType === Node.TEXT_NODE ) { - textNodes.push( childNode ); - } else if ( childNode.nodeType === Node.ELEMENT_NODE ) { - const childNodeTextNodes = getChildTextNodes( childNode ); - textNodes.push( ...childNodeTextNodes ); - } - } - - return textNodes; -}; - -export const simulateClick = ( event, blockElement ) => { - blockElement.focus(); - - requestAnimationFrame( () => { - const selectedText = blockElement.ownerDocument.getSelection(); - const clickRange = blockElement.ownerDocument.createRange(); - const textNodes = getChildTextNodes( blockElement ); - - // Get the mouse click position relative to the block - const clickRect = blockElement.getBoundingClientRect(); - const x = event.clientX - clickRect.left; - - // Translate event.nativeEvent.pageY to local block coordinate - const y = - event.nativeEvent.pageY - - blockElement.ownerDocument.documentElement.scrollTop - - clickRect.top; - - // Get the text node and determine the character offset based on the click position - let offset = 0; - let accumulatedWidth = 0; - - // Create a temporary range to measure text width - let textNode; - - for ( let i = 0; i < textNodes.length; i++ ) { - textNode = textNodes[ i ]; - - for ( let j = 0; j < textNode.textContent.length; j++ ) { - clickRange.setStart( textNode, j ); - clickRange.setEnd( textNode, j + 1 ); - const charRect = clickRange.getBoundingClientRect(); - - if ( y >= charRect.top - clickRect.top && y <= charRect.bottom - clickRect.top ) { - accumulatedWidth += charRect.width; - // If x is between the accumulated width minus half the character width and the full accumulated width, then the click is closer to the next character - if ( x >= accumulatedWidth - charRect.width / 2 && x <= accumulatedWidth ) { - offset = j + 1; - break; - } - // if x is less than the accumulated width, then the click is closer to the previous character - else if ( x < accumulatedWidth ) { - offset = j; - break; - } - } - } - } - - // Set the caret at the exact position where the click occurred - clickRange.setStart( textNode, offset ); - clickRange.collapse( true ); - - selectedText.removeAllRanges(); - selectedText.addRange( clickRange ); - } ); -};