diff --git a/js/lab/src/global.env.ts b/js/lab/src/global.env.ts index f253326d70..a453d880ba 100644 --- a/js/lab/src/global.env.ts +++ b/js/lab/src/global.env.ts @@ -17,3 +17,15 @@ declare interface Window { beakerx: any } + +type Proxy = { + get(): T; + set(value: T): void; +} + +interface ProxyConstructor { + revocable(target: T, handler: ProxyHandler): { proxy: T; revoke: () => void; }; + new (target: T, handler: ProxyHandler): T; +} + +declare var Proxy: ProxyConstructor; diff --git a/js/lab/src/plugin/autotranslation.ts b/js/lab/src/plugin/autotranslation.ts new file mode 100644 index 0000000000..6fb7589a7f --- /dev/null +++ b/js/lab/src/plugin/autotranslation.ts @@ -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. + */ + +/// + +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 { + 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(beakerxInstance, handler); + } +} diff --git a/js/lab/src/plugin/comm.ts b/js/lab/src/plugin/comm.ts index ec1799ee8a..5a9fa3c6ac 100644 --- a/js/lab/src/plugin/comm.ts +++ b/js/lab/src/plugin/comm.ts @@ -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'; @@ -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) => { diff --git a/js/lab/src/plugin/index.ts b/js/lab/src/plugin/index.ts index e59934fc91..d21f9b6765 100644 --- a/js/lab/src/plugin/index.ts +++ b/js/lab/src/plugin/index.ts @@ -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) { @@ -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) { diff --git a/js/notebook/src/types/global.env.d.ts b/js/notebook/src/types/global.env.d.ts index a924f45f39..c0dbfab4b0 100644 --- a/js/notebook/src/types/global.env.d.ts +++ b/js/notebook/src/types/global.env.d.ts @@ -37,18 +37,6 @@ declare interface NumberConstructor { isFinite: (number: number) => boolean } -type Proxy = { - get(): T; - set(value: T): void; -} - -interface ProxyConstructor { - revocable(target: T, handler: ProxyHandler): { proxy: T; revoke: () => void; }; - new (target: T, handler: ProxyHandler): T; -} - -declare var Proxy: ProxyConstructor; - declare interface Array { from: (arrayLike: any[]) => any[] }