Skip to content

Commit

Permalink
add: more options and preview in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Nov 18, 2024
1 parent 59ec7f4 commit 1612f78
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 50 deletions.
Binary file added addon/chrome/content/fonts/Roboto-Black.ttf
Binary file not shown.
Binary file added addon/chrome/content/fonts/Roboto-Bold.ttf
Binary file not shown.
Binary file added addon/chrome/content/fonts/Roboto-Light.ttf
Binary file not shown.
Binary file added addon/chrome/content/fonts/Roboto-Medium.ttf
Binary file not shown.
Binary file added addon/chrome/content/fonts/Roboto-Regular.ttf
Binary file not shown.
Binary file added addon/chrome/content/fonts/Roboto-Thin.ttf
Binary file not shown.
55 changes: 55 additions & 0 deletions addon/chrome/content/preferences.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@font-face {
font-family: "Roboto";
src: url("fonts/Roboto-Regular.ttf") format("truetype");
font-weight: normal;
font-style: normal;
}

@font-face {
font-family: "Roboto";
src: url("fonts/Roboto-Bold.ttf") format("truetype");
font-weight: 700;
font-style: normal;
}

@font-face {
font-family: "Roboto";
src: url("fonts/Roboto-Black.ttf") format("truetype");
font-weight: 900;
font-style: normal;
}

@font-face {
font-family: "Roboto";
src: url("fonts/Roboto-Light.ttf") format("truetype");
font-weight: 300;
font-style: normal;
}

@font-face {
font-family: "Roboto";
src: url("fonts/Roboto-Thin.ttf") format("truetype");
font-weight: 100;
font-style: normal;
}

@font-face {
font-family: "Roboto";
src: url("fonts/Roboto-Medium.ttf") format("truetype");
font-weight: 500;
font-style: normal;
}

#__addonRef__-parsingPreviewWarning {
padding-top: 8px;
}

#__addonRef__-parsingPreviewWarning img {
width: 20px;
height: 20px;
padding: 6px;
}

