slate#RemoveTextOperation TypeScript Examples

The following examples show how to use slate#RemoveTextOperation. 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: utils.ts    From slate-ot with MIT License 6 votes vote down vote up
makeOp = {
  insertText: (
    path: Path,
    offset: number,
    text: string
  ): InsertTextOperation => {
    return {
      type: 'insert_text',
      path,
      offset,
      text,
    };
  },

  removeText: (
    path: Path,
    offset: number,
    text: string
  ): RemoveTextOperation => {
    return {
      type: 'remove_text',
      path,
      offset,
      text,
    };
  },

  insertNode: (path: Path, node: Node): InsertNodeOperation => {
    return {
      type: 'insert_node',
      path,
      node,
    };
  },

  removeNode: (path: Path, node: Node): RemoveNodeOperation => {
    return {
      type: 'remove_node',
      path,
      node,
    };
  },

  splitNode: (path: Path, position: number): SplitNodeOperation => {
    return {
      type: 'split_node',
      path,
      position,
      target: null,
      properties: {},
    };
  },

  mergeNode: (path: Path, position: number): MergeNodeOperation => {
    return {
      type: 'merge_node',
      path,
      position,
      target: null,
      properties: {},
    };
  },

  moveNode: (path: Path, newPath: Path): MoveNodeOperation => {
    return {
      type: 'move_node',
      path,
      newPath,
    };
  },

  setNode: (path: Path, newProperties: Partial<Node>): SetNodeOperation => {
    return {
      type: 'set_node',
      path,
      properties: {},
      newProperties,
    };
  },
}
Example #2
Source File: transRemoveText.ts    From slate-ot with MIT License 4 votes vote down vote up
transRemoveText = (
  leftOp: RemoveTextOperation,
  rightOp: Operation,
  _side: 'left' | 'right'
): RemoveTextOperation[] => {
  switch (rightOp.type) {
    case 'insert_text': {
      if (!Path.equals(leftOp.path, rightOp.path)) {
        return [leftOp];
      }

      if (leftOp.offset + leftOp.text.length <= rightOp.offset) {
        return [leftOp];
      }

      if (rightOp.offset <= leftOp.offset) {
        return [
          {
            ...leftOp,
            offset: leftOp.offset + rightOp.text.length,
          },
        ];
      }

      const intersectingIndex = rightOp.offset - leftOp.offset;
      const leftText = leftOp.text.slice(0, intersectingIndex);
      const rightText = leftOp.text.slice(intersectingIndex);
      return [
        {
          ...leftOp,
          text: leftText + rightOp.text + rightText,
        },
      ];
    }

    case 'remove_text': {
      if (!Path.equals(leftOp.path, rightOp.path)) {
        return [leftOp];
      }
      if (leftOp.offset + leftOp.text.length <= rightOp.offset) {
        return [leftOp];
      }
      if (rightOp.offset + rightOp.text.length <= leftOp.offset) {
        return [
          {
            ...leftOp,
            offset: leftOp.offset - rightOp.text.length,
          },
        ];
      }

      // leftText and rightText both come from leftOp
      const leftTextEnd = Math.max(rightOp.offset - leftOp.offset, 0);
      const leftText = leftOp.text.slice(0, leftTextEnd);
      const rightTextStart = Math.min(
        leftOp.text.length,
        rightOp.offset + rightOp.text.length - leftOp.offset
      );
      const rightText = leftOp.text.slice(rightTextStart);
      return [
        {
          ...leftOp,
          offset: Math.min(leftOp.offset, rightOp.offset),
          text: leftText + rightText,
        },
      ];
    }

    case 'split_node': {
      if (Path.equals(leftOp.path, rightOp.path)) {
        // text to remove all within the former segment
        if (leftOp.offset + leftOp.text.length <= rightOp.position) {
          return [leftOp];
        }

        // text to remove all within the latter segment
        if (leftOp.offset >= rightOp.position) {
          return [
            {
              ...leftOp,
              path: Path.next(rightOp.path),
              offset: leftOp.offset - rightOp.position,
            },
          ];
        }

        // text to remove in both segments
        return [
          {
            ...leftOp,
            text: leftOp.text.slice(0, rightOp.position - leftOp.offset),
          },
          {
            ...leftOp,
            path: Path.next(rightOp.path),
            offset: 0,
            text: leftOp.text.slice(rightOp.position - leftOp.offset),
          },
        ];
      }

      return <RemoveTextOperation[]>pathTransform(leftOp, rightOp);
    }

    case 'merge_node': {
      if (Path.equals(leftOp.path, rightOp.path)) {
        return [
          {
            ...leftOp,
            path: Path.previous(rightOp.path),
            offset: leftOp.offset + rightOp.position,
          },
        ];
      }

      return <RemoveTextOperation[]>pathTransform(leftOp, rightOp);
    }

    // insert_node
    // remove_node
    // move_node
    // set_node
    default:
      return <RemoveTextOperation[]>pathTransform(leftOp, rightOp);
  }
}