Skip to content

Commit

Permalink
Merge pull request #732 from obgnail/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
obgnail authored Sep 3, 2024
2 parents fba1523 + 7137556 commit 6a9e872
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 55 deletions.
38 changes: 16 additions & 22 deletions plugin/custom/plugins/markdownLint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,22 @@ class markdownLintPlugin extends BaseCustomPlugin {
this.errors = [];
this.checkLintError = () => undefined;
this.fixLintError = () => undefined;
this.resetConfig = () => undefined;
this.l10n = require("./l10n.js");
this.entities = {
modal: document.querySelector("#plugin-markdownlint"),
iconGroup: document.querySelector("#plugin-markdownlint .plugin-markdownlint-icon-group"),
moveIcon: document.querySelector('#plugin-markdownlint .plugin-markdownlint-icon[action="move"]'),
tbody: document.querySelector("#plugin-markdownlint tbody"),
button: document.querySelector("#plugin-markdownlint-button"),
}
this.l10n = require("./l10n.js");
}

process = () => {
const _scrollSourceView = lineToGo => {
const cm = File.editor.sourceView.cm;
cm.scrollIntoView({ line: lineToGo - 1, ch: 0 });
cm.setCursor({ line: lineToGo - 1, ch: 0 });
}
const _getDetail = (infos = this.errors) => {
const label = "详细信息";
const obj = infos.map(i => this.utils.fromObject(i, ["lineNumber", "ruleNames", "errorDetail", "errorContext", "errorRange", "fixInfo"]));
const content = JSON.stringify(obj.length === 1 ? obj[0] : obj, null, "\t");
const components = [{ label, type: "textarea", rows: 15, readonly: "readonly", content }];
const components = [{ label: "详细信息", type: "textarea", rows: 15, readonly: "readonly", content }];
this.utils.modal({ title: "格式规范检测", width: "550px", components });
}
const _funcMap = {
Expand All @@ -65,10 +60,8 @@ class markdownLintPlugin extends BaseCustomPlugin {
this.checkLintError();
},
settings: () => {
const obj = this.config.rule_config;
const label = "当前配置";
const content = JSON.stringify(obj, null, "\t");
const components = [{ label, type: "textarea", rows: 15, readonly: "readonly", content }];
const content = JSON.stringify(this.config.rule_config, null, "\t");
const components = [{ label: "当前配置", type: "textarea", rows: 15, readonly: "readonly", content }];
this.utils.modal({ title: "格式规范检测", width: "550px", components });
},
detailAll: () => _getDetail(this.errors),
Expand All @@ -83,7 +76,7 @@ class markdownLintPlugin extends BaseCustomPlugin {
if (!File.editor.sourceView.inSourceMode) {
File.toggleSourceMode();
}
_scrollSourceView(lineToGo)
this.utils.scrollSourceView(lineToGo)
},
}
const initEventHandler = () => {
Expand Down Expand Up @@ -111,13 +104,13 @@ class markdownLintPlugin extends BaseCustomPlugin {
_funcMap.toggleSourceMode();
break;
case 0:
const a = ev.target.closest("[action]");
if (!a) {
const i = ev.target.closest("[action]");
if (!i) {
File.editor.restoreLastCursor(ev);
break;
}
const action = a.getAttribute("action");
const value = parseInt(a.dataset.value);
const action = i.getAttribute("action");
const value = parseInt(i.dataset.value);
_funcMap[action] && _funcMap[action](value);
break;
}
Expand Down Expand Up @@ -149,6 +142,7 @@ class markdownLintPlugin extends BaseCustomPlugin {
}
this.checkLintError = () => send("check");
this.fixLintError = (fixInfo = this.errors) => send("lint", { fixInfo });
this.resetConfig = () => worker.postMessage({ action: "assignConfig", payload: { config: this.config.rule_config } });
}

registerWorker();
Expand All @@ -167,9 +161,9 @@ class markdownLintPlugin extends BaseCustomPlugin {
const tbody = data.map((item, idx) => {
const [rule, _] = item.ruleNames;
const desc = (this.config.translate && this.l10n[rule]) || item.ruleDescription;
const info = `<a class="ion-information-circled" action="detailSingle" data-value="${idx}"></a>`;
const locate = `<a class="ion-android-locate" action="jumpToLine" data-value="${item.lineNumber}"></a>`;
const fixInfo = item.fixInfo ? `<a class="ion-wrench" action="fixSingle" data-value="${idx}"></a>` : '';
const info = `<i class="ion-information-circled" action="detailSingle" data-value="${idx}"></i>`;
const locate = `<i class="ion-android-locate" action="jumpToLine" data-value="${item.lineNumber}"></i>`;
const fixInfo = item.fixInfo ? `<i class="ion-wrench" action="fixSingle" data-value="${idx}"></i>` : '';
return `<tr><td>${item.lineNumber}</td><td>${rule}</td><td>${desc}</td><td>${info}${locate}${fixInfo}</td></tr>`
})
this.entities.tbody.innerHTML = tbody.length ? tbody.join("") : `<tr><td colspan="4">Empty</td></tr>`;
Expand All @@ -178,13 +172,13 @@ class markdownLintPlugin extends BaseCustomPlugin {

onLintMessage = async data => {
await this.utils.editCurrentFile(data);
this.utils.notification.show("已部分修复规范错误");
this.utils.notification.show("已修复规范错误");
this.checkLintError();
}

callback = async anchorNode => {
this.utils.toggleVisible(this.entities.modal);
await this.checkLintError();
this.checkLintError();
}
}

Expand Down
6 changes: 6 additions & 0 deletions plugin/global/core/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,12 @@ class utils {

static scrollByCid = (cid, height = -1, moveCursor = false, showHiddenElement = true) => this.scroll(File.editor.findElemById(cid), height, moveCursor, showHiddenElement);

static scrollSourceView = lineToGo => {
const cm = File.editor.sourceView.cm;
cm.scrollIntoView({ line: lineToGo - 1, ch: 0 });
cm.setCursor({ line: lineToGo - 1, ch: 0 });
}

// content: 字符串中,\n表示软换行;\n\n表示硬换行
static insertText = (anchorNode, content, restoreLastCursor = true) => {
if (restoreLastCursor) {
Expand Down
2 changes: 2 additions & 0 deletions plugin/global/settings/settings.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ REMOVE_USELESS_CLASS_NAME_WHEN_DOWNLOAD_SVG = false
# - false: 操作含有foreignObject的svg标签处处受限(比如说转为jpg),但是有好看的样式
# - true: 操作不受限,但是丢失了样式
REMOVE_FOREIGN_OBJECT_WHEN_DOWNLOAD_SVG = false
# 自动在资源管理器打开svg所在目录
SHOW_IN_FINDER_WHEN_DOWNLOAD_SVG = true
# 导出svg时,边框留白的宽度
# 第一项为左右宽度之和,第二项为上下宽度之和
BORDER_WHEN_DOWNLOAD_SVG = [50, 50]
Expand Down
23 changes: 16 additions & 7 deletions plugin/global/styles/markdownLint.css
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#plugin-markdownlint {
display: flex;
flex-direction: column;
top: 10%;
right: 25px;
margin: 5px;
overflow: auto;
resize: vertical;
width: ${modal_width};
line-height: ${this.config.modal_line_height};
width: ${modal_width};
min-width: 380px;
max-height: ${this.config.modal_max_height};
min-height: 150px;
min-width: 380px;
}

.plugin-markdownlint-icon-group {
display: flex;
flex-direction: row-reverse;
font-size: 1.2em;
font-size: 1.3em;
margin: 0.3em;
user-select: none;
}
Expand All @@ -30,7 +32,13 @@
transform: translateY(-2px) scale(1.2);
}

.plugin-markdownlint-table {
overflow: auto;
}

#plugin-markdownlint table {
cursor: default;
margin-top: 0;
font-size: ${this.config.modal_font_size};
}

Expand All @@ -40,12 +48,13 @@
text-align: center;
}

#plugin-markdownlint a + a {
margin-left: 0.7em;
#plugin-markdownlint i:hover {
color: var(--active-file-border-color, darkred);
cursor: pointer;
}

#plugin-markdownlint a:hover {
text-decoration: underline;
#plugin-markdownlint i + i {
margin-left: 0.7em;
}

