Skip to content
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
9 changes: 6 additions & 3 deletions src/custom-html.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Great enhancement! Making htmlToInsert accept both a string and an async function adds a lot of flexibility — love the use of await new Promise() here.

One small suggestion: to improve clarity and prevent misuse, you could consider wrapping new Promise(this.options.htmlToInsert) in a helper or add a check for whether the function expects resolve/reject.

But overall, clean upgrade and backward-compatible. Approved 🚀

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @param {Object} options An object containing the extension configuration. The
* following fields should be provided:
* - buttonText: the text of the button (default: `</>`)
* - htmlToInsert: the HTML code that should be inserted
* - htmlToInsert: the HTML code that should be inserted / resolver function
*/
function CustomHtml(options) {
this.button = document.createElement('button');
Expand Down Expand Up @@ -67,8 +67,11 @@
* @name onClick
* @function
*/
CustomHtml.prototype.onClick = function () {
CustomHtml.insertHtmlAtCaret(this.options.htmlToInsert);
CustomHtml.prototype.onClick = async function () {
var html = typeof this.options.htmlToInsert === "function"
? await new Promise(this.options.htmlToInsert) // resolver function
: this.options.htmlToInsert;
CustomHtml.insertHtmlAtCaret(html);
};

/**
Expand Down
8 changes: 8 additions & 0 deletions test/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ window.onload = function () {
, "header2"
, "quote"
, "customHtml"
, "imgHtml"
]
, extensions: {
"customHtml": new CustomHtml({
buttonText: "<hr>"
, htmlToInsert: "<hr class='someclass'>"
}),
"imgHtml": new CustomHtml({
buttonText: "<img>"
, htmlToInsert: resolve => {
var imgUrl = prompt("Image url", "http://");
resolve("<div><img src='"+ imgUrl +"' alt='' /></div>");
}
})
}
});
Expand Down