mue/src/features/navbar/components/Notes.jsx

175 lines
4.9 KiB
React
Raw Normal View History

import variables from 'config/variables';
import { PureComponent, memo, useState } from 'react';
2023-03-16 11:11:18 +00:00
import { MdContentCopy, MdAssignment, MdPushPin, MdDownload } from 'react-icons/md';
import { useFloating, shift } from '@floating-ui/react-dom';
2021-09-10 18:00:45 +00:00
import TextareaAutosize from '@mui/material/TextareaAutosize';
import { toast } from 'react-toastify';
import { Tooltip } from 'components/Elements';
import { saveFile } from 'utils/saveFile';
import EventBus from 'utils/eventbus';
class Notes extends PureComponent {
2021-03-23 13:10:34 +00:00
constructor() {
super();
this.state = {
notes: localStorage.getItem('notes') || '',
visibility: localStorage.getItem('notesPinned') === 'true' ? 'visible' : 'hidden',
showNotes: localStorage.getItem('notesPinned') === 'true' ? true : false,
};
}
2022-08-31 20:54:03 +00:00
setZoom() {
this.setState({
2022-11-06 11:59:59 +00:00
zoomFontSize: Number(((localStorage.getItem('zoomNavbar') || 100) / 100) * 1.2) + 'rem',
});
2022-08-31 20:54:03 +00:00
}
componentDidMount() {
EventBus.on('refresh', (data) => {
2022-10-30 16:43:08 +00:00
if (data === 'navbar') {
2022-08-31 20:54:03 +00:00
this.forceUpdate();
try {
this.setZoom();
} catch (e) {}
}
});
this.setZoom();
}
2022-10-30 16:43:08 +00:00
componentWillUnmount() {
EventBus.off('refresh');
}
setNotes = (e) => {
localStorage.setItem('notes', e.target.value);
this.setState({
notes: e.target.value,
});
};
showNotes() {
this.setState({
showNotes: true,
});
}
hideNotes() {
this.setState({
2022-11-06 11:59:59 +00:00
showNotes: localStorage.getItem('notesPinned') === 'true',
});
}
pin() {
2021-09-28 22:04:04 +00:00
variables.stats.postEvent('feature', 'Notes pin');
2022-11-06 11:59:59 +00:00
const notesPinned = localStorage.getItem('notesPinned') === 'true';
localStorage.setItem('notesPinned', !notesPinned);
this.setState({
showNotes: !notesPinned,
});
}
copy() {
2021-09-28 22:04:04 +00:00
variables.stats.postEvent('feature', 'Notes copied');
navigator.clipboard.writeText(this.state.notes);
toast(variables.getMessage('toasts.notes'));
}
download() {
const notes = localStorage.getItem('notes');
if (!notes || notes === '') {
return;
}
variables.stats.postEvent('feature', 'Notes download');
saveFile(this.state.notes, 'mue-notes.txt', 'text/plain');
}
render() {
return (
<div className="notes" onMouseLeave={() => this.hideNotes()} onFocus={() => this.showNotes()}>
<button
className="navbarButton"
onMouseEnter={() => this.showNotes()}
onFocus={() => this.showNotes()}
onBlur={() => this.hideNotes()}
ref={this.props.notesRef}
2022-08-31 20:54:03 +00:00
style={{ fontSize: this.state.zoomFontSize }}
2023-09-10 09:02:55 +00:00
aria-label={variables.getMessage('widgets.navbar.notes.title')}
>
2022-11-06 11:59:59 +00:00
<MdAssignment className="topicons" />
</button>
{this.state.showNotes && (
<span
className="notesContainer"
ref={this.props.floatRef}
style={{
position: this.props.position,
top: this.props.yPosition ?? '44',
left: this.props.xPosition ?? '',
}}
>
<div className="flexNotes">
<div className="topBarNotes" style={{ display: 'flex' }}>
<MdAssignment />
<span>{variables.getMessage('widgets.navbar.notes.title')}</span>
</div>
<div className="notes-buttons">
<Tooltip title={variables.getMessage('widgets.navbar.todo.pin')}>
<button onClick={() => this.pin()}>
<MdPushPin />
</button>
</Tooltip>
<Tooltip title={variables.getMessage('widgets.quote.copy')}>
2023-03-16 11:11:18 +00:00
<button onClick={() => this.copy()} disabled={this.state.notes === ''}>
<MdContentCopy />
</button>
</Tooltip>
<Tooltip title={variables.getMessage('widgets.background.download')}>
2023-03-16 11:11:18 +00:00
<button onClick={() => this.download()} disabled={this.state.notes === ''}>
<MdDownload />
</button>
</Tooltip>
</div>
<TextareaAutosize
placeholder={variables.getMessage('widgets.navbar.notes.placeholder')}
value={this.state.notes}
onChange={this.setNotes}
minRows={5}
maxLength={10000}
/>
</div>
</span>
)}
</div>
);
}
}
2022-10-30 16:56:26 +00:00
function NotesWrapper() {
const [reference, setReference] = useState(null);
const { x, y, refs, strategy } = useFloating({
placement: 'bottom',
middleware: [shift()],
elements: {
reference,
},
});
return (
<Notes
2024-01-05 12:13:58 +00:00
notesRef={setReference}
floatRef={refs.setFloating}
position={strategy}
xPosition={x}
yPosition={y}
/>
);
}
2022-10-30 16:56:26 +00:00
const MemoizedNotesWrapper = memo(NotesWrapper);
export { MemoizedNotesWrapper as default, MemoizedNotesWrapper as Notes };