2020-04-09 11:13:15 -04:00
|
|
|
const SCALES = ["100", "75", "50"];
|
|
|
|
|
2019-11-12 15:32:37 -05:00
|
|
|
function isUpload(token) {
|
|
|
|
return token.content.includes("upload://");
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasMetadata(token) {
|
|
|
|
return token.content.match(/(\d{1,4}x\d{1,4})/);
|
|
|
|
}
|
|
|
|
|
2020-04-09 11:13:15 -04:00
|
|
|
function appendMetaData(index, token) {
|
|
|
|
const sizePart = token.content
|
2020-02-06 10:19:24 -05:00
|
|
|
.split("|")
|
|
|
|
.find((x) => x.match(/\d{1,4}x\d{1,4}(,\s*\d{1,3}%)?/));
|
|
|
|
let selectedScale =
|
2020-04-09 11:13:15 -04:00
|
|
|
sizePart && sizePart.split(",").pop().trim().replace("%", "");
|
2020-09-04 07:42:47 -04:00
|
|
|
|
2020-04-09 11:13:15 -04:00
|
|
|
const overwriteScale = !SCALES.find((scale) => scale === selectedScale);
|
|
|
|
if (overwriteScale) selectedScale = "100";
|
2019-11-12 15:32:37 -05:00
|
|
|
|
2020-04-09 11:13:15 -04:00
|
|
|
token.attrs.push(["index-image", index]);
|
|
|
|
token.attrs.push(["scale", selectedScale]);
|
2019-11-12 15:32:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function rule(state) {
|
2020-04-09 11:13:15 -04:00
|
|
|
let currentIndex = 0;
|
2019-11-12 15:32:37 -05:00
|
|
|
|
|
|
|
for (let i = 0; i < state.tokens.length; i++) {
|
|
|
|
let blockToken = state.tokens[i];
|
|
|
|
const blockTokenImage = blockToken.tag === "img";
|
|
|
|
|
|
|
|
if (blockTokenImage && isUpload(blockToken) && hasMetadata(blockToken)) {
|
2020-04-09 11:13:15 -04:00
|
|
|
appendMetaData(currentIndex, blockToken);
|
|
|
|
currentIndex++;
|
2019-11-12 15:32:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!blockToken.children) continue;
|
|
|
|
|
|
|
|
for (let j = 0; j < blockToken.children.length; j++) {
|
|
|
|
let token = blockToken.children[j];
|
|
|
|
const childrenImage = token.tag === "img";
|
|
|
|
|
|
|
|
if (childrenImage && isUpload(blockToken) && hasMetadata(token)) {
|
2020-04-09 11:13:15 -04:00
|
|
|
appendMetaData(currentIndex, token);
|
|
|
|
currentIndex++;
|
2019-11-12 15:32:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 11:13:15 -04:00
|
|
|
}
|
2019-11-12 15:32:37 -05:00
|
|
|
|
2020-04-09 11:13:15 -04:00
|
|
|
function buildScaleButton(selectedScale, scale) {
|
2020-07-06 19:11:58 -04:00
|
|
|
const activeScaleClass = selectedScale === scale ? " active" : "";
|
2020-04-09 11:13:15 -04:00
|
|
|
return (
|
|
|
|
"<span class='scale-btn" +
|
|
|
|
activeScaleClass +
|
|
|
|
"' data-scale='" +
|
|
|
|
scale +
|
|
|
|
"'>" +
|
|
|
|
scale +
|
|
|
|
"%</span>"
|
|
|
|
);
|
2019-11-12 15:32:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
|
|
|
const opts = helper.getOptions();
|
|
|
|
if (opts.previewing) {
|
|
|
|
helper.whiteList([
|
2020-01-15 12:01:14 -05:00
|
|
|
"span.image-wrapper",
|
|
|
|
"span.button-wrapper",
|
2019-11-12 15:32:37 -05:00
|
|
|
"span[class=scale-btn]",
|
|
|
|
"span[class=scale-btn active]",
|
|
|
|
"span.separator",
|
|
|
|
"span.scale-btn[data-scale]",
|
|
|
|
"span.button-wrapper[data-image-index]",
|
|
|
|
]);
|
|
|
|
|
|
|
|
helper.registerPlugin((md) => {
|
2020-04-09 11:13:15 -04:00
|
|
|
const oldRule = md.renderer.rules.image;
|
|
|
|
|
|
|
|
md.renderer.rules.image = function (tokens, idx, options, env, slf) {
|
|
|
|
const token = tokens[idx];
|
|
|
|
const scaleIndex = token.attrIndex("scale");
|
|
|
|
const imageIndex = token.attrIndex("index-image");
|
|
|
|
|
|
|
|
if (scaleIndex !== -1) {
|
|
|
|
var selectedScale = token.attrs[scaleIndex][1];
|
|
|
|
var index = token.attrs[imageIndex][1];
|
|
|
|
|
|
|
|
let result = "<span class='image-wrapper'>";
|
|
|
|
result += oldRule(tokens, idx, options, env, slf);
|
|
|
|
|
|
|
|
result +=
|
|
|
|
"<span class='button-wrapper' data-image-index='" + index + "'>";
|
|
|
|
|
|
|
|
result += SCALES.map((scale) =>
|
|
|
|
buildScaleButton(selectedScale, scale)
|
2020-07-06 19:11:58 -04:00
|
|
|
).join("");
|
2020-04-09 11:13:15 -04:00
|
|
|
|
|
|
|
result += "</span></span>";
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return oldRule(tokens, idx, options, env, slf);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-12 15:32:37 -05:00
|
|
|
md.core.ruler.after("upload-protocol", "resize-controls", rule);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|