Skip to content

Commit

Permalink
Load office-js cdn only in excel #442
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Jun 27, 2024
1 parent 088138b commit c78b08e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/Client/States/OfficeInteropState.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Msg =
// create and update table element functions
| CreateAnnotationTable of tryUsePrevOutput:bool
| AnnotationtableCreated
| TryFindAnnotationTable
| AnnotationTableExists of TryFindAnnoTableResult
| InsertOntologyTerm of TermMinimal
| AddAnnotationBlock of InsertBuildingBlock
Expand Down
45 changes: 39 additions & 6 deletions src/Client/Update/InterfaceUpdate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,44 @@ open Shared
open Fable.Core.JsInterop
open Shared.ARCtrlHelper

module private Helper =
/// This seems like such a hack :(
module private ExcelHelper =

open Fable.Core
open ExcelJS.Fable.GlobalBindings

let initializeAddIn () = Office.onReady()
let initializeAddIn () = Office.onReady().``then``(fun _ -> ()) |> Async.AwaitPromise

/// Office-js will kill iframe loading in ARCitect, therefore we must load it conditionally
let addOfficeJsScript(callback: unit -> unit) =
let cdn = @"https://appsforoffice.microsoft.com/lib/1/hosted/office.js"
let _type = "text/javascript"
let s = Browser.Dom.document.createElement("script")
s?``type`` <- _type
s?src <- cdn
Browser.Dom.document.head.appendChild s |> ignore
s.onload <- fun _ -> callback()
()

/// Make a function that loops short sleep sequences until a mutable variable is set to true
/// do mutabel dotnet ref for variable
let myAwaitLoadedThenInit(loaded: ref<bool>) =
let rec loop() =
async {
if loaded.Value then
do! initializeAddIn()
else
do! Async.Sleep 100
do! loop()
}
loop()

let officeload() =
let loaded = ref false
async {
addOfficeJsScript(fun _ -> loaded.Value <- true)
do! myAwaitLoadedThenInit loaded
}

//open Fable.Core.JS

