How to use the slate.Text.createFromString 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 LucasYuNju / leanote-desktop-lite / src / components / SlateEditor.js View on Github external
this.setState({ state: nextState });
            }
            this.lastStartText = state.startText;
          });
        }
      }
    } else {
      let match = linkRegex.exec(startText.text);
      if (match) { // 将link源码解析成link元素,目前只允许按顺序写link
        state = state.transform()
          .extendBackward(match[1].length + match[2].length + 4)
          .delete()
          .insertInline(Inline.create({
            data: { href: match[2] },
            type: INLINES.LINK,
            nodes: [ Text.createFromString(`[${match[1]}](${match[2]})`) ]
          }))
          .collapseToEndOfNextText()
          .apply(OPTIONS);
        this.lastStartText = state.document.getPreviousText(state.startText.key);
      }
      setTimeout(() => {
        this.setState({ state });
      });
    }
    setTimeout(() => {
      const prevState = state;
      state = this.convertSrcToLink(state);
      if (state !== prevState) {
        this.setState({ state });
      }
    });
github orbiting / publikator-frontend / lib / editor / schema / blocks / ImageWithCaption.js View on Github external
// If has a second child element, but it's not a caption, change it -> resume
  } else if (children.get(1).type !== 'imageCaption') {
    updatedTransform = updatedTransform.setNodeByKey(
      children.get(1).key,
      { type: 'imageCaption' }
    )
  }

  // If has no third child element, insert one -> resume
  if (!children.get(2)) {
    updatedTransform = updatedTransform.insertNodeByKey(
      node.key,
      2,
      Block.create({
        type: 'imageSource',
        nodes: [Text.createFromString('')]
      })
    )
    // If has a third child element, but it's not a source, change it -> resume
  } else if (children.get(2).type !== 'imageSource') {
    updatedTransform = updatedTransform.setNodeByKey(
      children.get(2).key,
      { type: 'imageSource' }
    )
  }

  return updatedTransform
}
github orbiting / publikator-frontend / lib / editor / modules / Document / rules.js View on Github external
normalize: (transform, document, nodes) => {
      if (
        nodes.first().kind !== 'block' ||
        nodes.first().type !==
          get('Typography.Constants.H1')
      ) {
        const title = Block.create({
          type: get('Typography.Constants.H1'),
          nodes: [Text.createFromString('')]
        })
        return transform.insertNodeByKey(
          document.key,
          0,
          title
        )
      }
      return transform
    }
  }
github jakoblo / ufo / src / js / view-folder / view-folder-editor / slate-extensions / slate-rich-text / slate-rich-text-plugin.jsx View on Github external
const insertLink = (url: string, state: any) => {
  const domain = tld.getDomain(url);
  const transform = state.transform();

  if (state.isCollapsed) {
    return transform
      .insertInline({
        type: INLINE_TYPES.LINK.type,
        data: {
          href: url
        },
        nodes: [Text.createFromString(domain || url)]
      })
      .apply();
  } else {
    if (isLink()) {
      transform.unwrapInline(INLINE_TYPES.LINK.type);
    }

    return transform
      .wrapInline({
        type: INLINE_TYPES.LINK.type,
        data: {
          href: data.text
        }
      })
      .collapseToEnd()
      .apply();
github orbiting / publikator-frontend / lib / editor / modules / Image / handlers.js View on Github external
const transform = state.transform()

  if (target) transform.select(target)

  return transform
    .insertBlock({
      type: get('Image.Constants.IMAGE_WITH_CAPTION'),
      nodes: [
        Block.create({
          type: get('Image.Constants.IMAGE'),
          isVoid: true,
          data: { src }
        }),
        Block.create({
          type: get('Image.Constants.IMAGE_CAPTION'),
          nodes: [Text.createFromString('')]
        }),
        Block.create({
          type: get('Image.Constants.IMAGE_SOURCE'),
          nodes: [Text.createFromString('')]
        })
      ]
    })
    .apply()
}
github LucasYuNju / leanote-desktop-lite / src / components / SlateEditor.js View on Github external
convertSrcToLink = (state) => {
    const parent = state.document.getParent(state.startText.key);
    const previous = state.document.getPreviousText(state.startText.key);
    const lastParent = this.lastStartText ? state.document.getParent(this.lastStartText.key) : null;
    if (previous && this.lastStartText && previous.key === this.lastStartText.key && state.startText.text.length === 0) return state;
    if (lastParent && lastParent.type === INLINES.LINK && (lastParent.key !== parent.key || !state.selection.isFocused)) {
      const match = linkRegex.exec(lastParent.text)
      const nextState = state.transform()
        .setNodeByKey(lastParent.key, { data: { href: match[2] } })
        .removeNodeByKey(this.lastStartText.key)
        .insertNodeByKey(lastParent.key, 0, Text.createFromString(match[1]))
        .apply(OPTIONS);
      this.lastStartText = state.startText;
      return nextState;
    }
    return state;
  }
github MorpheoOrg / morpheo-analytics / src / client / js / business / notebook / components / Editor / slate.js View on Github external
addInnerParagraphCell(index) {
        const {state} = this.props;

        const document = state.document;
        const transform = state.transform();

        const block = Block.create({
            type: opts.exitBlockType,
            nodes: [Text.createFromString('')],
        });

        transform.insertNodeByKey(document.key, typeof index !== 'undefined' ? index : document.nodes.size, block);
        const newState = transform.focus().apply();

        this.props.setSlate({state: newState});
    }
github orbiting / publikator-frontend / lib / editor / modules / Image / components.js View on Github external
const clickHandler = (get, state, onChange) => () => {
  onChange(
    state
      .transform()
      .insertBlock({
        type: get('Image.Constants.IMAGE_WITH_CAPTION'),
        nodes: [
          Block.create({
            type: get('Image.Constants.IMAGE'),
            isVoid: true
          }),
          Block.create({
            type: get('Image.Constants.IMAGE_CAPTION'),
            nodes: [Text.createFromString('')]
          }),
          Block.create({
            type: get('Image.Constants.IMAGE_SOURCE'),
            nodes: [Text.createFromString('')]
          })
        ]
      })
      .apply()
  )
}
github jakoblo / ufo / src / js / view-folder / view-folder-editor / slate-extensions / slate-file / slate-file-utils.js View on Github external
export const getFileBlockProperties = (basename: string): Object => {
  const properties = {
    type: c.BLOCK_TYPE_FILE,
    isVoid: true,
    nodes: List([Text.createFromString(" ")]),
    data: {
      base: basename
    }
  };

  const test = Block.create(properties);
  return properties;
};