Skip to content

Commit

Permalink
#7670 add js autotranslation support for Lab
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Jurowicz committed Jul 10, 2018
1 parent d4c39f6 commit 0edcc20
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 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;
50 changes: 50 additions & 0 deletions js/lab/src/plugin/autotranslation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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;
});

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 { 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 @@ -59,6 +61,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
12 changes: 0 additions & 12 deletions js/notebook/src/types/global.env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ declare interface NumberConstructor {
isFinite: (number: number) => boolean
}

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;

declare interface Array<T> {
from: (arrayLike: any[]) => any[]
}
Expand Down

0 comments on commit 0edcc20

Please sign in to comment.