Skip to content

Commit

Permalink
Merge pull request #524 from aSeriousDeveloper/img-srcset
Browse files Browse the repository at this point in the history
`img` attributes to facilitate the use of Responsive Images & Media Libraries
  • Loading branch information
awcodes authored Jan 6, 2025
2 parents ce9c4fd + 041c42b commit 908f481
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
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 @@ -461,6 +461,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,
],
];
}
}

0 comments on commit 908f481

Please sign in to comment.