Skip to content

Commit

Permalink
add seperator policy
Browse files Browse the repository at this point in the history
  • Loading branch information
zxkmm committed Jan 18, 2024
1 parent fc537e8 commit 9ee250e
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 35 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -2,11 +2,21 @@
A SiYuan Plugin that able to remove the buttons in menu that you don't really use.
It currently support removing top bar icons, side bar icons, and menus with id `commonMenu`, which are two icons on top right corner of each doc, and calling from the right side of each paragraph.

# Download
go to release page or go to SiYuan's market and search `siyuan_rmv_btn`


## changelog 1.1.0 2024.01.12
- Add icons for reference in setting panel.
- Not anymore detect or read old config.
- Delete config data after uninstall, but not when turn off plugin.

## changelog 1.1.1 2024.01.19
- add seperator policy:
- two meet each other, hide one
- hide all
- don't touch

# Warning
- If you removed too much items and end up stopped working, please go to your workspace and delete the entire ``siyuan_rmv_btn`` folder.
- The handler handle main interface of SiYuan as soon as DOM changes. In which case if you added too many items, the performance would be influenced.
Expand Down
9 changes: 9 additions & 0 deletions README_zh_CN.md
Expand Up @@ -4,11 +4,20 @@
现阶段支持移除顶栏指定按钮,侧栏指定按钮,右键菜单项内容。
本插件在MIT协议下开源,没有联网行为,也没有从远端fetch恶意代码的能力。

# 下载
到release页面下载,或者到思源集市搜索“移除按钮”

## changelog 1.1.0 2024.01.12
- Add icons for reference in setting panel.
- Not anymore detect or read old config.
- Delete config data after uninstall, but not when turn off plugin.

## changelog 1.1.1 2024.01.19
- add seperator policy:
- two meet each other, hide one
- hide all
- don't touch

