import { createPluginFactory } from '@udecode/plate-common';

import { ImagePlugin } from './types';

export const ELEMENT_ATTACHMENT_IMAGE = 'attachment_image';

export const createAttachmentImagePlugin = createPluginFactory<ImagePlugin>({
  key: ELEMENT_ATTACHMENT_IMAGE,
  isElement: true,
  isVoid: true,
  then: (_editor, { type }) => ({
    deserializeHtml: {
      rules: [
        {
          validNodeName: 'IMG',
        },
      ],
      getNode: (el) => ({
        type,
        url: el.getAttribute('src'),
      }),
    },
  }),
});
