Skip to content

Commit

Permalink
Merge pull request #778 from obgnail/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
obgnail authored Oct 2, 2024
2 parents ca3c05c + f68269f commit 26aa890
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 175 deletions.
4 changes: 2 additions & 2 deletions plugin/custom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CustomPlugin extends BasePlugin {
if (!plugin) continue;

const arg = {
arg_name: plugin.showName,
arg_name: plugin.config.name,
arg_value: plugin.fixedName,
arg_disabled: true,
arg_hint: "未知错误!请向开发者反馈",
Expand Down Expand Up @@ -132,7 +132,7 @@ class customPluginLoader {
}

process = async () => {
const settings = await this.utils.readSetting("custom_plugin.default.toml", "custom_plugin.user.toml");
const settings = await this.utils.readCustomPluginSetting();
this.mergeSettings(settings);
this.errorSettingDetector(settings);
this.controller.pluginsSettings = settings;
Expand Down
2 changes: 1 addition & 1 deletion plugin/custom/plugins/hotkeyHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class hotkeyHubPlugin extends BaseCustomPlugin {
hotkey = () => [this.config.hotkey]

beforeProcess = async () => {
this.settings = await this.utils.readSetting("hotkey.default.toml", "hotkey.user.toml");
this.settings = await this.utils.readHotkeySetting();
}

process = () => {
Expand Down
6 changes: 5 additions & 1 deletion plugin/custom/plugins/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ class tocPlugin extends BaseCustomPlugin {
const { outline } = File.editor.library;
const toc = this.config.escape_header
? outline.getHeaderMatrix(true).map(([depth, text, cid]) => ({ depth, text, cid, children: [] }))
: headers.map(({ attributes: { depth, text }, cid }) => ({ depth, text, cid, children: [] }))
: headers.map(({ attributes, cid }) => {
let { depth, text } = attributes || {};
text = text.replace(/\[\^([^\]]+)\]/g, ""); // 去掉脚注
return { depth, text, cid, children: [] }
})

toc.forEach((node, idx) => {
const parent = findParent(toc, idx - 1, node.depth) || root;
Expand Down
43 changes: 19 additions & 24 deletions plugin/custom/请读我.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ enable = true # 是否启用此二级插件
hide = false # 是否在右键菜单中隐藏
order = 1 # 在右键菜单中的出现顺序(越大越排到后面,允许负数)

[helloWorld.config]
hotkey_string = "ctrl+alt+u"
console_message = "i am in process"
show_message = "this is hello world plugin"
hotkey_string = "ctrl+alt+u"
console_message = "i am in process"
show_message = "this is hello world plugin"
```

> 如果您对 TOML 不太了解,可以花三分钟了解 [TOML 教程](https://toml.io/cn/v1.0.0)
> name、enable、hide、order 是必要选项。


Expand Down Expand Up @@ -123,10 +122,9 @@ enable = true # 是否启用此二级插件
hide = false # 是否在右键菜单中隐藏
order = 1 # 在右键菜单中的出现顺序(越大越排到后面,允许负数)

[selectCheckboxes.config]
select_all_hotkey = "ctrl+alt+h" # 全选快捷键
select_none_hotkey = "ctrl+alt+j" # 取消全部选择快捷键
select_reverse_hotkey = "ctrl+alt+k" # 反选快捷键
select_all_hotkey = "ctrl+alt+h" # 全选快捷键
select_none_hotkey = "ctrl+alt+j" # 取消全部选择快捷键
select_reverse_hotkey = "ctrl+alt+k" # 反选快捷键
```

步骤二:打开目录 `plugin\custom\plugins`,在此目录下创建文件 `selectCheckboxes.js`,写入如下内容:
Expand Down Expand Up @@ -189,18 +187,16 @@ enable = true
hide = false
order = 1

# 插件配置
[myFullPathCopy.config]
# 快捷键
hotkey = "ctrl+shift+u"
# 如果在空白页调用此插件,使用的文件名(因为尚不存在该文件,需要用一个默认的文件名代替)
untitled_file_name = "untitled"
# 跳过空白的标题
ignore_empty_header = false
# 标题和提示之前添加空格
add_space = true
# 使用绝对路径
full_file_path = false
# 快捷键
hotkey = "ctrl+shift+u"
# 如果在空白页调用此插件,使用的文件名(因为尚不存在该文件,需要用一个默认的文件名代替)
untitled_file_name = "untitled"
# 跳过空白的标题
ignore_empty_header = false
# 标题和提示之前添加空格
add_space = true
# 使用绝对路径
full_file_path = false
```


Expand Down Expand Up @@ -282,10 +278,9 @@ class myFullPathCopy extends BaseCustomPlugin {
// 12
module.exports = { plugin: myFullPathCopy };

// 1. 创建 class,继承 BaseCustomPlugin 类。之后 myFullPathCopy 将自动拥有 utils、info、config 属性。
// 1. 创建 class,继承 BaseCustomPlugin 类。之后 myFullPathCopy 将自动拥有 utils、config 属性。
// - utils:插件系统自带的静态工具类,其定义在 `./plugin/global/core/plugin.js/utils`。其中有三个重要的函数:utils.getPlugin(fixedName) 和 utils.getCustomPlugin(fixedName) 用于获取已经实现的全部插件。utils.eventHub.addEventListener(eventType, listener) 用于监听 Typora 的生命周期事件。
// - info:该插件在 custom_plugin.user.toml 里的所有字段
// - config:等同于 info.config,即配置文件里的 config 属性
// - config:该插件在 custom_plugin.user.toml 里的所有字段
// 2. selector:允许运行命令的光标位置:当光标位于哪些位置时,此命令才可用。返回 null-like value 表示任何位置都可用,在这里的含义就是:只当光标位于【正文标题】时可用
// 3. hint:当鼠标移动到右键菜单时的提示
// 4. init:在这里初始化你要的变量
Expand Down
5 changes: 1 addition & 4 deletions plugin/global/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ const { utils, hook } = require("./utils");
const { BasePlugin, BaseCustomPlugin, LoadPlugins } = require("./plugin");

async function entry() {
/** 读取配置 */
const readSetting = () => utils.readSetting("settings.default.toml", "settings.user.toml");

/**
* 初始化全局变量
* 整个插件系统一共暴露了7个全局变量,实际有用的只有3个:BasePlugin, BaseCustomPlugin, LoadPlugins
Expand Down Expand Up @@ -40,7 +37,7 @@ async function entry() {
}

const launch = async () => {
const settings = await readSetting();
const settings = await utils.readBasePluginSetting();
const enable = settings && settings.global && settings.global.ENABLE;
if (!enable) {
console.warn("disable typora plugin");
Expand Down
6 changes: 0 additions & 6 deletions plugin/global/core/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ class BasePlugin extends IPlugin {

/** 二级插件 */
class BaseCustomPlugin extends IPlugin {
constructor(fixedName, setting) {
super(fixedName, setting.config);
this.info = setting;
this.showName = setting.name;
}

selector(isClick) {}
hint(isDisable) {}
callback(anchorNode) {}
Expand Down
24 changes: 20 additions & 4 deletions plugin/global/core/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class utils {


////////////////////////////// 纯函数 //////////////////////////////
static noop = () => undefined
static noop = args => args

/** @description param fn cannot be an async function that returns promiseLike object */
static throttle = (fn, delay) => {
Expand Down Expand Up @@ -349,11 +349,10 @@ class utils {
}
if (!plugin) return;

const mergeObj = isCustom ? { [fixedName]: { config: updateObj } } : { [fixedName]: updateObj };
const file = isCustom ? "custom_plugin.user.toml" : "settings.user.toml";
const settingPath = await this.getActualSettingPath(file);
const tomlObj = await this.readToml(settingPath);
const newSetting = this.merge(tomlObj, mergeObj);
const newSetting = this.merge(tomlObj, { [fixedName]: updateObj });
const newContent = this.stringifyToml(newSetting);
return this.writeFile(settingPath, newContent);
}
Expand All @@ -368,7 +367,7 @@ class utils {
});
}

static readSetting = async (defaultSetting, userSetting) => {
static _readSetting = async (defaultSetting, userSetting) => {
const default_ = this.getOriginSettingPath(defaultSetting);
const user_ = this.getOriginSettingPath(userSetting);
const home_ = this.getHomeSettingPath(userSetting);
Expand All @@ -387,6 +386,23 @@ class utils {
return {}
}
}
static readHotkeySetting = async () => this._readSetting("hotkey.default.toml", "hotkey.user.toml");
static readBasePluginSetting = async () => this._readSetting("settings.default.toml", "settings.user.toml");
static readCustomPluginSetting = async () => {
const settings = await this._readSetting("custom_plugin.default.toml", "custom_plugin.user.toml");
return this.fixCustomPluginSetting(settings);
}

// 兼容历史遗留问题
static fixCustomPluginSetting = async settings => {
Object.values(settings).map(plugin => {
if (plugin.config) {
Object.assign(plugin, plugin.config);
delete plugin.config;
}
})
return settings
}

static openSettingFolder = async () => this.showInFinder(await this.getActualSettingPath("settings.user.toml"))

Expand Down
Loading

0 comments on commit 26aa890

Please sign in to comment.