Skip to content
Snippets Groups Projects
Heading2.js 1.67 KiB
Newer Older
chris's avatar
chris committed
import React from 'react';
import { injectable } from 'inversify';
chris's avatar
chris committed
import { isEmpty } from 'lodash';
import { LeftSideButton } from 'wax-prosemirror-components';
import { Commands } from 'wax-prosemirror-utilities';
import Tools from '../../lib/Tools';
chris's avatar
chris committed

chris's avatar
chris committed
@injectable()
class Heading2 extends Tools {
  title = 'Change to heading level 2';
chris's avatar
chris committed
  label = 'Heading 2';
chris's avatar
chris committed
  name = 'Heading2';
chris's avatar
chris committed

  get run() {
    return (state, dispatch) => {
      Commands.setBlockType(state.config.schema.nodes.heading, { level: 2 })(
chris's avatar
chris committed
        state,
chris's avatar
chris committed
  get active() {
chris's avatar
chris committed
    return (state, activeViewId) => {
      let isActive = true;
      if (activeViewId !== 'main') return false;
      const { from, to } = state.selection;
      state.doc.nodesBetween(from, to, (node, pos) => {
chris's avatar
chris committed
        if (
          node.type.name === 'list_item' ||
          node.type.name === 'image' ||
          node.type.name === 'figure' ||
          node.type.name === 'figcaption'
        ) {
chris's avatar
chris committed
          isActive = false;
        }
      });
      if (!isActive) return false;
      return !Commands.setBlockType(state.config.schema.nodes.heading, {
chris's avatar
chris committed
        level: 2,
      })(state);
    };
  }

chris's avatar
chris committed
  select = (state, activeViewId) => {
    if (activeViewId !== 'main') return false;
    return true;
  };

chris's avatar
chris committed
  get enable() {
    return state => {
      return Commands.setBlockType(state.config.schema.nodes.heading, {
chris's avatar
chris committed
      })(state);
    };
  }
chris's avatar
chris committed

  renderTool(view) {
    if (isEmpty(view)) return null;
    // eslint-disable-next-line no-underscore-dangle
    return this._isDisplayed ? (
      <LeftSideButton item={this.toJSON()} key="BlockQuote" view={view} />
    ) : null;
  }
chris's avatar
chris committed
}