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

feat: allow media urls to be base 64 data urls #881

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion src/js/media/MediaType.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import PDF from "./types/PDF"
import Audio from "./types/Audio"
import Video from "./types/Video"
import Wistia from "./types/Wistia"
import Base64 from "./types/Base64"

/**
* Given a JavaScript Object for an event from a TimelineConfig,
Expand Down Expand Up @@ -217,7 +218,14 @@ export function lookupMediaType(m, image_only) {
name: "Imageblank",
match_str: "",
cls: Image
}
},
{
type: "base64",
name: "Base64",
match_str: /data:(image\/(?:apng|avif|gif|jpeg|png|webp));base64,[a-zA-Z0-9+\/]+/i
/i,
cls: Image
},
]

if (image_only) {
Expand Down
61 changes: 61 additions & 0 deletions src/js/media/types/Base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Media } from "../Media"
import { unhtmlify, transformMediaURL } from "../../core/Util"
import * as Browser from "../../core/Browser"

export default class Base64 extends Media {

_loadMedia() {
// Loading Message
this.loadingMessage();

// Create media?
if (!this.options.background) {
this.createMedia();
}

// After loaded
this.onLoaded();
}

createMedia() {
var self = this,
image_class = "tl-media-item tl-media-image";

// if (this.data.url.match(/.png(\?.*)?$/) || this.data.url.match(/.svg(\?.*)?$/)) {
// image_class = "tl-media-item tl-media-image"
// }

this._el.content_item = this.domCreate("img", image_class, this._el.content);

if (this.data.alt) {
this._el.content_item.alt = this.data.alt;
} else if (this.data.caption) {
this._el.content_item.alt = unhtmlify(this.data.caption);
}

if (this.data.title) {
this._el.content_item.title = this.data.title;
} else if (this.data.caption) {
this._el.content_item.title = unhtmlify(this.data.caption);
}

// Media Loaded Event
this._el.content_item.addEventListener('load', function(e) {
self.onMediaLoaded();
});

this._el.content_item.src = this.getImageURL();
}

getImageURL(w, h) {
return transformMediaURL(this.data.url);
}

_updateMediaDisplay(layout) {
if (Browser.firefox) {
//this._el.content_item.style.maxWidth = (this.options.width/2) - 40 + "px";
this._el.content_item.style.width = "auto";
}
}

}
Loading