# 链接
repo/源码:https://github.com/zxkmm/siyuan_rmv_btn

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "siyuan_rmv_btn",
"version": "1.1.0",
"version": "1.1.1",
"type": "module",
"description": "A plugin to remove menu items you don't really need.",
"repository": "https://github.com/zxkmm/siyuan_rmv_btn",
Expand Down
5 changes: 3 additions & 2 deletions plugin.json
Expand Up @@ -2,7 +2,7 @@
"name": "siyuan_rmv_btn",
"author": "zxkmm",
"url": "https://github.com/zxkmm/siyuan_rmv_btn",
"version": "1.1.0",
"version": "1.1.1",
"minAppVersion": "2.10.14",
"backends": [
"windows",
Expand Down Expand Up @@ -41,6 +41,7 @@
"菜单",
"remove",
"hide",
"menu"
"menu",
"siyuan_rmv_btn"
]
}
7 changes: 6 additions & 1 deletion src/i18n/en_US.json
Expand Up @@ -13,6 +13,11 @@
"rm_side_desc": "<head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><style>#siyuan_rmv_btn_setting_table svg {border: 1px;width: 1em;height: 1em;vertical-align: middle;}#siyuan_rmv_btn_setting_table table {border-collapse: collapse;width: 100%;margin: 0 auto;}#siyuan_rmv_btn_setting_table td {border: 1px;padding: 10px;text-align: right;}#siyuan_rmv_btn_setting_table th {border: 1px;padding: 10px;text-align: left;font-weight: bold;}#siyuan_rmv_btn_setting_table tr:hover {box-shadow: inset 0 0 10px #999;}</style></head><body> Enter the provided label name strings (e.g., \"outline, tag\" without quotes)<br>Separate with English or Chinesehalf-width commas, i.e., \",\" or \",\".<br>Options:<br><table id=\"siyuan_rmv_btn_setting_table\"><thead><tr><th>Items to Hide</th><th>Input String (Double-click to select)</th></tr></thead><tbody><tr><th><svg><use xlink:href=\"#iconAlignCenter\"></use></svg> Outline Icon</th><td>outline</td></tr><tr><th><svg><use xlink:href=\"#iconInbox\"></use></svg> Inbox Icon</th><td>inbox</td></tr><tr><th><svg><use xlink:href=\"#iconBookmark\"></use></svg> Bookmark Icon</th><td>bookmark</td></tr><tr><th><svg><use xlink:href=\"#iconTags\"></use></svg> Tag Icon</th><td>tag</td></tr><tr><th><svg><use xlink:href=\"#iconLink\"></use></svg> Backlink Icon</th><td>backlink</td></tr><tr><th><svg><use xlink:href=\"#iconGlobalGraph\"></use></svg> Global Graph Icon</th><td>globalGraph</td></tr><tr><th><svg><use xlink:href=\"#iconGraph\"></use></svg> Notebook Graph Icon</th><td>graph</td></tr></tbody></table></body>",
"ignore_warning": "remove button plugin: the warning above is just because you choosed a high performance observing implementation, if you don't like it, you may choose the other two selections in the setting panel. thank you",
"uninstall_hint": "Succesfully uninstalled Remove Button Plugin.",
"hintTitle": "About v1.1.0",
"seperateHandlePolicy": "Menu item separator hide policy",
"seperateHandlePolicydesc": "If you want to hide the menu item separators, please select a hide policy here",
"seperateHandlePolicyDontTouch": "Don't handle",
"seperateHandlePolicyHideAll": "Hide all",
"seperateHandlePolicyHideIfTwoMeetEachOther": "Hide one if two adjacent separators meet",
"hintTitle": "About v1.1.1",
"hintDesc": "<ul><li>● By <a href='https://github.com/zxkmm'>zxkmm</a> and open source under the MIT license.</li><li>● Please star⭐ my GitHub repository if you like this plugin.</li><li>● Link: <a href='https://github.com/zxkmm/siyuan_rmv_btn'>https://github.com/zxkmm/siyuan_rmv_btn</a></li><li>● If you want to hide elements related to SiYuan's commercial part, please consider donating to the makers of SiYuan (AKA @D and @V) (not me) before using.</li></ul>"
}
7 changes: 6 additions & 1 deletion src/i18n/zh_CN.json
Expand Up @@ -13,6 +13,11 @@
"rm_side_desc": "<head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><style>#siyuan_rmv_btn_setting_table svg {border: 1px;width: 1em;height: 1em;vertical-align: middle;}#siyuan_rmv_btn_setting_table table {border-collapse: collapse;width: 100%;margin: 0 auto;}#siyuan_rmv_btn_setting_table td {border: 1px;padding: 10px;text-align: right;}#siyuan_rmv_btn_setting_table th {border: 1px;padding: 10px;text-align: left;font-weight: bold;}#siyuan_rmv_btn_setting_table tr:hover {box-shadow: inset 0 0 10px #999;}</style></head><body>填写提供的标签名字符串(如“outline,tag”(不包括双引号))<br>用英文或中文半角逗号,即\",\"或\",\"隔开。<br>选项:<br><table id=\"siyuan_rmv_btn_setting_table\"><thead><tr><th>要隐藏的项目</th><th>输入的字符串(双击选中)</th></tr></thead><tbody><tr><th><svg><use xlink:href=\"#iconAlignCenter\"></use></svg> 大纲图标</th><td>outline</td></tr><tr><th><svg><use xlink:href=\"#iconInbox\"></use></svg> 收件箱图标</th><td>inbox</td></tr><tr><th><svg><use xlink:href=\"#iconBookmark\"></use></svg> 书签图标</th><td>bookmark</td></tr><tr><th><svg><use xlink:href=\"#iconTags\"></use></svg> 标签图标</th><td>tag</td></tr><tr><th><svg><use xlink:href=\"#iconLink\"></use></svg> 反链图标</th><td>backlink</td></tr><tr><th><svg><use xlink:href=\"#iconGlobalGraph\"></use></svg> 全局图谱图标</th><td>globalGraph</td></tr><tr><th><svg><use xlink:href=\"#iconGraph\"></use></svg> 单个笔记本图谱图标</th><td>graph</td></tr></tbody></table></body>",
"ignore_warning": "移除按钮插件: 上方的warning是因为您选择了高性能的监听方式,若不喜欢,请在设置页面选择另外两种方式,谢谢",
"uninstall_hint": "移除按钮插件: 成功卸载",
"hintTitle": "关于 v1.1.0",
"seperateHandlePolicy":"菜单项分割线隐藏策略",
"seperateHandlePolicydesc":"如果您想隐藏菜单项分割线,请在这里选择隐藏策略",
"seperateHandlePolicyDontTouch":"不处理",
"seperateHandlePolicyHideAll":"全部隐藏",
"seperateHandlePolicyHideIfTwoMeetEachOther":"如果出现两个相邻,则隐藏一个",
"hintTitle": "关于 v1.1.1",
"hintDesc": "<ul><li>● 由<a href='https://github.com/zxkmm'>zxkmm</a>制作, MIT 协议开源。</li><li>● 如果您喜欢这个插件,请给我的 GitHub 仓库点亮免费的星星⭐(Star)。</li><li>● 链接:<a href='https://github.com/zxkmm/siyuan_rmv_btn'>https://github.com/zxkmm/siyuan_rmv_btn</a></li><li>● 如果您想隐藏与思源商业部分相关的元素,请在使用前考虑向思源的制作者(即@D和@V)捐赠(不是捐给我)。</li></ul>"
}
106 changes: 76 additions & 30 deletions src/index.ts
Expand Up @@ -30,40 +30,81 @@ export default class siyuan_rmv_btn extends Plugin {
}
}

