Skip to content

Commit

Permalink
feat: add more interesting placeholder text
Browse files Browse the repository at this point in the history
Will now randomly choose the placeholder text from a pre-set list
  • Loading branch information
wardellbagby committed Aug 5, 2022
1 parent 09c7e5f commit 868cef4
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions packages/codemirror/main/CodeMirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,29 @@ import {
} from '@codemirror/state';
import { EditorView, keymap, placeholder } from '@codemirror/view';
import { useTheme } from '@mui/material';
import { sample } from 'lodash-es';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { editorTheme } from './editorTheme';
import { syllableCounts } from './syllableCounts';
import { TextSelectionData, textSelection } from './textSelection';

import { textSelection, TextSelectionData } from './textSelection';

const placeholders = [
'Type your lyrics...',
'Write your epic...',
'Show me your poetry...',
'Jot down your thoughts...',
'Input musings here...',
'Pen your next masterpiece...',
"Let's write some rhymes...",
"Let's start your song...",
'Tell me your story...',
'Speak (err, write?) your mind...',
'Let me assist your lyrics...',
'Create your next classic...',
"Let's make your magnum opus...",
'What do you want to write today?',
'Put your feelings to words...',
];
const textChanged = (onTextChanged: (text: string) => void) =>
EditorView.updateListener.of((update) => {
if (update.docChanged) {
Expand Down Expand Up @@ -69,6 +87,7 @@ export interface CodeMirrorEditorProps {
export const useCodeMirror = (props: CodeMirrorEditorProps) => {
const [view, setView] = useState<EditorView>(null);
const [container, setContainer] = useState<HTMLDivElement>(null);
const [resetCount, setResetCount] = useState(0);

const appTheme = useTheme();
const themeCompartment = useMemo(() => new Compartment(), []);
Expand Down Expand Up @@ -110,7 +129,7 @@ export const useCodeMirror = (props: CodeMirrorEditorProps) => {
syllableCounts(),
history(),
EditorView.lineWrapping,
placeholder('Type out some lyrics...'),
placeholder(sample(placeholders)),
themeCompartment.of(themeExtension),
textCompartment.of(textChangedExtension),
selectionCompartment.of(selectionExtension),
Expand All @@ -124,6 +143,7 @@ export const useCodeMirror = (props: CodeMirrorEditorProps) => {
textChangedExtension,
selectionExtension,
fileDroppedExtension,
resetCount,
]
);

Expand Down Expand Up @@ -183,11 +203,12 @@ export const useCodeMirror = (props: CodeMirrorEditorProps) => {
);
}, [container]);

const resetHistory = useCallback(
() =>
view?.setState(EditorState.create({ ...defaultConfig, doc: props.text })),
[view, defaultConfig, props.text]
);
const resetHistory = useCallback(() => {
setResetCount((count) => count + 1);
return view?.setState(
EditorState.create({ ...defaultConfig, doc: props.text })
);
}, [view, defaultConfig, props.text]);
const undo = useCallback(() => undoTextChange(view), [view]);
const redo = useCallback(() => redoTextChange(view), [view]);
const openFindReplaceDialog = useCallback(
Expand Down

0 comments on commit 868cef4

Please sign in to comment.