import * as React from 'react' ; import { URLStr } from "polar-shared/src/util/Strings" ; import { NULL_FUNCTION } from "polar-shared/src/util/Functions" ; import { BlockType } from './store/BlocksStore' ; export function useNoteFormatHandlers ( type , onUpdated ) { const doExecCommand = React . useCallback ( ( command , value ? ) => { if ( type !== 'markdown' ) { return ; } console . log ( 'doExecCommand: ' + command ) ; document . execCommand ( command , false , value ) ; onUpdated ( ) ; } , [ onUpdated , type ] ) ; const onBold = React . useCallback ( ( ) => { doExecCommand ( 'bold' ) ; } , [ doExecCommand ] ) ; const onItalic = React . useCallback ( ( ) => { doExecCommand ( 'italic' ) ; } , [ doExecCommand ] ) ; const onQuote = React . useCallback ( ( ) => { } , [ ] ) ; const onUnderline = React . useCallback ( ( ) => { } , [ ] ) ; const onStrikethrough = React . useCallback ( ( ) => { doExecCommand ( 'strikeThrough' ) ; } , [ doExecCommand ] ) ; const onSubscript = React . useCallback ( ( ) => { doExecCommand ( 'subscript' ) ; } , [ doExecCommand ] ) ; const onSuperscript = React . useCallback ( ( ) => { doExecCommand ( 'superscript' ) ; } , [ doExecCommand ] ) ; const onRemoveFormat = React . useCallback ( ( ) => { doExecCommand ( 'removeFormat' ) ; } , [ doExecCommand ] ) ; const doLink = React . useCallback ( ( link ) => { doExecCommand ( 'createlink' , link ) ; } , [ doExecCommand ] ) ; return { onBold , onItalic , onQuote , onUnderline , onStrikethrough , onSubscript , onSuperscript , doLink , onRemoveFormat } ; } export function useNoteFormatKeyboardHandler ( type , onUpdated = NULL_FUNCTION ) { const noteFormatHandlers = useNoteFormatHandlers ( type , onUpdated ) ; return React . useCallback ( ( event ) => { if ( event . metaKey || event . ctrlKey ) { if ( ! event . shiftKey && ! event . altKey ) { function abortEvent ( ) { event . stopPropagation ( ) ; event . preventDefault ( ) ; } switch ( event . key ) { case 'b' : noteFormatHandlers . onBold ( ) ; abortEvent ( ) ; break ; case 'i' : noteFormatHandlers . onItalic ( ) ; abortEvent ( ) ; break ; case 'u' : noteFormatHandlers . onUnderline ( ) ; abortEvent ( ) ; break ; } } } } , [ noteFormatHandlers ] ) ; }