Expand All @@ -31,13 +65,12 @@ module Interface =
let cmd =
Cmd.batch [
Cmd.ofMsg (Ontologies.GetOntologies |> OntologyMsg)
log ("HOST",host)
match host with
| Swatehost.Excel ->
Cmd.OfPromise.either
OfficeInterop.Core.tryFindActiveAnnotationTable
Cmd.OfAsync.either
ExcelHelper.officeload
()
(OfficeInterop.AnnotationTableExists >> OfficeInteropMsg)
(fun _ -> TryFindAnnotationTable |> OfficeInteropMsg)
(curry GenericError Cmd.none >> DevMsg)
| Swatehost.Browser ->
Cmd.none
Expand Down
56 changes: 32 additions & 24 deletions src/Client/Update/OfficeInteropUpdate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ open Shared
open OfficeInteropTypes

module OfficeInterop =
let update (currentModel:Messages.Model) (excelInteropMsg: OfficeInterop.Msg) : Messages.Model * Cmd<Messages.Msg> =
let update (model:Messages.Model) (msg: OfficeInterop.Msg) : Messages.Model * Cmd<Messages.Msg> =

match excelInteropMsg with
match msg with

| AutoFitTable hidecols ->
let p = fun () -> ExcelJS.Fable.GlobalBindings.Excel.run (OfficeInterop.Core.autoFitTable hidecols)
Expand All @@ -21,18 +21,26 @@ module OfficeInterop =
()
(curry GenericInteropLogs Cmd.none >> DevMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel, cmd
model, cmd

| TryFindAnnotationTable ->
let cmd =
Cmd.OfPromise.either
OfficeInterop.Core.tryFindActiveAnnotationTable
()
(OfficeInterop.AnnotationTableExists >> OfficeInteropMsg)
(curry GenericError Cmd.none >> DevMsg)
model, cmd
| AnnotationTableExists annoTableOpt ->
let exists =
match annoTableOpt with
| Success name -> true
| _ -> false
let nextState = {
currentModel.ExcelState with
model.ExcelState with
HasAnnotationTable = exists
}
currentModel.updateByExcelState nextState,Cmd.none
model.updateByExcelState nextState,Cmd.none

| InsertOntologyTerm (term) ->
let cmd =
Expand All @@ -41,7 +49,7 @@ module OfficeInterop =
term
(curry GenericLog Cmd.none >> DevMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel, cmd
model, cmd

| AddAnnotationBlock (minBuildingBlockInfo) ->
let cmd =
Expand All @@ -50,7 +58,7 @@ module OfficeInterop =
(minBuildingBlockInfo)
(curry GenericInteropLogs Cmd.none >> DevMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel, cmd
model, cmd

| AddAnnotationBlocks minBuildingBlockInfos ->
let cmd =
Expand All @@ -59,7 +67,7 @@ module OfficeInterop =
minBuildingBlockInfos
(curry GenericInteropLogs Cmd.none >> DevMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel, cmd
model, cmd

| ImportFile buildingBlockTables ->
let nextCmd =
Expand All @@ -68,7 +76,7 @@ module OfficeInterop =
buildingBlockTables
(curry GenericInteropLogs Cmd.none >> DevMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel, nextCmd
model, nextCmd

| RemoveBuildingBlock ->
let cmd =
Expand All @@ -77,7 +85,7 @@ module OfficeInterop =
()
(curry GenericInteropLogs Cmd.none >> DevMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel, cmd
model, cmd

| UpdateUnitForCells (unitTerm) ->
let cmd =
Expand All @@ -86,7 +94,7 @@ module OfficeInterop =
unitTerm
(curry GenericInteropLogs Cmd.none >> DevMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel, cmd
model, cmd

| CreateAnnotationTable(tryUsePrevOutput) ->
let cmd =
Expand All @@ -95,14 +103,14 @@ module OfficeInterop =
(false,tryUsePrevOutput)
(curry GenericInteropLogs (AnnotationtableCreated |> OfficeInteropMsg |> Cmd.ofMsg) >> DevMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel,cmd
model,cmd

| AnnotationtableCreated ->
let nextState = {
currentModel.ExcelState with
model.ExcelState with
HasAnnotationTable = true
}
currentModel.updateByExcelState nextState, Cmd.none
model.updateByExcelState nextState, Cmd.none


| GetParentTerm ->
Expand All @@ -112,7 +120,7 @@ module OfficeInterop =
()
(fun tmin -> tmin |> Option.map (fun t -> ARCtrl.OntologyAnnotation.fromTerm t.toTerm) |> TermSearch.UpdateParentTerm |> TermSearchMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel, cmd
model, cmd
//
| FillHiddenColsRequest ->
failwith "FillHiddenColsRequest Not implemented yet"
Expand All @@ -133,11 +141,11 @@ module OfficeInterop =
// (curry GenericError (UpdateFillHiddenColsState FillHiddenColsState.Inactive |> OfficeInteropMsg |> Cmd.ofMsg) >> DevMsg)
//let stateCmd = UpdateFillHiddenColsState FillHiddenColsState.ExcelCheckHiddenCols |> OfficeInteropMsg |> Cmd.ofMsg
//let cmds = Cmd.batch [cmd; stateCmd]
currentModel, Cmd.none
model, Cmd.none

| FillHiddenColumns (termsWithSearchResult) ->
let nextState = {
currentModel.ExcelState with
model.ExcelState with
FillHiddenColsStateStore = FillHiddenColsState.ExcelWriteFoundTerms
}
let cmd =
Expand All @@ -146,15 +154,15 @@ module OfficeInterop =
(termsWithSearchResult)
(curry GenericInteropLogs (UpdateFillHiddenColsState FillHiddenColsState.Inactive |> OfficeInteropMsg |> Cmd.ofMsg) >> DevMsg)
(curry GenericError (UpdateFillHiddenColsState FillHiddenColsState.Inactive |> OfficeInteropMsg |> Cmd.ofMsg) >> DevMsg)
currentModel.updateByExcelState nextState, cmd
model.updateByExcelState nextState, cmd


| UpdateFillHiddenColsState newState ->
let nextState = {
currentModel.ExcelState with
model.ExcelState with
FillHiddenColsStateStore = newState
}
currentModel.updateByExcelState nextState, Cmd.none
model.updateByExcelState nextState, Cmd.none
//
| InsertFileNames (fileNameList) ->
let cmd =
Expand All @@ -163,7 +171,7 @@ module OfficeInterop =
(fileNameList)
(curry GenericLog Cmd.none >> DevMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel, cmd
model, cmd

//
| GetSelectedBuildingBlockTerms ->
Expand All @@ -179,7 +187,7 @@ module OfficeInterop =
]
)
(curry GenericError (UpdateCurrentRequestState RequestBuildingBlockInfoStates.Inactive |> BuildingBlockDetails |> Cmd.ofMsg) >> DevMsg)
currentModel, cmd
model, cmd

// DEV
| TryExcel ->
Expand All @@ -189,15 +197,15 @@ module OfficeInterop =
()
((fun x -> curry GenericLog Cmd.none ("Debug",x)) >> DevMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel, cmd
model, cmd
| TryExcel2 ->
let cmd =
Cmd.OfPromise.either
OfficeInterop.Core.exampleExcelFunction2
()
((fun x -> curry GenericLog Cmd.none ("Debug",x)) >> DevMsg)
(curry GenericError Cmd.none >> DevMsg)
currentModel, cmd
model, cmd
//| _ ->
// printfn "Hit currently non existing message"
// currentState, Cmd.none
1 change: 0 additions & 1 deletion src/Client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<!-- The "Edge" mode tells IE to use the best available mode; thus IE11 should use IE11 mode. -->
<!--<meta http-equiv="X-UA-Compatible" content="IE=edge" />-->
<title>Swate</title>
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>
<!--<script src="https://kit.fontawesome.com/0d3e0ea7a6.js" crossorigin="anonymous"></script>-->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand Down

0 comments on commit c78b08e

Please sign in to comment.