aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/context.html18
-rw-r--r--ext/bg/css/settings.css154
-rw-r--r--ext/bg/js/conditions-ui.js4
-rw-r--r--ext/bg/js/search.js52
-rw-r--r--ext/bg/js/settings.js25
-rw-r--r--ext/bg/legal.html20
-rw-r--r--ext/bg/search.html22
-rw-r--r--ext/bg/settings.html165
-rw-r--r--ext/fg/float.html2
-rw-r--r--ext/fg/js/float.js18
-rw-r--r--ext/fg/js/frontend.js14
-rw-r--r--ext/fg/js/popup-proxy-host.js6
-rw-r--r--ext/fg/js/popup-proxy.js4
-rw-r--r--ext/fg/js/popup.js21
-rw-r--r--ext/mixed/css/display.css4
-rw-r--r--ext/mixed/js/display.js191
-rw-r--r--ext/mixed/js/extension.js15
-rw-r--r--ext/mixed/js/scroll.js100
18 files changed, 583 insertions, 252 deletions
diff --git a/ext/bg/context.html b/ext/bg/context.html
index 198ccd42..51346838 100644
--- a/ext/bg/context.html
+++ b/ext/bg/context.html
@@ -14,20 +14,20 @@
.btn-group {
display: flex;
+ margin-top: 10px;
+ white-space: nowrap;
}
</style>
</head>
<body>
- <p>
+ <div>
<input type="checkbox" id="enable-search">
- </p>
- <p>
- <div class="btn-group" style="white-space: nowrap">
- <button type="button" id="open-search" title="Search (Alt + Insert)" class="btn btn-default btn-xs glyphicon glyphicon-search"></button>
- <button type="button" id="open-options" title="Options" class="btn btn-default btn-xs glyphicon glyphicon-wrench"></button>
- <button type="button" id="open-help" title="Help" class="btn btn-default btn-xs glyphicon glyphicon-question-sign"></button>
- </div>
- </p>
+ </div>
+ <div class="btn-group">
+ <button type="button" id="open-search" title="Search (Alt + Insert)" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-search"></span></button>
+ <button type="button" id="open-options" title="Options" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-wrench"></span></button>
+ <button type="button" id="open-help" title="Help" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-question-sign"></span></button>
+ </div>
<script src="/mixed/lib/jquery.min.js"></script>
<script src="/mixed/lib/bootstrap-toggle/bootstrap-toggle.min.js"></script>
diff --git a/ext/bg/css/settings.css b/ext/bg/css/settings.css
new file mode 100644
index 00000000..12bbe8a8
--- /dev/null
+++ b/ext/bg/css/settings.css
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2019 Alex Yatskov <alex@foosoft.net>
+ * Author: Alex Yatskov <alex@foosoft.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#anki-spinner, #anki-general, #anki-error,
+#dict-spinner, #dict-error, #dict-warning, #dict-purge, #dict-import-progress,
+#debug, .options-advanced, .storage-hidden, #storage-spinner {
+ display: none;
+}
+
+.tab-content {
+ border-bottom: 1px #ddd solid;
+ border-left: 1px #ddd solid;
+ border-right: 1px #ddd solid;
+ padding: 10px;
+}
+
+#field-templates {
+ font-family: monospace;
+ overflow-x: hidden;
+ white-space: pre;
+}
+
+.bottom-links {
+ padding-bottom: 1em;
+}
+
+.label-light {
+ font-weight: normal;
+}
+
+.form-control.is-invalid {
+ border-color: #f00000;
+}
+
+.condition {
+ display: flex;
+ -flex-wrap: wrap;
+}
+.condition-input {
+ flex: 1 1 auto;
+}
+.condition-line-break {
+ flex: 1 0 100%;
+ display: none;
+}
+.condition>.input-group-btn {
+ width: auto;
+}
+.condition>.condition-prefix:after {
+ content: "IF";
+}
+.condition:nth-child(n+2)>.condition-prefix:after {
+ content: "AND";
+}
+
+.input-group .condition-prefix {
+ flex: 0 0 auto;
+}
+.input-group .condition-prefix,
+.input-group .condition-group-separator-label {
+ width: 60px;
+ text-align: center;
+}
+.input-group .condition-group-separator-label {
+ padding: 6px 12px;
+ font-weight: bold;
+ display: inline-block;
+}
+.input-group .condition-type,
+.input-group .condition-operator {
+ width: auto;
+ text-align: center;
+ text-align-last: center;
+}
+
+.condition-group>.condition>div:first-child {
+ border-bottom-left-radius: 0;
+}
+.condition-group>.condition:nth-child(n+2)>div:first-child {
+ border-top-left-radius: 0;
+}
+.condition-group>.condition:nth-child(n+2)>div:last-child>button {
+ border-top-right-radius: 0;
+}
+.condition-group>.condition:nth-last-child(n+2)>div:last-child>button {
+ border-bottom-right-radius: 0;
+}
+.condition-group-options>.condition-add {
+ border-top-left-radius: 0;
+ border-top-right-radius: 0;
+}
+
+.condition-groups>*:last-of-type {
+ display: none;
+}
+
+#custom-popup-css {
+ width: 100%;
+ min-height: 34px;
+ height: 96px;
+ resize: vertical;
+ font-family: 'Courier New', Courier, monospace;
+ white-space: pre;
+}
+
+[data-show-for-browser] {
+ display: none;
+}
+
+[data-browser=edge] [data-show-for-browser~=edge],
+[data-browser=chrome] [data-show-for-browser~=chrome],
+[data-browser=firefox] [data-show-for-browser~=firefox],
+[data-browser=firefox-mobile] [data-show-for-browser~=firefox-mobile] {
+ display: initial;
+}
+
+@media screen and (max-width: 740px) {
+ .col-xs-6 {
+ float: none;
+ width: 100%;
+ }
+ .col-xs-6+.col-xs-6 {
+ margin-top: 15px;
+ }
+}
+
+@media screen and (max-width: 600px) {
+ .condition {
+ flex-wrap: wrap;
+ }
+ .condition-input {
+ order: 2;
+ }
+ .condition-line-break {
+ display: block;
+ order: 1;
+ }
+}
diff --git a/ext/bg/js/conditions-ui.js b/ext/bg/js/conditions-ui.js
index a6f54a1c..43c6dc08 100644
--- a/ext/bg/js/conditions-ui.js
+++ b/ext/bg/js/conditions-ui.js
@@ -36,7 +36,7 @@ ConditionsUI.Container = class Container {
this.container.empty();
- for (const conditionGroup of conditionGroups) {
+ for (const conditionGroup of toIterable(conditionGroups)) {
this.children.push(new ConditionsUI.ConditionGroup(this, conditionGroup));
}
@@ -122,7 +122,7 @@ ConditionsUI.ConditionGroup = class ConditionGroup {
this.separator = ConditionsUI.instantiateTemplate('#condition-group-separator-template').appendTo(parent.container);
this.addButton = this.options.find('.condition-add');
- for (const condition of conditionGroup.conditions) {
+ for (const condition of toIterable(conditionGroup.conditions)) {
this.children.push(new ConditionsUI.Condition(this, condition));
}
diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js
index 6ff710f0..13ed1e08 100644
--- a/ext/bg/js/search.js
+++ b/ext/bg/js/search.js
@@ -19,20 +19,27 @@
class DisplaySearch extends Display {
constructor() {
- super($('#spinner'), $('#content'));
+ super(document.querySelector('#spinner'), document.querySelector('#content'));
this.optionsContext = {
depth: 0,
url: window.location.href
};
- this.search = $('#search').click(this.onSearch.bind(this));
- this.query = $('#query').on('input', this.onSearchInput.bind(this));
- this.intro = $('#intro');
+ this.search = document.querySelector('#search');
+ this.query = document.querySelector('#query');
+ this.intro = document.querySelector('#intro');
+ this.introHidden = false;
this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract});
- window.wanakana.bind(this.query.get(0));
+ if (this.search !== null) {
+ this.search.addEventListener('click', (e) => this.onSearch(e), false);
+ }
+ if (this.query !== null) {
+ this.query.addEventListener('input', () => this.onSearchInput(), false);
+ window.wanakana.bind(this.query);
+ }
}
onError(error) {
@@ -40,23 +47,50 @@ class DisplaySearch extends Display {
}
onSearchClear() {
- this.query.focus().select();
+ if (this.query === null) {
+ return;
+ }
+
+ this.query.focus();
+ this.query.select();
}
onSearchInput() {
- this.search.prop('disabled', this.query.val().length === 0);
+ this.search.disabled = (this.query === null || this.query.value.length === 0);
}
async onSearch(e) {
+ if (this.query === null) {
+ return;
+ }
+
try {
e.preventDefault();
- this.intro.slideUp();
- const {length, definitions} = await apiTermsFind(this.query.val(), this.optionsContext);
+ this.hideIntro();
+ const {length, definitions} = await apiTermsFind(this.query.value, this.optionsContext);
super.termsShow(definitions, await apiOptionsGet(this.optionsContext));
} catch (e) {
this.onError(e);
}
}
+
+ hideIntro() {
+ if (this.introHidden) {
+ return;
+ }
+
+ this.introHidden = true;
+
+ if (this.intro === null) {
+ return;
+ }
+
+ const size = this.intro.getBoundingClientRect();
+ this.intro.style.height = `${size.height}px`;
+ this.intro.style.transition = 'height 0.4s ease-in-out 0s';
+ window.getComputedStyle(this.intro).getPropertyValue('height'); // Commits height so next line can start animation
+ this.intro.style.height = '0';
+ }
}
window.yomichan_search = new DisplaySearch();
diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js
index a0fe7c70..9838ea02 100644
--- a/ext/bg/js/settings.js
+++ b/ext/bg/js/settings.js
@@ -151,6 +151,7 @@ async function formWrite(options) {
function formSetupEventListeners() {
$('#dict-purge-link').click(utilAsync(onDictionaryPurge));
$('#dict-file').change(utilAsync(onDictionaryImport));
+ $('#dict-file-button').click(onDictionaryImportButtonClick);
$('#field-templates-reset').click(utilAsync(onAnkiFieldTemplatesReset));
$('input, select, textarea').not('.anki-model').not('.profile-form *').change(utilAsync(onFormOptionsChanged));
@@ -240,6 +241,8 @@ async function onFormOptionsChanged(e) {
}
async function onReady() {
+ showExtensionInformation();
+
formSetupEventListeners();
await profileOptionsSetup();
@@ -422,7 +425,7 @@ async function onDictionaryPurge(e) {
dictionarySpinnerShow(true);
await utilDatabasePurge();
- for (const options of await getOptionsArray()) {
+ for (const options of toIterable(await getOptionsArray())) {
options.dictionaries = utilBackgroundIsolate({});
options.general.mainDictionary = '';
}
@@ -446,6 +449,11 @@ async function onDictionaryPurge(e) {
}
}
+function onDictionaryImportButtonClick() {
+ const dictFile = document.querySelector('#dict-file');
+ dictFile.click();
+}
+
async function onDictionaryImport(e) {
const dictFile = $('#dict-file');
const dictControls = $('#dict-importer').hide();
@@ -466,7 +474,7 @@ async function onDictionaryImport(e) {
const exceptions = [];
const summary = await utilDatabaseImport(e.target.files[0], updateProgress, exceptions);
- for (const options of await getOptionsArray()) {
+ for (const options of toIterable(await getOptionsArray())) {
options.dictionaries[summary.title] = utilBackgroundIsolate({
enabled: true,
priority: 0,
@@ -741,3 +749,16 @@ function storageSpinnerShow(show) {
spinner.hide();
}
}
+
+
+/*
+ * Information
+ */
+
+function showExtensionInformation() {
+ const node = document.getElementById('extension-info');
+ if (node === null) { return; }
+
+ const manifest = chrome.runtime.getManifest();
+ node.textContent = `${manifest.name} v${manifest.version}`;
+}
diff --git a/ext/bg/legal.html b/ext/bg/legal.html
index 28c7fb21..26ac033d 100644
--- a/ext/bg/legal.html
+++ b/ext/bg/legal.html
@@ -8,10 +8,10 @@
<link rel="stylesheet" type="text/css" href="/mixed/lib/bootstrap/css/bootstrap-theme.min.css">
</head>
<body>
- <div class="container-fluid">
+ <div class="container">
<h3>Yomichan License</h3>
<pre>
-Copyright (C) 2016-2017 Alex Yatskov
+Copyright (C) 2016-2019 Alex Yatskov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -28,10 +28,20 @@ along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.
</pre>
<h3>EDRDG License</h3>
<pre>
-This package uses the <a href="http://www.csse.monash.edu.au/~jwb/edict.html">EDICT</a> and <a href="http://www.csse.monash.edu.au/~jwb/kanjidic.html">KANJIDIC</a> dictionary files. These files are
-the property of the <a href="http://www.edrdg.org/"> Electronic Dictionary Research and Development Group</a>,
-and are used in conformance with the Group's <a href="http://www.edrdg.org/edrdg/licence.html">licence</a>.
+This package uses the <a href="https://www.edrdg.org/jmdict/edict.html">EDICT</a> and <a href="https://www.edrdg.org/wiki/index.php/KANJIDIC_Project">KANJIDIC</a> dictionary files. These files are
+the property of the <a href="https://www.edrdg.org/">Electronic Dictionary Research and Development Group</a>,
+and are used in conformance with the Group's <a href="https://www.edrdg.org/edrdg/licence.html">licence</a>.
</pre>
+ <h3>Third-Party Software Licenses</h3>
+ <ul>
+ <li><a href="https://github.com/twbs/bootstrap/blob/v3.3.7/LICENSE" target="_blank" rel="noopener">Bootstrap v3.3.7</a></li>
+ <li><a href="https://github.com/minhur/bootstrap-toggle/blob/2.2.0/LICENSE" target="_blank" rel="noopener">Bootstrap Toggle v2.2.0</a></li>
+ <li><a href="https://github.com/dfahlander/Dexie.js/blob/v2.0.0-beta.10/LICENSE" target="_blank" rel="noopener">Dexie v2.0.0-beta.10</a></li>
+ <li><a href="https://github.com/wycats/handlebars.js/blob/v4.0.6/LICENSE" target="_blank" rel="noopener">Handlebars v4.0.6</a></li>
+ <li><a href="https://github.com/jquery/jquery/blob/3.2.1/LICENSE.txt" target="_blank" rel="noopener">jQuery v3.2.1</a></li>
+ <li><a href="https://github.com/Stuk/jszip/blob/v3.1.3/LICENSE.markdown" target="_blank" rel="noopener">JSZip v3.1.3</a></li>
+ <li><a href="https://github.com/WaniKani/WanaKana/blob/2.2.3/LICENSE" target="_blank" rel="noopener">WanaKana v2.2.3</a></li>
+ </ul>
</div>
</div>
</body>
diff --git a/ext/bg/search.html b/ext/bg/search.html
index 38c5a4e9..668b2436 100644
--- a/ext/bg/search.html
+++ b/ext/bg/search.html
@@ -9,22 +9,20 @@
<link rel="stylesheet" type="text/css" href="/mixed/css/display.css">
</head>
<body>
- <div class="container-fluid">
- <div id="intro">
+ <div class="container">
+ <div id="intro" style="overflow: hidden;">
<div class="page-header">
<h1>Yomichan Search</h1>
</div>
- <p>Search your installed dictionaries by entering a Japanese expression into the field below.</p>
+ <p style="margin-bottom: 0;">Search your installed dictionaries by entering a Japanese expression into the field below.</p>
</div>
- <p>
- <form class="input-group">
- <input type="text" class="form-control" placeholder="Search for..." id="query" autofocus>
- <span class="input-group-btn">
- <input type="submit" class="btn btn-default form-control" id="search" value="Search" disabled>
- </span>
- </form>
- </p>
+ <form class="input-group" style="padding-top: 10px;">
+ <input type="text" class="form-control" placeholder="Search for..." id="query" autofocus>
+ <span class="input-group-btn">
+ <input type="submit" class="btn btn-default form-control" id="search" value="Search" disabled>
+ </span>
+ </form>
<div id="spinner">
<img src="/mixed/img/spinner.gif">
@@ -34,7 +32,6 @@
</div>
<script src="/mixed/lib/handlebars.min.js"></script>
- <script src="/mixed/lib/jquery.min.js"></script>
<script src="/mixed/lib/wanakana.min.js"></script>
<script src="/mixed/js/extension.js"></script>
@@ -49,6 +46,7 @@
<script src="/fg/js/source.js"></script>
<script src="/mixed/js/display.js"></script>
<script src="/mixed/js/japanese.js"></script>
+ <script src="/mixed/js/scroll.js"></script>
<script src="/bg/js/search.js"></script>
<script src="/bg/js/search-frontend.js"></script>
diff --git a/ext/bg/settings.html b/ext/bg/settings.html
index 6fdd54c4..0bc5e14c 100644
--- a/ext/bg/settings.html
+++ b/ext/bg/settings.html
@@ -6,116 +6,14 @@
<title>Yomichan Options</title>
<link rel="stylesheet" type="text/css" href="/mixed/lib/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/mixed/lib/bootstrap/css/bootstrap-theme.min.css">
- <style>
- #anki-spinner, #anki-general, #anki-error,
- #dict-spinner, #dict-error, #dict-warning, #dict-purge, #dict-import-progress,
- #debug, .options-advanced, .storage-hidden, #storage-spinner {
- display: none;
- }
-
- .tab-content {
- border-bottom: 1px #ddd solid;
- border-left: 1px #ddd solid;
- border-right: 1px #ddd solid;
- padding: 10px;
- }
-
- #field-templates {
- font-family: monospace;
- overflow-x: hidden;
- white-space: pre;
- }
-
- .bottom-links {
- padding-bottom: 1em;
- }
-
- .label-light {
- font-weight: normal;
- }
-
- .form-control.is-invalid {
- border-color: #f00000;
- }
-
- .condition>.condition-prefix:after {
- content: "IF";
- }
- .condition:nth-child(n+2)>.condition-prefix:after {
- content: "AND";
- }
-
- .input-group .condition-prefix,
- .input-group .condition-group-separator-label {
- width: 60px;
- text-align: center;
- }
- .input-group .condition-group-separator-label {
- padding: 6px 12px;
- font-weight: bold;
- display: inline-block;
- }
- .input-group .condition-type,
- .input-group .condition-operator {
- width: auto;
- text-align: center;
- text-align-last: center;
- }
-
- .condition-group>.condition>div:first-child {
- border-bottom-left-radius: 0;
- }
- .condition-group>.condition:nth-child(n+2)>div:first-child {
- border-top-left-radius: 0;
- }
- .condition-group>.condition:nth-child(n+2)>div:last-child>button {
- border-top-right-radius: 0;
- }
- .condition-group>.condition:nth-last-child(n+2)>div:last-child>button {
- border-bottom-right-radius: 0;
- }
- .condition-group-options>.condition-add {
- border-top-left-radius: 0;
- border-top-right-radius: 0;
- }
-
- .condition-groups>*:last-of-type {
- display: none;
- }
-
- #custom-popup-css {
- width: 100%;
- min-height: 34px;
- height: 96px;
- resize: vertical;
- font-family: 'Courier New', Courier, monospace;
- white-space: pre;
- }
-
- [data-show-for-browser] {
- display: none;
- }
-
- [data-browser=edge] [data-show-for-browser~=edge],
- [data-browser=chrome] [data-show-for-browser~=chrome],
- [data-browser=firefox] [data-show-for-browser~=firefox],
- [data-browser=firefox-mobile] [data-show-for-browser~=firefox-mobile] {
- display: initial;
- }
-
- @media screen and (max-width: 740px) {
- .col-xs-6 {
- float: none;
- width: 100%;
- }
- .col-xs-6+.col-xs-6 {
- margin-top: 15px;
- }
- }
- </style>
+ <link rel="stylesheet" type="text/css" href="/bg/css/settings.css">
</head>
<body>
- <div class="container-fluid">
+ <div class="container">
+ <div class="page-header">
+ <h1>Yomichan Options</h1>
+ </div>
+
<div class="profile-form">
<h3>Profiles</h3>
@@ -211,7 +109,8 @@
<div class="input-group-addon condition-prefix"></div>
<div class="input-group-btn"><select class="form-control btn btn-default condition-type"><optgroup label="Type"></optgroup></select></div>
<div class="input-group-btn"><select class="form-control btn btn-default condition-operator"><optgroup label="Operator"></optgroup></select></div>
- <input type="text" class="form-control" />
+ <div class="condition-line-break"></div>
+ <div class="condition-input"><input type="text" class="form-control" /></div>
<div class="input-group-btn"><button class="btn btn-danger condition-remove" title="Remove"><span class="glyphicon glyphicon-remove"></span></button></div>
</div></template>
<template id="condition-group-separator-template"><div class="input-group">
@@ -293,14 +192,14 @@
<div class="form-group">
<div class="row">
<div class="col-xs-6">
- <label for="popup-display-mode">Popup position for horizontal text</label>
+ <label for="popup-horizontal-text-position">Popup position for horizontal text</label>
<select class="form-control" id="popup-horizontal-text-position">
<option value="below">Below text</option>
<option value="above">Above text</option>
</select>
</div>
<div class="col-xs-6">
- <label for="popup-display-mode">Popup position for vertical text</label>
+ <label for="popup-vertical-text-position">Popup position for vertical text</label>
<select class="form-control" id="popup-vertical-text-position">
<option value="default">Same as for horizontal text</option>
<option value="before">Before text reading direction</option>
@@ -315,11 +214,11 @@
<div class="form-group options-advanced">
<div class="row">
<div class="col-xs-6">
- <label for="popup-display-mode">Popup width <span class="label-light">(in pixels)</span></label>
+ <label for="popup-width">Popup width <span class="label-light">(in pixels)</span></label>
<input type="number" min="1" id="popup-width" class="form-control">
</div>
<div class="col-xs-6">
- <label for="popup-display-mode">Popup height <span class="label-light">(in pixels)</span></label>
+ <label for="popup-height">Popup height <span class="label-light">(in pixels)</span></label>
<input type="number" min="1" id="popup-height" class="form-control">
</div>
</div>
@@ -328,11 +227,11 @@
<div class="form-group options-advanced">
<div class="row">
<div class="col-xs-6">
- <label for="popup-display-mode">Horizontal popup offset <span class="label-light">(in pixels)</span></label>
+ <label for="popup-horizontal-offset">Horizontal popup offset <span class="label-light">(in pixels)</span></label>
<input type="number" min="0" id="popup-horizontal-offset" class="form-control">
</div>
<div class="col-xs-6">
- <label for="popup-display-mode">Vertical popup offset <span class="label-light">(in pixels)</span></label>
+ <label for="popup-vertical-offset">Vertical popup offset <span class="label-light">(in pixels)</span></label>
<input type="number" min="0" id="popup-vertical-offset" class="form-control">
</div>
</div>
@@ -341,11 +240,11 @@
<div class="form-group options-advanced">
<div class="row">
<div class="col-xs-6">
- <label for="popup-display-mode">Horizontal popup offset for vertical text <span class="label-light">(in pixels)</span></label>
+ <label for="popup-horizontal-offset2">Horizontal popup offset for vertical text <span class="label-light">(in pixels)</span></label>
<input type="number" min="0" id="popup-horizontal-offset2" class="form-control">
</div>
<div class="col-xs-6">
- <label for="popup-display-mode">Vertical popup offset for vertical text <span class="label-light">(in pixels)</span></label>
+ <label for="popup-vertical-offset2">Vertical popup offset for vertical text <span class="label-light">(in pixels)</span></label>
<input type="number" min="0" id="popup-vertical-offset2" class="form-control">
</div>
</div>
@@ -414,7 +313,7 @@
</p>
<div class="checkbox">
- <label><input type="checkbox" id="enable-search-within-first-popup"> Enable search within the first popup with Alt+B</label>
+ <label><input type="checkbox" id="enable-search-within-first-popup"> Enable search when clicking glossary entries</label>
</div>
<div class="checkbox">
@@ -469,7 +368,8 @@
<a href="https://foosoft.net/projects/yomichan" target="_blank" rel="noopener">download free dictionaries</a>
for use with this extension and to learn about importing proprietary EPWING dictionaries.
</p>
- <input type="file" id="dict-file">
+ <button class="btn btn-primary" id="dict-file-button">Import Dictionary</button>
+ <div hidden><input type="file" id="dict-file"></div>
</div>
</div>
@@ -503,7 +403,7 @@
</div></div>
<div>
- <input type="button" value="Refresh" id="storage-refresh" />
+ <button class="btn btn-default" id="storage-refresh">Refresh</button>
</div>
</div>
@@ -621,6 +521,29 @@
</div>
<div>
+ <h3>Keyboard Shortcuts</h3>
+
+ <p class="help-block">
+ Yomichan includes keyboard shortcuts for common actions which can be adjusted
+ using the web browser's settings.
+ Instructions on how to access this settings page are listed below for a few browsers.
+ </p>
+
+ <h4>Chrome</h4>
+ <ul class="help-block">
+ <li>Open the settings page (<a href="chrome://settings/">chrome://settings/</a>)</li>
+ <li>Click the "Extensions" link in the left sidebar (<a href="chrome://extensions/">chrome://extensions/</a>)</li>
+ <li>Open the left side panel using the hamburger (<strong>&#8801;</strong>) menu button, then click "Keyboard shortcuts"</li>
+ </ul>
+
+ <h4>Firefox</h4>
+ <ul class="help-block">
+ <li>Open the extensions page (<a href="about:addons">about:addons</a>)</li>
+ <li>Click the button on the right with the gear icon, then click "Manage Extension Shortcuts"</li>
+ </ul>
+ </div>
+
+ <div>
<h3>Support Development</h3>
<p class="help-block">
@@ -639,7 +562,7 @@
<pre id="debug"></pre>
<div class="pull-right bottom-links">
- <small><a href="search.html">Search</a> &bull; <a href="https://foosoft.net/projects/yomichan/" target="_blank" rel="noopener">Homepage</a> &bull; <a href="legal.html">Legal</a></small>
+ <small><span id="extension-info"></span> &bull; <a href="search.html">Search</a> &bull; <a href="https://foosoft.net/projects/yomichan/" target="_blank" rel="noopener">Homepage</a> &bull; <a href="legal.html">Legal</a></small>
</div>
</div>
diff --git a/ext/fg/float.html b/ext/fg/float.html
index 465db589..52c7faa3 100644
--- a/ext/fg/float.html
+++ b/ext/fg/float.html
@@ -31,7 +31,6 @@
</div>
</div>
- <script src="/mixed/lib/jquery.min.js"></script>
<script src="/mixed/lib/wanakana.min.js"></script>
<script src="/mixed/js/extension.js"></script>
@@ -41,6 +40,7 @@
<script src="/fg/js/document.js"></script>
<script src="/fg/js/source.js"></script>
<script src="/mixed/js/display.js"></script>
+ <script src="/mixed/js/scroll.js"></script>
<script src="/fg/js/float.js"></script>
diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js
index fd7986b8..2e952efb 100644
--- a/ext/fg/js/float.js
+++ b/ext/fg/js/float.js
@@ -19,7 +19,7 @@
class DisplayFloat extends Display {
constructor() {
- super($('#spinner'), $('#definitions'));
+ super(document.querySelector('#spinner'), document.querySelector('#definitions'));
this.autoPlayAudioTimer = null;
this.styleNode = null;
@@ -30,7 +30,7 @@ class DisplayFloat extends Display {
this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract});
- $(window).on('message', utilAsync(this.onMessage.bind(this)));
+ window.addEventListener('message', (e) => this.onMessage(e), false);
}
onError(error) {
@@ -42,8 +42,16 @@ class DisplayFloat extends Display {
}
onOrphaned() {
- $('#definitions').hide();
- $('#error-orphaned').show();
+ const definitions = document.querySelector('#definitions');
+ const errorOrphaned = document.querySelector('#error-orphaned');
+
+ if (definitions !== null) {
+ definitions.style.setProperty('display', 'none', 'important');
+ }
+
+ if (errorOrphaned !== null) {
+ errorOrphaned.style.setProperty('display', 'block', 'important');
+ }
}
onSearchClear() {
@@ -86,7 +94,7 @@ class DisplayFloat extends Display {
}
};
- const {action, params} = e.originalEvent.data;
+ const {action, params} = e.data;
const handler = handlers[action];
if (handler) {
handler(params);
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
index 167e82c0..d5bb00c0 100644
--- a/ext/fg/js/frontend.js
+++ b/ext/fg/js/frontend.js
@@ -128,7 +128,7 @@ class Frontend {
}
this.popupTimerClear();
- this.searchClear();
+ this.searchClear(true);
}
onMouseOut(e) {
@@ -138,7 +138,7 @@ class Frontend {
onFrameMessage(e) {
const handlers = {
popupClose: () => {
- this.searchClear();
+ this.searchClear(true);
},
selectionCopy: () => {
@@ -153,7 +153,7 @@ class Frontend {
}
onResize() {
- this.searchClear();
+ this.searchClear(true);
}
onClick(e) {
@@ -265,7 +265,7 @@ class Frontend {
async updateOptions() {
this.options = await apiOptionsGet(this.getOptionsContext());
if (!this.options.enable) {
- this.searchClear();
+ this.searchClear(false);
}
}
@@ -320,7 +320,7 @@ class Frontend {
textSource.cleanup();
}
if (hideResults && this.options.scanning.autoHideResults) {
- this.searchClear();
+ this.searchClear(true);
}
this.pendingLookup = false;
@@ -392,8 +392,8 @@ class Frontend {
return true;
}
- searchClear() {
- this.popup.hide();
+ searchClear(changeFocus) {
+ this.popup.hide(changeFocus);
this.popup.clearAutoPlayTimer();
if (this.options.scanning.selectText && this.textSourceLast) {
diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js
index 396f7556..cb9741be 100644
--- a/ext/fg/js/popup-proxy-host.js
+++ b/ext/fg/js/popup-proxy-host.js
@@ -40,7 +40,7 @@ class PopupProxyHost {
createNestedPopup: ({parentId}) => this.createNestedPopup(parentId),
show: ({id, elementRect, options}) => this.show(id, elementRect, options),
showOrphaned: ({id, elementRect, options}) => this.show(id, elementRect, options),
- hide: ({id}) => this.hide(id),
+ hide: ({id, changeFocus}) => this.hide(id, changeFocus),
setVisible: ({id, visible}) => this.setVisible(id, visible),
containsPoint: ({id, x, y}) => this.containsPoint(id, x, y),
termsShow: ({id, elementRect, writingMode, definitions, options, context}) => this.termsShow(id, elementRect, writingMode, definitions, options, context),
@@ -98,9 +98,9 @@ class PopupProxyHost {
return await popup.showOrphaned(elementRect, options);
}
- async hide(id) {
+ async hide(id, changeFocus) {
const popup = this.getPopup(id);
- return popup.hide();
+ return popup.hide(changeFocus);
}
async setVisible(id, visible) {
diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js
index 235e1730..072cebc9 100644
--- a/ext/fg/js/popup-proxy.js
+++ b/ext/fg/js/popup-proxy.js
@@ -58,11 +58,11 @@ class PopupProxy {
return await this.invokeHostApi('showOrphaned', {id, elementRect, options});
}
- async hide() {
+ async hide(changeFocus) {
if (this.id === null) {
return;
}
- return await this.invokeHostApi('hide', {id: this.id});
+ return await this.invokeHostApi('hide', {id: this.id, changeFocus});
}
async setVisible(visible) {
diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js
index 08965084..64da9aef 100644
--- a/ext/fg/js/popup.js
+++ b/ext/fg/js/popup.js
@@ -105,7 +105,7 @@ class Popup {
container.style.height = `${height}px`;
container.style.visibility = 'visible';
- this.hideChildren();
+ this.hideChildren(true);
}
static getPositionForHorizontalText(elementRect, width, height, maxWidth, maxHeight, optionsGeneral) {
@@ -206,16 +206,21 @@ class Popup {
this.invokeApi('orphaned');
}
- hide() {
- this.hideChildren();
+ hide(changeFocus) {
+ if (this.isContainerHidden()) {
+ changeFocus = false;
+ }
+ this.hideChildren(changeFocus);
this.hideContainer();
- this.focusParent();
+ if (changeFocus) {
+ this.focusParent();
+ }
}
- hideChildren() {
- // recursively hides all children
- if (this.child && !this.child.isContainerHidden()) {
- this.child.hide();
+ hideChildren(changeFocus) {
+ // Recursively hides all children.
+ if (this.child !== null && !this.child.isContainerHidden()) {
+ this.child.hide(changeFocus);
}
}
diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css
index eadb9dae..8a4cf4a7 100644
--- a/ext/mixed/css/display.css
+++ b/ext/mixed/css/display.css
@@ -230,3 +230,7 @@ div.glossary-item.compact-glossary {
.info-output td {
text-align: right;
}
+
+.entry:not(.entry-current) .current {
+ display: none;
+}
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js
index 3a728ee8..46016192 100644
--- a/ext/mixed/js/display.js
+++ b/ext/mixed/js/display.js
@@ -28,11 +28,14 @@ class Display {
this.index = 0;
this.audioCache = {};
this.optionsContext = {};
+ this.eventListeners = [];
this.dependencies = {};
- $(document).keydown(this.onKeyDown.bind(this));
- $(document).on('wheel', this.onWheel.bind(this));
+ this.windowScroll = new WindowScroll();
+
+ document.addEventListener('keydown', this.onKeyDown.bind(this));
+ document.addEventListener('wheel', this.onWheel.bind(this), {passive: false});
}
onError(error) {
@@ -52,12 +55,13 @@ class Display {
try {
e.preventDefault();
- const link = $(e.target);
+ const link = e.target;
+ this.windowScroll.toY(0);
const context = {
source: {
definitions: this.definitions,
- index: Display.entryIndexFind(link),
- scroll: $('html,body').scrollTop()
+ index: this.entryIndexFind(link),
+ scroll: this.windowScroll.y
}
};
@@ -67,7 +71,7 @@ class Display {
context.source.source = this.context.source;
}
- const kanjiDefs = await apiKanjiFind(link.text(), this.optionsContext);
+ const kanjiDefs = await apiKanjiFind(link.textContent, this.optionsContext);
this.kanjiShow(kanjiDefs, this.options, context);
} catch (e) {
this.onError(e);
@@ -80,7 +84,7 @@ class Display {
const {docRangeFromPoint, docSentenceExtract} = this.dependencies;
- const clickedElement = $(e.target);
+ const clickedElement = e.target;
const textSource = docRangeFromPoint(e.clientX, e.clientY, this.options);
if (textSource === null) {
return false;
@@ -102,11 +106,12 @@ class Display {
textSource.cleanup();
}
+ this.windowScroll.toY(0);
const context = {
source: {
definitions: this.definitions,
- index: Display.entryIndexFind(clickedElement),
- scroll: $('html,body').scrollTop()
+ index: this.entryIndexFind(clickedElement),
+ scroll: this.windowScroll.y
}
};
@@ -124,38 +129,38 @@ class Display {
onAudioPlay(e) {
e.preventDefault();
- const link = $(e.currentTarget);
- const definitionIndex = Display.entryIndexFind(link);
- const expressionIndex = link.closest('.entry').find('.expression .action-play-audio').index(link);
+ const link = e.currentTarget;
+ const entry = link.closest('.entry');
+ const definitionIndex = this.entryIndexFind(entry);
+ const expressionIndex = Display.indexOf(entry.querySelectorAll('.expression .action-play-audio'), link);
this.audioPlay(this.definitions[definitionIndex], expressionIndex);
}
onNoteAdd(e) {
e.preventDefault();
- const link = $(e.currentTarget);
- const index = Display.entryIndexFind(link);
- this.noteAdd(this.definitions[index], link.data('mode'));
+ const link = e.currentTarget;
+ const index = this.entryIndexFind(link);
+ this.noteAdd(this.definitions[index], link.dataset.mode);
}
onNoteView(e) {
e.preventDefault();
- const link = $(e.currentTarget);
- const index = Display.entryIndexFind(link);
- apiNoteView(link.data('noteId'));
+ const link = e.currentTarget;
+ apiNoteView(link.dataset.noteId);
}
onKeyDown(e) {
const noteTryAdd = mode => {
- const button = Display.adderButtonFind(this.index, mode);
- if (button.length !== 0 && !button.hasClass('disabled')) {
+ const button = this.adderButtonFind(this.index, mode);
+ if (button !== null && !button.classList.contains('disabled')) {
this.noteAdd(this.definitions[this.index], mode);
}
};
const noteTryView = mode => {
- const button = Display.viewerButtonFind(this.index);
- if (button.length !== 0 && !button.hasClass('disabled')) {
- apiNoteView(button.data('noteId'));
+ const button = this.viewerButtonFind(this.index);
+ if (button !== null && !button.classList.contains('disabled')) {
+ apiNoteView(button.dataset.noteId);
}
};
@@ -237,7 +242,8 @@ class Display {
80: /* p */ () => {
if (e.altKey) {
- if ($('.entry').eq(this.index).data('type') === 'term') {
+ const entry = this.getEntry(this.index);
+ if (entry !== null && entry.dataset.type === 'term') {
this.audioPlay(this.definitions[this.index], this.firstExpressionIndex);
}
@@ -259,13 +265,12 @@ class Display {
}
onWheel(e) {
- const event = e.originalEvent;
const handler = () => {
- if (event.altKey) {
- if (event.deltaY < 0) { // scroll up
+ if (e.altKey) {
+ if (e.deltaY < 0) { // scroll up
this.entryScrollIntoView(this.index - 1, null, true);
return true;
- } else if (event.deltaY > 0) { // scroll down
+ } else if (e.deltaY > 0) { // scroll down
this.entryScrollIntoView(this.index + 1, null, true);
return true;
}
@@ -273,12 +278,14 @@ class Display {
};
if (handler()) {
- event.preventDefault();
+ e.preventDefault();
}
}
async termsShow(definitions, options, context) {
try {
+ this.clearEventListeners();
+
if (!context || context.focus !== false) {
window.focus();
}
@@ -310,7 +317,7 @@ class Display {
}
const content = await apiTemplateRender('terms.html', params);
- this.container.html(content);
+ this.container.innerHTML = content;
const {index, scroll} = context || {};
this.entryScrollIntoView(index || 0, scroll);
@@ -318,13 +325,13 @@ class Display {
this.autoPlayAudio();
}
- $('.action-add-note').click(this.onNoteAdd.bind(this));
- $('.action-view-note').click(this.onNoteView.bind(this));
- $('.action-play-audio').click(this.onAudioPlay.bind(this));
- $('.kanji-link').click(this.onKanjiLookup.bind(this));
- $('.source-term').click(this.onSourceTermView.bind(this));
+ this.addEventListeners('.action-add-note', 'click', this.onNoteAdd.bind(this));
+ this.addEventListeners('.action-view-note', 'click', this.onNoteView.bind(this));
+ this.addEventListeners('.action-play-audio', 'click', this.onAudioPlay.bind(this));
+ this.addEventListeners('.kanji-link', 'click', this.onKanjiLookup.bind(this));
+ this.addEventListeners('.source-term', 'click', this.onSourceTermView.bind(this));
if (this.options.scanning.enablePopupSearch) {
- $('.glossary-item').click(this.onTermLookup.bind(this));
+ this.addEventListeners('.glossary-item', 'click', this.onTermLookup.bind(this));
}
await this.adderButtonUpdate(['term-kanji', 'term-kana'], sequence);
@@ -335,6 +342,8 @@ class Display {
async kanjiShow(definitions, options, context) {
try {
+ this.clearEventListeners();
+
if (!context || context.focus !== false) {
window.focus();
}
@@ -362,13 +371,13 @@ class Display {
}
const content = await apiTemplateRender('kanji.html', params);
- this.container.html(content);
+ this.container.innerHTML = content;
const {index, scroll} = context || {};
this.entryScrollIntoView(index || 0, scroll);
- $('.action-add-note').click(this.onNoteAdd.bind(this));
- $('.action-view-note').click(this.onNoteView.bind(this));
- $('.source-term').click(this.onSourceTermView.bind(this));
+ this.addEventListeners('.action-add-note', 'click', this.onNoteAdd.bind(this));
+ this.addEventListeners('.action-view-note', 'click', this.onNoteView.bind(this));
+ this.addEventListeners('.source-term', 'click', this.onSourceTermView.bind(this));
await this.adderButtonUpdate(['kanji'], sequence);
} catch (e) {
@@ -390,14 +399,13 @@ class Display {
for (let i = 0; i < states.length; ++i) {
const state = states[i];
for (const mode in state) {
- const button = Display.adderButtonFind(i, mode);
- if (state[mode]) {
- button.removeClass('disabled');
- } else {
- button.addClass('disabled');
+ const button = this.adderButtonFind(i, mode);
+ if (button === null) {
+ continue;
}
- button.removeClass('pending');
+ button.classList.toggle('disabled', !state[mode]);
+ button.classList.remove('pending');
}
}
} catch (e) {
@@ -409,22 +417,29 @@ class Display {
index = Math.min(index, this.definitions.length - 1);
index = Math.max(index, 0);
- $('.current').hide().eq(index).show();
+ const entryPre = this.getEntry(this.index);
+ if (entryPre !== null) {
+ entryPre.classList.remove('entry-current');
+ }
+
+ const entry = this.getEntry(index);
+ if (entry !== null) {
+ entry.classList.add('entry-current');
+ }
- const container = $('html,body').stop();
- const entry = $('.entry').eq(index);
+ this.windowScroll.stop();
let target;
if (scroll) {
target = scroll;
} else {
- target = index === 0 ? 0 : entry.offset().top;
+ target = index === 0 || entry === null ? 0 : Display.getElementTop(entry);
}
if (smooth) {
- container.animate({scrollTop: target}, 200);
+ this.windowScroll.animate(this.windowScroll.x, target, 200);
} else {
- container.scrollTop(target);
+ this.windowScroll.toY(target);
}
this.index = index;
@@ -446,7 +461,7 @@ class Display {
async noteAdd(definition, mode) {
try {
- this.spinner.show();
+ this.setSpinnerVisible(true);
const context = {};
if (this.noteUsesScreenshot()) {
@@ -459,21 +474,28 @@ class Display {
const noteId = await apiDefinitionAdd(definition, mode, context, this.optionsContext);
if (noteId) {
const index = this.definitions.indexOf(definition);
- Display.adderButtonFind(index, mode).addClass('disabled');
- Display.viewerButtonFind(index).removeClass('pending disabled').data('noteId', noteId);
+ const adderButton = this.adderButtonFind(index, mode);
+ if (adderButton !== null) {
+ adderButton.classList.add('disabled');
+ }
+ const viewerButton = this.viewerButtonFind(index);
+ if (viewerButton !== null) {
+ viewerButton.classList.remove('pending', 'disabled');
+ viewerButton.dataset.noteId = noteId;
+ }
} else {
throw 'Note could note be added';
}
} catch (e) {
this.onError(e);
} finally {
- this.spinner.hide();
+ this.setSpinnerVisible(false);
}
}
async audioPlay(definition, expressionIndex) {
try {
- this.spinner.show();
+ this.setSpinnerVisible(true);
const expression = expressionIndex === -1 ? definition : definition.expressions[expressionIndex];
let url = await apiAudioGetUrl(expression, this.options.general.audioSource);
@@ -505,7 +527,7 @@ class Display {
} catch (e) {
this.onError(e);
} finally {
- this.spinner.hide();
+ this.setSpinnerVisible(false);
}
}
@@ -542,6 +564,15 @@ class Display {
return apiForward('popupSetVisible', {visible});
}
+ setSpinnerVisible(visible) {
+ this.spinner.style.display = visible ? 'block' : '';
+ }
+
+ getEntry(index) {
+ const entries = this.container.querySelectorAll('.entry');
+ return index >= 0 && index < entries.length ? entries[index] : null;
+ }
+
static clozeBuild(sentence, source) {
const result = {
sentence: sentence.text.trim()
@@ -556,19 +587,51 @@ class Display {
return result;
}
- static entryIndexFind(element) {
- return $('.entry').index(element.closest('.entry'));
+ entryIndexFind(element) {
+ const entry = element.closest('.entry');
+ return entry !== null ? Display.indexOf(this.container.querySelectorAll('.entry'), entry) : -1;
}
- static adderButtonFind(index, mode) {
- return $('.entry').eq(index).find(`.action-add-note[data-mode="${mode}"]`);
+ adderButtonFind(index, mode) {
+ const entry = this.getEntry(index);
+ return entry !== null ? entry.querySelector(`.action-add-note[data-mode="${mode}"]`) : null;
}
- static viewerButtonFind(index) {
- return $('.entry').eq(index).find('.action-view-note');
+ viewerButtonFind(index) {
+ const entry = this.getEntry(index);
+ return entry !== null ? entry.querySelector('.action-view-note') : null;
}
static delay(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
+
+ static indexOf(nodeList, node) {
+ for (let i = 0, ii = nodeList.length; i < ii; ++i) {
+ if (nodeList[i] === node) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ addEventListeners(selector, type, listener, options) {
+ this.container.querySelectorAll(selector).forEach((node) => {
+ node.addEventListener(type, listener, options);
+ this.eventListeners.push([node, type, listener, options]);
+ });
+ }
+
+ clearEventListeners() {
+ for (const [node, type, listener, options] of this.eventListeners) {
+ node.removeEventListener(type, listener, options);
+ }
+ this.eventListeners = [];
+ }
+
+ static getElementTop(element) {
+ const elementRect = element.getBoundingClientRect();
+ const documentRect = document.documentElement.getBoundingClientRect();
+ return elementRect.top - documentRect.top;
+ }
}
diff --git a/ext/mixed/js/extension.js b/ext/mixed/js/extension.js
index d7085e5b..5c803132 100644
--- a/ext/mixed/js/extension.js
+++ b/ext/mixed/js/extension.js
@@ -17,13 +17,24 @@
*/
+// toIterable is required on Edge for cross-window origin objects.
function toIterable(value) {
if (typeof Symbol !== 'undefined' && typeof value[Symbol.iterator] !== 'undefined') {
return value;
}
- const array = JSON.parse(JSON.stringify(value));
- return Array.isArray(array) ? array : [];
+ if (value !== null && typeof value === 'object') {
+ const length = value.length;
+ if (typeof length === 'number' && Number.isFinite(length)) {
+ const array = [];
+ for (let i = 0; i < length; ++i) {
+ array.push(value[i]);
+ }
+ return array;
+ }
+ }
+
+ throw 'Could not convert to iterable';
}
function extensionHasChrome() {
diff --git a/ext/mixed/js/scroll.js b/ext/mixed/js/scroll.js
new file mode 100644
index 00000000..824fd92b
--- /dev/null
+++ b/ext/mixed/js/scroll.js
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2019 Alex Yatskov <alex@foosoft.net>
+ * Author: Alex Yatskov <alex@foosoft.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+class WindowScroll {
+ constructor() {
+ this.animationRequestId = null;
+ this.animationStartTime = 0;
+ this.animationStartX = 0;
+ this.animationStartY = 0;
+ this.animationEndTime = 0;
+ this.animationEndX = 0;
+ this.animationEndY = 0;
+ this.requestAnimationFrameCallback = (t) => this.onAnimationFrame(t);
+ }
+
+ toY(y) {
+ this.to(this.x, y);
+ }
+
+ toX(x) {
+ this.to(x, this.y);
+ }
+
+ to(x, y) {
+ this.stop();
+ window.scroll(x, y);
+ }
+
+ animate(x, y, time) {
+ this.animationStartX = this.x;
+ this.animationStartY = this.y;
+ this.animationStartTime = window.performance.now();
+ this.animationEndX = x;
+ this.animationEndY = y;
+ this.animationEndTime = this.animationStartTime + time;
+ this.animationRequestId = window.requestAnimationFrame(this.requestAnimationFrameCallback);
+ }
+
+ stop() {
+ if (this.animationRequestId === null) {
+ return;
+ }
+
+ window.cancelAnimationFrame(this.animationRequestId);
+ this.animationRequestId = null;
+ }
+
+ onAnimationFrame(time) {
+ if (time >= this.animationEndTime) {
+ window.scroll(this.animationEndX, this.animationEndY);
+ this.animationRequestId = null;
+ return;
+ }
+
+ const t = WindowScroll.easeInOutCubic((time - this.animationStartTime) / (this.animationEndTime - this.animationStartTime));
+ window.scroll(
+ WindowScroll.lerp(this.animationStartX, this.animationEndX, t),
+ WindowScroll.lerp(this.animationStartY, this.animationEndY, t)
+ );
+
+ this.animationRequestId = window.requestAnimationFrame(this.requestAnimationFrameCallback);
+ }
+
+ get x() {
+ return window.scrollX || window.pageXOffset;
+ }
+
+ get y() {
+ return window.scrollY || window.pageYOffset;
+ }
+
+ static easeInOutCubic(t) {
+ if (t < 0.5) {
+ return (4.0 * t * t * t);
+ } else {
+ t = 1.0 - t;
+ return 1.0 - (4.0 * t * t * t);
+ }
+ }
+
+ static lerp(start, end, percent) {
+ return (end - start) * percent + start;
+ }
+}