Skip to content

Commit

Permalink
feat: markmap supports config IMAGE_QUALITY_WHEN_DOWNLOAD_SVG、BACKGRO…
Browse files Browse the repository at this point in the history
…UND_COLOR_WHEN_DOWNLOAD_SVG
  • Loading branch information
obgnail committed Dec 29, 2024
1 parent 89bad25 commit 5cfaeab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions plugin/global/settings/settings.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ REMOVE_USELESS_CLASS_NAME_WHEN_DOWNLOAD_SVG = true
REMOVE_FOREIGN_OBJECT_WHEN_DOWNLOAD_SVG = false
# 导出时,图形的内边距,第一项为水平内边距,第二项为垂直内边距
BORDER_WHEN_DOWNLOAD_SVG = [50, 50]
# 导出格式为jpg或webp,图片的质量(范围:从0到1)
IMAGE_QUALITY_WHEN_DOWNLOAD_SVG = 0.92
# 导出不带透明通道的图片格式(如jpg),设置背景颜色
BACKGROUND_COLOR_WHEN_DOWNLOAD_SVG = "#ffffff"
# 导出前询问导出路径
SHOW_DIALOG_WHEN_DOWNLOAD_SVG = true
# 导出后打开文件所在目录
Expand Down
9 changes: 7 additions & 2 deletions plugin/markmap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ class tocMarkmap {
AUTO_COLLAPSE_PARAGRAPH_WHEN_FOLD: "实验性特性,不建议开启。仅当插件「章节折叠」开启时可用",
FOLDER_WHEN_DOWNLOAD_SVG: "若为空或不存在,则使用 TEMP 目录",
FILENAME_WHEN_DOWNLOAD_SVG: `支持变量:filename、timestamp、uuid\n支持后缀:${Downloader.getFormats()[0].extensions.join("、")}`,
IMAGE_QUALITY_WHEN_DOWNLOAD_SVG: "仅适用 jpg、webp 格式",
BACKGROUND_COLOR_WHEN_DOWNLOAD_SVG: "仅适用不带透明通道的图片格式(如 jpg)",
REMOVE_CSS_VARIABLE_WHEN_DOWNLOAD_SVG: "有些 SVG 解析器无法解析 CSS 变量,勾选此选项可以提高兼容性",
REMOVE_USELESS_CLASS_NAME_WHEN_DOWNLOAD_SVG: "若需要手动修改导出的 SVG 文件,请勿勾选此选项",
REMOVE_FOREIGN_OBJECT_WHEN_DOWNLOAD_SVG: "牺牲样式,提高兼容性。若导出的图片异常,请勾选此选项",
Expand Down Expand Up @@ -580,6 +582,8 @@ class tocMarkmap {
return [
{ fieldset, type: "number", inline: true, min: 1, max: 1000, step: 1, ...borderKV("水平内边距", 0) },
{ fieldset, type: "number", inline: true, min: 1, max: 1000, step: 1, ...borderKV("垂直内边距", 1) },
{ fieldset, type: "number", inline: true, min: 0.01, max: 1, step: 0.01, ...inputKV("图片质量", "IMAGE_QUALITY_WHEN_DOWNLOAD_SVG") },
{ fieldset, type: "color", inline: true, ...inputKV("背景颜色", "BACKGROUND_COLOR_WHEN_DOWNLOAD_SVG") },
{ fieldset, type: "input", inline: true, placeholder: this.utils.tempFolder, ...inputKV("默认文件夹", "FOLDER_WHEN_DOWNLOAD_SVG") },
{ fieldset, type: "input", inline: true, ...inputKV("默认文件名", "FILENAME_WHEN_DOWNLOAD_SVG") },
{ fieldset, label: "", ...checkboxKV(components) },
Expand Down Expand Up @@ -1009,7 +1013,8 @@ class Downloader {
}
ctx.drawImage(img, 0, 0, width, height)

const base64 = canvas.toDataURL(`image/${format}`).replace(`data:image/${format};base64,`, "")
const encoderOptions = parseFloat(plugin.config.IMAGE_QUALITY_WHEN_DOWNLOAD_SVG)
const base64 = canvas.toDataURL(`image/${format}`, encoderOptions).replace(`data:image/${format};base64,`, "")
return Buffer.from(base64, "base64")
}

Expand All @@ -1021,7 +1026,7 @@ class Downloader {
static png = async (plugin) => this._toImage(plugin, "png")

static jpg = async (plugin) => this._toImage(plugin, "jpeg", (ctx, canvas) => {
ctx.fillStyle = "#fff"
ctx.fillStyle = plugin.config.BACKGROUND_COLOR_WHEN_DOWNLOAD_SVG
ctx.fillRect(0, 0, canvas.width, canvas.height)
})

Expand Down

0 comments on commit 5cfaeab

Please sign in to comment.