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

Use @nteract/tranforms #655

Merged
merged 4 commits into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 16 additions & 4 deletions lib/components/inspector/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
import React from "react";
import { observer } from "mobx-react";
import ResizableBox from "react-resizable-box";
import { richestMimetype, standardTransforms } from "@nteract/transforms";
import { List as ImmutableList } from "immutable";

const displayOrder = new ImmutableList([
"text/html",
"text/markdown",
"text/plain"
]);

import type Kernel from "./../../kernel";

Expand All @@ -15,6 +23,11 @@ const InspectorComponent = observer(({ store: { kernel }, panel }: Props) => {
return null;
}
panel.show();

const bundle = kernel.inspector.bundle;
const mimetype = richestMimetype(bundle, displayOrder, standardTransforms);
// $FlowFixMe React element `Transform`. Expected React component instead of Transform
const Transform = standardTransforms.get(mimetype);
return (
<ResizableBox
isResizable={{ top: true }}
Expand All @@ -32,10 +45,9 @@ const InspectorComponent = observer(({ store: { kernel }, panel }: Props) => {
/>
</div>
</div>
<div
className="hydrogen-panel-body"
dangerouslySetInnerHTML={{ __html: kernel.inspector.HTML }}
/>
<div className="hydrogen-panel-body">
<Transform data={bundle.get(mimetype)} />
</div>
</ResizableBox>
);
});
Expand Down
47 changes: 10 additions & 37 deletions lib/components/inspector/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/* @flow */

import React from "react";
import * as transformime from "transformime";
import { Map as ImmutableMap } from "immutable";

import { log, reactFactory } from "./../../utils";
import store from "./../../store";
import { getCodeToInspect } from "./../../code-manager";
import InspectorComponent from "./component";

import type Kernel from "./../../kernel";

const transform = transformime.createTransform();

export default class Inspector {
constructor() {
const element = document.createElement("div");
Expand Down Expand Up @@ -41,40 +37,17 @@ export default class Inspector {
kernel.inspect(code, cursorPos, (result: {
data: Object,
found: Boolean
}) => this.showInspectionResult(kernel, result));
// TODO: handle case when inspect request returns an error
}

showInspectionResult(
kernel: Kernel,
result: { data: Object, found: Boolean }
) {
log("Inspector: Result:", result);
}) => {
log("Inspector: Result:", result);

if (!result.found) {
kernel.setInspectorVisibility(false);
atom.notifications.addInfo("No introspection available!");
return;
}

const onInspectResult = ({ mimetype, el }) => {
if (mimetype === "text/plain" || mimetype === "text/markdown") {
kernel.setInspectorResult(el.outerHTML);
} else if (mimetype === "text/html") {
const container = document.createElement("div");
container.appendChild(el);
kernel.setInspectorResult(container.outerHTML);
} else {
console.error("Inspector: Rendering error:", mimetype, el);
atom.notifications.addInfo("Cannot render introspection result!");
if (!result.found) {
kernel.setInspectorVisibility(false);
atom.notifications.addInfo("No introspection available!");
return;
}
};

const onError = error => {
console.error("Inspector: Rendering error:", error);
atom.notifications.addInfo("Cannot render introspection result!");
};
const bundle = new ImmutableMap(result.data);

transform(result.data).then(onInspectResult, onError);
kernel.setInspectorResult(bundle);
});
}
}
73 changes: 10 additions & 63 deletions lib/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Emitter } from "atom";
import { observable, action } from "mobx";
import { is, Map as ImmutableMap } from "immutable";

