diff --git a/src/Client/Client.fs b/src/Client/Client.fs index 9d280b5f..8da6057b 100644 --- a/src/Client/Client.fs +++ b/src/Client/Client.fs @@ -52,10 +52,18 @@ let ARCitect_subscription (initial: Messages.Model) : (SubId * Subscribe unit) : System.IDisposable = + let rmv = Spreadsheet.KeyboardShortcuts.initEventListener dispatch + { new System.IDisposable with + member _.Dispose() = rmv() + } [ // Only subscribe to ARCitect messages when host is set correctly via query param. if initial.PersistentStorageState.Host = Some (Swatehost.ARCitect) then ["ARCitect"], subscription + //https://elmish.github.io/elmish/docs/subscription.html#aggregating-multiple-subscribers + //if initial.PersistentStorageState.Host.IsSome && initial.PersistentStorageState.Host.Value.IsStandalone then + // ["KeyboardShortCuts"], keyboardEventSubscription ] #if DEBUG diff --git a/src/Client/Host.fs b/src/Client/Host.fs index 3d84d091..33fd146f 100644 --- a/src/Client/Host.fs +++ b/src/Client/Host.fs @@ -5,11 +5,13 @@ module Host type Swatehost = | Browser | Excel -| ARCitect //WIP - +| ARCitect with static member ofQueryParam (queryInteger: int option) = match queryInteger with | Some 1 -> Swatehost.ARCitect | Some 2 -> Swatehost.Excel - | _ -> Browser \ No newline at end of file + | _ -> Browser + + member this.IsStandalone = + this = Swatehost.Browser || this = Swatehost.ARCitect \ No newline at end of file diff --git a/src/Client/MainComponents/KeyboardShortcuts.fs b/src/Client/MainComponents/KeyboardShortcuts.fs index 3ffeaf41..77f2f419 100644 --- a/src/Client/MainComponents/KeyboardShortcuts.fs +++ b/src/Client/MainComponents/KeyboardShortcuts.fs @@ -5,22 +5,34 @@ let private onKeydownEvent (dispatch: Messages.Msg -> unit) = //e.preventDefault() //e.stopPropagation() let e = e :?> Browser.Types.KeyboardEvent + log "KEY DOWN" match e.ctrlKey, e.which with | false, _ -> () // Ctrl + c | _, _ -> - match e.ctrlKey, e.which with + match (e.ctrlKey || e.metaKey), e.which with + // Ctrl + c | true, 67. -> + log 67 Spreadsheet.CopySelectedCell |> Messages.SpreadsheetMsg |> dispatch - // Ctrl + c + // Ctrl + x | true, 88. -> + log 88 Spreadsheet.CutSelectedCell |> Messages.SpreadsheetMsg |> dispatch // Ctrl + v | true, 86. -> + log 86 Spreadsheet.PasteSelectedCell |> Messages.SpreadsheetMsg |> dispatch + | _, 46. -> // del + Spreadsheet.ClearSelected |> Messages.SpreadsheetMsg |> dispatch | _, _ -> () -///These events only get reapplied on reload, not during hot reload -let addOnKeydownEvent dispatch = - Browser.Dom.document.body.removeEventListener("keydown", onKeydownEvent dispatch) - Browser.Dom.document.body.addEventListener("keydown", onKeydownEvent dispatch) +/// +/// Returns a function to remove the event listener +/// +/// +let initEventListener (dispatch) : unit -> unit = + log "INIT" + let handle = fun (e: Browser.Types.Event) -> onKeydownEvent dispatch e + Browser.Dom.window.addEventListener("message", handle) + fun () -> Browser.Dom.window.removeEventListener("keydown", handle) diff --git a/src/Client/Update/InterfaceUpdate.fs b/src/Client/Update/InterfaceUpdate.fs index 06b90743..6defe0a3 100644 --- a/src/Client/Update/InterfaceUpdate.fs +++ b/src/Client/Update/InterfaceUpdate.fs @@ -39,14 +39,9 @@ module Interface = (OfficeInterop.AnnotationTableExists >> OfficeInteropMsg) (curry GenericError Cmd.none >> DevMsg) | Swatehost.Browser -> - Cmd.batch [ - Cmd.ofEffect (fun dispatch -> Spreadsheet.KeyboardShortcuts.addOnKeydownEvent dispatch) - ] + Cmd.none | Swatehost.ARCitect -> - Cmd.batch [ - Cmd.ofEffect (fun dispatch -> Spreadsheet.KeyboardShortcuts.addOnKeydownEvent dispatch) - Cmd.ofEffect (fun _ -> ARCitect.ARCitect.send ARCitect.Init) - ] + Cmd.ofEffect (fun _ -> ARCitect.ARCitect.send ARCitect.Init) ] model, cmd | CreateAnnotationTable usePrevOutput ->