2021-06-08 01:23:25 +05:30
|
|
|
import { Image } from '@tiptap/extension-image';
|
2021-11-11 11:23:49 +05:30
|
|
|
import { PARSE_HTML_PRIORITY_HIGHEST } from '../constants';
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
const resolveImageEl = (element) =>
|
|
|
|
element.nodeName === 'IMG' ? element : element.querySelector('img');
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
export default Image.extend({
|
2022-01-26 12:08:38 +05:30
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
...this.parent?.(),
|
|
|
|
inline: true,
|
|
|
|
};
|
2021-09-30 23:02:18 +05:30
|
|
|
},
|
2021-09-04 01:27:46 +05:30
|
|
|
addAttributes() {
|
|
|
|
return {
|
|
|
|
...this.parent?.(),
|
2021-09-30 23:02:18 +05:30
|
|
|
uploading: {
|
|
|
|
default: false,
|
|
|
|
},
|
2021-09-04 01:27:46 +05:30
|
|
|
src: {
|
|
|
|
default: null,
|
|
|
|
/*
|
|
|
|
* GitLab Flavored Markdown provides lazy loading for rendering images. As
|
|
|
|
* as result, the src attribute of the image may contain an embedded resource
|
|
|
|
* instead of the actual image URL. The image URL is moved to the data-src
|
|
|
|
* attribute.
|
|
|
|
*/
|
|
|
|
parseHTML: (element) => {
|
2021-09-30 23:02:18 +05:30
|
|
|
const img = resolveImageEl(element);
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
return img.dataset.src || img.getAttribute('src');
|
2021-09-04 01:27:46 +05:30
|
|
|
},
|
|
|
|
},
|
2021-09-30 23:02:18 +05:30
|
|
|
canonicalSrc: {
|
2021-11-11 11:23:49 +05:30
|
|
|
default: null,
|
|
|
|
parseHTML: (element) => element.dataset.canonicalSrc,
|
2022-08-27 11:52:29 +05:30
|
|
|
renderHTML: () => '',
|
2021-11-11 11:23:49 +05:30
|
|
|
},
|
|
|
|
alt: {
|
2021-09-30 23:02:18 +05:30
|
|
|
default: null,
|
|
|
|
parseHTML: (element) => {
|
2021-11-11 11:23:49 +05:30
|
|
|
const img = resolveImageEl(element);
|
|
|
|
|
|
|
|
return img.getAttribute('alt');
|
2021-09-30 23:02:18 +05:30
|
|
|
},
|
|
|
|
},
|
2021-11-11 11:23:49 +05:30
|
|
|
title: {
|
2021-09-04 01:27:46 +05:30
|
|
|
default: null,
|
|
|
|
parseHTML: (element) => {
|
2021-09-30 23:02:18 +05:30
|
|
|
const img = resolveImageEl(element);
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
return img.getAttribute('title');
|
2021-09-04 01:27:46 +05:30
|
|
|
},
|
|
|
|
},
|
2023-03-04 22:38:38 +05:30
|
|
|
width: {
|
|
|
|
default: null,
|
|
|
|
parseHTML: (element) => {
|
|
|
|
const img = resolveImageEl(element);
|
|
|
|
|
|
|
|
return img.getAttribute('width');
|
|
|
|
},
|
|
|
|
},
|
|
|
|
height: {
|
|
|
|
default: null,
|
|
|
|
parseHTML: (element) => {
|
|
|
|
const img = resolveImageEl(element);
|
|
|
|
|
|
|
|
return img.getAttribute('height');
|
|
|
|
},
|
|
|
|
},
|
2022-08-27 11:52:29 +05:30
|
|
|
isReference: {
|
|
|
|
default: false,
|
|
|
|
renderHTML: () => '',
|
|
|
|
},
|
2021-09-04 01:27:46 +05:30
|
|
|
};
|
|
|
|
},
|
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
2021-11-11 11:23:49 +05:30
|
|
|
priority: PARSE_HTML_PRIORITY_HIGHEST,
|
2021-09-04 01:27:46 +05:30
|
|
|
tag: 'a.no-attachment-icon',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'img[src]',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
},
|
2022-03-02 08:16:31 +05:30
|
|
|
renderHTML({ HTMLAttributes }) {
|
|
|
|
return [
|
|
|
|
'img',
|
|
|
|
{
|
|
|
|
src: HTMLAttributes.src,
|
|
|
|
alt: HTMLAttributes.alt,
|
|
|
|
title: HTMLAttributes.title,
|
2023-03-04 22:38:38 +05:30
|
|
|
width: HTMLAttributes.width,
|
|
|
|
height: HTMLAttributes.height,
|
2022-03-02 08:16:31 +05:30
|
|
|
},
|
|
|
|
];
|
|
|
|
},
|
2021-09-30 23:02:18 +05:30
|
|
|
});
|