import { bold } from './bold' ; import { code , codeBlock } from './code' ; import { italic } from './italic' ; import { link } from './link' ; import { unorderedListCommand , orderedListCommand , checkedListCommand } from './list' ; import { quote } from './quote' ; import { hr } from './hr' ; import { title } from './title' ; import { title1 } from './title1' ; import { title2 } from './title2' ; import { title3 } from './title3' ; import { title4 } from './title4' ; import { title5 } from './title5' ; import { title6 } from './title6' ; import { comment } from './comment' ; import { group } from './group' ; import { divider } from './divider' ; import { codePreview , codeEdit , codeLive } from './preview' ; import { fullscreen } from './fullscreen' ; import { image } from './image' ; import { strikethrough } from './strikeThrough' ; import { insertTextAtPosition } from '../utils/InsertTextAtPosition' ; import { ContextStore , ExecuteCommandState } from '../Context' ; export interface CommandOrchestrator { executeCommand ( command ) : void ; } export interface ICommandChildHandle < T = string > extends ICommandBase < T > { children ? : ( handle : { close : ( ) => void ; execute : ( ) => void ; getState ? : TextAreaCommandOrchestrator [ 'getState' ] ; textApi ? : TextAreaTextApi ; } ) => React . ReactElement ; } export interface ICommandChildCommands < T = string > extends ICommandBase < T > { children ? : Array < ICommand < T > > ; } export interface ICommandBase < T > { parent ? : ICommand < any > ; keyCommand ? : string ; name ? : string ; shortcuts ? : string ; groupName ? : string ; icon ? : React . ReactElement ; value ? : T ; position ? : 'right' ; liProps ? : React . LiHTMLAttributes < HTMLLIElement > ; buttonProps ? : React . ButtonHTMLAttributes < HTMLButtonElement > | null ; execute ? : ( state : TextState , api : TextAreaTextApi , dispatch ? : React . Dispatch < ContextStore > , executeCommandState ? : ExecuteCommandState ) => void ; } export type ICommand < T = string > = ICommandChildCommands < T > | ICommandChildHandle < T > ; export interface TextRange { start : number ; end : number ; } export interface TextState { text : string ; selectedText : string ; selection : TextRange ; } const getCommands = ( ) => [ comment , bold , italic , strikethrough , hr , title , divider , link , quote , code , codeBlock , image , divider , unorderedListCommand , orderedListCommand , checkedListCommand , ] ; const getExtraCommands = ( ) => [ codeEdit , codeLive , codePreview , divider , fullscreen ] ; function getStateFromTextArea ( textArea ) { return { selection : { start : textArea . selectionStart , end : textArea . selectionEnd , } , text : textArea . value , selectedText : textArea . value . slice ( textArea . selectionStart , textArea . selectionEnd ) , } ; } class TextAreaTextApi { textArea : HTMLTextAreaElement ; constructor ( textArea ) { this . textArea = textArea ; } param text replaceSelection ( text ) { insertTextAtPosition ( this . textArea , text ) ; return getStateFromTextArea ( this . textArea ) ; } param selection setSelectionRange ( selection ) { this . textArea . focus ( ) ; this . textArea . selectionStart = selection . start ; this . textArea . selectionEnd = selection . end ; return getStateFromTextArea ( this . textArea ) ; } } class TextAreaCommandOrchestrator implements CommandOrchestrator { textArea : HTMLTextAreaElement ; textApi : TextAreaTextApi ; constructor ( textArea ) { this . textArea = textArea ; this . textApi = new TextAreaTextApi ( textArea ) ; } getState ( ) { if ( ! this . textArea ) return false ; return getStateFromTextArea ( this . textArea ) ; } executeCommand ( command , dispatch ? , state ? ) { command . execute && command . execute ( getStateFromTextArea ( this . textArea ) , this . textApi , dispatch , state ) ; } } export { title , title1 , title2 , title3 , title4 , title5 , title6 , bold , codeBlock , italic , strikethrough , hr , group , divider , link , quote , code , image , unorderedListCommand , orderedListCommand , checkedListCommand , codeEdit , codeLive , codePreview , fullscreen , getCommands , getExtraCommands , getStateFromTextArea , TextAreaCommandOrchestrator , TextAreaTextApi , } ;