Skip to content

Commit

Permalink
Fix uncollapse link and clicking on images (#1705)
Browse files Browse the repository at this point in the history
* Add collapsed image link text

* Prevent collapsed image from being partially hidden when hovering over

* Allow clicking on image node to open in new tab

* Open original media in new tab instead of canvas

* Update tests

* Make eslint happy
  • Loading branch information
Kuuuube authored Dec 27, 2024
1 parent 58ecbeb commit 7d4ae00
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@
"ext/js/core/extension-error.js",
"ext/js/core/json.js",
"ext/js/data/anki-note-data-creator.js",
"ext/js/data/array-buffer-util.js",
"ext/js/dictionary/dictionary-data-util.js",
"ext/js/display/display-content-manager.js",
"ext/js/display/pronunciation-generator.js",
Expand Down
1 change: 0 additions & 1 deletion ext/css/display.css
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,6 @@ button.action-button:active {
.entry {
padding: var(--entry-vertical-padding) var(--entry-horizontal-padding);
position: relative;
content-visibility: auto;
contain-intrinsic-height: auto 500px;
}
.entry+.entry {
Expand Down
2 changes: 0 additions & 2 deletions ext/css/structured-content.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
}
.gloss-image-link:hover {
color: var(--accent-color-dark);
}
.gloss-image-link[href]:hover {
cursor: pointer;
}
.gloss-image-container-overlay {
Expand Down
6 changes: 0 additions & 6 deletions ext/data/structured-content-style.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
["color", "inherit"]
]
},
{
"selectors": [".gloss-image-link[href]:hover"],
"styles": [
["cursor", "pointer"]
]
},
{
"selectors": [".gloss-image-container-overlay"],
"styles": [
Expand Down
14 changes: 14 additions & 0 deletions ext/js/display/display-content-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import {EventListenerCollection} from '../core/event-listener-collection.js';
import {base64ToArrayBuffer} from '../data/array-buffer-util.js';

/**
* The content manager which is used when generating HTML display content.
Expand Down Expand Up @@ -86,6 +87,19 @@ export class DisplayContentManager {
this._loadMediaRequests = [];
}

/**
* @param {string} path
* @param {string} dictionary
* @param {Window} window
*/
async openMediaInTab(path, dictionary, window) {
const data = await this._display.application.api.getMedia([{path, dictionary}]);
const buffer = base64ToArrayBuffer(data[0].content);
const blob = new Blob([buffer], {type: data[0].mediaType});
const blobUrl = URL.createObjectURL(blob);
window.open(blobUrl, '_blank')?.focus();
}

/**
* @param {MouseEvent} e
*/
Expand Down
12 changes: 12 additions & 0 deletions ext/js/display/structured-content-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ export class StructuredContentGenerator {
const overlay = this._createElement('span', 'gloss-image-container-overlay');
imageContainer.appendChild(overlay);

const linkText = this._createElement('span', 'gloss-image-link-text');
linkText.textContent = 'Image';
node.appendChild(linkText);

if (this._contentManager instanceof DisplayContentManager) {
node.addEventListener('click', () => {
if (this._contentManager instanceof DisplayContentManager) {
void this._contentManager.openMediaInTab(path, dictionary, window);
}
});
}

node.dataset.path = path;
node.dataset.dictionary = dictionary;
node.dataset.imageLoadState = 'not-loaded';
Expand Down
24 changes: 12 additions & 12 deletions test/data/anki-note-builder-test-results.json

Large diffs are not rendered by default.

0 comments on commit 7d4ae00

Please sign in to comment.