Example #1
0
/**
 * Convert an article in a list item node.
 * @param {SummaryArticle} article
 * @return {Block} item
 */
function articleToBlock(article) {
    const { title, ref, articles } = article;

    const nodes = [
        Block.create({
            type: BLOCKS.TEXT,
            nodes: [
                Inline.create({
                    type: INLINES.LINK,
                    nodes: [
                        Text.createFromString(title)
                    ],
                    data: {
                        href: ref || ''
                    }
                })
            ]
        })
    ];

    if (articles.size > 0) {
        nodes.push(articlesToBlock(articles));
    }

    return Block.create({
        type: BLOCKS.LIST_ITEM,
        nodes
    });
}
Example #2
0
  function renderPlaceholder(props) {
    var editor = props.editor,
        node = props.node;

    if (!editor.props.placeholder) return;
    if (editor.state.isComposing) return;
    if (node.kind != 'block') return;
    if (!_slate.Text.isTextList(node.nodes)) return;
    if (node.text != '') return;
    if (editor.value.document.getBlocks().size > 1) return;

    var style = {
      pointerEvents: 'none',
      display: 'inline-block',
      width: '0',
      maxWidth: '100%',
      whiteSpace: 'nowrap',
      opacity: '0.333'
    };

    return _react2.default.createElement(
      'span',
      { contentEditable: false, style: style },
      editor.props.placeholder
    );
  }
Example #3
0
/**
 * Convert a summary to document.
 * @param  {Summary}  summary
 * @return {Document} document
 */
function summaryToDocument(summary) {
    const { parts } = summary;
    const nodes = [
        Block.create({
            type: BLOCKS.HEADING_1,
            nodes: [
                Text.createFromString('Summary')
            ]
        })
    ];

    parts.forEach((part, i) => {
        const { title, articles } = part;

        if (title) {
            nodes.push(Block.create({
                type: BLOCKS.HEADING_2,
                nodes: [
                    Text.createFromString(title)
                ]
            }));
        } else if (i > 0) {
            nodes.push(Block.create({
                type: BLOCKS.HR
            }));
        }

        if (!articles.isEmpty()) {
            nodes.push(articlesToBlock(articles));
        }
    });

    return Document.create({ nodes });
}
Example #4
0
  children.forEach(child => {
    // If the child is a non-text node, push the current node and the new child
    // onto the array, then creating a new node for future selection tracking.
    if (Node.isNode(child) && !Text.isText(child)) {
      if (node.text.length || node.__anchor != null || node.__focus != null)
        array.push(node)
      array.push(child)
      node = Text.create()
      length = 0
    }

    // If the child is a string insert it into the node.
    if (typeof child == 'string') {
      setNode(node.insertText(node.text.length, child, options.marks))
      length += child.length
    }

    // If the node is a `Text` add its text and marks to the existing node. If
    // the existing node is empty, and the `key` option wasn't set, preserve the
    // child's key when updating the node.
    if (Text.isText(child)) {
      const { __anchor, __focus } = child
      let i = node.text.length

      if (!options.key && node.text.length == 0) {
        setNode(node.set('key', child.key))
      }

      child.getLeaves().forEach(leaf => {
        let { marks } = leaf
        if (options.marks) marks = marks.union(options.marks)
        setNode(node.insertText(i, leaf.text, marks))
        i += leaf.text.length
      })

      if (__anchor != null) node.__anchor = __anchor + length
      if (__focus != null) node.__focus = __focus + length

      length += child.text.length
    }

    // If the child is a selection object store the current position.
    if (child == ANCHOR || child == CURSOR) node.__anchor = length
    if (child == FOCUS || child == CURSOR) node.__focus = length
  })
 return () =>
   editor.insertNodeByKey(
     node.key,
     0,
     Block.create({
       type: 'paragraph',
       nodes: [Text.create()],
     })
   )
Example #6
0
export function createText(tagName, attributes, children) {
  const { key } = attributes
  const leaves = createLeaves('leaves', {}, children)
  const text = Text.create({ key, leaves })
  let length = 0

  leaves.forEach(leaf => {
    incrementPoint(leaf, length)
    preservePoint(leaf, () => text)
    length += leaf.text.length
  })

  return text
}
Example #7
0
    parts.forEach((part, i) => {
        const { title, articles } = part;

        if (title) {
            nodes.push(Block.create({
                type: BLOCKS.HEADING_2,
                nodes: [
                    Text.createFromString(title)
                ]
            }));
        } else if (i > 0) {
            nodes.push(Block.create({
                type: BLOCKS.HR
            }));
        }

        if (!articles.isEmpty()) {
            nodes.push(articlesToBlock(articles));
        }
    });
Example #8
0
  const push = node => {
    const last = nodes.last()
    const isString = typeof node === 'string'

    if (last && last.__string && (isString || node.__string)) {
      const text = isString ? node : node.text
      const { length } = last.text
      const next = preservePoints(last, l => l.insertText(length, text))
      incrementPoints(node, length)
      copyPoints(node, next)
      next.__string = true
      nodes = nodes.pop().push(next)
    } else if (isString) {
      node = Text.create({ text: node })
      node.__string = true
      nodes = nodes.push(node)
    } else {
      nodes = nodes.push(node)
    }
  }
