debian-mirror-gitlab/app/assets/javascripts/content_editor/extensions/image.js

70 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-06-08 01:23:25 +05:30
import { Image } from '@tiptap/extension-image';
2021-09-30 23:02:18 +05:30
import { VueNodeViewRenderer } from '@tiptap/vue-2';
import ImageWrapper from '../components/wrappers/image.vue';
const resolveImageEl = (element) =>
element.nodeName === 'IMG' ? element : element.querySelector('img');
2021-10-27 15:23:28 +05:30
export default Image.extend({
2021-09-30 23:02:18 +05:30
defaultOptions: {
...Image.options,
2021-10-27 15:23:28 +05:30
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
return {
src: img.dataset.src || img.getAttribute('src'),
};
},
},
2021-09-30 23:02:18 +05:30
canonicalSrc: {
default: null,
parseHTML: (element) => {
return {
canonicalSrc: element.dataset.canonicalSrc,
};
},
},
2021-09-04 01:27:46 +05:30
alt: {
default: null,
parseHTML: (element) => {
2021-09-30 23:02:18 +05:30
const img = resolveImageEl(element);
2021-09-04 01:27:46 +05:30
return {
alt: img.getAttribute('alt'),
};
},
},
};
},
parseHTML() {
return [
{
priority: 100,
tag: 'a.no-attachment-icon',
},
{
tag: 'img[src]',
},
];
},
2021-09-30 23:02:18 +05:30
addNodeView() {
return VueNodeViewRenderer(ImageWrapper);
},
});