How to use the slate.Block.createList function in slate

To help you get started, we’ve selected a few slate 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 olymp / olymp / packages / slate / utils / add-block.js View on Github external
const isActive = hasBlock(value, type);
    const isList =
      hasBlock(value, 'bulleted-list-item') ||
      hasBlock(value, 'numbered-list-item');

    if (isList) {
      transform = transform
        .setBlock(isActive ? defaultNode : type)
        .unwrapBlock('bulleted-list')
        .unwrapBlock('numbered-list');
    } else if (isAtomic) {
      transform = transform.insertBlock({
        type,
        isVoid,
        data: {},
        nodes: defaultNodes || Block.createList([createP()]),
      });
    } else {
      transform = transform.setBlock(isActive ? defaultNode : type);
    }
  } else {
    // Handle the extra wrapping required for list buttons.
    const isList =
      hasBlock(value, 'bulleted-list-item') ||
      hasBlock(value, 'numbered-list-item');
    const isType = blocks.some(
      block => !!document.getClosest(block, parent => parent.type === type)
    );

    if (isList && isType) {
      transform = transform
        .setBlock(defaultNode)
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / plugins / PastePlugin.ts View on Github external
const {fragment, html, text} = transfer
    const {type} = transfer
    if (type === 'fragment') {
      onProgress({status: 'fragment'})
      // Check if we have all block types in the schema,
      // otherwise, use html version
      const allSchemaBlockTypes = blockContentType.of
        .map(ofType => ofType.name)
        .concat('contentBlock')
      const allBlocksHasSchemaDef = fragment.nodes
        .map(node => node.type)
        .every(nodeType => allSchemaBlockTypes.includes(nodeType))
      if (allBlocksHasSchemaDef) {
        ensureNoPlaceholder(editor)
        const {focusBlock} = editor.value
        const newNodesList = Block.createList(fragment.nodes.map(node => processNode(node, editor)))
        const newDoc = new Document({
          key: fragment.key,
          nodes: newNodesList
        })
        newDoc.nodes.forEach((block, index) => {
          if (
            index === 0 &&
            focusBlock &&
            !editor.query('isVoid', focusBlock) &&
            focusBlock.nodes.size === 1 &&
            focusBlock.text === ''
          ) {
            editor
              .insertBlock(block)
              .moveToEndOfBlock()
              .removeNodeByKey(focusBlock.key)
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor-slate / plugins / onPasteSlateContent.js View on Github external
function onPaste(event, data, change) {
    if (typeof data.fragment === 'undefined' || data.fragment === null) {
      return null
    }
    const newNodesList = Block.createList(data.fragment.nodes.toArray().map(processNode))
    const newDoc = new Document({
      key: data.fragment.key,
      nodes: newNodesList
    })
    return change.insertFragment(newDoc)
  }
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor-slate / conversion / toSlate.js View on Github external
export default function toSlate(array, context) {
  if (array.length === 0) {
    return State.create({
      document: Document.create({
        nodes: Block.createList([
          Raw.deserializeNode({
            kind: 'block',
            type: SLATE_DEFAULT_NODE,
            nodes: [
              {kind: 'text', text: '', ranges: []}
            ]
          })
        ])
      })
    })
  }
  return State.create({
    document: Document.create({
      nodes: Block.createList(array.map(node => DESERIALIZE.node(node, context)))
    })
  })
github olymp / olymp / packages / slate / utils / create-list.js View on Github external
export default items =>
  Block.createList(
    items
      .map((item) => {
        if (!item) {
          return empty();
        } else if (item.component && item.key) {
          const block = { type: item.key, kind: 'block' };
          if (!item.editable) {
            block.isVoid = true;
          } else {
            block.nodes = Block.createList([empty()]);
          }
          return block;
        }
      })
      .filter(x => x)
  );
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor-slate / conversion / toSlate.js View on Github external
nodes: Block.createList(para.children.map(child => {
        return Block.create({
          type: 'listItem',
          nodes: Block.createList(child.children.map((childChild) => {
            return DESERIALIZE.node(childChild, context)
          }))
        })
      }))
    })
github olymp / olymp / packages / slate / utils / create-list.es6 View on Github external
export default items =>
  Block.createList(
    items
      .map((item) => {
        if (!item) {
          return empty();
        } else if (item.component && item.key) {
          const block = { type: item.key, kind: 'block' };
          if (!item.editable) {
            block.isVoid = true;
          } else {
            block.nodes = Block.createList([empty()]);
          }
          return block;
        }
      })
      .filter(x => x),
  );
github olymp / olymp / packages / slate / utils / create-list.tsx View on Github external
.map((item) => {
        if (!item) {
          return empty();
        } else if (item.component && item.key) {
          const block = { type: item.key, kind: 'block' };
          if (!item.editable) {
            block.isVoid = true;
          } else {
            block.nodes = Block.createList([empty()]);
          }
          return block;
        }
      })
      .filter(x => x)
github olymp / olymp / packages / slate / utils / create-list.tsx View on Github external
export default items =>
  Block.createList(
    items
      .map((item) => {
        if (!item) {
          return empty();
        } else if (item.component && item.key) {
          const block = { type: item.key, kind: 'block' };
          if (!item.editable) {
            block.isVoid = true;
          } else {
            block.nodes = Block.createList([empty()]);
          }
          return block;
        }
      })
      .filter(x => x)
  );
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor-slate / conversion / toSlate.js View on Github external
document: Document.create({
        nodes: Block.createList([
          Raw.deserializeNode({
            kind: 'block',
            type: SLATE_DEFAULT_NODE,
            nodes: [
              {kind: 'text', text: '', ranges: []}
            ]
          })
        ])
      })
    })
  }
  return State.create({
    document: Document.create({
      nodes: Block.createList(array.map(node => DESERIALIZE.node(node, context)))
    })
  })
}