Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #38

Merged
merged 4 commits into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
"version": "0.0.0",
"ppx-flags": ["reason/reactjs_jsx_ppx.native"],
"bsc-flags": ["-w", "-40"],
"bs-dependencies": ["rehydrate", "reason-js"],
"bs-dependencies": ["rehydrate", "reason-js", "rebase"],
"sources": [{
"dir": "src/extension",
"subdirs": [
"autoconverter", {
"dir": "common",
"subdirs": ["components"]
}, {
"dir": "inline",
"subdirs": [{
"dir": "content",
"subdirs": ["convert", "inline"]
}, {
"dir": "popup",
"dir": "common",
"subdirs": ["components", "vendor"]
}, {
"dir": "popup"
}
]
}],
Expand Down
111 changes: 56 additions & 55 deletions docs/Popup.bundle.js

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"description": "Adds Reason to the browser",
"repository": {
"type": "git",
"url": "https://github.com/rickyvetter/reason-tools.git"
"url": "https://github.com/reasonml/reason-tools.git"
},
"author": "rickyvetter",
"license": "MIT",
"homepage": "https://github.com/rickyvetter/reason-tools",
"homepage": "https://github.com/reasonml/reason-tools",
"keywords": [
"reason",
"reasonml",
Expand Down Expand Up @@ -44,19 +44,20 @@
"dependencies": {
"@opam-alpha/merlin": "^ 2.5.0",
"@opam-alpha/ocaml": "4.2.3",
"dependency-env": "https://github.com/reasonml/dependency-env.git",
"dependency-env": "reasonml/dependency-env",
"highlight.js": "^9.8.0",
"nopam": "https://github.com/reasonml/nopam.git",
"nopam": "reasonml/nopam",
"ocamlBetterErrors": "0.0.10",
"react": "^15.3.2",
"react-codemirror": "^0.2.6",
"react-copy-to-clipboard": "^4.2.3",
"react-dom": "^15.3.2",
"realpath": "*",
"reason": "https://github.com/facebook/reason.git",
"reason": "facebook/reason",
"rebase": "glennsl/rebase#v0.0.1",
"reason-js": "0.0.15",
"rebel": "https://github.com/reasonml/rebel.git",
"rehydrate": "https://github.com/reasonml/rehydrate.git"
"rebel": "reasonml/rebel",
"rehydrate": "reasonml/rehydrate"
},
"rebel": {
"targets": [
Expand All @@ -83,6 +84,6 @@
"generate-json-webpack-plugin": "^0.2.1",
"webpack": "^1.13.2",
"zip-webpack-plugin": "^0.2.0",
"bs-platform": "https://github.com/bloomberg/bucklescript.git"
"bs-platform": "bloomberg/bucklescript"
}
}
32 changes: 15 additions & 17 deletions src/extension/Background.re
Original file line number Diff line number Diff line change
@@ -1,55 +1,53 @@
open Rebase;
open Core;

module Refmt = {
type t = (string, string);

external refmt : string => t = "refmt" [@@bs.module ("../../../../_build/refmt/src/app.js", "Refmt")];

let parse: t => RefmtProtocol.response = fun
| ("Failure", error) => Failure error
let parse = fun
| ("Failure", error) => Error error
| (conversion, outText) =>
switch (conversion |> Js.String.split "to") {
| [| inLang, outLang |] => Success { outText, inLang, outLang }
| _ => Failure "Encountered some weird unknown conversion";
| [| inLang, outLang |] => Ok Protocol.Refmt.{ outText, inLang, outLang }
| _ => Error "Encountered some weird unknown conversion";
};
};

RefmtProtocol.listen
Protocol.Refmt.listen
(fun request respond =>
request.input |> Refmt.refmt
|> Refmt.parse
|> respond );

Message.receive "background:open"
(fun text _ _ =>
Chrome.Tabs.create { "url": ("popup.html#" ^ (Util.btoa text)) }
);

external executeScript : Js.t {. file: string } => (unit => unit) => unit = "chrome.tabs.executeScript" [@@bs.val];
Protocol.OpenInTab.listen
(fun text => Chrome.Tabs.create { "url": ("popup.html#" ^ (Util.btoa text)) });

let loadContentScripts tabId callback => {
executeScript { "file": "Content.bundle.js" } (fun () => {
Message.sendTab tabId "content:notify-loaded" ();
Chrome.Tabs.executeScriptFile { "file": "Content.bundle.js" } (fun () => {
Protocol.NotifyLoaded.send tabId;
callback ();
});
};

let ensureLoaded tabId callback =>
Message.queryTab tabId "content:query-loaded" () (fun loaded => {
Protocol.QueryLoaded.query tabId (fun loaded => {
if (not loaded) {
loadContentScripts tabId callback
} else {
callback ();
}
});

Message.receive "background:load-content-scripts" (fun _ sender _ => loadContentScripts sender##tab##id noop);
Protocol.LoadScripts.listen
(fun tabId => loadContentScripts tabId noop);

let refmtSelection tabId =>
ensureLoaded tabId (Message.sendTab tabId "content:refmt-selection");
ensureLoaded tabId (fun () => Protocol.RefmtSelection.send tabId);

let toggleConversion tabId =>
ensureLoaded tabId (Message.sendTab tabId "content:toggle");
ensureLoaded tabId (fun () => Protocol.ToggleConversion.send tabId);

Chrome.ContextMenus.create {
"title": "Refmt",
Expand Down
120 changes: 6 additions & 114 deletions src/extension/Content.re
Original file line number Diff line number Diff line change
@@ -1,129 +1,21 @@
open Core;
open Core.Dom;
open Dom;
open Common;

Hljs.registerLanguage "ocaml" [%bs.raw "require('highlight.js/lib/languages/ocaml')"];
Hljs.configure { "classPrefix": "", "languages": [| "ocaml" |] };

let stylesheet = Document.createElement "link";

let css = [%bs.raw {|require('../../../../src/css/ocamlDoc.css')|}];

Element.setType stylesheet "text/css";
Element.setRel stylesheet "stylesheet";
Element.setHref stylesheet (Chrome.Extension.getURL css);

type saveState = {
mutable stylesheets: list Element.t,
};
let savedState = { stylesheets: [stylesheet] };

let swapStyleSheets _ => {
let stylesheets =
getElementsByTagName None "link"
|> List.filter (fun link => (Element.getAttribute link "rel") == "stylesheet");

stylesheets
|> List.iter Element.remove;

savedState.stylesheets
|> List.iter Head.appendChild;

savedState.stylesheets = stylesheets;
};

let readjustViewport () =>
if (not (Str.isEmpty Location.hash)) {
[%bs.raw {| window.location.href = window.location.href |}];
};

type swapState = { mutable remaining: int };

let doListing mode state listing => {
open Retrieve;
let { els, text, replace } = listing;

RefmtProtocol.send
{ input: normalizeText text }
(fun response => {
switch response {
| RefmtProtocol.Failure _ => ()
| RefmtProtocol.Success { outText } => Replace.replaceListing els outText replace
};

/* we're in an async callback, so keep track of when we're finished by keeping count */
state.remaining = state.remaining - 1;
if (mode == `initial && state.remaining <= 0) {
/* when we're done, the DOM has most likely been shifted,
* so we need to go back to where we're supposed to be */
readjustViewport()
}
});
};

let swapSyntax mode => {
let listings = Retrieve.getListings ();
let state = {
remaining: List.length listings
};

listings
|> List.iter (doListing mode state);

UI.updateSyntaxSwapButton ();
};

let toggle () => {
swapStyleSheets ();
UI.toggle swapStyleSheets swapSyntax;
swapSyntax `initial;
};

if (Detect.mightBeOcamlDoc ()) {
toggle ();
ConvertPage.toggle ();
};

Message.receive "content:toggle" (fun _ _ _ => toggle ());

let open_ text =>
Message.send "background:open" text;
Protocol.ToggleConversion.listen
ConvertPage.toggle;

Message.receive "content:refmt-selection" (fun _ _ _ => {
Protocol.RefmtSelection.listen (fun () => {
let selection = Window.getSelection ();
let text = selection |> Selection.toString |> normalizeText;

Selection.removeAllRanges selection;

RefmtProtocol.send
{ input: text }
(fun response => {
switch response {
| Failure _ => ()
| Success { outText, inLang, outLang } => {
let host = Document.createElement "div";
let shadow = Element.attachShadow host { "mode": "closed" };

Body.appendChild host;

/* React must render directly to the shadow root, otherwise onclick handlers won't work */
ReactDOMRe.render
<InlinePopover
inLang inText=text
outLang outText
close=(fun () => Body.removeChild host)
open_=open_
/>
(Element.toReasonJsElement shadow);

/* Only after React has rendered can we attach the stylesheets, otherwise React will render over them */
Element.appendChild shadow (Node.cloneNode stylesheet);
let style = Document.createElement "style";
Element.setType style "text/css";
Element.setInnerText style InlineStyles.stylesheet;
Element.appendChild shadow style;
}
};
}
)

Overlay.try_ text;
});
11 changes: 3 additions & 8 deletions src/extension/ContentLoader.re
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
open Core;

let loaded = ref false;

let load () =>
Message.send "background:load-content-scripts" ();

if (Detect.mightBeOcamlDoc ()) {
load ();
Protocol.LoadScripts.send ();
};

Message.receive "content:notify-loaded" (fun _ _ _ => loaded := true);
Message.receive "content:query-loaded" (fun _ _ respond => respond !loaded);
Protocol.NotifyLoaded.listen (fun () => loaded := true);
Protocol.QueryLoaded.listen (fun () => !loaded);
3 changes: 2 additions & 1 deletion src/extension/Page.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
open Rebase;
open Core;
open Core.Dom;
open Dom;

[%bs.raw {|require('../../../../src/popup.html')|}];
[%bs.raw {|require('../../../../src/images/logo19.png')|}];
Expand Down
21 changes: 9 additions & 12 deletions src/extension/Popup.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
open Rebase;
open Core;
open Core.Dom;
open Dom;

[%bs.raw {|require('../../../../src/popup.html')|}];
[%bs.raw {|require('../../../../src/images/logo19.png')|}];
Expand All @@ -10,9 +11,6 @@ open Core.Dom;
[%bs.raw {|require('codemirror/mode/javascript/javascript')|}];
[%bs.raw {|require('codemirror/mode/mllike/mllike')|}];

let open_ text =>
Message.send "background:open" text;

let generateShareableLink text =>
"https://reasonml.github.io/reason-tools/popup.html#" ^ (Util.btoa text);

Expand All @@ -24,8 +22,8 @@ let getSelection () =>
maybeMaybeArray
|> Js.Null_undefined.to_opt
|> Option.map (fun maybeArray => Array.unsafe_get maybeArray 0)
|> Option.and_then (fun s => Str.isEmpty s ? None : Some s)
|> Option.map_or_else resolve reject
|> Option.andThen (fun s => Str.isEmpty s ? None : Some s)
|> Option.mapOrElse resolve reject
)
);

Expand All @@ -36,7 +34,7 @@ let getLatestInput () =>
(fun response =>
response##latestRefmtString
|> Js.Null.to_opt
|> Option.map_or_else resolve reject
|> Option.mapOrElse resolve reject
)
);

Expand All @@ -57,12 +55,11 @@ let rec inputChanged input => {
/* this isn't guaranteed to be sync or speedy, so
* don't set this.state.in here, since it could cause lag.
*/
RefmtProtocol.send
{ input: input }
Protocol.Refmt.send input
(fun
| RefmtProtocol.Failure error =>
| Error error =>
render input error None None link
| RefmtProtocol.Success { outText, inLang, outLang } =>
| Ok { outText, inLang, outLang } =>
render input outText (Some inLang) (Some outLang) link
);

Expand All @@ -77,7 +74,7 @@ and render inText outText inLang outLang link =>
outText
outLang
link
onOpen=open_
onOpen=Protocol.OpenInTab.send
onInputChanged=inputChanged
/>
(ReasonJs.Document.getElementById "app");
Expand Down
Loading