Skip to content

Commit

Permalink
Merge pull request #727 from obgnail/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
obgnail authored Sep 1, 2024
2 parents 9700655 + b0ad371 commit fba1523
Show file tree
Hide file tree
Showing 21 changed files with 389 additions and 327 deletions.
33 changes: 23 additions & 10 deletions plugin/custom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ class CustomPlugin extends BasePlugin {
}

hotkey = () => {
const isString = s => typeof s === "string"
const hotkeys = [];
for (const [fixedName, plugin] of Object.entries(this.plugins)) {
if (!plugin) continue;
if (!plugin || !plugin.hasOwnProperty("hotkey")) continue;
try {
const hotkey = plugin.hotkey();
if (!hotkey) continue;

const callback = () => {
const $anchorNode = this.utils.getAnchorNode();
const anchorNode = $anchorNode && $anchorNode[0];
const selector = plugin.selector();
const target = (selector && anchorNode) ? anchorNode.closest(selector) : anchorNode;
plugin.callback(target);
if (isString(hotkey) || (Array.isArray(hotkey) && hotkey.every(isString))) {
hotkeys.push({ hotkey, callback: plugin.callback });
} else if (Array.isArray(hotkey) && hotkey.every(this.utils.isObject)) {
hotkeys.push(...hotkey);
}
hotkeys.push({ hotkey, callback });
} catch (e) {
console.error("register hotkey error:", fixedName, e);
}
Expand Down Expand Up @@ -118,12 +114,29 @@ class customPluginLoader {
}
}

decorateCallback = async () => {
for (const plugin of Object.values(this.controller.plugins)) {
if (!plugin || !plugin.hasOwnProperty("callback") || !plugin.hasOwnProperty("selector")) continue;
const { callback: originCallback } = plugin;
plugin.callback = anchorNode => {
if (!anchorNode) {
const $anchor = this.utils.getAnchorNode();
const anchor = $anchor && $anchor[0];
const selector = plugin.selector(true);
anchorNode = (selector && anchor) ? anchor.closest(selector) : anchor;
}
originCallback(anchorNode);
}
}
}

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

callback = () => {
callback = anchorNode => {
const replaceFlag = 2;
const tailSpace = " ";
this.utils.entities.querySelectorAllInWrite("p[cid]").forEach(ele => {
Expand Down
2 changes: 1 addition & 1 deletion plugin/custom/plugins/bingSpeech.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class bingSpeechPlugin extends BaseCustomPlugin {

hotkey = () => [this.config.hotkey]

callback = async () => {
callback = async anchorNode => {
const voiceList = [
"zh-CN-YunxiNeural",
"zh-CN-XiaoxiaoNeural",
Expand Down
2 changes: 1 addition & 1 deletion plugin/custom/plugins/blockSideBySide.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class blockSideBySidePlugin extends BaseCustomPlugin {

styleTemplate = () => true

callback = async () => {
callback = async anchorNode => {
const enable = this.utils.styleTemplater.getStyleContent(this.fixedName);
const func = enable ? "unregister" : "register";
await this.utils.styleTemplater[func](this.fixedName);
Expand Down
2 changes: 1 addition & 1 deletion plugin/custom/plugins/darkMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class darkModePlugin extends BaseCustomPlugin {

process = () => this.isDarkMode && this.enableDarkMode();

callback = () => this.toggleDarkMode()
callback = anchorNode => this.toggleDarkMode()
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion plugin/custom/plugins/imageReviewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class imageReviewerPlugin extends BaseCustomPlugin {

hotkey = () => [this.config.hotkey]

callback = () => {
callback = anchorNode => {
if (this.utils.isHidden(this.entities.reviewer)) {
this.show();
} else {
Expand Down
Loading

0 comments on commit fba1523

Please sign in to comment.