Skip to content
Snippets Groups Projects
MultipleDropDownQuestion.js 1.72 KiB
Newer Older
chris's avatar
chris committed
import { injectable } from 'inversify';
chris's avatar
chris committed
import { findWrapping } from 'prosemirror-transform';
import { v4 as uuidv4 } from 'uuid';
chris's avatar
chris committed
import { Tools, Commands } from 'wax-prosemirror-core';
chris's avatar
chris committed
import helpers from '../MultipleChoiceQuestionService/helpers/helpers';
chris's avatar
chris committed

@injectable()
chris's avatar
chris committed
class MultipleDropDownQuestion extends Tools {
chris's avatar
chris committed
  title = 'Add Multiple Drop Down Question';
  icon = 'mulitpleDropDownQuestion';
  name = 'Multiple Drop Down';

  get run() {
    return main => {
      const { dispatch } = main;
      const { state } = main;
      helpers.checkifEmpty(main);
      const { $from, $to } = main.state.selection;
chris's avatar
chris committed
      const range = $from.blockRange($to);
      const { tr } = main.state;
chris's avatar
chris committed

      const wrapping =
        range &&
        findWrapping(
          range,
          state.config.schema.nodes.multiple_drop_down_container,
          {
            id: uuidv4(),
          },
        );
      if (!wrapping) return false;
      tr.wrap(range, wrapping);
      dispatch(tr);
    };
chris's avatar
chris committed
  }

  get active() {
chris's avatar
chris committed
    return state => {
      if (
        Commands.isParentOfType(
          state,
          state.config.schema.nodes.multiple_drop_down_container,
        )
      ) {
        return true;
      }
      return false;
    };
chris's avatar
chris committed
  }

chris's avatar
chris committed
  select = (state, activeViewId, activeView) => {
    const { disallowedTools } = activeView.props;
    let status = true;
    const { from, to } = state.selection;
    if (from === null || disallowedTools.includes('MultipleDropDown'))
      return false;

    state.doc.nodesBetween(from, to, (node, pos) => {
      if (node.type.groups.includes('questions')) {
        status = false;
      }
    });
    return status;
  };
chris's avatar
chris committed

  get enable() {
    return state => {};
  }
}

chris's avatar
chris committed
export default MultipleDropDownQuestion;