當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript apputils.IStateDB類代碼示例

本文整理匯總了TypeScript中@jupyterlab/apputils.IStateDB的典型用法代碼示例。如果您正苦於以下問題:TypeScript IStateDB類的具體用法?TypeScript IStateDB怎麽用?TypeScript IStateDB使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了IStateDB類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: activate

/**
 * Activate the editor tracker plugin.
 */
function activate(app: JupyterLab, registry: IDocumentRegistry, restorer: ILayoutRestorer, editorServices: IEditorServices, state: IStateDB, launcher: ILauncher | null): IEditorTracker {
  const factory = new FileEditorFactory({
    editorServices,
    factoryOptions: {
      name: FACTORY,
      fileExtensions: ['*'],
      defaultFor: ['*']
    }
  });
  const { commands, shell } = app;
  const id = 'editor:settings';
  const tracker = new InstanceTracker<FileEditor>({
    namespace: 'editor',
    shell
  });

  let lineNumbers = true;
  let wordWrap = true;

  // Handle state restoration.
  restorer.restore(tracker, {
    command: 'file-operations:open',
    args: widget => ({ path: widget.context.path, factory: FACTORY }),
    name: widget => widget.context.path
  });

  // Fetch the initial state of the settings.
  state.fetch(id).then(settings => {
    if (!settings) {
      return;
    }
    if (typeof settings['wordWrap'] === 'string') {
      commands.execute(CommandIDs.wordWrap, settings);
    }
    if (typeof settings['lineNumbers'] === 'string') {
      commands.execute(CommandIDs.lineNumbers, settings);
    }
  });

  /**
   * Save the editor widget settings state.
   */
  function saveState(): Promise<void> {
    return state.save(id, { lineNumbers, wordWrap });
  }

  factory.widgetCreated.connect((sender, widget) => {
    widget.title.icon = EDITOR_ICON_CLASS;
    // Notify the instance tracker if restore data needs to update.
    widget.context.pathChanged.connect(() => { tracker.save(widget); });
    tracker.add(widget);
    widget.editor.lineNumbers = lineNumbers;
    widget.editor.wordWrap = wordWrap;
  });
  registry.addWidgetFactory(factory);

  /**
   * Handle the settings of new widgets.
   */
  tracker.widgetAdded.connect((sender, widget) => {
    let editor = widget.editor;
    editor.lineNumbers = lineNumbers;
    editor.wordWrap = wordWrap;
  });

  /**
   * Toggle editor line numbers
   */
  function toggleLineNums(args: JSONObject): Promise<void> {
    lineNumbers = !lineNumbers;
    tracker.forEach(widget => {
      widget.editor.lineNumbers = lineNumbers;
    });
    return saveState();
  }

  /**
   * Toggle editor line wrap
   */
  function toggleLineWrap(args: JSONObject): Promise<void> {
    wordWrap = !wordWrap;
    tracker.forEach(widget => {
      widget.editor.wordWrap = wordWrap;
    });
    return saveState();
  }

  /**
   * A test for whether the tracker has an active widget.
   */
  function hasWidget(): boolean {
    return tracker.currentWidget !== null;
  }

  commands.addCommand(CommandIDs.lineNumbers, {
    execute: toggleLineNums,
    isEnabled: hasWidget,
//.........這裏部分代碼省略.........
開發者ID:eskirk,項目名稱:jupyterlab,代碼行數:101,代碼來源:index.ts

示例2: activateCellTools

/**
 * Activate the cell tools extension.
 */
function activateCellTools(app: JupyterLab, tracker: INotebookTracker, editorServices: IEditorServices, state: IStateDB): Promise<ICellTools> {
  const id = 'cell-tools';
  const celltools = new CellTools({ tracker });
  const activeCellTool = new CellTools.ActiveCellTool();
  const slideShow = CellTools.createSlideShowSelector();
  const nbConvert = CellTools.createNBConvertSelector();
  const editorFactory = editorServices.factoryService.newInlineEditor
    .bind(editorServices.factoryService);
  const metadataEditor = new CellTools.MetadataEditorTool({ editorFactory });

  // Create message hook for triggers to save to the database.
  const hook = (sender: any, message: Message): boolean => {
    switch (message) {
      case Widget.Msg.ActivateRequest:
        state.save(id, { open: true });
        break;
      case Widget.Msg.AfterHide:
      case Widget.Msg.CloseRequest:
        state.remove(id);
        break;
      default:
        break;
    }
    return true;
  };

  celltools.title.label = 'Cell Tools';
  celltools.id = id;
  celltools.addItem({ tool: activeCellTool, rank: 1 });
  celltools.addItem({ tool: slideShow, rank: 2 });
  celltools.addItem({ tool: nbConvert, rank: 3 });
  celltools.addItem({ tool: metadataEditor, rank: 4 });
  MessageLoop.installMessageHook(celltools, hook);

  // Wait until the application has finished restoring before rendering.
  Promise.all([state.fetch(id), app.restored]).then(([args]) => {
    const open = (args && args['open'] as boolean) || false;

    // After initial restoration, check if the cell tools should render.
    if (tracker.size) {
      app.shell.addToLeftArea(celltools);
      if (open) {
        app.shell.activateById(celltools.id);
      }
    }

    // For all subsequent widget changes, check if the cell tools should render.
    app.shell.currentChanged.connect((sender, args) => {
      // If there are any open notebooks, add cell tools to the side panel if
      // it is not already there.
      if (tracker.size) {
        if (!celltools.isAttached) {
          app.shell.addToLeftArea(celltools);
        }
        return;
      }
      // If there are no notebooks, close cell tools.
      celltools.close();
    });
  });

  return Promise.resolve(celltools);
}
開發者ID:charnpreetsingh185,項目名稱:jupyterlab,代碼行數:66,代碼來源:index.ts

示例3: activateEditorCommands

/**
 * Set up the editor widget menu and commands.
 */
function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMenu: IMainMenu, palette: ICommandPalette, state: IStateDB): void {
  let { commands } = app;
  let theme: string = CodeMirrorEditor.DEFAULT_THEME;
  let keyMap: string = 'default';
  let matchBrackets = false;
  let id = 'codemirror:settings';

  // Fetch the initial state of the settings.
  state.fetch(id).then(settings => {
    if (!settings) {
      return;
    }
    if (typeof settings['theme'] === 'string') {
      commands.execute(CommandIDs.changeTheme, settings);
    }
    if (typeof settings['keyMap'] === 'string') {
      commands.execute(CommandIDs.changeKeyMap, settings);
    }
    if (typeof settings['matchBrackets'] === 'boolean') {
      if (settings['matchBrackets'] !== matchBrackets) {
        commands.execute(CommandIDs.matchBrackets);
      }
    }
  });

  /**
   * Save the codemirror settings state.
   */
  function saveState(): Promise<void> {
    return state.save(id, { theme, keyMap, matchBrackets });
  }

  /**
   * Handle the settings of new widgets.
   */
  tracker.widgetAdded.connect((sender, widget) => {
    if (widget.editor instanceof CodeMirrorEditor) {
      let cm = widget.editor.editor;
      cm.setOption('keyMap', keyMap);
      cm.setOption('theme', theme);
      cm.setOption('matchBrackets', matchBrackets);
    }
  });

  /**
   * A test for whether the tracker has an active widget.
   */
  function hasWidget(): boolean {
    return tracker.currentWidget !== null;
  }

  /**
   * Toggle editor matching brackets
   */
  function toggleMatchBrackets(): Promise<void> {
    matchBrackets = !matchBrackets;
    tracker.forEach(widget => {
      let editor = widget.editor;
      if (editor instanceof CodeMirrorEditor) {
        let cm = editor.editor;
        cm.setOption('matchBrackets', matchBrackets);
      }
    });
    return saveState();
  }

  /**
   * Create a menu for the editor.
   */
  function createMenu(): Menu {
    let menu = new Menu({ commands });
    let themeMenu = new Menu({ commands });
    let keyMapMenu = new Menu({ commands });

    menu.title.label = 'Editor';
    themeMenu.title.label = 'Theme';
    keyMapMenu.title.label = 'Key Map';

    commands.addCommand(CommandIDs.changeTheme, {
      label: args => args['theme'] as string,
      execute: args => {
        theme = args['theme'] as string || CodeMirrorEditor.DEFAULT_THEME;
        tracker.forEach(widget => {
          if (widget.editor instanceof CodeMirrorEditor) {
            let cm = widget.editor.editor;
            cm.setOption('theme', theme);
          }
        });
        return saveState();
      },
      isEnabled: hasWidget,
      isToggled: args => { return args['theme'] === theme; }
    });


    commands.addCommand(CommandIDs.changeKeyMap, {
      label: args => {
//.........這裏部分代碼省略.........
開發者ID:faricacarroll,項目名稱:jupyterlab,代碼行數:101,代碼來源:index.ts

示例4: switch

 const hook = (sender: any, message: Message): boolean => {
   switch (message) {
     case Widget.Msg.ActivateRequest:
       state.save(id, { open: true });
       break;
     case Widget.Msg.AfterHide:
     case Widget.Msg.CloseRequest:
       state.remove(id);
       break;
     default:
       break;
   }
   return true;
 };
開發者ID:charnpreetsingh185,項目名稱:jupyterlab,代碼行數:14,代碼來源:index.ts

示例5: saveState

 /**
  * Save the editor widget settings state.
  */
 function saveState(): Promise<void> {
   return state.save(id, { lineNumbers, wordWrap });
 }
開發者ID:eskirk,項目名稱:jupyterlab,代碼行數:6,代碼來源:index.ts

示例6: saveState

 /**
  * Save the codemirror settings state.
  */
 function saveState(): Promise<void> {
   return state.save(id, { theme, keyMap, matchBrackets });
 }
開發者ID:faricacarroll,項目名稱:jupyterlab,代碼行數:6,代碼來源:index.ts


注:本文中的@jupyterlab/apputils.IStateDB類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。