How to use the @typewriter/editor.diff function in @typewriter/editor

To help you get started, we’ve selected a few @typewriter/editor examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github typewriter-editor / typewriter / packages / modules / src / input.ts View on Github external
function getTextChange(list: MutationRecord[]): Delta | null {
      const mutation = getTextChangeMutation(list);
      if (!mutation || mutation.oldValue == null || mutation.target.nodeValue == null) return null;

      const change = editor.delta();
      const index = getNodeIndex(root, paper, mutation.target);
      change.retain(index);
      const diffs = diff(mutation.oldValue.replace(/\xA0/g, ' '), mutation.target.nodeValue.replace(/\xA0/g, ' '));
      diffs.forEach(([ action, string ]) => {
        if (action === diff.EQUAL) change.retain(string.length);
        else if (action === diff.DELETE) change.delete(string.length);
        else if (action === diff.INSERT) {
          change.insert(string, editor.activeFormats);
        }
      });
      change.chop();
      return change;
    }

@typewriter/editor

The core editor for typewriter. Manages the data model and updates. Can be run headless.

MIT
Latest version published 4 years ago

Package Health Score

51 / 100
Full package analysis