"use strict";

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

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

var _UICommand = _interopRequireDefault(require("./UICommand"));

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

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

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

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

var _uuid = _interopRequireDefault(require("./uuid"));

require("./listType.css");

var _CustomStyleItem = _interopRequireDefault(require("./CustomStyleItem"));

var _createPopUp = _interopRequireDefault(require("./createPopUp"));

var _CustomStyleSubMenu = _interopRequireDefault(require("./CustomStyleSubMenu"));

var _CustomStyleEditor = _interopRequireDefault(require("./CustomStyleEditor"));

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

var _PopUpPosition = require("./PopUpPosition");

var _customStyle = require("../customStyle");

var _TextAlignCommand = require("../TextAlignCommand");

var _TextLineSpacingCommand = require("../TextLineSpacingCommand");

var _ParagraphSpacingCommand = require("../ParagraphSpacingCommand");

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _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-1039 2020-09-24
// UI to show the list buttons
class CustomMenuUI extends React.PureComponent {
  constructor() {
    super(...arguments);

    _defineProperty(this, "_activeCommand", null);

    _defineProperty(this, "_popUp", null);

    _defineProperty(this, "_stylePopup", null);

    _defineProperty(this, "_styleName", null);

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

    _defineProperty(this, "_menu", null);

    _defineProperty(this, "_id", (0, _uuid.default)());

    _defineProperty(this, "_modalId", null);

    _defineProperty(this, "state", {
      expanded: false,
      style: {
        display: 'none',
        top: '',
        left: ''
      }
    });

    _defineProperty(this, "_onUIEnter", (command, event) => {
      this.showSubMenu(command, event);
    });

    _defineProperty(this, "_execute", (command, e) => {
      const {
        dispatch,
        editorState,
        editorView,
        onCommand
      } = this.props;

      if (command.execute(editorState, dispatch, editorView, e)) {
        onCommand && onCommand();
      }
    });
  }