#plugin-markdownlint-button {
Expand Down
1 change: 1 addition & 0 deletions plugin/global/styles/markmap.css
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ ${show_outline}
#plugin-markmap-svg {
flex: 1;
user-select: none;
color: currentColor;
}

#plugin-markmap-svg .markmap-foreign {
Expand Down
14 changes: 11 additions & 3 deletions plugin/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,23 @@ class helpPlugin extends BasePlugin {
isWin: Boolean(File.isWin),
isLinux: Boolean(File.isLinux),
isMac: Boolean(File.isMac),
enablePlugin: plugins.join("|"),
isFocusMode: File.isFocusMode,
isTypeWriterMode: File.isTypeWriterMode,
inSourceMode: File.editor.sourceView.inSourceMode,
isSidebarShown: File.editor.library.isSidebarShown(),
theme: theme,
enablePlugin: plugins.join("|"),
}
}

newIssue = async () => {
const info = await this.getInfo();
const url = "https://github.com/obgnail/typora_plugin/issues/new?body=" + encodeURIComponent(JSON.stringify(info));
this.utils.openUrl(url);
const components = [{ label: "环境信息", type: "textarea", rows: 10, readonly: "readonly", content: JSON.stringify(info, null, "\t") }];
const { response } = await this.utils.dialog.modalAsync({ title: "用户反馈", width: "500px", components });
if (response === 1) {
const url = "https://github.com/obgnail/typora_plugin/issues/new?body=" + encodeURIComponent(JSON.stringify(info));
this.utils.openUrl(url);
}
}

