How to use the @sanity/block-tools.editorValueToBlocks function in @sanity/block-tools

To help you get started, we’ve selected a few @sanity/block-tools 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 sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / utils / createOperationToPatches.ts View on Github external
function mergeNodePatch(
    operation: Operation,
    afterValue: SlateValue,
    formBuilderValue: FormBuilderValue[] | null
  ) {
    const patches = []
    // Value is undefined
    if (!formBuilderValue) {
      const blocks = editorValueToBlocks(afterValue.toJSON(VALUE_TO_JSON_OPTS), blockContentType)
      return [setIfMissing(blocks), set(blocks)]
    }
    // Value is empty
    if (formBuilderValue && formBuilderValue.length === 0) {
      return [set(editorValueToBlocks(afterValue.toJSON(VALUE_TO_JSON_OPTS), blockContentType), [])]
    }
    if (operation.path.size === 1) {
      const mergedBlock = toBlock(afterValue, operation.path.get(0))
      const targetBlock = toBlock(afterValue, operation.path.get(0) - 1)
      patches.push(
        unset([
          {
            _key: mergedBlock._key
          }
        ])
      )
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / utils / createOperationToPatches.ts View on Github external
function insertNodePatch(
    operation: Operation,
    beforeValue: SlateValue,
    afterValue: SlateValue,
    formBuilderValue: FormBuilderValue[] | null
  ) {
    // Don't send anything if this is just a placeholder
    if (
      afterValue.document.nodes.size > 0 &&
      afterValue.document.nodes.every(node => node.data.get('placeholder'))
    ) {
      return []
    }
    // Value is undefined
    if (!formBuilderValue) {
      const blocks = editorValueToBlocks(afterValue.toJSON(VALUE_TO_JSON_OPTS), blockContentType)
      return [setIfMissing(blocks), set(blocks, [])]
    }
    // Value is empty
    if (formBuilderValue && formBuilderValue.length === 0) {
      return [set(editorValueToBlocks(afterValue.toJSON(VALUE_TO_JSON_OPTS), blockContentType), [])]
    }
    const block = toBlock(afterValue, operation.path.get(0))
    if (operation.path.size === 1) {
      let position: InsertPosition = 'after'
      let positionPath
      if (operation.path.get(0) === 0) {
        const firstNode = beforeValue.document.nodes.first()
        positionPath = firstNode ? [{_key: firstNode.key}] : [0]
        position = 'before'
      } else {
        positionPath = [{_key: beforeValue.document.nodes.get(operation.path.get(0) - 1).key}]
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / utils / createOperationToPatches.ts View on Github external
function splitNodePatch(
    operation: Operation,
    afterValue: SlateValue,
    formBuilderValue: FormBuilderValue[] | null
  ) {
    // Value is undefined
    if (!formBuilderValue) {
      const blocks = editorValueToBlocks(afterValue.toJSON(VALUE_TO_JSON_OPTS), blockContentType)
      return [setIfMissing(blocks), set(blocks)]
    }
    // Value is empty
    if (formBuilderValue && formBuilderValue.length === 0) {
      return [set(editorValueToBlocks(afterValue.toJSON(VALUE_TO_JSON_OPTS), blockContentType), [])]
    }
    const patches = []
    const splitBlock = toBlock(afterValue, operation.path.get(0))
    if (operation.path.size === 1) {
      patches.push(set(splitBlock, [{_key: splitBlock._key}]))
      const newBlock = toBlock(afterValue, operation.path.get(0) + 1)
      patches.push(insert([newBlock], 'after', [{_key: splitBlock._key}]))
    }
    if (operation.path.size > 1) {
      patches.push(set(splitBlock, [{_key: splitBlock._key}]))
    }
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / utils / changeToPatches.js View on Github external
blocks: Block[],
  blockContentType
) {
  const operationIndex = operations.indexOf(operation)
  const nextOperation = operations.get(operationIndex + 1)
  if (
    nextOperation &&
    nextOperation.type === 'merge_node' &&
    nextOperation.path.length === 1 &&
    nextOperation.path[0] === operation.path[0] &&
    operation.type === 'set_node' &&
    isEqual(Object.keys(operation.properties), ['data'])
  ) {
    return []
  }
  const appliedBlocks = editorValueToBlocks(
    change.applyOperations([operation]).value.toJSON(VALUE_TO_JSON_OPTS),
    blockContentType
  )
  // Value is undefined
  if (!blocks && appliedBlocks) {
    return setIfMissing(appliedBlocks)
  }
  // Value is empty
  if (blocks && blocks.length === 0) {
    return set(appliedBlocks, [])
  }
  const changedBlock = appliedBlocks[operation.path[0]]
  setKey(changedBlock._key, changedBlock)
  return set(changedBlock, [{_key: blocks[operation.path[0]]._key}])
}
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / utils / createOperationToPatches.ts View on Github external
formBuilderValue: FormBuilderValue[] | null
  ) {
    if (
      afterValue.document.nodes.size > 0 &&
      afterValue.document.nodes.every(node => node.data.get('placeholder'))
    ) {
      return [unset([])]
    }
    // Value is undefined
    if (!formBuilderValue) {
      const blocks = editorValueToBlocks(afterValue.toJSON(VALUE_TO_JSON_OPTS), blockContentType)
      return [setIfMissing(blocks), set(blocks)]
    }
    // Value is empty
    if (formBuilderValue && formBuilderValue.length === 0) {
      return [set(editorValueToBlocks(afterValue.toJSON(VALUE_TO_JSON_OPTS), blockContentType), [])]
    }
    const block = toBlock(afterValue, operation.path.get(0))
    return [set(block, [{_key: block._key}])]
  }
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / utils / changeToPatches.js View on Github external
function setNodePatchSimple(
  change: Change,
  operation: Operation,
  blocks: Block[],
  blockContentType
) {
  const appliedBlocks = editorValueToBlocks(
    change.applyOperations([operation]).value.toJSON(VALUE_TO_JSON_OPTS),
    blockContentType
  )
  // Value is undefined
  if (!blocks && appliedBlocks) {
    return setIfMissing(appliedBlocks)
  }
  // Value is empty
  if (blocks && blocks.length === 0) {
    return set(appliedBlocks, [])
  }
  const changedBlock = appliedBlocks[operation.path[0]]
  setKey(changedBlock._key, changedBlock)
  return set(changedBlock, [{_key: blocks[operation.path[0]]._key}])
}
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / utils / changeToPatches.js View on Github external
function splitNodePatch(
  change: Change,
  operation: Operation,
  operations: Operation[],
  blocks: Block[],
  blockContentType
) {
  const patches = []
  const appliedBlocks = editorValueToBlocks(
    change.applyOperations([operation]).value.toJSON(VALUE_TO_JSON_OPTS),
    blockContentType
  )
  const splitBlock = appliedBlocks[operation.path[0]]

  if (operation.path.length === 1) {
    patches.push(set(splitBlock, [{_key: splitBlock._key}]))
    const newBlock = appliedBlocks[operation.path[0] + 1]
    let newKey
    const operationIndex = operations.indexOf(operation)
    const nextOperation = operations.get(operationIndex + 1)
    if (
      nextOperation &&
      nextOperation.type === 'set_node' &&
      nextOperation.path[0] === operation.path[0] + 1 &&
      isEqual(Object.keys(nextOperation.properties), ['data']) &&
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / utils / createOperationToPatches.ts View on Github external
function toBlock(editorValue: SlateValue, index: number) {
    if (!editorValue.document.nodes.get(index)) {
      throw new Error(`No block found at index ${index} in value`)
    }
    return editorValueToBlocks(
      {
        document: {
          nodes: [editorValue.document.nodes.get(index).toJSON(VALUE_TO_JSON_OPTS)]
        }
      },
      blockContentType
    )[0]
  }
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / utils / changeToPatches.js View on Github external
function mergeNodePatch(
  change: Change,
  operation: Operation,
  operations: Operation,
  blocks: Block[],
  blockContentType
) {
  const patches = []
  const appliedBlocks = editorValueToBlocks(
    change.applyOperations([operation]).value.toJSON(VALUE_TO_JSON_OPTS),
    blockContentType
  )
  if (operation.path.length === 1) {
    const mergedBlock = blocks[operation.path[0]]
    const targetBlock = appliedBlocks[operation.path[0] - 1]
    patches.push(
      unset([
        {
          _key: mergedBlock._key
        }
      ])
    )
    patches.push(set(targetBlock, [{_key: blocks[operation.path[0] - 1]._key}]))
  }
  if (operation.path.length === 2) {