@codemirror/state#StateField TypeScript Examples

The following examples show how to use @codemirror/state#StateField. 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: EmbedDecoration.ts    From obsidian-plantuml with MIT License 6 votes vote down vote up
////////////////
// Utility Code
////////////////

// Generic helper for creating pairs of editor state fields and
// effects to model imperatively updated decorations.
// source: https://github.com/ChromeDevTools/devtools-frontend/blob/8f098d33cda3dd94b53e9506cd3883d0dccc339e/front_end/panels/sources/DebuggerPlugin.ts#L1722
function defineStatefulDecoration(): {
    update: StateEffectType<DecorationSet>;
    field: StateField<DecorationSet>;
} {
    const update = StateEffect.define<DecorationSet>();
    const field = StateField.define<DecorationSet>({
        create(): DecorationSet {
            return Decoration.none;
        },
        update(deco, tr): DecorationSet {
            return tr.effects.reduce((deco, effect) => (effect.is(update) ? effect.value : deco), deco.map(tr.changes));
        },
        provide: field => EditorView.decorations.from(field),
    });
    return {update, field};
}
Example #2
Source File: code.tsx    From nota with MIT License 6 votes vote down vote up
highlightField = StateField.define<DecorationSet>({
  create() {
    return Decoration.none;
  },
  update(highlights, tr) {
    highlights = highlights.map(tr.changes);
    for (let e of tr.effects) {
      if (e.is(addHighlight)) {
        let { to, from, color } = e.value;
        let mark = Decoration.mark({
          class: `cm-highlight bgcolor-${color}`,
        });
        highlights = highlights.update({
          add: [mark.range(from, to)],
        });
      } else if (e.is(clearHighlights)) {
        return highlights.update({ filter: _ => false });
      }
    }
    return highlights;
  },
  provide: f => EditorView.decorations.from(f),
})
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);
  }