about = () => {
Expand Down
49 changes: 26 additions & 23 deletions plugin/markmap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ class tocMarkmap {
FILENAME_WHEN_DOWNLOAD_SVG: _filename,
FOLDER_WHEN_DOWNLOAD_SVG: _folder,
BORDER_WHEN_DOWNLOAD_SVG: _border,
SHOW_IN_FINDER_WHEN_DOWNLOAD_SVG: _showInFinder,
REMOVE_FOREIGN_OBJECT_WHEN_DOWNLOAD_SVG: _removeForeign,
REMOVE_USELESS_CLASS_NAME_WHEN_DOWNLOAD_SVG: _removeUselessClass,
REMEMBER_FOLD_WHEN_UPDATE: _rememberFold,
Expand All @@ -532,7 +533,7 @@ class tocMarkmap {
FIT_WHEN_FOLD: "折叠图形节点时自动重新适配窗口",
COLLAPSE_WHEN_FOLD: "实验性特性,依赖「章节折叠」插件,不推荐开启",
REMOVE_USELESS_CLASS: "若非需要手动修改导出的图形文件,请勿勾选此选项",
REMOVE_FOREIGN_OBJECT: "若非需要手动修改导出的图形文件,请勿勾选此选项",
REMOVE_FOREIGN_OBJECT: "保留样式但兼容性较差。若图片显示异常,请勾选此选项",
SAVE_FOLDER: "为空则使用 tmp 目录",
SAVE_FILE: "支持变量:filename、timestamp、uuid",
}
Expand Down Expand Up @@ -603,9 +604,11 @@ class tocMarkmap {
const borderKV = idx => ({ value: _border[idx], callback: w => _border[idx] = w });
const checkboxList = [
{ label: "删除无用的类名", value: "removeUselessClass", checked: _removeUselessClass, info: INFO.REMOVE_USELESS_CLASS },
{ label: "替换 foreignObject 标签", value: "removeForeignObject", checked: _removeForeign, info: INFO.REMOVE_FOREIGN_OBJECT },
{ label: "替换 &lt;foreignObject&gt; 标签", value: "removeForeignObject", checked: _removeForeign, info: INFO.REMOVE_FOREIGN_OBJECT },
{ label: "导出后自动打开文件所在目录", value: "showInFinder", checked: _showInFinder },
];
const checkboxCallback = submit => {
setCfg("SHOW_IN_FINDER_WHEN_DOWNLOAD_SVG", submit.includes("showInFinder"));
setCfg("REMOVE_USELESS_CLASS_NAME_WHEN_DOWNLOAD_SVG", submit.includes("removeUselessClass"));
setCfg("REMOVE_FOREIGN_OBJECT_WHEN_DOWNLOAD_SVG", submit.includes("removeForeignObject"));
}
Expand All @@ -632,35 +635,32 @@ class tocMarkmap {
}

download = async () => {
const removeSvgForeignObject = svg => {
const removeForeignObject = svg => {
svg.querySelectorAll("foreignObject").forEach(foreign => {
const { textContent, previousSibling } = foreign;
const x = parseInt(foreign.getAttribute("width")) + parseInt(foreign.getAttribute("x")) - 2;
const y = parseInt(foreign.closest("g").querySelector("line").getAttribute("y1")) - 4;
// const y = 16;

const text = document.createElement("text");
const [xAttr, yAttr] = (previousSibling.tagName === "line") ? ["x2", "y2"] : ["cx", "cy"];
const x = parseInt(previousSibling.getAttribute(xAttr)) - 12;
const y = parseInt(previousSibling.getAttribute(yAttr)) - 5;
text.setAttribute("x", x);
text.setAttribute("y", y);
text.setAttribute("text-anchor", "end");
text.textContent = textContent;
const katex = foreign.querySelector(".katex-html");
text.textContent = katex ? katex.textContent : foreign.textContent;
foreign.parentNode.replaceChild(text, foreign);
})
}

const removeClassName = svg => svg.querySelectorAll("*").forEach(ele => {
if (ele.classList.contains("markmap-node")) {
ele.removeAttribute("class");
}
})
const removeClassName = svg => svg.querySelectorAll(".markmap-node").forEach(ele => ele.removeAttribute("class"))

const removeSvgUselessStyle = svg => {
const removeUselessStyle = svg => {
const style = svg.querySelector("style");
if (style) {
style.textContent = style.textContent.replace(".markmap-node>circle{cursor:pointer}", "");
}
}

const getSvgBounding = svg => {
const getBounding = svg => {
const { width, height } = this.entities.svg.querySelector("g").getBoundingClientRect();
const match = svg.querySelector("g").getAttribute("transform").match(/scale\((?<scale>.+?\))/);
if (!match || !match.groups || !match.groups.scale) return {};
Expand All @@ -680,14 +680,14 @@ class tocMarkmap {
return { minX: 0, maxX: realWidth, width: realWidth, minY: minY, maxY: maxY, height: realHeight }
}

const changeSvgAttr = svg => {
const setAttribute = svg => {
svg.removeAttribute("id");
svg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
svg.setAttribute("class", "markmap");
}

const setSvgSize = svg => {
const { width = 100, height = 100, minY = 0 } = getSvgBounding(svg);
const setSize = svg => {
const { width = 100, height = 100, minY = 0 } = getBounding(svg);
const [borderX, borderY] = this.config.BORDER_WHEN_DOWNLOAD_SVG;
const svgWidth = width + borderX;
const svgHeight = height + borderY;
Expand Down Expand Up @@ -715,17 +715,20 @@ class tocMarkmap {
const content = div.innerHTML.replace(/<br>/g, "<br/>");
const path = this.utils.Package.Path.join(getFileFolder(), getFileName());
const ok = await this.utils.writeFile(path, content);
if (ok) {
if (!ok) return;
if (this.config.SHOW_IN_FINDER_WHEN_DOWNLOAD_SVG) {
this.utils.showInFinder(path);
} else {
this.utils.notification.show("已导出到指定目录");
}
}

const svg = this.entities.svg.cloneNode(true);
changeSvgAttr(svg);
setSvgSize(svg);
removeSvgUselessStyle(svg);
setAttribute(svg);
setSize(svg);
removeUselessStyle(svg);
if (this.config.REMOVE_FOREIGN_OBJECT_WHEN_DOWNLOAD_SVG) {
removeSvgForeignObject(svg);
removeForeignObject(svg);
}
if (this.config.REMOVE_USELESS_CLASS_NAME_WHEN_DOWNLOAD_SVG) {
removeClassName(svg);
Expand Down

0 comments on commit 6a9e872

Please sign in to comment.