rmvMenuItems(_toRemoveListArray_, _monitorImplementation_) {
if (_monitorImplementation_ == 1) { //DOMNodeInserted
targetNode.addEventListener('DOMNodeInserted', function (e) {
const buttons = Array.from(targetNode.getElementsByTagName('button'));
rmvMenuItems(_toRemoveListArray_, _monitorImplementation_, _seperateHidingPolicy_) {

//seperate pilocy: 1; don't touch
// 2: hide all
// 3: hide if two meet each other

function hideButtonsAndSeparators(_items_) {

buttons.forEach((button) => {
let labelElement = button.getElementsByClassName('b3-menu__label')[0];
for (let i = 0; i < _items_.length; i++) { //hide btns
let item = _items_[i];
if (item.classList.contains('b3-menu__item')) {
let labelElement = item.getElementsByClassName('b3-menu__label')[0];
if (labelElement) {
let span_text = labelElement.textContent.trim();
if (_toRemoveListArray_.includes(span_text)) {
button.style.display = 'none';
item.style.display = 'none';
}
}
});
}
}

if (_seperateHidingPolicy_ == 3) {
// test seperate and hide if two meet each other
// TODO: didn't handle the situation that more than two seperators meet each other....
// 烧脑。。。
let startSeparatorIndex = -1;
let previousSeparatorHidden = false;
for (let i = 0; i < _items_.length; i++) {
if (_items_[i].classList.contains('b3-menu__separator')) {
if (startSeparatorIndex === -1) {
startSeparatorIndex = i;
} else {
let allButtonsHidden = true;
for (let j = startSeparatorIndex + 1; j < i; j++) {
if (_items_[j].style.display !== 'none') {
allButtonsHidden = false;
break;
}
}
if (allButtonsHidden && !previousSeparatorHidden) {
_items_[startSeparatorIndex].style.display = 'none';
previousSeparatorHidden = true;
} else {
previousSeparatorHidden = false;
}
startSeparatorIndex = i;
}
}
}
} else if (_seperateHidingPolicy_ == 2) {
// hide all
for (let i = 0; i < _items_.length; i++) {
if (_items_[i].classList.contains('b3-menu__separator')) {
_items_[i].style.display = 'none';
}
}
}


}


if (_monitorImplementation_ == 1) { //DOMNodeInserted
targetNode.addEventListener('DOMNodeInserted', function (e) {
const buttons = Array.from(targetNode.getElementsByTagName('button'));
hideButtonsAndSeparators(buttons);
}, false);
console.log(this.i18n.ignore_warning);

} else if (_monitorImplementation_ == 2) { //MutationObserver
var observer = new MutationObserver(function (mutationsList, observer) {
for (let mutation of mutationsList) {
// if((mutation.type === 'childList') || (mutation.type === 'subtree')) {
if (mutation.type) {
const buttons = Array.from(document.getElementById('commonMenu').getElementsByTagName('button'));

buttons.forEach((button) => {
let labelElement = button.getElementsByClassName('b3-menu__label')[0];
if (labelElement) {
let span_text = labelElement.textContent.trim();
if (_toRemoveListArray_.includes(span_text)) {
button.style.display = 'none';
}
}
});
hideButtonsAndSeparators(buttons);
}
}
});
Expand All @@ -74,16 +115,7 @@ export default class siyuan_rmv_btn extends Plugin {
for (let mutation of mutationsList) {
if ((mutation.type === 'childList') || (mutation.type === 'subtree')) {
const buttons = Array.from(document.getElementById('commonMenu').getElementsByTagName('button'));

buttons.forEach((button) => {
let labelElement = button.getElementsByClassName('b3-menu__label')[0];
if (labelElement) {
let span_text = labelElement.textContent.trim();
if (_toRemoveListArray_.includes(span_text)) {
button.style.display = 'none';
}
}
});
hideButtonsAndSeparators(buttons);
}
}
});
Expand All @@ -92,6 +124,7 @@ export default class siyuan_rmv_btn extends Plugin {
}
}


rmvTopButtonBarIcons(_toRemoveListArray_) {
_toRemoveListArray_.forEach(elementType => {
const styleElement = document.createElement('style');
Expand Down Expand Up @@ -163,6 +196,18 @@ export default class siyuan_rmv_btn extends Plugin {
3: "MutationObserver(type hard coded)"
}
});
this.settingUtils.addItem({
key: "seperateHandlePolicy",
value: 1,
type: "select",
title: this.i18n.seperateHandlePolicy,
description: this.i18n.seperateHandlePolicydesc,
options: {
1: this.i18n.seperateHandlePolicyDontTouch,
2: this.i18n.seperateHandlePolicyHideAll,
3: this.i18n.seperateHandlePolicyHideIfTwoMeetEachOther,
}
});
this.settingUtils.addItem({
key: "unwantedMenuItem",
value: "",
Expand Down Expand Up @@ -215,7 +260,8 @@ export default class siyuan_rmv_btn extends Plugin {
this.convertStringToArray(
this.settingUtils.get("unwantedMenuItem")
),
this.settingUtils.get("listenImplementation")
this.settingUtils.get("listenImplementation"),
this.settingUtils.get("seperateHandlePolicy")
)
}
}
Expand Down

0 comments on commit 9ee250e

Please sign in to comment.