Example #9
0
/**
 * Exit the current table, by inserting a default block after the table.
 */
function onModEnter(event, change, opts) {
    var state = change.state;

    if (!state.isCollapsed) {
        return;
    }

    event.preventDefault();

    var exitBlock = Slate.Block.create({
        type: opts.exitBlockType,
        nodes: [Slate.Text.create('')]
    });

    var cell = state.startBlock;
    var table = TablePosition.create(state, cell).table;
    var tableParent = state.document.getParent(table.key);
    var insertionIndex = tableParent.nodes.indexOf(table) + 1;

    return change.insertNodeByKey(tableParent.key, insertionIndex, exitBlock).collapseToStartOf(exitBlock);
}
Example #10
0
export function createText(tagName, attributes, children) {
  const { key, marks } = attributes
  const list = createChildren(children)
  let node

  if (list.size > 1) {
    throw new Error(
      `The <text> hyperscript tag must only contain a single node's worth of children.`
    )
  } else if (list.size === 0) {
    node = Text.create({ key })
  } else {
    node = list.first()

    node = preservePoints(node, n => {
      if (key) n = n.set('key', key)
      if (marks) n = n.set('marks', Mark.createSet(marks))
      return n
    })
  }

  return node
}
Example #11
0
  function renderPlaceholder(props) {
    const { editor, node } = props
    if (!editor.props.placeholder) return
    if (editor.state.isComposing) return
    if (node.object != 'block') return
    if (!Text.isTextList(node.nodes)) return
    if (node.text != '') return
    if (editor.value.document.getBlocks().size > 1) return

    const style = {
      pointerEvents: 'none',
      display: 'inline-block',
      width: '0',
      maxWidth: '100%',
      whiteSpace: 'nowrap',
      opacity: '0.333',
    }

    return (
      <span contentEditable={false} style={style}>
        {editor.props.placeholder}
      </span>
    )
  }
 texts: create('List<Text>', function (v) {
   return slate.Text.isTextList(v);
 })
 text: create('Text', function (v) {
   return slate.Text.isText(v);
 }),
Example #14
0
/** @jsx h */

import h from '../..'
import { Value, Document, Block, Text } from 'slate'

export const input = (
  <value normalize={false}>
    <document>
      <block type="paragraph">Valid block</block>
      <text>Invalid text</text>
    </document>
  </value>
)

export const output = Value.fromJSON(
  {
    document: Document.create({
      nodes: [
        Block.create({
          type: 'paragraph',
          nodes: [Text.create('Valid block')],
        }),
        Text.create('Invalid text'),
      ],
    }),
  },
  { normalize: false }
)
Example #15
0
texts: create('List<Text>', v => Text.isTextList(v)),
Example #16
0
text: create('Text', v => Text.isText(v)),
Example #17
0
function createChildren(children, options = {}) {
  const array = []
  let length = 0

  // When creating the new node, try to preserve a key if one exists.
  const firstText = children.find(c => Text.isText(c))
  const key = options.key ? options.key : firstText ? firstText.key : undefined
  let node = Text.create({ key })

  // Create a helper to update the current node while preserving any stored
  // anchor or focus information.
  function setNode(next) {
    const { __anchor, __focus } = node
    if (__anchor != null) next.__anchor = __anchor
    if (__focus != null) next.__focus = __focus
    node = next
  }

  children.forEach(child => {
    // If the child is a non-text node, push the current node and the new child
    // onto the array, then creating a new node for future selection tracking.
    if (Node.isNode(child) && !Text.isText(child)) {
      if (node.text.length || node.__anchor != null || node.__focus != null)
        array.push(node)
      array.push(child)
      node = Text.create()
      length = 0
    }

    // If the child is a string insert it into the node.
    if (typeof child == 'string') {
      setNode(node.insertText(node.text.length, child, options.marks))
      length += child.length
    }

    // If the node is a `Text` add its text and marks to the existing node. If
    // the existing node is empty, and the `key` option wasn't set, preserve the
    // child's key when updating the node.
    if (Text.isText(child)) {
      const { __anchor, __focus } = child
      let i = node.text.length

      if (!options.key && node.text.length == 0) {
        setNode(node.set('key', child.key))
      }

      child.getLeaves().forEach(leaf => {
        let { marks } = leaf
        if (options.marks) marks = marks.union(options.marks)
        setNode(node.insertText(i, leaf.text, marks))
        i += leaf.text.length
      })

      if (__anchor != null) node.__anchor = __anchor + length
      if (__focus != null) node.__focus = __focus + length

      length += child.text.length
    }

    // If the child is a selection object store the current position.
    if (child == ANCHOR || child == CURSOR) node.__anchor = length
    if (child == FOCUS || child == CURSOR) node.__focus = length
  })

  // Make sure the most recent node is added.
  array.push(node)

  return array
}
Example #18
0
 const firstText = children.find(c => Text.isText(c))