Skip to content

Commit

Permalink
Merge pull request #81 from obgnail/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
obgnail authored Aug 14, 2023
2 parents e1ca8d6 + 9c9e82a commit 84ff66f
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 170 deletions.
10 changes: 4 additions & 6 deletions plugin/auto_number.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(() => {
const config = global._pluginUtils.getPluginSetting("auto_number");

const bast_css = `
const base_css = `
#write { counter-reset: write-h2 Figures Tables Fences; }
h1 { counter-reset: write-h2 Figures Tables Fences; }
h2 { counter-reset: write-h3 Figures Tables Fences; }
Expand Down Expand Up @@ -186,7 +186,7 @@

const getStyleString = () => {
return [
bast_css,
base_css,
(config.ENABLE_CONTENT) ? content_css : "",
(config.ENABLE_SIDE_BAR) ? side_bar_css : "",
(config.ENABLE_TOC) ? toc_css : "",
Expand Down Expand Up @@ -219,6 +219,7 @@

afterGetHeaderMatrix: headers => {
if (!this.inExport) return;
this.inExport = false;

const pValue = {H2: 0, H3: 0, H4: 0, H5: 0, H6: 0};
headers.forEach(header => {
Expand Down Expand Up @@ -257,8 +258,6 @@
}
header[1] = numbering + header[1];
})

this.inExport = false;
}
}

Expand Down Expand Up @@ -295,9 +294,8 @@
];

const dynamicCallArgsGenerator = () => {
const ele = document.getElementById("plugin-auto-number-style");
let arg_name, arg_value;
if (ele) {
if (!!document.getElementById(config.ID)) {
arg_name = "禁用";
arg_value = "disable";
} else {
Expand Down
10 changes: 2 additions & 8 deletions plugin/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,6 @@
}
})

window.addEventListener("keydown", ev => {
if (config.HOTKEY(ev)) {
call()
ev.preventDefault();
ev.stopPropagation();
}
})

if (config.USE_BUILTIN) {
modal.builtinSelect.addEventListener("change", ev => {
const option = modal.builtinSelect.options[modal.builtinSelect.selectedIndex];
Expand All @@ -305,6 +297,8 @@
}
}

global._pluginUtils.registerWindowHotkey(config.HOTKEY, call);

module.exports = {call};

console.log("commander.js had been injected");
Expand Down
62 changes: 46 additions & 16 deletions plugin/fence_enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
坏处是:绿皮
*/
const config = global._pluginUtils.getPluginSetting("fence_enhance");
const enableIndent = config.ENABLE_INDENT && !global._pluginUtils.isBetaVersion;

(() => {
const css = `
Expand All @@ -23,17 +24,27 @@
z-index: 8;
font-size: 1.2em;
}
#write .fence-enhance .typora-copy-code, .typora-fold-code {
#write .fence-enhance .typora-copy-code, .typora-fold-code, .typora-indent-code {
opacity: 0.5;
cursor: pointer;
}
#write .fence-enhance .typora-copy-code {
#write .fence-enhance .typora-copy-code, .typora-indent-code {
margin-left: .5em;
}
`
global._pluginUtils.insertStyle("plugin-fence-enhance-style", css);
})()

const createButton = (className, hint, iconClassName) => {
const button = document.createElement("div");
button.classList.add(className);
hint && button.setAttribute("ty-hint", hint);
const span = document.createElement("span");
span.className = iconClassName;
button.appendChild(span);
return button
}

const addEnhanceElement = fence => {
let enhance = fence.querySelector(".fence-enhance");
if (!enhance) {
Expand All @@ -46,20 +57,15 @@

let foldButton;
if (config.ENABLE_FOLD) {
foldButton = document.createElement("div");
foldButton.classList.add("typora-fold-code");
const span = document.createElement("span");
span.className = "fa fa-minus";
foldButton.appendChild(span);
foldButton = createButton("typora-fold-code", "折叠", "fa fa-minus");
enhance.appendChild(foldButton);
}

if (enableIndent) {
const indentButton = createButton("typora-indent-code", "调整缩进", "fa fa-indent");
enhance.appendChild(indentButton);
}
if (config.ENABLE_COPY) {
const copyButton = document.createElement("div");
copyButton.classList.add("typora-copy-code");
const span = document.createElement("span");
span.className = "fa fa-clipboard";
copyButton.appendChild(span);
const copyButton = createButton("typora-copy-code", "复制", "fa fa-clipboard");
enhance.appendChild(copyButton);
}

Expand All @@ -82,16 +88,19 @@
document.getElementById("write").addEventListener("click", ev => {
const copy = ev.target.closest(".typora-copy-code");
const fold = ev.target.closest(".typora-fold-code");
if (!copy && !fold) return;
const indent = ev.target.closest(".typora-indent-code");
if (!copy && !fold && !indent) return;

ev.preventDefault();
ev.stopPropagation();
document.activeElement.blur();

if (copy) {
copyCode(ev, copy);
} else {
} else if (fold) {
foldCode(ev, fold);
} else {
indentCode(ev, indent);
}
})

Expand Down Expand Up @@ -147,6 +156,18 @@
}
}

const indentCode = (ev, indentButton) => {
const fence = indentButton.closest(".md-fences");
if (!fence || !File.editor.fences.formatContent) return;

const cid = fence.getAttribute("cid");
File.editor.refocus(cid);
File.editor.fences.formatContent();

indentButton.firstElementChild.className = "fa fa-check";
setTimeout(() => indentButton.firstElementChild.className = "fa fa-indent", config.WAIT_RECOVER_INTERVAL);
}

$("#write").on("mouseenter", ".md-fences", function () {
if (config.AUTO_HIDE) {
this.querySelector(".fence-enhance").style.visibility = "";
Expand All @@ -165,7 +186,7 @@

dynamicUtil.target = target;

return [
const arr = [
{
arg_name: "折叠/展开代码块",
arg_value: "fold_current",
Expand All @@ -175,6 +196,12 @@
arg_value: "copy_current",
},
]
enableIndent && arr.push({
arg_name: "调整缩进",
arg_value: "indent_current"
})

return arr
}

const callArgs = [
Expand Down Expand Up @@ -228,6 +255,9 @@
copy_current: () => {
dynamicUtil.target.querySelector(".typora-copy-code").click();
},
indent_current: () => {
dynamicUtil.target.querySelector(".typora-indent-code").click();
},
set_auto_hide: () => {
config.AUTO_HIDE = !config.AUTO_HIDE;
const visibility = (config.AUTO_HIDE) ? "hidden" : "";
Expand Down
2 changes: 1 addition & 1 deletion plugin/file_counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
}
}).observe(document.getElementById("file-library-tree"), {subtree: true, childList: true});

const _timer = setInterval(() => setAllDirCount() && clearInterval(_timer), config.LOOP_DETECT_INTERVAL);
global._pluginUtils.loopDetector(setAllDirCount, null, config.LOOP_DETECT_INTERVAL);

module.exports = {};

Expand Down
11 changes: 7 additions & 4 deletions plugin/global/settings/settings.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# 修改此文件前请注意备份,修改后请重启Typora
############### window_tab ###############
[window_tab]
# 启用插件
Expand All @@ -20,8 +21,6 @@ CLOSE_HOTKEY = ["ctrl+w"]
SWITCH_NEXT_TAB_HOTKEY = ["ctrl+PageDown", "ctrl+Tab"]
# 切换上一个标签页的快捷键
SWITCH_PREVIOUS_TAB_HOTKEY = ["ctrl+PageUp", "ctrl+shift+Tab"]
# 系统内部使用,请勿设置
LOOP_DETECT_INTERVAL = 30


############### search_multi ###############
Expand Down Expand Up @@ -77,7 +76,7 @@ REMOVE_WHEN_EDIT = true
SHOW_KEYWORD_OUTLINE = true
# 定位时高亮关键字提示所在行
SHOW_KEYWORD_BAR = true
# 高亮的样式
# 高亮的颜色
STYLE_COLOR = [
# 浅一些的颜色
'#bbeeff',
Expand All @@ -104,6 +103,7 @@ STYLE_COLOR = [
]
# 当搜索关键字数量超出STYLE_COLOR范围时面板显示的颜色(页面中无颜色)
# 20个关键字肯定够用了,此选项没太大意义
# 就算20个真的不够用,那我也是建议你修改STYLE_COLOR选项,而不是修改此选项
DEFAULT_COLOR = "aquamarine"
# Do not edit this field unless you know what you are doing
# 性能选项:关键字数量大于X时使用fenceMultiHighlighterList(以空间换时间)。若<0,则总是使用
Expand Down Expand Up @@ -176,6 +176,8 @@ ENABLE = true
# 快捷键
HOTKEY = "ctrl+shift+k"


############### read_only ###############
[read_only]
# 启用插件
ENABLE = true
Expand Down Expand Up @@ -228,7 +230,6 @@ EXCLUDE_HOTKEY = [
"alt+F4", # 退出
]
# 脚本内部使用
LOOP_DETECT_INTERVAL = 30
CLICK_CHECK_INTERVAL = 500


Expand Down Expand Up @@ -297,6 +298,8 @@ ENABLE = true
AUTO_HIDE = false
# 启用复制代码功能
ENABLE_COPY = true
# 启用代码调整缩进功能(正式版才可使用)
ENABLE_INDENT = true
# 启用折叠代码功能
ENABLE_FOLD = true
# 折叠形式
Expand Down
33 changes: 28 additions & 5 deletions plugin/global/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@
}
}

const getPluginSetting = fixed_name => {
return global._plugin_settings[fixed_name];
}

const metaKeyPressed = ev => File.isMac ? ev.metaKey : ev.ctrlKey;
const shiftKeyPressed = ev => !!ev.shiftKey;
const altKeyPressed = ev => !!ev.altKey;

const getPluginSetting = fixed_name => global._plugin_settings[fixed_name];
const getDirname = () => global.dirname || global.__dirname;
const getFilePath = () => File.filePath || File.bundle && File.bundle.filePath;
const joinPath = (...paths) => Package.Path.join(getDirname(), ...paths);
Expand All @@ -45,6 +42,7 @@
const uuid = Math.random();
detectorContainer[uuid] = setInterval(() => {
if (!until()) return;
clearInterval(detectorContainer[uuid]);

const decorator = (original, before, after) => {
return function () {
Expand All @@ -64,7 +62,6 @@
};
}
obj[func] = decorator(obj[func], before, after);
clearInterval(detectorContainer[uuid]);
delete detectorContainer[uuid];
}, 20);
}
Expand All @@ -77,6 +74,30 @@
decorate(() => !!File, File.editor.fences, "addCodeBlock", before, after)
}

const loopDetector = (until, after, detectInterval = 20) => {
const uuid = Math.random();
detectorContainer[uuid] = setInterval(() => {
if (until()) {
clearInterval(detectorContainer[uuid]);
after && after();
delete detectorContainer[uuid];
}
}, detectInterval);
}

const hotkeyList = []
const registerWindowHotkey = (hotkey, call) => hotkey instanceof Function && hotkeyList.push({hotkey, call});
window.addEventListener("keydown", ev => {
for (let hotkey of hotkeyList) {
if (hotkey.hotkey(ev)) {
hotkey.call();
ev.preventDefault();
ev.stopPropagation();
return
}
}
}, true)

const dragFixedModal = (handleElement, moveElement, withMetaKey = true) => {
handleElement.addEventListener("mousedown", ev => {
if (withMetaKey && !metaKeyPressed(ev) || ev.button !== 0) return;
Expand Down Expand Up @@ -125,6 +146,8 @@
decorate,
decorateOpenFile,
decorateAddCodeBlock,
loopDetector,
registerWindowHotkey,
dragFixedModal,
};
})()
29 changes: 17 additions & 12 deletions plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/* 1. 整个插件系统向外暴露的变量:
1. global._plugins_had_injected: 是否所有的插件都已加载完毕
2. global._plugins: 全部的插件
3. global._pluginUtils: 通用的工具
4. global._plugin_settings: 全部的插件配置
2. global._plugins使用声明式(声明替代代码开发)
1. name: 展示的插件名
2. fixed_name: 固定的插件名(可以看作是插件的UUID)
3. enable: 是否启用
4. clickable: 是否在右键菜单中可点击
5. call: 插件的入口函数
6. call_args: 固定的插件参数,如果存在,那么将在右键菜单中会显示第三级菜单,当用户点击后就会传递参数给call函数
7. dynamic_call_args_generator: 插件动态参数,用户在不同区域、不同时间点击右键菜单时,显示不同的第三级菜单
8. meta: 用于传递自定义变量
3. 核心逻辑位于right_click_menu.js中,
4. 使用例子可以看collapse_paragraph.js。此插件实现了用户在不同区域(标题处点击、非标题处点击)右键菜单会有不同的第三级菜单。
*/
window.onload = () => {
/* 1. global._plugins使用声明式(声明替代代码开发)
1. name: 展示的插件名
2. fixed_name: 固定的插件名(可以看作是插件的UUID)
3. enable: 是否启用
4. clickable: 是否在右键菜单中可点击
5. call: 插件的入口函数
6. call_args: 固定的插件参数,如果存在,那么将在右键菜单中会显示第三级菜单,当用户点击后就会传递参数给call函数
7. dynamic_call_args_generator: 插件动态参数,用户在不同区域、不同时间点击右键菜单时,显示不同的第三级菜单
8. meta: 用于传递自定义变量
2. 核心逻辑位于right_click_menu.js中,
3. 使用例子可以看collapse_paragraph.js。此插件实现了用户在不同区域(标题处点击、非标题处点击)右键菜单会有不同的第三级菜单。
*/
global._plugins_had_injected = false;
global._plugins = [
{
Expand Down
8 changes: 1 addition & 7 deletions plugin/md_padding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@
})
}

window.addEventListener("keydown", ev => {
if (config.HOTKEY(ev)) {
call();
ev.preventDefault();
ev.stopPropagation();
}
}, true)
global._pluginUtils.registerWindowHotkey(config.HOTKEY, call);

module.exports = {
call,
Expand Down
Loading

0 comments on commit 84ff66f

Please sign in to comment.