|
| 1 | +/** |
| 2 | + * This component provides testing hooks for critical editor operations like copy/paste. |
| 3 | + * It renders invisible buttons that can be triggered programmatically or via keyboard shortcuts. |
| 4 | + * These buttons are critical for automated testing with Cypress and provide a consistent way |
| 5 | + * to trigger clipboard operations across different browsers and environments. |
| 6 | + * |
| 7 | + * */ |
| 8 | + |
| 9 | +import { Editor } from '@tiptap/react' |
| 10 | +import { useClipboardShortcuts } from './hooks/useClipboardShortcuts' |
| 11 | +import { useHierarchicalSelection } from './hooks/useHierarchicalSelection' |
| 12 | +import { useEffect, useState } from 'react' |
| 13 | +import { useClipboardListener } from './hooks/useClipboardListener' |
| 14 | +/** |
| 15 | + * Controllers Component |
| 16 | + * |
| 17 | + * This component provides testing hooks and advanced selection utilities |
| 18 | + * for the editor, including hierarchical selection and clipboard operations. |
| 19 | + */ |
| 20 | +type SelectionLevel = 'element' | 'parent' | 'section' | 'heading' | 'list' | 'document' |
| 21 | + |
| 22 | +type ControllersProps = { |
| 23 | + editor: Editor | null |
| 24 | + debug?: boolean |
| 25 | +} |
| 26 | + |
| 27 | +const Controllers = ({ editor, debug = false }: ControllersProps) => { |
| 28 | + const [isTestEnv, setIsTestEnv] = useState(false) |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + setIsTestEnv( |
| 32 | + process.env.NODE_ENV === 'test' || process.env.NEXT_PUBLIC_DEBUG_CONTROLLERS === 'true' |
| 33 | + ) |
| 34 | + }, []) |
| 35 | + |
| 36 | + // Clipboard shortcuts |
| 37 | + useClipboardShortcuts() |
| 38 | + useClipboardListener() |
| 39 | + // Hierarchical selection utilities |
| 40 | + const { selectHierarchical, selectElement } = useHierarchicalSelection(editor) |
| 41 | + |
| 42 | + // Copy selected content |
| 43 | + const copySelectedContent = () => { |
| 44 | + if (!editor) return |
| 45 | + |
| 46 | + editor.commands.focus() |
| 47 | + const copyEvent = new ClipboardEvent('copy', { |
| 48 | + bubbles: true, |
| 49 | + cancelable: true |
| 50 | + }) |
| 51 | + document.activeElement?.dispatchEvent(copyEvent) |
| 52 | + if (!copyEvent.defaultPrevented) { |
| 53 | + document.execCommand('copy') |
| 54 | + } |
| 55 | + |
| 56 | + console.log('[Controllers] Copy operation triggered') |
| 57 | + } |
| 58 | + |
| 59 | + // Paste from clipboard |
| 60 | + const pasteFromClipboard = () => { |
| 61 | + if (!editor) return |
| 62 | + |
| 63 | + editor.commands.focus() |
| 64 | + const pasteEvent = new ClipboardEvent('paste', { |
| 65 | + bubbles: true, |
| 66 | + cancelable: true |
| 67 | + }) |
| 68 | + document.activeElement?.dispatchEvent(pasteEvent) |
| 69 | + if (!pasteEvent.defaultPrevented) { |
| 70 | + document.execCommand('paste') |
| 71 | + } |
| 72 | + |
| 73 | + console.log('[Controllers] Paste operation triggered') |
| 74 | + } |
| 75 | + |
| 76 | + // Select hierarchically and then copy |
| 77 | + const selectAndCopy = (level: SelectionLevel) => { |
| 78 | + if (!editor) return |
| 79 | + selectHierarchical(level) |
| 80 | + setTimeout(() => copySelectedContent(), 10) |
| 81 | + } |
| 82 | + |
| 83 | + // Expose selection methods globally |
| 84 | + useEffect(() => { |
| 85 | + if (!editor) return |
| 86 | + |
| 87 | + // @ts-ignore |
| 88 | + window._editorSelectAndCopy = selectAndCopy |
| 89 | + // @ts-ignore |
| 90 | + window._editorSelect = selectHierarchical |
| 91 | + // @ts-ignore |
| 92 | + window._editorSelectElement = selectElement |
| 93 | + |
| 94 | + return () => { |
| 95 | + // @ts-ignore |
| 96 | + delete window._editorSelectAndCopy |
| 97 | + // @ts-ignore |
| 98 | + delete window._editorSelect |
| 99 | + // @ts-ignore |
| 100 | + delete window._editorSelectElement |
| 101 | + } |
| 102 | + }, [editor, selectHierarchical, selectElement]) |
| 103 | + |
| 104 | + const buttonClasses = |
| 105 | + debug || isTestEnv |
| 106 | + ? 'p-2 bg-blue-500 text-white text-xs rounded m-1' |
| 107 | + : 'opacity-0 pointer-events-auto m-1' |
| 108 | + |
| 109 | + return ( |
| 110 | + <div className="controller-buttons absolute right-0 top-0 z-50 flex flex-col items-end"> |
| 111 | + {/* Clipboard controls */} |
| 112 | + <div className="flex"> |
| 113 | + <button |
| 114 | + onClick={copySelectedContent} |
| 115 | + id="btn_copyselectedcontent" |
| 116 | + className={buttonClasses} |
| 117 | + aria-label="Copy selected text" |
| 118 | + title="Copy selected text (like Cmd+C)" |
| 119 | + data-testid="copy-button"> |
| 120 | + {debug || isTestEnv ? 'Copy' : ''} |
| 121 | + </button> |
| 122 | + |
| 123 | + <button |
| 124 | + onClick={pasteFromClipboard} |
| 125 | + id="btn_pastefromclipboard" |
| 126 | + className={buttonClasses} |
| 127 | + aria-label="Paste from clipboard" |
| 128 | + title="Paste from clipboard (like Cmd+V)" |
| 129 | + data-testid="paste-button"> |
| 130 | + {debug || isTestEnv ? 'Paste' : ''} |
| 131 | + </button> |
| 132 | + </div> |
| 133 | + |
| 134 | + {/* Selection controls */} |
| 135 | + {(debug || isTestEnv) && ( |
| 136 | + <div className="mt-2 flex max-w-xs flex-wrap justify-end"> |
| 137 | + <button |
| 138 | + onClick={() => selectHierarchical('element')} |
| 139 | + id="btn_select_element" |
| 140 | + className={buttonClasses} |
| 141 | + data-testid="select-element"> |
| 142 | + Select Element |
| 143 | + </button> |
| 144 | + |
| 145 | + <button |
| 146 | + onClick={() => selectHierarchical('parent')} |
| 147 | + id="btn_select_parent" |
| 148 | + className={buttonClasses} |
| 149 | + data-testid="select-parent"> |
| 150 | + Select Parent |
| 151 | + </button> |
| 152 | + |
| 153 | + <button |
| 154 | + onClick={() => selectHierarchical('section')} |
| 155 | + id="btn_select_section" |
| 156 | + className={buttonClasses} |
| 157 | + data-testid="select-section"> |
| 158 | + Select Section |
| 159 | + </button> |
| 160 | + |
| 161 | + <button |
| 162 | + onClick={() => selectHierarchical('heading')} |
| 163 | + id="btn_select_heading" |
| 164 | + className={buttonClasses} |
| 165 | + data-testid="select-heading"> |
| 166 | + Select Heading |
| 167 | + </button> |
| 168 | + |
| 169 | + <button |
| 170 | + onClick={() => selectHierarchical('list')} |
| 171 | + id="btn_select_list" |
| 172 | + className={buttonClasses} |
| 173 | + data-testid="select-list"> |
| 174 | + Select List |
| 175 | + </button> |
| 176 | + |
| 177 | + <button |
| 178 | + onClick={() => selectHierarchical('document')} |
| 179 | + id="btn_select_document" |
| 180 | + className={buttonClasses} |
| 181 | + data-testid="select-document"> |
| 182 | + Select All |
| 183 | + </button> |
| 184 | + </div> |
| 185 | + )} |
| 186 | + </div> |
| 187 | + ) |
| 188 | +} |
| 189 | + |
| 190 | +export default Controllers |
0 commit comments