Skip to content

Commit

Permalink
update custom_plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
obgnail committed Aug 21, 2023
1 parent af16c37 commit 8718ae5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ const BUILTIN = [

使用步骤:

1. 修改 `./plugin/global/settings/settings.toml`找到 custom 选项,添加参数
1. 修改 `./plugin/custom/custom_plugin.toml`添加配置
2.`./plugin/custom/plugins` 目录下,创建和 plugin 参数同名的文件,在此文件中创建一个 class 继承自 BaseCustomPlugin,并导出为 `plugin`


Expand All @@ -342,25 +342,30 @@ const BUILTIN = [

实现:

步骤一:修改 `./plugin/global/settings/settings.toml`找到 custom 选项,添加如下参数
步骤一:修改 `./plugin/custom/custom_plugin.toml`添加配置

- name:右键菜单中展示的名称
- enable:是否启用此插件
- plugin:处理插件逻辑的文件
- config:插件自己的配置

```toml
# ./plugin/global/settings/settings.toml
[[custom.PLUGINS]]
# ./plugin/custom/custom_plugin.toml
[[PLUGINS]]
name = "复制标题路径"
enable = true
plugin = "fullPathCopy"
[PLUGINS.config]
ignore_empty_header = false
add_space = true
```

步骤二:在 `./plugin/custom/plugins` 目录下,创建和 plugin 参数同名的文件(`fullPathCopy.js`),在此文件中创建一个 class 继承自 BaseCustomPlugin,并导出为 `plugin`

1. 创建同名的 class,继承 BaseCustomPlugin 类。此时,fullPathCopy 将自动拥有 utils 属性。
1. 创建同名的 class,继承 BaseCustomPlugin 类。此时,fullPathCopy 将自动拥有 utils 属性 和 info 属性

> utils: 插件系统自带的静态工具类,其定义在 `./plugin/global/core/plugin.js/utils`。其中有个最重要的函数:`utils.getPlugin(fixed_name)` 用于获取已经实现的全部插件,调用其 API。具体的 API 可看 openPlatformAPI.md 文件。
> - utils:插件系统自带的静态工具类,其定义在 `./plugin/global/core/plugin.js/utils`。其中有个最重要的函数:`utils.getPlugin(fixed_name)` 用于获取已经实现的全部插件,调用其 API。具体的 API 可看 openPlatformAPI.md 文件。
> - info:该插件在 `custom_plugin.toml` 里的所有配置。
2. selector:当用户在哪个位置右键弹出菜单时,出现此命令(空串:任何位置都展示),在这里的含义就是:只在【正文标题】弹出此命令

Expand Down Expand Up @@ -405,6 +410,7 @@ class fullPathCopy extends BaseCustomPlugin {
const nameList = ["一级标题", "二级标题", "三级标题", "四级标题", "五级标题", "六级标题"];
const pList = [];
let ele = anchorNode;

while (ele) {
const idx = paragraphList.indexOf(ele.tagName);
if (idx !== -1) {
Expand All @@ -423,19 +429,28 @@ class fullPathCopy extends BaseCustomPlugin {
let headerIdx = 0;
for (const p of pList) {
while (headerIdx < 6 && p.ele.tagName !== paragraphList[headerIdx]) {
result.push("" + nameList[headerIdx]);
if (!this.config.ignore_empty_header) {
const name = this.getHeaderName("", nameList[headerIdx]);
result.push(name);
}
headerIdx++;
}

if (p.ele.tagName === paragraphList[headerIdx]) {
result.push(p.ele.querySelector("span").textContent + " " + nameList[headerIdx]);
const name = this.getHeaderName(p.ele.querySelector("span").textContent, nameList[headerIdx])
result.push(name);
headerIdx++;
}
}

const text = this.utils.Package.Path.join(...result);
navigator.clipboard.writeText(text);
}

getHeaderName = (title, name) => {
const space = (this.config.add_space) ? " " : "";
return title + space + name
}
}

// 10
Expand Down
13 changes: 13 additions & 0 deletions plugin/custom/custom_plugin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 自定义的命令
# name: 右键菜单中展示的名称
# enable: 是否启用此自定义插件
# plugin: 自定义插件
# config: 插件配置
[[plugins]]
name = "复制标题路径"
enable = true
plugin = "fullPathCopy"
[plugins.config]
ignore_empty_header = false
add_space = true
full_file_path = false
17 changes: 10 additions & 7 deletions plugin/custom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ class CustomPlugin extends global._basePlugin {
this.custom = {};
this.dynamicUtil = {target: null};

this.config.PLUGINS.forEach(_plugin => {
if (!_plugin.enable) return
const allPlugins = this.utils.readToml("./plugin/custom/custom_plugin.toml");
allPlugins.plugins.forEach(info => {
if (!info.enable) return
try {
const {plugin} = this.utils.requireFilePath(`./plugin/custom/plugins/${_plugin.plugin}`);
const {plugin} = this.utils.requireFilePath(`./plugin/custom/plugins/${info.plugin}`);
if (!plugin) return;

const instance = new plugin(_plugin.name, _plugin.plugin, this.utils);
const instance = new plugin(info, this.utils);
if (this.check(instance)) {
instance.init();
const style = instance.style();
Expand Down Expand Up @@ -96,9 +97,11 @@ class CustomPlugin extends global._basePlugin {
}

class BaseCustomPlugin {
constructor(showName, name, utils) {
this.showName = showName;
this.name = name;
constructor(info, utils) {
this.info = info;
this.showName = info.name;
this.name = info.plugin;
this.config = info.config;
this.utils = utils;
}

Expand Down
15 changes: 12 additions & 3 deletions plugin/custom/plugins/fullPathCopy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,33 @@ class fullPathCopy extends BaseCustomPlugin {

pList.reverse();

const filePath = File.getFileName();
const filePath = (this.config.full_file_path) ? this.utils.getFilePath() : File.getFileName();
const result = [filePath];
let headerIdx = 0;
for (const p of pList) {
while (headerIdx < 6 && p.ele.tagName !== paragraphList[headerIdx]) {
result.push("无 " + nameList[headerIdx]);
if (!this.config.ignore_empty_header) {
const name = this.getHeaderName("无", nameList[headerIdx]);
result.push(name);
}
headerIdx++;
}

if (p.ele.tagName === paragraphList[headerIdx]) {
result.push(p.ele.querySelector("span").textContent + " " + nameList[headerIdx]);
const name = this.getHeaderName(p.ele.querySelector("span").textContent, nameList[headerIdx]);
result.push(name);
headerIdx++;
}
}

const text = this.utils.Package.Path.join(...result);
navigator.clipboard.writeText(text);
}

getHeaderName = (title, name) => {
const space = (this.config.add_space) ? " " : "";
return title + space + name
}
}

module.exports = {
Expand Down
9 changes: 1 addition & 8 deletions plugin/global/settings/settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,7 @@ ENABLE = true
NAME = "自定义插件"
# 是否在右键菜单中可点击
CLICKABLE = true
# 自定义的命令
# name: 右键菜单中展示的名称
# enable: 是否启用此自定义插件
# className: 自定义插件
[[custom.PLUGINS]]
name = "复制标题路径"
enable = true
plugin = "fullPathCopy"


############### right_click_menu ###############
[right_click_menu]
Expand Down

0 comments on commit 8718ae5

Please sign in to comment.