Skip to content
File

Blob: src/shared/editor/schema/image.ts

typescript32 lines
1import { mergeAttributes } from "@tiptap/core";
2import { Image, type ImageOptions } from "@tiptap/extension-image";
3import { MAX_IMAGE_BRIGHTNESS, normalizeImageBrightness } from "./image-model";
4export { normalizeImageAlign, type ImageAlign } from "./image-model";
5 
6export const SharedImage = Image.extend<ImageOptions>({
7 addAttributes() {
8 return {
9 ...this.parent?.(),
10 align: { default: "left" },
11 width: { default: null },
12 naturalWidth: { default: null },
13 naturalHeight: { default: null },
14 pendingInsertId: { default: null },
15 brightness: {
16 default: MAX_IMAGE_BRIGHTNESS,
17 parseHTML: (element) => normalizeImageBrightness(element.getAttribute("data-brightness")),
18 renderHTML: (attrs) => {
19 const brightness = normalizeImageBrightness(attrs.brightness);
20 return brightness === MAX_IMAGE_BRIGHTNESS
21 ? {}
22 : { "data-brightness": brightness, style: `filter: brightness(${brightness / 100})` };
23 },
24 },
25 };
26 },
27 
28 renderHTML({ HTMLAttributes }) {
29 return ["img", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)];
30 },
31});