Skip to content

Commit 7cbe82d

Browse files
committed
add image size tag in image option header
1 parent 48edd7d commit 7cbe82d

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Component, onWillStart, useState } from "@odoo/owl";
2+
import { useDomState } from "@html_builder/core/utils";
3+
import { loadImageDataURL, getImageSizeFromCache } from "@html_editor/utils/image_processing";
4+
import { KeepLast } from "@web/core/utils/concurrency";
5+
6+
export class ImageSizeTag extends Component {
7+
static template = "website.ImageSizeTag";
8+
setup() {
9+
this.keepLast = new KeepLast();
10+
this.state = useState({ size: 0 });
11+
useDomState((imageEl) => this.updateImageSize(imageEl));
12+
onWillStart(() => this.updateImageSize(this.env.getEditingElement()));
13+
}
14+
15+
async updateImageSize(imageEl) {
16+
if (imageEl !== this.env.getEditingElement()) {
17+
return;
18+
}
19+
const src = imageEl.src;
20+
await this.keepLast.add(loadImageDataURL(src));
21+
this.state.size = Math.round((getImageSizeFromCache(src) / 1024) * 10) / 10;
22+
}
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<templates xml:space="preserve">
3+
<t t-name="website.ImageSizeTag">
4+
<span class="badge text-bg-dark" title="Size"><t t-out="state.size"/> kB</span>
5+
</t>
6+
</templates>

addons/website/static/src/builder/plugins/image/image_tool_option_plugin.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ALIGNMENT_STYLE_PADDING,
1111
} from "@html_builder/utils/option_sequence";
1212
import { ReplaceMediaOption, searchSupportedParentLinkEl } from "./replace_media_option";
13+
import { ImageSizeTag } from "./image_size_tag";
1314

1415
export const REPLACE_MEDIA_SELECTOR = "img, .media_iframe_video, span.fa, i.fa";
1516
export const REPLACE_MEDIA_EXCLUDE =
@@ -27,6 +28,10 @@ class ImageToolOptionPlugin extends Plugin {
2728
];
2829
static shared = ["canHaveHoverEffect"];
2930
resources = {
31+
builder_header_middle_buttons: {
32+
Component: ImageSizeTag,
33+
selector: "img",
34+
},
3035
builder_options: [
3136
withSequence(REPLACE_MEDIA, {
3237
OptionComponent: ReplaceMediaOption,

0 commit comments

Comments
 (0)