import Editor from "../editor" ; import DirectionType from "../const/direction-type" ; import getSelection from "../selection/get-selection" ; import { Cursor } from "../selection/util" ; import input from "./input" ; import paste from "./paste" ; import enter from "./enter" ; import backspace from "./backspace" ; import focusAt from "../selection/focus-at" ; import { nextTick } from "../util" ; class Operator { isFireFox : boolean = navigator . userAgent . indexOf ( "Firefox" ) > - 1 ; editor : Editor ; constructor ( editor ) { this . editor = editor ; } onBlur ( event ) { } onClick ( event ) { let section = this . editor . mountedWindow . getSelection ( ) ; let target = event . target as HTMLElement ; if ( target . nodeName === "IMG" ) { section ?. removeAllRanges ( ) ; let range = new Range ( ) ; range . selectNode ( target ) ; section ?. addRange ( range ) ; } nextTick ( ( ) => { document . dispatchEvent ( new Event ( "editorChange" ) ) ; } ) ; } onDoubleClick ( event ) { } onInput ( text , start , event ) { input ( this . editor , text , start , event ) ; } onCut ( event ) { let selection = getSelection ( this . editor ) ; setTimeout ( ( ) => { backspace ( this . editor , selection . range [ 0 ] , selection . range [ 1 ] ) ; } , 30 ) ; } onPaste ( event ) { paste ( this . editor , event ) ; } onSave ( ) { this . editor . articleManage . save ( ) ; } onTab ( start , end , event ) { } onEnter ( start , end , event ) { enter ( this . editor , start , end , event ) ; } onBackspace ( start , end , event ) { backspace ( this . editor , start , end , event ) ; } onArraw ( direction , event ) { nextTick ( ( ) => { document . dispatchEvent ( new Event ( "editorChange" ) ) ; } ) ; } onCommand ( start , end , key , shift , event ) { if ( key === "enter" ) { let component = this . editor . storeManage . getBlockById ( start . id ) ; let operator = component . addEmptyParagraph ( ! shift ) ; focusAt ( this . editor , operator ?. [ 0 ] || start , operator ?. [ 1 ] ) ; return ; } if ( key === "z" ) { return ; } if ( ! shift && [ "a" , "c" , "x" , "v" ] . includes ( key ) ) { return ; } event . preventDefault ( ) ; if ( key === "s" ) { this . onSave ( ) ; return ; } } onKeyDown ( event ) { const key = event . key . toLowerCase ( ) ; let { range : [ start , end ] , } = getSelection ( this . editor ) ; if ( event . ctrlKey || event . metaKey ) { this . onCommand ( start , end , key , event . shiftKey , event ) ; return ; } if ( key === "tab" ) { this . onTab ( start , end , event ) ; return ; } if ( key === "enter" ) { this . onEnter ( start , end , event ) ; return ; } if ( key === "backspace" ) { this . onBackspace ( start , end , event ) ; return ; } if ( /^arrow/i . test ( event . key ) ) { let map = { ArrowUp : DirectionType . up , ArrowDown : DirectionType . down , ArrowLeft : DirectionType . left , ArrowRight : DirectionType . right , } ; this . onArraw ( map [ event . key ] , event ) ; return ; } } } export default Operator ;