import { log } from "./utils";
import store from "./store";
Expand All @@ -13,7 +14,7 @@ export default class Kernel {
@observable executionState = "loading";
@observable inspector = {
visible: false,
HTML: "",
bundle: new ImmutableMap(),
height: 240
};

Expand Down Expand Up @@ -47,11 +48,11 @@ export default class Kernel {
this.inspector.height = height;
}

@action setInspectorResult(HTML: string) {
if (this.inspector.visible === true && this.inspector.HTML === HTML) {
@action setInspectorResult(bundle: ImmutableMap<string, any>) {
if (this.inspector.visible === true && is(this.inspector.bundle, bundle)) {
this.setInspectorVisibility(false);
} else if (HTML) {
this.inspector.HTML = HTML;
} else if (bundle.size !== 0) {
this.inspector.bundle = bundle;
this.setInspectorVisibility(true);
}
}
Expand All @@ -76,7 +77,6 @@ export default class Kernel {
throw new Error("Kernel: interrupt method not implemented");
}

/* eslint-disable no-unused-vars */
shutdown() {
throw new Error("Kernel: shutdown method not implemented");
}
Expand All @@ -101,7 +101,6 @@ export default class Kernel {
throw new Error("Kernel: inspect method not implemented");
}

/* eslint-enable no-unused-vars */
_parseIOMessage(message: Message) {
let result = this._parseDisplayIOMessage(message);

Expand Down Expand Up @@ -131,7 +130,6 @@ export default class Kernel {
return null;
}

/* eslint-disable camelcase*/
_parseResultIOMessage(message: Message) {
const { msg_type } = message.header;

Expand All @@ -140,65 +138,20 @@ export default class Kernel {
}
return null;
}
/* eslint-enable camelcase*/

_parseDataMime(data: Object) {
if (!data) {
return null;
}

const mime = this._getMimeType(data);

if (!mime) {
return null;
}

let result;
if (mime === "text/plain") {
result = {
data: {
"text/plain": data[mime]
},
type: "text",
stream: "pyout"
};
} else {
result = {
data: {},
type: mime,
stream: "pyout"
};
result.data[mime] = data[mime];
}
const result = {
data: data,
stream: "pyout"
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Classic message spec before v5. 😉


return result;
}

_getMimeType(data: Object) {
const imageMimes = Object.getOwnPropertyNames(data).filter(mime =>
mime.startsWith("image/"));

let mime;
if ({}.hasOwnProperty.call(data, "text/html")) {
mime = "text/html";
} else if ({}.hasOwnProperty.call(data, "image/svg+xml")) {
mime = "image/svg+xml";
} else if (!(imageMimes.length === 0)) {
mime = imageMimes[0];
} else if ({}.hasOwnProperty.call(data, "text/markdown")) {
mime = "text/markdown";
} else if ({}.hasOwnProperty.call(data, "application/pdf")) {
mime = "application/pdf";
} else if ({}.hasOwnProperty.call(data, "text/latex")) {
mime = "text/latex";
} else if ({}.hasOwnProperty.call(data, "text/plain")) {
mime = "text/plain";
}

return mime;
}

/* eslint-disable camelcase*/
_parseErrorIOMessage(message: Message) {
const { msg_type } = message.header;

Expand All @@ -208,7 +161,6 @@ export default class Kernel {

return null;
}
/* eslint-enable camelcase*/

_parseErrorMessage(message: Message) {
let errorString;
Expand All @@ -224,7 +176,6 @@ export default class Kernel {
data: {
"text/plain": errorString
},
type: "text",
stream: "error"
};

Expand All @@ -240,7 +191,6 @@ export default class Kernel {
? message.content.text
: message.content.data
},
type: "text",
stream: message.content.name
};

Expand All @@ -256,7 +206,6 @@ export default class Kernel {
? message.content.text
: message.content.data
},
type: "text",
stream: "stdout"
};

Expand All @@ -272,7 +221,6 @@ export default class Kernel {
? message.content.text
: message.content.data
},
type: "text",
stream: "stderr"
};
}
Expand All @@ -284,7 +232,6 @@ export default class Kernel {
if (message.header.msg_type === "execute_input") {
return {
data: message.content.execution_count,
type: "number",
stream: "execution_count"
};
}
Expand Down
Loading