Skip to content

Commit

Permalink
Merge pull request #700 from Journaly/type-writer-sounds
Browse files Browse the repository at this point in the history
⌨️ J-Editor / FEATURE: Typewriter Sounds! 🎵
  • Loading branch information
robin-macpherson authored May 17, 2022
2 parents 239128e + 63ff8df commit 7010652
Show file tree
Hide file tree
Showing 17 changed files with 431 additions and 221 deletions.
4 changes: 0 additions & 4 deletions packages/web/components/Dashboard/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ const Header: React.FC<Props> = ({ onMenuClick }) => {
{showNotificationFeed && <NotificationFeed onClose={() => setShowNotificationFeed(false)} />}
<style jsx>{`
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
display: flex;
justify-content: space-between;
align-items: center;
Expand Down
66 changes: 52 additions & 14 deletions packages/web/components/JournalyEditor/JournalyEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ import PostBodyStyles from '@/components/PostBodyStyles'
import Toolbar from './Toolbar'
import RenderElement from './RenderElement'
import RenderLeaf from './RenderLeaf'
import {
withLinks,
withImages,
toggleMark,
options,
MarkType,
} from './helpers'
import { withLinks, withImages, toggleMark, options, MarkType } from './helpers'
import usePlayPolyphonicSound from '@/hooks/usePlayPolyphonicSound'
import useAutosavedState from '@/hooks/useAutosavedState'

/**
* The Journaly Rich Text Editor
Expand All @@ -33,6 +29,22 @@ const HOTKEYS: { [key in HotKey]: MarkType } = {
'mod+u': 'underline',
}

const KEY_BLACK_LIST = new Set([
'Shift',
'Tab',
'Meta',
'Control',
'Alt',
'ArrowUp',
'ArrowLeft',
'ArrowDown',
'ArrowRight',
'PageUp',
'PageDown',
'Home',
'End',
])

type JournalyEditorProps = {
value: Descendant[]
setValue: (value: Descendant[]) => void
Expand All @@ -52,10 +64,7 @@ const JournalyEditor = ({
const renderElement = useCallback((props) => <RenderElement {...props} />, [])
const renderLeaf = useCallback((props) => <RenderLeaf {...props} />, [])
const editor = useMemo(() => {
const withPlugins: ((ed: Editor) => Editor)[] = [
withHistory,
withLinks,
]
const withPlugins: ((ed: Editor) => Editor)[] = [withHistory, withLinks]

if (allowInlineImages) {
withPlugins.push(withImages)
Expand All @@ -64,6 +73,27 @@ const JournalyEditor = ({
return pipe(withReact(createEditor()), ...withPlugins)
}, [])

const [shouldPlayTypewriterSounds, setShouldPlayTypewriterSounds] = useAutosavedState(false, {
key: 'shouldPlayTypewriterSounds',
})

const playTypewriterSound = usePlayPolyphonicSound('/static/sounds/typewriter-key-sound.wav', 3)
const playTypewriterReturnSound = usePlayPolyphonicSound(
'/static/sounds/typewriter-return-sound.wav',
1,
)

const handlePlayTypewriterSound = (e: React.KeyboardEvent) => {
if (shouldPlayTypewriterSounds) {
if (KEY_BLACK_LIST.has(e.key)) return
else if (e.key === 'Enter') {
playTypewriterReturnSound()
} else {
playTypewriterSound()
}
}
}

useEffect(() => {
;(slateRef as React.MutableRefObject<Editor>).current = editor
}, [editor])
Expand All @@ -76,7 +106,13 @@ const JournalyEditor = ({
<div className="editor-wrapper">
<div className="editor-container">
<Slate editor={editor} value={value} onChange={(value) => setValue(value)}>
<Toolbar allowInlineImages={allowInlineImages} />
<Toolbar
allowInlineImages={allowInlineImages}
shouldPlayTypewriterSounds={shouldPlayTypewriterSounds}
onToggleShouldPlayTypewriterSounds={() =>
setShouldPlayTypewriterSounds(!shouldPlayTypewriterSounds)
}
/>
<EditablePlugins
plugins={plugins}
renderElement={[renderElement]}
Expand All @@ -85,15 +121,17 @@ const JournalyEditor = ({
spellCheck
onKeyDown={[
(event: React.KeyboardEvent) => {
handlePlayTypewriterSound(event)
Object.entries(HOTKEYS).forEach(([hotkey, mark]) => {
// Convert React keyboard event to native keyboard event
if (isHotkey(hotkey, (event as unknown) as KeyboardEvent)) {
if (isHotkey(hotkey, event as unknown as KeyboardEvent)) {
event.preventDefault()
toggleMark(editor, mark)
}
})
},
]}
onKeyDownDeps={[shouldPlayTypewriterSounds]}
data-testid="post-body"
/>
</Slate>
Expand All @@ -112,7 +150,7 @@ const JournalyEditor = ({
min-height: 200px;
}
.editor-container > :global([contenteditable="true"]) {
.editor-container > :global([contenteditable='true']) {
flex: 1;
padding-top: 15px;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const BaseToolbarButton = ({ onClick, active, children }: ButtonProps) => {
height: 100%;
padding: 1px;
border: none;
margin-right: 10px;
border-radius: 5px;
background-color: ${theme.colors.gray800};
cursor: pointer;
Expand Down
Loading

1 comment on commit 7010652

@vercel
Copy link

@vercel vercel bot commented on 7010652 May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.