"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isMenuDisabled = exports.clearStyles = void 0;
var _slate = require("slate");
var _elementTypes = require("../../constants/element-types");
var _core = require("../../core");
const isMenuDisabled = function (editor) {
  let readonly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  if (readonly) return true;
  if (editor.selection == null) return true;
  const [match] = _slate.Editor.nodes(editor, {
    match: n => {
      const type = (0, _core.getNodeType)(n);
      if ([_elementTypes.CODE_BLOCK, _elementTypes.CODE_LINE].includes(type)) return true; // code-block
      if (_slate.Editor.isVoid(editor, n)) return true; // void node

      return false;
    },
    universal: true
  });
  if (match) return true;
  return false;
};
exports.isMenuDisabled = isMenuDisabled;
const removeMarks = (editor, textNode) => {
  // Iterate text node properties, clearing styles
  const keys = Object.keys(textNode);
  keys.forEach(key => {
    if (key === 'text') {
      // Keep only the text attribute （Text attributes are necessary）
      return;
    }
    // All other attributes are deleted
    _slate.Editor.removeMark(editor, key);
  });
};
const clearStyles = editor => {
  const nodeEntries = _slate.Editor.nodes(editor, {
    match: n => _slate.Text.isText(n),
    universal: true
  });
  for (const nodeEntry of nodeEntries) {
    // single text node
    const n = nodeEntry[0];
    removeMarks(editor, n);
  }
};
exports.clearStyles = clearStyles;