How to use the slate.Text.create 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 sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / utils / createPatchToOperations.ts View on Github external
// Set patches patching either string values or span objects
    if (patch.type === 'set') {
      const valueIsString = isString(patch.value)
      const patchSpan = valueIsString ? null : (patch.value as Span)
      const patchText = valueIsString ? patch.value : patchSpan.text
      // If single leaf, we can just replace the text with the current marks
      if (textNode.leaves.size === 1) {
        let marks
        // eslint-disable-next-line max-depth
        if (valueIsString) {
          marks = textNode.leaves.map(leaf => leaf.marks).get(0)
        } else {
          marks = Mark.createSet(patchSpan.marks.map(mark => ({type: mark})))
        }
        editor.replaceNodeByPath(textPath, Text.create({text: patchText, marks}))
        return editor.operations
      }

      // Build the new text
      workTextNode.leaves[leafIndex] = {
        object: 'leaf',
        text: patchText,
        marks: valueIsString
          ? node.leaves.get(leafIndex).marks
          : Mark.createSet(patchSpan.marks.map(mark => ({type: mark})))
      }
      // Replace it
      editor.replaceNodeByPath(textPath, Text.fromJSON(workTextNode))
      return editor.operations
    }
github Simon-Initiative / authoring-client / src / editors / content / learning / contiguoustext / utils.tsx View on Github external
const value = new wrappers[contentType](rawValue);
    const data = { value };
    return node.merge({ data });
  }

  // Handle Links
  if (contentType === 'Link') {
    delete rawValue.content;
    const value = new wrappers[contentType]().with(rawValue);
    const data = { value };
    return node.merge({ data });
  }

  // Cite, Extra, InputRef (or anthing else) just gets
  // stripped out and replaced with a text node
  return Text.create({ text: node.text });
}
github GitbookIO / slate-edit-code / lib / validation / validateNode.js View on Github external
validate(node) {
            const { nodes } = node;

            const toRemove = nodes.filterNot(n => n.object === 'text');
            if (!toRemove.isEmpty()) {
                // Remove them, and the rest
                // will be done in the next validation call.
                return { toRemove };
            } else if (nodes.size > 1) {
                // Else, there are only text nodes

                return { toJoin: nodes };
            } else if (nodes.size === 0) {
                return { toAdd: [Text.create()] };
            }

            // There is a single text node -> valid
            return null;
        },
github grafana / grafana / public / app / features / explore / Value.ts View on Github external
const lines = text.split('\n').map(line =>
    Block.create({
      type: 'code_line',
      nodes: [Text.create(line)],
    })
  );
github ianstormtaylor / slate / packages / slate-hyperscript / src / creators.js View on Github external
export function createText(tagName, attributes, children) {
  const { key, marks } = attributes
  const list = createChildren(children)
  let node

  if (list.size > 1) {
    throw new Error(
      `The 
github olymp / olymp / packages / slate / add-block.es6 View on Github external
if (isList) {
      transform = transform
        .setBlock(isActive ? defaultNode : type)
        .unwrapBlock('bulleted-list')
        .unwrapBlock('numbered-list');
    } else {
      if (initNode) {
        node = initNode(node, { value, schema, parentKey, index, transform });
      }
      const block = createBlock(node);

      if (defaultNodes && typeof defaultNodes === 'function') {
        defaultNodes = defaultNodes({ value, node, schema, parentKey, index, transform });
      } else if (!defaultNodes && !block.isVoid) {
        defaultNodes = [Text.create(defaultText)];
      }

      if (block && block.kind === 'block') {
        transform = parentKey
          ? transform.insertNodeByKey(parentKey, index, block)
          : transform.insertBlock(block);
      } else if (block) {
        transform = parentKey
          ? transform.insertNodeByKey(parentKey, index, block)
          : transform.insertInline(block);
      }

      if (defaultNodes) {
        defaultNodes.forEach((item, index) => {
          if (typeof item === 'string' && schema.nodes[item]) {
            transform = addBlock(
github ConvertKit / slate-plugins / packages / slate-lists / src / create-schema.js View on Github external
normalize: (editor, error) => {
          switch (error.code) {
            case "child_min_invalid":
              editor.insertNodeByKey(
                error.node.key,
                0,
                Block.create({
                  type: blocks.list_item_child,
                  nodes: [Text.create()]
                })
              );
              return;
            case "child_type_invalid":
              editor.wrapBlockByKey(error.child.key, {
                type: blocks.list_item_child
              });
              return;
            case "parent_type_invalid":
              editor.wrapBlockByKey(error.node.key, blocks.unordered_list);
              return;
            default:
              return;
          }
        }
      }
github KohheePeace / slate-tuto / src / slate-editor / rules / checkListRules.js View on Github external
const checkListItems = List(el.childNodes).map(line =>
      Block.create({
        type: isCheckList ? BLOCKS.CHECK_LIST_ITEM : BLOCKS.LIST_ITEM,
        nodes: [Text.create(line.innerText)]
      }))
github ianstormtaylor / slate / packages / slate-hyperscript / src / creators.js View on Github external
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)
    }
  }
github GitbookIO / slate-edit-table / lib / schema / validateNode.js View on Github external
function makeEmptyCell(opts: Options): Block {
    return Block.create({
        type: opts.typeCell,
        nodes: List([Text.create('')])
    });
}