Skip to content

Instantly share code, notes, and snippets.

@vaibhavsagar
Last active October 29, 2019 20:57
Show Gist options
  • Save vaibhavsagar/24b1754b8a269fd8c54a89cb73e64fa8 to your computer and use it in GitHub Desktop.
Save vaibhavsagar/24b1754b8a269fd8c54a89cb73e64fa8 to your computer and use it in GitHub Desktop.
Simple reflex-platform app with JavaScript interop.

Revision history for small-viz

0.1.0.0 -- YYYY-mm-dd

  • First version. Released on an unsuspecting world.
let
# ./updater versions.json reflex-platform
fetcher = { owner, repo, rev, sha256, ... }: builtins.fetchTarball {
inherit sha256;
url = "https://github.com/${owner}/${repo}/tarball/${rev}";
};
reflex-platform = fetcher (builtins.fromJSON (builtins.readFile ./versions.json)).reflex-platform;
in (import reflex-platform { system = builtins.currentSystem; }).project ({ pkgs, ... }: {
useWarp = true;
withHoogle = false;
packages = {
small-viz = ./.;
};
shells = {
ghc = ["small-viz"];
ghcjs = ["small-viz"];
};
})
let
# ./updater versions.json reflex-platform
fetcher = { owner, repo, rev, sha256, ... }: builtins.fetchTarball {
inherit sha256;
url = "https://github.com/${owner}/${repo}/tarball/${rev}";
};
reflex-platform = fetcher (builtins.fromJSON (builtins.readFile ./versions.json)).reflex-platform;
pkgs = (import reflex-platform {}).nixpkgs;
project = import ./default.nix;
html = pkgs.writeText "index.html" ''
<!DOCTYPE html>
<html>
<head>
<script language="javascript" src="all.js"></script>
</head>
<body>
</body>
</html>
'';
in pkgs.runCommand "glitch" {} ''
mkdir -p $out
cp ${html} $out/index.html
${pkgs.closurecompiler}/bin/closure-compiler \
--externs=${project.ghcjs.small-viz}/bin/small-viz.jsexe/all.js.externs \
--jscomp_off=checkVars \
--js_output_file="$out/all.js" \
-O ADVANCED \
-W QUIET \
${project.ghcjs.small-viz}/bin/small-viz.jsexe/all.js
''
Copyright (c) 2019, Vaibhav Sagar
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Vaibhav Sagar nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
{-# LANGUAGE OverloadedStrings #-}
import Reflex.Dom
import Language.Javascript.JSaddle (liftJSM, toJSVal)
import Viz (viz)
main = mainWidgetWithHead widgetHead $ el "div" $ do
t <- textArea def
e <- _element_raw . fst <$> el' "div" blank
performEvent_ $ ffor (updated (_textArea_value t)) $ \text -> liftJSM $ do
jsE <- toJSVal e
jsT <- toJSVal text
viz jsE jsT
where
widgetHead :: DomBuilder t m => m ()
widgetHead = do
script "https://cdn.jsdelivr.net/npm/viz.js@2.1.2/viz.min.js"
script "https://cdn.jsdelivr.net/npm/viz.js@2.1.2/full.render.min.js"
script src = elAttr "script" ("type" =: "text/javascript" <> "src" =: src) blank
import Distribution.Simple
main = defaultMain
cabal-version: >=1.10
-- Initial package description 'small-viz.cabal' generated by 'cabal init'.
-- For further documentation, see http://haskell.org/cabal/users-guide/
name: small-viz
version: 0.1.0.0
-- synopsis:
-- description:
-- bug-reports:
license: BSD3
license-file: LICENSE
author: Vaibhav Sagar
maintainer: vaibhavsagar@gmail.com
-- copyright:
-- category:
build-type: Simple
extra-source-files: CHANGELOG.md
executable small-viz
main-is: Main.hs
other-modules: Viz
-- other-extensions:
build-depends: base >=4.12 && <4.13
, lens
, jsaddle
, reflex
, reflex-dom
-- hs-source-dirs:
default-language: Haskell2010
if impl(ghcjs)
ghc-options: -dedupe
#! /usr/bin/env nix-shell
#! nix-shell -i bash
#! nix-shell -p curl jq nix
set -eufo pipefail
FILE=$1
PROJECT=$2
OWNER=$(jq -r '.[$project].owner' --arg project "$PROJECT" < "$FILE")
REPO=$(jq -r '.[$project].repo' --arg project "$PROJECT" < "$FILE")
DEFAULT_BRANCH=$(jq -r '.[$project].branch // "master"' --arg project "$PROJECT" < "$FILE")
BRANCH=${3:-$DEFAULT_BRANCH}
REV=$(curl "https://api.github.com/repos/$OWNER/$REPO/branches/$BRANCH" | jq -r '.commit.sha')
SHA256=$(nix-prefetch-url --unpack "https://github.com/$OWNER/$REPO/tarball/$REV")
TJQ=$(jq '.[$project] = {owner: $owner, repo: $repo, branch: $branch, rev: $rev, sha256: $sha256}' \
--arg project "$PROJECT" \
--arg owner "$OWNER" \
--arg repo "$REPO" \
--arg branch "$BRANCH" \
--arg rev "$REV" \
--arg sha256 "$SHA256" \
< "$FILE")
[[ $? == 0 ]] && echo "${TJQ}" >| "$FILE"
{
"reflex-platform": {
"owner": "reflex-frp",
"repo": "reflex-platform",
"branch": "develop",
"rev": "8f4b8973a06f78c7aaf1a222f8f8443cd934569f",
"sha256": "167smg7dyvg5yf1wn9bx6yxvazlk0qk64rzgm2kfzn9mx873s0vp"
}
}
module Viz where
import Language.Javascript.JSaddle
import Control.Lens ((^.))
viz :: JSVal -> JSVal -> JSM ()
viz element string = do
viz <- new (jsg "Viz") ()
render <- viz ^. js1 "renderSVGElement" string
result <- render ^. js1 "then" (fun $ \_ _ [e] -> do
outer <- e ! "outerHTML"
element ^. jss "innerHTML" outer)
result ^. js1 "catch" (fun $ \_ _ [err] ->
element ^. jss "innerHTML" err)
pure ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment