Skip to content

Commit

Permalink
#7670 add js autotranslation support for Lab (#7672)
Browse files Browse the repository at this point in the history
* #7670 add js autotranslation support for Lab

* #7670 open autotranslation comm after registering extension
  • Loading branch information
Mariusz Jurowicz authored and LeeTZ committed Jul 16, 2018
1 parent d9e1e3e commit a9acc64
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 11 deletions.
12 changes: 12 additions & 0 deletions js/lab/src/global.env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@
declare interface Window {
beakerx: any
}

type Proxy<T> = {
get(): T;
set(value: T): void;
}

interface ProxyConstructor {
revocable<T extends object>(target: T, handler: ProxyHandler<T>): { proxy: T; revoke: () => void; };
new <T extends object>(target: T, handler: ProxyHandler<T>): T;
}

declare var Proxy: ProxyConstructor;
51 changes: 51 additions & 0 deletions js/lab/src/plugin/autotranslation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/// <reference path='../global.env.ts'/>

import {BEAKER_AUTOTRANSLATION} from "./comm";

export namespace Autotranslation {
export const LOCK_PROXY = 'LOCK_PROXY';
export const TABLE_FOCUSED = 'tableFocused';

export function proxify(beakerxInstance: any, kernelInstance): Proxy<any> {
let autotranslationComm;

kernelInstance.connectToComm(BEAKER_AUTOTRANSLATION)['then'](comm => {
autotranslationComm = comm;
autotranslationComm.open();
});

const handler = {
get(obj, prop) {
return prop in obj ? obj[prop] : undefined;
},

set(obj, prop, value) {
obj[prop] = value;

if (prop !== LOCK_PROXY && prop !== TABLE_FOCUSED && !window.beakerx[LOCK_PROXY]) {
autotranslationComm.send({ name: prop, value });
}

return true;
}
};

return new Proxy<any>(beakerxInstance, handler);
}
}
4 changes: 4 additions & 0 deletions js/lab/src/plugin/comm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { sendJupyterCodeCells, getCodeCellsByTag } from './codeCells';
import {messageData, messageState} from '../interface/messageData';
import { Kernel } from "@jupyterlab/services";
import { CodeCell } from '@jupyterlab/cells';
import {Autotranslation} from "./autotranslation";
import LOCK_PROXY = Autotranslation.LOCK_PROXY;

export const BEAKER_GETCODECELLS = 'beakerx.getcodecells';
export const BEAKER_AUTOTRANSLATION = 'beakerx.autotranslation';
Expand Down Expand Up @@ -48,7 +50,9 @@ const getMsgHandlers = (
[BEAKER_AUTOTRANSLATION]: (msg) => {
const state: messageState = msg.content.data.state;

window.beakerx[LOCK_PROXY] = true;
window.beakerx[state.name] = JSON.parse(state.value);
window.beakerx[LOCK_PROXY] = false;
},

[BEAKER_TAG_RUN]: (msg) => {
Expand Down
4 changes: 4 additions & 0 deletions js/lab/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { registerCommTargets } from './comm';
import {extendHighlightModes, registerCommentOutCmd} from './codeEditor';
import { enableInitializationCellsFeature } from './initializationCells';
import UIOptionFeaturesHelper from "./UIOptionFeaturesHelper";
import {Autotranslation} from "./autotranslation";
import proxify = Autotranslation.proxify;

function displayHTML(widget: Widget, html: string): void {
if (!widget.node || !html) {
Expand Down Expand Up @@ -60,6 +62,8 @@ class BeakerxExtension implements DocumentRegistry.WidgetExtension {
registerCommentOutCmd(panel);
registerCommTargets(panel, context);

window.beakerx = proxify(window.beakerx, context.session.kernel);

const originalProcessFn = app.commands.processKeydownEvent;
app.commands.processKeydownEvent = (event) => {
if (window.beakerx.tableFocused) {
Expand Down
24 changes: 13 additions & 11 deletions js/lab/src/plugin/initializationCells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,19 @@ export function enableInitializationCellsFeature(panel: NotebookPanel): void {
}

export function runInitCells(panel: NotebookPanel, options: IInitCellsOptions): void {
const cells: CodeCell[] = getInitCells(panel);

handleUntrustedKernelInitCells(cells, options);

if (!canExecuteInitCells(panel, options, cells)) {
return;
}

console.log(logPrefix, 'running all initialization cells');
cells.forEach((cell: CodeCell) => CodeCell.execute(cell, panel.session));
console.log(logPrefix, `finished running ${cells.length} initialization cell${(cells.length !== 1 ? 's' : '')}`);
panel.ready.then(() => {
const cells: CodeCell[] = getInitCells(panel);

handleUntrustedKernelInitCells(cells, options);

if (!canExecuteInitCells(panel, options, cells)) {
return;
}

console.log(logPrefix, 'running all initialization cells');
cells.forEach((cell: CodeCell) => CodeCell.execute(cell, panel.session));
console.log(logPrefix, `finished running ${cells.length} initialization cell${(cells.length !== 1 ? 's' : '')}`);
});
}

export function getInitCells(panel: NotebookPanel): CodeCell[] {
Expand Down

0 comments on commit a9acc64

Please sign in to comment.