"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _CustomMenuButton = _interopRequireDefault(require("./CustomMenuButton"));

var _HeadingCommand = _interopRequireDefault(require("../HeadingCommand"));

var _CustomStyleCommand = _interopRequireDefault(require("../CustomStyleCommand"));

var React = _interopRequireWildcard(require("react"));

var _prosemirrorState = require("prosemirror-state");

var _prosemirrorView = require("prosemirror-view");

var _prosemirrorTransform = require("prosemirror-transform");

var _prosemirrorModel = require("prosemirror-model");

var _ParagraphNodeSpec = require("../ParagraphNodeSpec");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

// [FS] IRAD-1042 2020-09-09
// To include custom styles in the toolbar
let HEADING_COMMANDS = {
  [_ParagraphNodeSpec.RESERVED_STYLE_NONE]: new _HeadingCommand.default(0)
};

class HeadingCommandMenuButton extends React.PureComponent {
  constructor() {
    super(...arguments);

    _defineProperty(this, "props", void 0);

    _defineProperty(this, "hasRuntime", true);
  }

  //[FS] IRAD-1085 2020-10-09
  //method to build commands for list buttons
  getCommandGroups() {
    HEADING_COMMANDS = {
      // [FS] IRAD-1074 2020-12-09
      // When apply 'None' from style menu, not clearing the applied custom style.
      [_ParagraphNodeSpec.RESERVED_STYLE_NONE]: new _CustomStyleCommand.default(_ParagraphNodeSpec.RESERVED_STYLE_NONE, _ParagraphNodeSpec.RESERVED_STYLE_NONE)
    }; // Check runtime is avilable in editorview
    // Get styles form server configured in runtime

    if (this.props.editorView && this.props.editorView.runtime && typeof this.props.editorView.runtime.getStylesAsync === 'function') {
      let HEADING_NAMES = null;
      this.props.editorView.runtime.getStylesAsync().then(result => {
        if (result) {
          HEADING_NAMES = result;

          if (null != HEADING_NAMES) {
            HEADING_NAMES.forEach(obj => {
              HEADING_COMMANDS[obj.styleName] = new _CustomStyleCommand.default(obj, obj.styleName);
            });
          }
        }

        return [HEADING_COMMANDS];
      });
      this.hasRuntime = true;
    } else {
      this.hasRuntime = false;
    }

    return [HEADING_COMMANDS];
  }

  staticCommands() {
    const MENU_COMMANDS = {
      ['newstyle']: new _CustomStyleCommand.default('newstyle', 'New Style..')
    }; // [FS] IRAD-1176 2021-02-08
    // Added a menu "Edit All" for Edit All custom styles

    MENU_COMMANDS['editall'] = new _CustomStyleCommand.default('editall', 'Edit All');
    MENU_COMMANDS['clearstyle'] = new _CustomStyleCommand.default('clearstyle', 'Clear Style');
    return [MENU_COMMANDS];
  }

  isAllowedNode(node) {
    return node.type.name === 'paragraph' || node.type.name === 'ordered_list';
  }

  render() {
    const {
      dispatch,
      editorState,
      editorView
    } = this.props;
    const {
      selection,
      doc
    } = editorState;
    const {
      from,
      to
    } = selection;
    let customStyleName;
    let selectedStyleCount = 0; // [FS] IRAD-1088 2020-10-05
    // get the custom style name from node attribute

    doc.nodesBetween(from, to, (node, pos) => {
      // [FS] IRAD-1231 2021-03-05
      // Issue fix : Applied custom style name shows only when click start and end position of paragraph,
      // otherwise shows 'None'.
      if (this.isAllowedNode(node)) {
        if (node.attrs.styleName) {
          // [FS] IRAD-1043 2020-10-27
          // Show blank as style name when select paragraphs with multiple custom styles applied
          selectedStyleCount++; // [FS] IRAD-1100 2020-10-30
          // Issue fix: style name shows blank when select multiple paragraph with same custom style applied

          if (1 === selectedStyleCount || 1 < selectedStyleCount && node.attrs.styleName === customStyleName) {
            customStyleName = node.attrs.styleName.includes(_ParagraphNodeSpec.RESERVED_STYLE_NONE_NUMBERING) ? _ParagraphNodeSpec.RESERVED_STYLE_NONE : node.attrs.styleName;
          } else {
            customStyleName = _ParagraphNodeSpec.RESERVED_STYLE_NONE;
          }
        } // [FS] IRAD-1231 2021-03-02
        // Show the custom style as None for paste paragraph from outside.
        else {
            node.attrs.styleName = _ParagraphNodeSpec.RESERVED_STYLE_NONE;
            customStyleName = _ParagraphNodeSpec.RESERVED_STYLE_NONE;
          }
      }
    });
    return /*#__PURE__*/React.createElement(_CustomMenuButton.default, {
      className: "width-100" // [FS] IRAD-1008 2020-07-16
      // Disable font type menu on editor disable state
      ,
      commandGroups: this.getCommandGroups(),
      disabled: editorView && editorView.disabled || !this.hasRuntime ? true : false,
      dispatch: dispatch,
      editorState: editorState,
      editorView: editorView,
      label: customStyleName,
      parent: this,
      staticCommand: this.staticCommands()
    });
  }

}

var _default = HeadingCommandMenuButton;
exports.default = _default;