@edifice.io/tiptap-extensions
Version:
Edifice Rich Text Editor Extensions
1 lines • 11.3 kB
Source Map (JSON)
{"version":3,"file":"image.cjs","sources":["../../src/image/image.ts"],"sourcesContent":["import { WorkspaceElement } from '@edifice.io/client';\nimport { ImageResizer } from '@edifice.io/utilities';\nimport { mergeAttributes, nodeInputRule } from '@tiptap/core';\nimport TiptapImage from '@tiptap/extension-image';\nimport { Plugin } from 'prosemirror-state';\n\nexport const IMAGE_INPUT_REGEX =\n /(?:^|\\s)(!\\[(.+|:?)]\\((\\S+)(?:(?:\\s+)[\"'](\\S+)[\"'])?\\))$/;\n\nexport interface CustomImageOptions {\n HTMLAttributes: Record<string, string>;\n sizes: string[];\n uploadFile?: (file: File) => Promise<WorkspaceElement | null>;\n}\n\ninterface AttributesProps {\n width: number | string;\n height: number | string;\n size: string;\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n customImage: {\n setAttributes: (options: AttributesProps) => ReturnType;\n setNewImage: (options: {\n src: string;\n alt?: string;\n title?: string;\n }) => ReturnType;\n };\n }\n}\n\nexport const Image = TiptapImage.extend<CustomImageOptions>({\n name: 'custom-image',\n draggable: true,\n selectable: true,\n\n addOptions() {\n return {\n ...this.parent?.(),\n inline: true,\n content: 'inline*',\n sizes: ['small', 'medium', 'large'],\n HTMLAttributes: {\n class: 'custom-image',\n },\n uploadFile: () => {\n return Promise.resolve(null);\n },\n };\n },\n\n addAttributes() {\n return {\n ...this.parent?.(),\n size: {\n default: 'medium',\n rendered: false,\n },\n alt: {\n renderHTML: (attributes) => {\n return {\n alt: attributes.alt,\n };\n },\n parseHTML: (element) => element.getAttribute('alt'),\n },\n title: {\n renderHTML: (attributes) => {\n return {\n title: attributes.title,\n };\n },\n parseHTML: (element) => element.getAttribute('title'),\n },\n width: {\n default: '350',\n renderHTML: (attributes) => {\n if (\n attributes.width !== null &&\n attributes.width !== undefined &&\n !Number.isNaN(attributes.width)\n ) {\n return {\n width: parseInt(attributes.width),\n };\n }\n return {};\n },\n parseHTML: (element) => element.getAttribute('width'),\n },\n height: {\n renderHTML: (attributes) => {\n if (\n attributes.height !== null &&\n attributes.height !== undefined &&\n !Number.isNaN(attributes.height)\n ) {\n return {\n height: parseInt(attributes.height),\n };\n }\n return {};\n },\n parseHTML: (element) => element.getAttribute('height'),\n },\n style: {\n renderHTML: (attributes) => {\n return attributes.style\n ? {\n style: attributes.style,\n }\n : {};\n },\n parseHTML: (element) => {\n return null;\n },\n },\n };\n },\n\n parseHTML() {\n return [\n {\n tag: 'img[src]:not([src^=\"data:\"])',\n getAttrs: (el: HTMLImageElement) => {\n const attr = { src: el.getAttribute('src') };\n // Check old content format and get the width from the parent element\n if (el.parentElement?.className.includes('image-container')) {\n if (el.parentElement.style?.width) {\n attr['width'] = el.parentElement.style.width;\n }\n }\n if (el.style?.width) {\n attr['width'] = el.style.width;\n }\n\n // Check old content smiley\n const oldSmileyList = [\n 'happy',\n 'proud',\n 'dreamy',\n 'love',\n 'tired',\n 'angry',\n 'worried',\n 'sick',\n 'joker',\n 'sad',\n ];\n if (\n oldSmileyList.filter((smiley) => attr.src.includes(smiley + '.png'))\n .length > 0\n ) {\n attr['style'] = {\n width: '1.5em',\n height: '1.5em',\n fontSize: '16px',\n };\n attr['width'] = 'null';\n attr['height'] = 'null';\n }\n\n return attr;\n },\n },\n ];\n },\n\n renderHTML({ HTMLAttributes }) {\n return [\n 'img',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n ];\n },\n\n addInputRules() {\n return [\n nodeInputRule({\n find: IMAGE_INPUT_REGEX,\n type: this.type,\n getAttributes: (match) => {\n const [, , alt, src, title] = match;\n\n return {\n src,\n alt,\n title,\n };\n },\n }),\n ];\n },\n\n addCommands() {\n return {\n setNewImage:\n (attrs) =>\n ({ tr, dispatch }) => {\n const { selection } = tr;\n const node = this.type.create(attrs);\n\n if (dispatch) {\n tr.replaceRangeWith(selection.from, selection.to, node);\n }\n\n return true;\n },\n setAttributes:\n (attributes) =>\n ({ tr, dispatch }) => {\n const { selection } = tr;\n\n const nodeAttrs = tr.doc.nodeAt(tr.selection.from);\n const options = {\n ...nodeAttrs.attrs,\n ...attributes,\n };\n const node = this.type.create(options);\n\n if (dispatch) {\n tr.replaceRangeWith(selection.from, selection.to, node);\n }\n\n return true;\n },\n };\n },\n\n addProseMirrorPlugins() {\n const uploadNode = async (file: File) => {\n /**\n * Resize the image\n */\n const resizedImage = await ImageResizer.resizeImageFile(file);\n\n /**\n * Upload the image\n */\n const image = await this.options.uploadFile(resizedImage);\n\n /**\n * Get the image url\n */\n const imageUrl = `/workspace/${image.public ? 'pub/' : ''}document/${\n image._id\n }?timestamp=${new Date().getTime()}`;\n\n /**\n * Create the image node\n */\n\n const node = this.type.create({\n src: imageUrl,\n alt: image.alt,\n title: image.title,\n });\n\n return node;\n };\n\n const getFilteredFiles = (files: FileList) => {\n return Array.from(files).filter((file) =>\n /image\\/(png|jpeg|jpg|gif|webp|heic|avif)/.test(file.type),\n );\n };\n\n const handleImageInsert = async (\n editor: any,\n file: File,\n position?: number,\n ) => {\n const node = await uploadNode(file);\n if (!node) return;\n\n const transaction =\n position !== undefined\n ? editor.state.tr.insert(position, node)\n : editor.state.tr.replaceSelectionWith(node);\n\n editor.dispatch(transaction);\n };\n\n return [\n new Plugin({\n props: {\n handlePaste: (editor, e) => {\n const files = getFilteredFiles(e.clipboardData?.files);\n if (files.length === 0) return false;\n\n for (const file of files) {\n handleImageInsert(editor, file);\n }\n\n return true;\n },\n handleDrop: (editor, e, _s, moved) => {\n if (moved) return false;\n\n const files = getFilteredFiles(e.dataTransfer.files);\n if (files.length === 0) return false;\n\n const { pos: position } = editor.posAtCoords({\n left: e.clientX,\n top: e.clientY,\n });\n\n for (const file of files) {\n handleImageInsert(editor, file, position);\n }\n return true;\n },\n },\n }),\n ];\n },\n});\n"],"names":["mergeAttributes","nodeInputRule","ImageResizer","Plugin"],"mappings":"2PAMa,kBACX,2DA2BW,MAAQ,YAAY,OAA2B,CAC1D,KAAM,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