  render() {
    const {
      dispatch,
      editorState,
      editorView,
      commandGroups,
      staticCommand,
      onCommand
    } = this.props;
    const children = [];
    const children1 = [];
    commandGroups.forEach((group, ii) => {
      Object.keys(group).forEach(label => {
        const command = group[label];
        const hasText = 'None' !== label;
        children.push( /*#__PURE__*/React.createElement(_CustomStyleItem.default, {
          command: command,
          disabled: editorView && editorView.disabled ? true : false,
          dispatch: dispatch,
          editorState: editorState,
          editorView: editorView,
          hasText: hasText,
          key: label,
          label: label,
          onClick: this._onUIEnter,
          onCommand: onCommand
        }));
      });
    });
    staticCommand.forEach((group, ii) => {
      Object.keys(group).forEach(label => {
        const command = group[label];
        children1.push( /*#__PURE__*/React.createElement(_CustomStyleItem.default, {
          command: command,
          disabled: editorView && editorView.disabled ? true : false,
          dispatch: dispatch,
          editorState: editorState,
          editorView: editorView,
          hasText: false,
          key: label,
          label: command._customStyleName,
          onClick: this._onUIEnter,
          onCommand: onCommand
        }));
      });
    });
    return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
      className: "dropbtn",
      id: this._id
    }, /*#__PURE__*/React.createElement("div", {
      className: "stylenames"
    }, children), /*#__PURE__*/React.createElement("hr", null), children1));
  }

  //shows the alignment and line spacing option
  showSubMenu(command, event) {
    const anchor = event ? event.currentTarget : null; // close the popup toggling effect

    if (this._stylePopup) {
      this._stylePopup.close();

      this._stylePopup = null;
      return;
    }

    this._popUp = (0, _createPopUp.default)(_CustomStyleSubMenu.default, {
      command: command
    }, {
      anchor,
      autoDismiss: false,
      IsChildDialog: true,
      onClose: val => {
        if (this._popUp) {
          this._popUp = null;

          if (undefined !== val && val.command._customStyle) {
            // do edit,remove,rename code here
            if ('remove' === val.type) {
              (0, _customStyle.removeStyle)(val.command._customStyleName, val.command._customStyle, this.props.editorState, this.props.editorView.dispatch); // [FS] IRAD-1099 2020-11-17
              // Issue fix: Even the applied style is removed the style name is showing in the editor

              this.removeCustomStyleName(this.props.editorState, val.command._customStyleName, this.props.editorView.dispatch);
            } else if ('rename' === val.type) {
              window.alert('Rename Style...');
            } else {
              this.showStyleWindow(command, event);
            }
          }
        }
      }
    });
  } // [FS] IRAD-1099 2020-11-17
  // Issue fix: Even the applied style is removed the style name is showing in the editor


  removeCustomStyleName(editorState, removedStyleName, dispatch) {
    const {
      selection,
      doc
    } = editorState;
    let {
      from,
      to
    } = selection;
    const {
      empty
    } = selection;

    if (empty) {
      from = selection.$from.before(1);
      to = selection.$to.after(1);
    }

    let tr = editorState.tr;
    const customStyleName = 'None';
    const tasks = [];
    const textAlignNode = [];
    doc.nodesBetween(0, doc.nodeSize - 2, (node, pos) => {
      if (node.content && node.content.content && node.content.content.length) {
        if (node.content && node.content.content && node.content.content[0].marks && node.content.content[0].marks.length) {
          node.content.content[0].marks.some(mark => {
            if (node.attrs.styleName === removedStyleName) {
              tasks.push({
                node,
                pos,
                mark
              });
            }
          });
        } else {
          textAlignNode.push({
            node,
            pos
          });
        }
      }
    });

    if (!tasks.length) {
      textAlignNode.forEach(eachnode => {
        const {
          node
        } = eachnode;
        node.attrs.styleName = customStyleName;
      }); // to remove both text align format and line spacing

      tr = this.removeTextAlignAndLineSpacing(tr, editorState.schema);
    }

    tasks.forEach(job => {
      const {
        node,
        mark,
        pos
      } = job;
      tr = tr.removeMark(pos, pos + node.nodeSize, mark.type); // reset the custom style name to NONE after remove the styles

      node.attrs.styleName = customStyleName;
    }); // to remove both text align format and line spacing

    tr = this.removeTextAlignAndLineSpacing(tr, editorState.schema);
    editorState.doc.nodesBetween(from, to, (node, startPos) => {
      if (node.type.name === 'paragraph') {
        tr = tr.setNodeMarkup(startPos, undefined, node.attrs);
      }
    });

    if (dispatch && tr.docChanged) {
      dispatch(tr);
      return true;
    }

    return false;
  } // to remove the text align, line spacing, paragraph spacing after and before format if applied.


  removeTextAlignAndLineSpacing(tr, schema) {
    tr = (0, _TextAlignCommand.setTextAlign)(tr, schema, null);
    tr = (0, _TextLineSpacingCommand.setTextLineSpacing)(tr, schema, null);
    tr = (0, _ParagraphSpacingCommand.setParagraphSpacing)(tr, schema, '0', true);
    tr = (0, _ParagraphSpacingCommand.setParagraphSpacing)(tr, schema, '0', false);
    return tr;
  } //shows the alignment and line spacing option


  showStyleWindow(command, event) {
    // const anchor = event ? event.currentTarget : null;
    // close the popup toggling effect
    if (this._stylePopup) {
      this._stylePopup.close();

      this._stylePopup = null; // return;
    }

    this._styleName = command._customStyleName;
    this._stylePopup = (0, _createPopUp.default)(_CustomStyleEditor.default, {
      stylename: command._customStyleName,
      // mode: 1, //edit
      description: command._customStyle.description,
      styles: command._customStyle.styles
    }, {
      position: _PopUpPosition.atViewportCenter,
      autoDismiss: false,
      IsChildDialog: false,
      onClose: val => {
        if (this._stylePopup) {
          //handle save style object part here
          if (undefined !== val) {
            const {
              dispatch
            } = this.props.editorView;
            let tr = this.props.editorState.tr; // const doc = this.props.editorState.doc;

            (0, _customStyle.saveStyle)(val); // tr = tr.setSelection(TextSelection.create(doc, 0, 0));
            // Apply created styles to document

            tr = (0, _CustomStyleCommand.applyStyle)(val.styles, val.stylename, this.props.editorState, tr);
            dispatch(tr);
            this.props.editorView.focus();

            this._stylePopup.close();

            this._stylePopup = null;
          }
        }
      }
    });
  }

}

var _default = CustomMenuUI;
exports.default = _default;