Skip to content

Commit

Permalink
Use elm/file to simplify file export
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Hrček committed Dec 9, 2018
1 parent 3950ab1 commit 4f893db
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 31 deletions.
18 changes: 0 additions & 18 deletions dist/index.html
Expand Up @@ -32,24 +32,6 @@
})
});

/* Port for initiating download of graph export file */
app.ports.download.subscribe(function(msg) {
/* taken from https://stackoverflow.com/a/33542499/403702 */
var blob = new Blob([msg.data], {
type: 'text/plain'
});
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, msg.filename);
} else {
var elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = msg.filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
});

/* Port for calling external graphviz library (viz.js). Given graphviz source
* String (without layout info) it will generate output in graphviz "plain" format,
* enriched by layout information (x,y) of individual nodes.
Expand Down
4 changes: 3 additions & 1 deletion elm.json
Expand Up @@ -8,6 +8,7 @@
"direct": {
"elm/browser": "1.0.1",
"elm/core": "1.0.2",
"elm/file": "1.0.1",
"elm/html": "1.0.0",
"elm/json": "1.1.2",
"elm/parser": "1.1.0",
Expand All @@ -18,6 +19,7 @@
},
"indirect": {
"avh4/elm-fifo": "1.0.4",
"elm/bytes": "1.0.7",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2"
Expand All @@ -31,4 +33,4 @@
"elm/random": "1.0.0"
}
}
}
}
12 changes: 5 additions & 7 deletions src/Main.elm
Expand Up @@ -4,6 +4,7 @@ import Browser
import Browser.Dom
import Browser.Events
import Export
import File.Download
import Graph exposing (NodeId)
import GraphUtil
import GraphViz.VizJs
Expand Down Expand Up @@ -229,19 +230,16 @@ update msg model =

Download exportFormat ->
let
( fileExtension, graphToString ) =
( graphToString, fileName ) =
case exportFormat of
Types.Dot ->
( "gv", Export.toDot )
( Export.toDot, "graph.dot" )

Types.Tgf ->
( "tgf", Export.toTgf )
( Export.toTgf, "graph.tgf" )
in
( model
, Ports.download
{ filename = "graph." ++ fileExtension
, data = graphToString model.graph
}
, File.Download.string fileName "text/plain" (graphToString model.graph)
)

ReceiveLayoutInfoFromGraphviz jsonVal ->
Expand Down
6 changes: 1 addition & 5 deletions src/Ports.elm
@@ -1,6 +1,5 @@
port module Ports exposing
( download
, receiveGraphVizPlain
( receiveGraphVizPlain
, requestBoundingBoxesForContext
, requestBoundingBoxesForEverything
, requestEdgeTextBoundingBox
Expand Down Expand Up @@ -76,9 +75,6 @@ port requestBoundingBox : String -> Cmd msg
port setBoundingBox : (BBox -> msg) -> Sub msg


port download : { filename : String, data : String } -> Cmd msg


{-| Request to transform Graphviz dot String to Graphviz plain.
The resulting plain output will contain additional info about node layout which we'll parse out and adjust node positions for nicer layout.
-}
Expand Down

0 comments on commit 4f893db

Please sign in to comment.