import { Node_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { normalizeLineHeight } from "./../../_shared/line-spacing-utils.js";
import { mergeAttributes } from "./../../tiptap/index.js";
import { Fragment, Slice } from "./../../tiptap/pm/model.js";
import { Plugin, PluginKey } from "./../../tiptap/pm/state.js";
import { createRicosNodeExtension } from "./../../tiptap-extensions/index.js";
import {
  createDirectionAttributeDecorationPlugin,
  createSpanWrapperDecorationPlugin
} from "../decoration-plugins/index.js";
import styles from "../statics/styles-scss.js";
const createStyleAttribute = (node) => {
  const attrLineHeight = node.attrs.textStyle?.lineHeight;
  const lineHeight = attrLineHeight ? `line-height: ${normalizeLineHeight(attrLineHeight)}` : "";
  const style = lineHeight;
  return { style };
};
const paragraph = createRicosNodeExtension({
  groups: ["shortcuts-enabled", "no-trailing-node"],
  type: "html-node",
  name: Node_Type.PARAGRAPH,
  reconfigure: (config, _extensions, _props, _settings, services) => {
    return {
      ...config,
      addProseMirrorPlugins() {
        return [
          createSpanWrapperDecorationPlugin([Node_Type.PARAGRAPH]),
          createDirectionAttributeDecorationPlugin(
            Node_Type.PARAGRAPH,
            services.context.localeContent
          ),
          new Plugin({
            key: new PluginKey("handlePlainTextPaste"),
            props: {
              handlePaste: (view, event) => {
                const { state } = view;
                const { tr } = state;
                if (event.clipboardData?.types.includes("text/html")) {
                  return false;
                }
                const clipboardText = event.clipboardData?.getData("text/plain").trim();
                if (!clipboardText) {
                  return false;
                }
                const textLines = clipboardText.split(/(?:\r\n|\r|\n)/g);
                const nodes = textLines.map((line) => {
                  if (line === "") {
                    return state.schema.nodes.PARAGRAPH.create();
                  } else {
                    const textNode = state.schema.text(line);
                    return state.schema.nodes.PARAGRAPH.create(null, textNode);
                  }
                });
                view.dispatch(
                  tr.replaceSelection(Slice.maxOpen(Fragment.fromArray(nodes))).scrollIntoView()
                );
                return true;
              }
            }
          })
        ];
      }
    };
  },
  createExtensionConfig() {
    return {
      name: this.name,
      priority: 1e3,
      defining: true,
      addOptions() {
        return {
          HTMLAttributes: {
            class: styles.paragraph
          }
        };
      },
      group: "block smartBlockCellContent layoutCellContent collapsibleListItemTitleContent collapsibleListItemBodyContent tableCellContent listItemFirstChildContent listItemSecondChildContent",
      content: "inline*",
      addAttributes() {
        return {
          indentation: null,
          textStyle: {
            default: null,
            parseHTML: (element) => {
              try {
                const attrValue = element.getAttribute("textstyle");
                return attrValue ? JSON.parse(attrValue) : null;
              } catch {
                return null;
              }
            }
          },
          level: null
        };
      },
      parseHTML() {
        return [{ tag: "p" }, { tag: "br" }];
      },
      renderHTML({ HTMLAttributes, node }) {
        const styles2 = createStyleAttribute(node);
        const attrs = mergeAttributes(
          this.options.HTMLAttributes,
          { ...HTMLAttributes, textStyle: JSON.stringify(HTMLAttributes.textStyle) },
          styles2
        );
        return ["p", attrs, 0];
      },
      addCommands() {
        return {
          setParagraph: () => ({ commands }) => {
            return commands.updateTextNode(this.name);
          }
        };
      },
      addKeyboardShortcuts() {
        return {
          // 'Mod-Alt-0': () => this.editor.commands.setParagraph(),
        };
      }
    };
  }
});
export {
  paragraph
};
//# sourceMappingURL=extension.js.map
