Skip to content

Commit

Permalink
setting UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Polygononon committed Nov 26, 2022
1 parent a807eb3 commit 508f408
Show file tree
Hide file tree
Showing 5 changed files with 515 additions and 21 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ Zotero标签可以显示在标题左侧,有时候条目有不同数量标签

![](https://spr1ng.live/file/87ac5698538744a03d424.png)

设置界面
> 鼠标中键,Mac用户没有鼠标右键,可以设置Zotero.ZoteroStyle.progressOpacity=0隐藏进度条
1. Zotero.ZoteroStyle.progressColor=#F06292 - 设置进度条颜色
2. Zotero.ZoteroStyle.progressOpacity=.5 - 设置进度条透明度
3. Zotero.ZoteroStyle.tagSize=8 - 设置标签宽度
4. 展开标题后保留的列(即将支持)
![](https://spr1ng.live/file/1f8ea01f480f45a0149ca.png)

## 主要功能

1. 标签右对齐,标签由`圆角正方形`->`圆形`🔴🟤🔵
1. 标签右对齐,标签由`圆角正方形`->`圆形`
2. 增加`只显示标题`按钮🌸
3. 显示阅读进度,是否划水,一看便知👋
4. 本插件可与`Chartero`共存,若安装`Chartero`不插件将不再渲染进度条,因为这一功能我已合并到`Chartero`,而且`Chartero`支持同步数据

🙌建议配合Zotero Tag使用,本插件无个性化设置界面(其实是我还不会写,用的bootstrap模板`支持以后的Zotero`,现有的大多是overlay`后面升级的Zotero可能不支持这种模式的插件`的)

Expand Down
94 changes: 74 additions & 20 deletions src/events.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Addon, addonName } from "./addon";
import AddonModule from "./module";
import Setting from "./setting";

class AddonEvents extends AddonModule {
public Zotero: any;
public window: any;
public document: any;
public notifierCallback : any;
public setting: any;
public intervalID: number;
public tagSize = 5; // em
public progressOpacity = .7; // s
public progressColor = "#5AC1BD"; //
public recordInterval = 5; // s
public maxHangTime = 60; // s
public mode = "normal"; // default
Expand Down Expand Up @@ -60,6 +65,10 @@ class AddonEvents extends AddonModule {
this.modifyRenderCell
)

// setting
this.setting = new Setting(AddonModule)
this.setting.init(this.Zotero)
this.setting.settingNode.style.display = "none"
// event
let notifierID = this.Zotero.Notifier.registerObserver(
this.notifierCallback,
Expand All @@ -75,19 +84,21 @@ class AddonEvents extends AddonModule {
);

// listen to Zotero's state
this.window.addEventListener('activate', () => {
console.log('activate')
this.state.activate = true
// once Zotero is activated again, it will continue to record read time
this.intervalID = this.window.setInterval(this.recordReadTime.bind(this), this.recordInterval * 1e3)
}, true);
this.window.addEventListener('deactivate', () => {
console.log('deactivate')
this.state.activate = false
this.state.hangCount = 0;
// once Zotero is deactivate again, it will stop to record read time
this.window.clearInterval(this.intervalID)
}, true);
if (!this.Zotero.Chartero) {
this.window.addEventListener('activate', () => {
console.log('activate')
this.state.activate = true
// once Zotero is activated again, it will continue to record read time
this.intervalID = this.window.setInterval(this.recordReadTime.bind(this), this.recordInterval * 1e3)
}, true);
this.window.addEventListener('deactivate', () => {
console.log('deactivate')
this.state.activate = false
this.state.hangCount = 0;
// once Zotero is deactivate again, it will stop to record read time
this.window.clearInterval(this.intervalID)
}, true);
}
}

private addSwitchButton(): void {
Expand Down Expand Up @@ -131,6 +142,15 @@ class AddonEvents extends AddonModule {
switchDisplay(_Zotero)
this.innerHTML = maxSvg
}
} else if (event.button == 1) {
// setting ui
console.log("init Setting")
let settingNode = _Zotero.getMainWindow().document.querySelector("#Zotero-Style-Setting")
if (settingNode.style.display = "none") {
settingNode.style.display = ""
} else {
settingNode.style.display = "none"
}
} else if (event.button == 2) {
console.log("right click")
_Zotero.ZoteroStyle.events.progress = !_Zotero.ZoteroStyle.events.progress
Expand All @@ -142,7 +162,7 @@ class AddonEvents extends AddonModule {
let toolbar = this.document.querySelector("#zotero-items-toolbar")
let toolbarbutton = toolbar.querySelector("toolbarbutton").cloneNode()
toolbarbutton.setAttribute("id", "zotero-tb-switch-itemtree")
toolbarbutton.setAttribute("tooltiptext", "switch itemtree")
toolbarbutton.setAttribute("tooltiptext", "Zotero Style For You")
toolbarbutton.setAttribute("type", "")
toolbarbutton.setAttribute("class", "")
toolbarbutton.setAttribute("onmousedown", "")
Expand All @@ -158,9 +178,14 @@ class AddonEvents extends AddonModule {
private addStyle(): void {
console.log("Start style")
let mainWindow = this.document.querySelector('#main-window')
let oldStyle = mainWindow.querySelector("#pageStyle")
if (oldStyle) oldStyle.remove()
let style = this.createElement("style")
style.setAttribute('type', 'text/css')
style.setAttribute('id', 'pageStyle')
// some setting value
let tagSize = this.getValue("Zotero.ZoteroStyle.tagSize", this.tagSize)
let progressOpacity = this.getValue("Zotero.ZoteroStyle.progressOpacity", this.progressOpacity)
style.textContent = `
.primary {
display: flex;
Expand All @@ -169,9 +194,10 @@ class AddonEvents extends AddonModule {
}
.tag-box {
position: relative;
width: 5em;
width: ${tagSize}em;
height: 1em;
line-height: 1em;
margin-left: auto;
}
#zotero-items-tree .cell.primary .zotero-tag {
height: .9em;
Expand All @@ -190,9 +216,9 @@ class AddonEvents extends AddonModule {
position: absolute;
left: 3.25em;
top: 0;
width: calc(100% - 9em);
width: calc(100% - 3.5em - ${tagSize}em) !important;
height: 100%;
opacity: .7;
opacity: ${progressOpacity};
}
`
mainWindow.appendChild(style)
Expand Down Expand Up @@ -229,6 +255,7 @@ class AddonEvents extends AddonModule {
// 2693 _renderPrimaryCell(index, data, column)
let document = Zotero.getMainWindow().document
let createElement = (name) => document.createElementNS("http://www.w3.org/1999/xhtml", name)

// render the tag
let tagBoxNode = createElement("span")
tagBoxNode.setAttribute("class", "tag-box")
Expand All @@ -243,6 +270,9 @@ class AddonEvents extends AddonModule {
tagNode.style.left = `${i*1.25+delta}em`
tagBoxNode.appendChild(tagNode)
})
if (Zotero.Chartero) {
return primaryCell
}
// render the read progress
let progressNode = createElement("span")
progressNode.setAttribute("class", "zotero-style-progress")
Expand All @@ -252,7 +282,6 @@ class AddonEvents extends AddonModule {
// create sub span in this progress node
const recordKey = `Zotero.ZoteroStyle.record`;
let record = JSON.parse(Zotero.Prefs.get(recordKey) || "{}");
console.log(record)
// i.e.
const testTitle = "Satellite remote sensing of aerosol optical depth: advances, challenges, and perspectives"
record[testTitle] = {
Expand All @@ -265,7 +294,6 @@ class AddonEvents extends AddonModule {
"total": 12
}
const title = args[1]
console.log(title)
if (record && record[title]) {
let recordTimeObj = record[title]
const total = recordTimeObj["total"]
Expand All @@ -284,14 +312,17 @@ class AddonEvents extends AddonModule {
maxSec = meanSec + (maxSec - meanSec) * .5
const minSec = 60
const pct = 1 / total * 100
let obj = Zotero.ZoteroStyle.events
let progressColor = obj.getValue("Zotero.ZoteroStyle.progressColor", obj.progressColor)
let [r, g, b] = progressColor.colorRgb()
for (let i=0; i<total; i++) {
// pageSpan represent a page, color represent the length of read time
let pageSpan = createElement("span")
let alpha = (recordTimeObj[i] || 0) / (maxSec > minSec ? maxSec : minSec)
pageSpan.style = `
width: ${pct}%;
height: 100%;
background-color: rgba(90, 193, 189, ${alpha < 1 ? alpha : 1});
background-color: rgba(${r}, ${g}, ${b}, ${alpha < 1 ? alpha : 1});
display: inline-block;
`
progressNode.appendChild(pageSpan)
Expand Down Expand Up @@ -375,4 +406,27 @@ class AddonEvents extends AddonModule {
}
}

String.prototype.colorRgb = function(){
var sColor = this.toLowerCase();
//十六进制颜色值的正则表达式
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
// 如果是16进制颜色
if (sColor && reg.test(sColor)) {
if (sColor.length === 4) {
var sColorNew = "#";
for (var i=1; i<4; i+=1) {
sColorNew += sColor.slice(i, i+1).concat(sColor.slice(i, i+1));
}
sColor = sColorNew;
}
//处理六位的颜色值
var sColorChange = [];
for (var i=1; i<7; i+=2) {
sColorChange.push(parseInt("0x"+sColor.slice(i, i+2)));
}
return sColorChange;
}
return sColor;
};

export default AddonEvents;
29 changes: 29 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@ class AddonModule {
constructor(parent: any) {
this._Addon = parent;
}

public setValue(k: string, v: string) {
var _Zotero = Components.classes["@zotero.org/Zotero;1"].getService(
Components.interfaces.nsISupports
).wrappedJSObject;
if (typeof(v) != "string") {
v = JSON.stringify(v)
}
_Zotero.Prefs.set(k, v)
}

public getValue(k: string, v: any = {}) {
var _Zotero = Components.classes["@zotero.org/Zotero;1"].getService(
Components.interfaces.nsISupports
).wrappedJSObject;
let _v = _Zotero.Prefs.get(k)
if (v != undefined) {
try {
if (typeof(v) == "object") {
_v = JSON.parse(_v)
} else if (typeof(v) == "number") {
_v = Number(_v)
}
} catch {
return v
}
}
return typeof(_v)==typeof(v) ? _v : v
}
}

export default AddonModule;
127 changes: 127 additions & 0 deletions src/setting.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="Zotero-Style-Setting">
<ul class="history" style="display: none;">
<li class="line">set hhi.ddaD</li>
<li class="line">get Zotero.XXX.aaa</li>
<li class="line" selected>remove 标题 in jo</li>

</ul>
<div class="input-box">
<span>></span>
<input type="text" placeholder="Enter your command here...">
</div>
</div>
</body>
<style>
#Zotero-Style-Setting {
---radius---: 10px;
position: fixed;
left: 20%;
bottom: 20%;
width: 60%;
min-height: 50px;
/* height: 70px; */
border-radius: var(---radius---);
border: 1px solid black;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;

}
input {
width: 90%;
height: 45px;
/* border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px; */
border: 1px solid red;
border: none;
outline: none;
border-radius: 5px;
font-size: 20px;
/* margin-bottom: 10px; */
}
.history {
width: 100%;
/* border: 1px solid teal; */
margin-top: 0;
padding: 0;
background-color: rgba(248, 240, 240, .4);
}
.history .line {
list-style-type: none;
width: calc(100% - 40px);
/* background-color: #eee; */
line-height: 2.5em;
padding: auto 10px;
display: inline-block;
padding-left: 20px;
padding-right: 20px;

}
.history .line:active {
background-color: rgba(220, 240, 240, 1);;
}
.history .line[selected] {
background-color: rgba(220, 240, 240, 1);
}
.history .line:first-child {
border-radius: var(---radius---) var(---radius---) 0 0;
}
</style>
<script>
let settingNode = document.querySelector("#Zotero-Style-Setting")
let inputNode = document.querySelector("input")
let historyNode = settingNode.querySelector(".history")
settingNode.addEventListener("keyup", (event)=>{
console.log(event)
let isInit = false
if (event.key=="ArrowUp") {
// 鼠标键
// 如果没显示history
if (historyNode.style.display=="none") {
// 让他显示,并默认选择第一个
historyNode.style.display = ""
isInit = true
console.log("初始化")
}
} else if (event.key=="Enter") {
// 回车则获取当前selected,填入input
inputNode.value = historyNode.querySelector(".line[selected]").innerText
// 并且收起historyNode
historyNode.style.display = "none"
}
// 选择逻辑
if ((event.key=="ArrowUp" || event.key=="ArrowDown") && !isInit) {
let lines = historyNode.querySelectorAll(".line")
for (let i=0;i<lines.length;i++){
if (lines[i].hasAttribute("selected")) {
lines[i].removeAttribute("selected")
// 得到当前被选择索引,选择下一个/上一个
if (event.key=="ArrowUp") {
i -= 1
} else if (event.key=="ArrowDown") {
i += 1
}
if (i==-1) {
i = lines.length - 1
}
if (i==lines.length) {
i = 0
}
lines[i].setAttribute("selected", "")
break
}
}
}
})
</script>
</html>
Loading

0 comments on commit 508f408

Please sign in to comment.