@codemirror/state#Transaction TypeScript Examples

The following examples show how to use @codemirror/state#Transaction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: editor.ts    From starboard-notebook with Mozilla Public License 2.0 6 votes vote down vote up
function tabKeyRun(t: { state: EditorState; dispatch: (transaction: Transaction) => void }): boolean {
  if (t.state.selection.ranges.some((r) => !r.empty)) {
    return indentMore({ state: t.state, dispatch: t.dispatch });
  }
  // So we can tab past the editor we don't tab if the cursor is at the very start of the document.
  // Note: this can be improved, we could instead see if we have pressed any keys since focusing the editor and decide
  // based on that.
  const r = t.state.selection.ranges[0];
  if (r && r.to === 0 && r.from === 0) {
    return false;
  }
  t.dispatch(
    t.state.update(t.state.replaceSelection("\t"), {
      scrollIntoView: true,
      annotations: Transaction.userEvent.of("input"),
    })
  );
  return true;
}
Example #2
Source File: PumlView.ts    From obsidian-plantuml with MIT License 6 votes vote down vote up
function syncDispatch(from: number) {
    return (tr: Transaction) => {
        views[from].update([tr]);
        if (tr.changes && tr.annotation && !tr.changes.empty && !tr.annotation(syncAnnotation)) {
            for (let i = 0; i < views.length; i++) {
                if(i !== from) {
                    views[i].dispatch({
                        changes: tr.changes,
                        annotations: syncAnnotation.of(true)
                    })
                }
            }

        }

    }
}
Example #3
Source File: main.ts    From better-word-count with MIT License 5 votes vote down vote up
// activeLeafChange(leaf: WorkspaceLeaf) {
  //   if (!(leaf.view.getViewType() === "markdown")) {
  //     this.barManager.updateAltStatusBar();
  //   }
  // }

  // async saveSettings(): Promise<void> {
  //   await this.saveData(this.settings);
  // }

  // initLeaf(): void {
  //   if (this.app.workspace.getLeavesOfType(VIEW_TYPE_STATS).length) {
  //     return;
  //   }
  //   this.app.workspace.getRightLeaf(false).setViewState({
  //     type: VIEW_TYPE_STATS,
  //   });
  // }


  createCMExtension() {

    const cmStateField = StateField.define<DecorationSet>({
      create(state: EditorState) {
        return  Decoration.none;
      },
      update(effects: DecorationSet, tr: Transaction) {
        let text = "";
        const selection = tr.newSelection.main;
        if (selection.empty) {
          const textIter = tr.newDoc.iter();
          while (!textIter.done) {
            text = text + textIter.next().value;
          }
        } else {
          const textIter = tr.newDoc.iterRange(selection.from, selection.to);
          while (!textIter.done) {
            text = text + textIter.next().value;
          }
        }

        BetterWordCount.updateStatusBar(text);
        
        return effects;
      },

      provide: (f: any) => EditorView.decorations.from(f),
    });
    
    this.registerEditorExtension(cmStateField);
  }