Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

img attributes to facilitate the use of Responsive Images & Media Libraries #524

Merged
merged 5 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ See `vendor/awcodes/filament-tiptap-editor/src/Actions/LinkAction.php` for imple

You may override the default Media modal with your own Action and assign to the `media_action` key in the config file. Make sure the default name for your action is `filament_tiptap_media`.

The Media Modal can make use of 3 attributes not exposed by default:

- `srcset` is used for selecting a series of responsive images to display for different browser viewports. [Docs](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/srcset)
- `sizes` goes alongside `srcset` to specify sizing rules for responsive images. [Docs](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/sizes)
- `media` provides support for an arbitrary ID value to better integrate with Media stored within a Database.

See `vendor/awcodes/filament-tiptap-editor/src/Actions/MediaAction.php` for implementation.

### Grid Builder Modal
Expand Down
21 changes: 20 additions & 1 deletion resources/js/extensions/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,26 @@ export const CustomImage = Image.extend({
};
}
}
}
},
srcset: {
default: null,
},
sizes: {
default: null,
},
media: {
default: null,
parseHTML: element => element.getAttribute('data-media-id'),
renderHTML: attributes => {
if (!attributes.media) {
return {}
}

return {
'data-media-id': attributes.media,
}
},
},
};
},
});
3 changes: 3 additions & 0 deletions resources/js/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ export default function tiptap({
width: media?.width,
height: media?.height,
lazy: media?.lazy,
srcset: media?.srcset,
sizes: media?.sizes,
media: media?.media,
})
.run();
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/Extensions/Nodes/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ public function addAttributes(): array
: null;
},
],
'srcset' => [
'default' => null,
],
'sizes' => [
'default' => null,
],
'media' => [
'default' => null,
'parseHTML' => fn ($DOMNode) => $DOMNode->getAttribute('data-media-id') ?: null,
'renderHTML' => fn ($attributes) => $attributes->media ? ['data-media-id' => $attributes->media] : null,
],
];
}
}
Loading