From a9acc64ad32fa453747e71f2d41011f9743a4d33 Mon Sep 17 00:00:00 2001 From: Mariusz Jurowicz Date: Mon, 16 Jul 2018 22:55:46 +0200 Subject: [PATCH] #7670 add js autotranslation support for Lab (#7672) * #7670 add js autotranslation support for Lab * #7670 open autotranslation comm after registering extension --- js/lab/src/global.env.ts | 12 ++++++ js/lab/src/plugin/autotranslation.ts | 51 ++++++++++++++++++++++++ js/lab/src/plugin/comm.ts | 4 ++ js/lab/src/plugin/index.ts | 4 ++ js/lab/src/plugin/initializationCells.ts | 24 ++++++----- 5 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 js/lab/src/plugin/autotranslation.ts 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..3d40fc9a37 --- /dev/null +++ b/js/lab/src/plugin/autotranslation.ts @@ -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. + */ + +/// + +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; + 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(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 f94a33803c..fb1a47a356 100644 --- a/js/lab/src/plugin/index.ts +++ b/js/lab/src/plugin/index.ts @@ -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) { @@ -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) { diff --git a/js/lab/src/plugin/initializationCells.ts b/js/lab/src/plugin/initializationCells.ts index d8c17c9f83..aa50cbfc00 100644 --- a/js/lab/src/plugin/initializationCells.ts +++ b/js/lab/src/plugin/initializationCells.ts @@ -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[] {