Skip to content

Commit

Permalink
Start working on keyboard shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Feb 15, 2024
1 parent 36e57f3 commit 0260a81
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/Client/Client.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,18 @@ let ARCitect_subscription (initial: Messages.Model) : (SubId * Subscribe<Message
{ new System.IDisposable with
member _.Dispose() = rmv()
}
let keyboardEventSubscription (dispatch: Messages.Msg -> 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
Expand Down
8 changes: 5 additions & 3 deletions src/Client/Host.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
| _ -> Browser

member this.IsStandalone =
this = Swatehost.Browser || this = Swatehost.ARCitect
24 changes: 18 additions & 6 deletions src/Client/MainComponents/KeyboardShortcuts.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
| _, _ -> ()

///<summary>These events only get reapplied on reload, not during hot reload</summary>
let addOnKeydownEvent dispatch =
Browser.Dom.document.body.removeEventListener("keydown", onKeydownEvent dispatch)
Browser.Dom.document.body.addEventListener("keydown", onKeydownEvent dispatch)
/// <summary>
/// Returns a function to remove the event listener
/// </summary>
/// <param name="eventHandler"></param>
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)
9 changes: 2 additions & 7 deletions src/Client/Update/InterfaceUpdate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down

0 comments on commit 0260a81

Please sign in to comment.