#__addonRef__-pluginInfo {
padding-top: 28px;
}
50 changes: 44 additions & 6 deletions addon/chrome/content/preferences.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,50 @@
></html:input>
</hbox>
</groupbox>
<groupbox>
<groupbox id="__addonRef__-parsing">
<label><html:h2 data-l10n-id="parsing-title"></html:h2></label>
<hbox align="center">
<html:label
data-l10n-id="parsingContrast"
for="__addonRef__-parsingContrast"
data-l10n-id="opacityContrast"
for="__addonRef__-opacityContrast"
></html:label>
<html:input
id="__addonRef__-parsingContrast"
preference="__prefsPrefix__.parsingContrast"
id="__addonRef__-opacityContrast"
preference="__prefsPrefix__.opacityContrast"
type="number"
min="1"
max="5"
step="1"
></html:input>
</hbox>
<hbox align="center">
<html:label
data-l10n-id="weightContrast"
for="__addonRef__-weightContrast"
></html:label>
<html:input
id="__addonRef__-weightContrast"
preference="__prefsPrefix__.weightContrast"
type="number"
min="1"
max="5"
step="1"
></html:input>
</hbox>
<hbox align="center">
<html:label
data-l10n-id="weightOffset"
for="__addonRef__-weightOffset"
></html:label>
<html:input
id="__addonRef__-weightOffset"
preference="__prefsPrefix__.weightOffset"
type="number"
min="-3"
max="3"
step="1"
></html:input>
</hbox>
<hbox align="center">
<html:label
data-l10n-id="parsingOffset"
Expand All @@ -59,8 +87,18 @@
step="1"
></html:input>
</hbox>
<vbox no-highlight="true">
<html:div id="__addonRef__-parsingPreview"
>Zotero is a free, easy-to-use tool to help you collect, organize,
annotate, cite, and share research.</html:div
>
<hbox id="__addonRef__-parsingPreviewWarning" align="center">
<html:img src="chrome://zotero/skin/20/universal/error.svg"></html:img>
<html:div data-l10n-id="parsingPreviewWarning"></html:div>
</hbox>
</vbox>
</groupbox>
<vbox>
<vbox id="__addonRef__-pluginInfo">
<html:label
data-l10n-id="pref-help"
data-l10n-args='{"time": "__buildTime__","name": "__addonName__","version":"__buildVersion__"}'
Expand Down
9 changes: 7 additions & 2 deletions addon/locale/en-US/preferences.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ enableReaderToolbarButton =
disableForLanguages = Disable when the item language matches:
disableForLanguagesInput =
.placeholder = Separated by commas
parsing-title = Parsing
parsingContrast = Bionic contrast level:
parsingOffset = Bionic parsing offset:
opacityContrast = Opacity contrast level:
weightContrast = Font weight contrast level:
weightOffset = Font weight offset:
parsingOffset = Parsing offset:
parsingPreviewWarning = The Bionic parsing preview is for reference only and may not be fully accurate. Actual rendering may vary due to the embedded fonts in the PDF.
pref-help = { $name } Build { $version } { $time }
9 changes: 7 additions & 2 deletions addon/locale/zh-CN/preferences.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ enableReaderToolbarButton =
disableForLanguages = 当条目语言为以下时禁用:
disableForLanguagesInput =
.placeholder = 以逗号分隔
parsing-title = 解析
parsingContrast = Bionic对比度等级:
parsingOffset = Bionic解析偏移:
opacityContrast = 透明度对比等级:
weightContrast = 字重对比等级:
weightOffset = 字重偏移:
parsingOffset = 解析偏移:
parsingPreviewWarning = Bionic解析预览仅供参考,可能不完全准确。实际渲染可能因PDF中的嵌入字体而有所不同。
pref-help = { $name } Build { $version } { $time }
4 changes: 3 additions & 1 deletion addon/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
pref("__prefsPrefix__.enableBionicReader", true);
pref("__prefsPrefix__.disableForLanguages", "");
pref("__prefsPrefix__.enableReaderToolbarButton", true);
pref("__prefsPrefix__.parsingContrast", 1);
pref("__prefsPrefix__.opacityContrast", 1);
pref("__prefsPrefix__.weightContrast", 3);
pref("__prefsPrefix__.weightOffset", 0);
pref("__prefsPrefix__.parsingOffset", 0);
2 changes: 1 addition & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { config } from "../package.json";
import { initMenus } from "./modules/menu";
import { initPreferencePane } from "./modules/preferencePane";
import { initPreferencePane } from "./modules/preferences";
import { initSettings, unInitSettings } from "./modules/settings";
import { initReader, refreshReaders, unInitReader } from "./modules/reader";
import { initLocale } from "./utils/locale";
Expand Down
6 changes: 6 additions & 0 deletions src/modules/preferencePane.ts → src/modules/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ function initPreferencePane() {
src: rootURI + "chrome/content/preferences.xhtml",
pluginID: addon.data.config.addonID,
label: "Bionic",
scripts: [
`chrome://${addon.data.config.addonRef}/content/scripts/preferences.js`,
],
stylesheets: [
`chrome://${addon.data.config.addonRef}/content/preferences.css`,
],
});
}
8 changes: 6 additions & 2 deletions src/modules/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,15 @@ async function refreshReaders(readers?: _ZoteroTypes.ReaderInstance[]) {
function setWindowPrefs(reader: _ZoteroTypes.ReaderInstance, win: Window) {
win.__BIONIC_READER_ENABLED = getCurrentItemStatus(reader.itemID || -1);
win.__BIONIC_PARSING_OFFSET = getPref("parsingOffset") || 0;
win.__BIONIC_PARSING_CONTRAST = getPref("parsingContrast") || 1;
win.__BIONIC_OPACITY_CONTRAST = getPref("opacityContrast") || 0;
win.__BIONIC_WEIGHT_CONTRAST = getPref("weightContrast") || 0;
win.__BIONIC_WEIGHT_OFFSET = getPref("weightOffset") || 0;
}

function deleteWindowPrefs(win: Window) {
delete win.__BIONIC_READER_ENABLED;
delete win.__BIONIC_PARSING_OFFSET;
delete win.__BIONIC_PARSING_CONTRAST;
delete win.__BIONIC_OPACITY_CONTRAST;
delete win.__BIONIC_WEIGHT_CONTRAST;
delete win.__BIONIC_WEIGHT_OFFSET;
}
4 changes: 3 additions & 1 deletion src/modules/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ let prefsObservers: symbol[] = [];
const PREFS_TO_OBSERVE = [
"enableBionicReader",
"parsingOffset",
"parsingContrast",
"opacityContrast",
"weightContrast",
"weightOffset",
];

function initSettings() {
Expand Down
162 changes: 162 additions & 0 deletions src/preferences/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/* eslint-disable no-restricted-globals */
import { config } from "../../package.json";
import { computeFont } from "../utils/font";

function main() {
document.querySelector("#prefs-navigation")?.addEventListener("select", init);

setTimeout(() => {
init();
}, 1000);
}

main();

let initialized = false;

function init() {
if (initialized) {
return;
}
const parsingContainer = document.querySelector(
`#${config.addonRef}-parsing`,
);
if (!parsingContainer) {
return;
}
initialized = true;
document
.querySelector("#prefs-navigation")
?.removeEventListener("select", init);

parsingContainer.addEventListener("input", (event) => {
updatePreview();
});
updatePreview();
}

const PREVIEW_STRING =
"Zotero is a free, easy-to-use tool to help you collect, organize, annotate, cite, and share research.";
const BIONIC_GROUPS = [
{ startIdx: 0, endIdx: 3, isBold: true },
{ startIdx: 3, endIdx: 7, isBold: false },
{ startIdx: 7, endIdx: 8, isBold: true },
{ startIdx: 8, endIdx: 10, isBold: false },
{ startIdx: 10, endIdx: 11, isBold: true },
{ startIdx: 11, endIdx: 12, isBold: false },
{ startIdx: 12, endIdx: 14, isBold: true },
{ startIdx: 14, endIdx: 18, isBold: false },
{ startIdx: 18, endIdx: 20, isBold: true },
{ startIdx: 20, endIdx: 23, isBold: false },
{ startIdx: 23, endIdx: 24, isBold: true },
{ startIdx: 24, endIdx: 26, isBold: false },
{ startIdx: 26, endIdx: 27, isBold: true },
{ startIdx: 27, endIdx: 30, isBold: false },
{ startIdx: 30, endIdx: 32, isBold: true },
{ startIdx: 32, endIdx: 35, isBold: false },
{ startIdx: 35, endIdx: 36, isBold: true },
{ startIdx: 36, endIdx: 38, isBold: false },
{ startIdx: 38, endIdx: 40, isBold: true },
{ startIdx: 40, endIdx: 43, isBold: false },
{ startIdx: 43, endIdx: 44, isBold: true },
{ startIdx: 44, endIdx: 47, isBold: false },
{ startIdx: 47, endIdx: 51, isBold: true },
{ startIdx: 51, endIdx: 56, isBold: false },
{ startIdx: 56, endIdx: 60, isBold: true },
{ startIdx: 60, endIdx: 66, isBold: false },
{ startIdx: 66, endIdx: 70, isBold: true },
{ startIdx: 70, endIdx: 76, isBold: false },
{ startIdx: 76, endIdx: 78, isBold: true },
{ startIdx: 78, endIdx: 82, isBold: false },
{ startIdx: 82, endIdx: 83, isBold: true },
{ startIdx: 83, endIdx: 86, isBold: false },
{ startIdx: 86, endIdx: 89, isBold: true },
{ startIdx: 89, endIdx: 92, isBold: false },
{ startIdx: 92, endIdx: 96, isBold: true },
{ startIdx: 96, endIdx: 101, isBold: false },
];

function updatePreview() {
const previewContainer = document.querySelector(
`#${config.addonRef}-parsingPreview`,
);

if (!previewContainer) {
return;
}

const fontData = computeFont({
alpha: 1,
font: 'normal normal 14px "Roboto", sans-serif',
opacityContrast:
parseInt(
(
document.querySelector(
`#${config.addonRef}-opacityContrast`,
) as HTMLInputElement
)?.value,
) || 1,
weightContrast:
parseInt(
(
document.querySelector(
`#${config.addonRef}-weightContrast`,
) as HTMLInputElement
)?.value,
) || 3,
weightOffset:
parseInt(
(
document.querySelector(
`#${config.addonRef}-weightOffset`,
) as HTMLInputElement
)?.value,
) || 0,
});

const parsingOffset =
parseInt(
(
document.querySelector(
`#${config.addonRef}-parsingOffset`,
) as HTMLInputElement
)?.value,
) || 0;

previewContainer.innerHTML = "";

for (let i = 0; i < BIONIC_GROUPS.length; i++) {
const group = BIONIC_GROUPS[i];
let startIdx = group.startIdx;
let endIdx = group.endIdx;
if (parsingOffset) {
const nextGroup = BIONIC_GROUPS[i + 1];
const prevGroup = BIONIC_GROUPS[i - 1];
if (group.isBold && nextGroup) {
endIdx = Math.max(
// Can grow until the next group ends
Math.min(endIdx + parsingOffset, nextGroup.endIdx),
startIdx + 1,
);
} else if (!group.isBold && prevGroup) {
startIdx = Math.min(
// Can shrink until the previous group starts
Math.max(startIdx + parsingOffset, prevGroup.startIdx + 1),
endIdx,
);
}
}

if (startIdx >= endIdx) {
continue;
}

const span = document.createElement("span");
span.textContent = PREVIEW_STRING.slice(startIdx, endIdx);
span.style.font = group.isBold ? fontData.bold.font : fontData.light.font;
if (!group.isBold) {
span.style.opacity = String(fontData.light.alpha);
}
previewContainer.appendChild(span);
}
}
Loading

0 comments on commit 1612f78

Please sign in to comment.