import React from 'react'; import prettier from 'prettier'; import parserMarkdown from 'prettier/parser-markdown'; import { EditingModes } from './AppendEditor'; import { ChevronToggleButton, CopyButton, EyeButton, PencilButton, PrintButton, } from './Buttons'; // Import types import { HtmlElementId } from './AppendEditor'; enum HtmlClassName { chevronToggleButton = 'menu-button chevron-toggle-button', menuButton = 'menu-button', on = ' on', off = ' off', } interface MenuProps { borderlessMode?: boolean; editingMode?: string; fixedHeightMode?: boolean; fullWidthMode?: boolean; monacoEditorLanguage: string; onConfirmPrintUrl: () => void; overflowMode?: boolean; refreshEdit: () => void; refreshView: () => void; saveText: (text: string) => void; showMenuOptionsEdit?: boolean; //showMenuOptionsMonacoEditor?: boolean; showMenuOptionsShare?: boolean; showMenuOptionsView?: boolean; text: string; toggleBorderlessMode: () => void; toggleFixedHeightMode: () => void; toggleFullWidthMode: () => void; toggleOverflowMode: () => void; toggleShowMenu: () => void; toggleShowMenuOptionsEdit: () => void; //toggleShowMenuOptionsMonacoEditor?: () => void; toggleShowMenuOptionsView: () => void; toggleShowMenuOptionsShare: () => void; viewMode?: boolean; } interface MenuState { message?: string; displayMessageShare: boolean; displayMessageEdit: boolean; } export default class Menu extends React.Component { clearTooltipTimer: NodeJS.Timeout | undefined; constructor(props: MenuProps) { super(props); this.state = { message: '', displayMessageEdit: false, displayMessageShare: false, }; } resetMessageTimer = () => { if (this.clearTooltipTimer) { clearTimeout(this.clearTooltipTimer); } this.clearTooltipTimer = setTimeout(() => { this.setState({ displayMessageEdit: false, displayMessageShare: false, }); }, 5000); }; showMessageEdit = () => { this.setState( { displayMessageEdit: true, displayMessageShare: false, }, () => { this.resetMessageTimer(); } ); }; showMessageShare = () => { this.setState( { displayMessageEdit: false, displayMessageShare: true, }, () => { this.resetMessageTimer(); } ); }; copyToClipboard = (text: string) => { const textField = document.createElement('textarea'); textField.value = text; document.body.appendChild(textField); textField.select(); document.execCommand('copy'); this.showMessageShare(); textField.remove(); }; copyText = () => { if (this.props.text) { this.setState({ message: 'Copied Text to clipboard' }, () => { this.copyToClipboard(this.props.text); }); } else { this.setState({ message: 'No text to copy. Your note is empty' }, () => { this.showMessageShare(); }); } }; copyHtml = () => { if (!this.props.viewMode) { this.setState( { message: 'Unable to copy HTML. Please turn View Mode on' }, () => this.showMessageShare() ); } else if (!this.props.text) { this.setState({ message: 'No HTML to copy. Your note is empty' }, () => { this.showMessageShare(); }); } else { const renderedNote = document.getElementById('renderedNote'); if (renderedNote?.firstElementChild?.innerHTML) { this.setState({ message: 'Copied HTML to clipboard' }); this.copyToClipboard(renderedNote?.firstElementChild.innerHTML); } } }; formatText = () => { if ( this.props.monacoEditorLanguage !== 'markdown' && this.props.editingMode === EditingModes.useMonacoEditor ) { this.setState( { message: 'Error: Your Monaco Editor language is not Markdown. Formatting is only available for Markdown', }, () => { this.showMessageEdit(); } ); } else if (this.props.text) { this.setState( { message: 'Formatted Markdown text with Prettier' }, () => { try { const formattedText = prettier.format(this.props.text, { parser: 'markdown', plugins: [parserMarkdown], }); this.props.saveText(formattedText); this.props.refreshEdit(); this.props.refreshView(); this.showMessageEdit(); } catch (e) { this.setState({ message: 'Error formatting text: ' + e }, () => { this.showMessageEdit(); }); console.log('Error formatting text: ' + e); } } ); } else { this.setState( { message: 'No text to format. Your note is empty' }, () => { this.showMessageEdit(); } ); } }; uncheckBoxes = () => { const { text } = this.props; const checkedBoxes = new RegExp(/- \[x\]/gm); if (checkedBoxes.test(text)) { const newText = text.replace(checkedBoxes, '- [ ]'); this.props.saveText(newText); this.props.refreshEdit(); this.props.refreshView(); this.setState({ message: 'Unchecked all checkboxes' }, () => { this.showMessageEdit(); }); } else { this.setState({ message: 'No checked checkboxes found' }, () => { this.showMessageEdit(); }); } }; render() { // You can render any custom fallback UI return [
,
{this.props.showMenuOptionsView && [ , , , , ]} {this.props.showMenuOptionsShare && [ , , ,

{this.state.message}

, ]} {this.props.showMenuOptionsEdit && [ , ,

{this.state.message}

, ]}
, ]; } }